Change to new header API

This commit is contained in:
Dolan
2018-10-05 01:33:17 +01:00
parent 3508fd97ec
commit 97f76fb62c
4 changed files with 215 additions and 100 deletions

View File

@ -1,24 +1,38 @@
// http://officeopenxml.com/WPsection.php
import { XmlComponent } from "file/xml-components";
import { FooterWrapper } from "../../../footer-wrapper";
import { HeaderWrapper } from "../../../header-wrapper";
import { IPageBordersOptions, IPageNumberTypeAttributes, PageBorders, PageNumberFormat, PageNumberType } from "./";
import { Columns } from "./columns/columns";
import { IColumnsAttributes } from "./columns/columns-attributes";
import { DocumentGrid } from "./doc-grid/doc-grid";
import { IDocGridAttributesProperties } from "./doc-grid/doc-grid-attributes";
import { FooterReference, IFooterOptions } from "./footer-reference/footer-reference";
import { HeaderReference, IHeaderOptions } from "./header-reference/header-reference";
import { FooterReferenceType } from "./footer-reference";
import { FooterReference } from "./footer-reference/footer-reference";
import { HeaderReferenceType } from "./header-reference";
import { HeaderReference } from "./header-reference/header-reference";
import { PageMargin } from "./page-margin/page-margin";
import { IPageMarginAttributes } from "./page-margin/page-margin-attributes";
import { PageSize } from "./page-size/page-size";
import { IPageSizeAttributes, PageOrientation } from "./page-size/page-size-attributes";
import { TitlePage } from "./title-page/title-page";
export interface IHeaderFooterGroup<T> {
default?: T;
first?: T;
even?: T;
}
interface IHeadersOptions {
headers?: IHeaderOptions[];
headers?: IHeaderFooterGroup<HeaderWrapper>;
}
interface IFootersOptions {
footers?: IFooterOptions[];
footers?: IHeaderFooterGroup<FooterWrapper>;
}
interface ITitlePageOptions {
titlePage?: boolean;
}
export type SectionPropertiesOptions = IPageSizeAttributes &
@ -28,104 +42,129 @@ export type SectionPropertiesOptions = IPageSizeAttributes &
IHeadersOptions &
IFootersOptions &
IPageNumberTypeAttributes &
IPageBordersOptions;
IPageBordersOptions &
ITitlePageOptions;
export class SectionProperties extends XmlComponent {
private readonly options: SectionPropertiesOptions;
constructor(options?: SectionPropertiesOptions) {
constructor(options: SectionPropertiesOptions = {}) {
super("w:sectPr");
const defaultOptions = {
width: 11906,
height: 16838,
top: 1440,
right: 1440,
bottom: 1440,
left: 1440,
header: 708,
footer: 708,
gutter: 0,
mirror: false,
space: 708,
linePitch: 360,
orientation: PageOrientation.PORTRAIT,
headers: [],
footers: [],
pageNumberStart: undefined,
pageNumberFormatType: PageNumberFormat.DECIMAL,
pageBorders: undefined,
pageBorderTop: undefined,
pageBorderRight: undefined,
pageBorderBottom: undefined,
pageBorderLeft: undefined,
titlePage: false,
};
const {
width = 11906,
height = 16838,
top = 1440,
right = 1440,
bottom = 1440,
left = 1440,
header = 708,
footer = 708,
gutter = 0,
mirror = false,
space = 708,
linePitch = 360,
orientation = PageOrientation.PORTRAIT,
headers,
footers,
pageNumberFormatType = PageNumberFormat.DECIMAL,
pageNumberStart,
pageBorders,
pageBorderTop,
pageBorderRight,
pageBorderBottom,
pageBorderLeft,
titlePage = false,
} = options;
const mergedOptions = {
...defaultOptions,
...options,
};
this.options = options;
this.root.push(new PageSize(width, height, orientation));
this.root.push(new PageMargin(top, right, bottom, left, header, footer, gutter, mirror));
this.root.push(new Columns(space));
this.root.push(new DocumentGrid(linePitch));
this.root.push(new PageSize(mergedOptions.width, mergedOptions.height, mergedOptions.orientation));
this.root.push(
new PageMargin(
mergedOptions.top,
mergedOptions.right,
mergedOptions.bottom,
mergedOptions.left,
mergedOptions.header,
mergedOptions.footer,
mergedOptions.gutter,
mergedOptions.mirror,
),
);
this.root.push(new Columns(mergedOptions.space));
this.root.push(new DocumentGrid(mergedOptions.linePitch));
this.addHeaders(headers);
this.addFooters(footers);
for (const header of mergedOptions.headers) {
this.root.push(
new HeaderReference({
headerType: header.headerType,
headerId: header.headerId,
}),
);
}
this.root.push(new PageNumberType(pageNumberStart, pageNumberFormatType));
for (const footer of mergedOptions.footers) {
this.root.push(
new FooterReference({
footerType: footer.footerType,
footerId: footer.footerId,
}),
);
}
this.root.push(new PageNumberType(mergedOptions.pageNumberStart, mergedOptions.pageNumberFormatType));
if (
mergedOptions.pageBorders ||
mergedOptions.pageBorderTop ||
mergedOptions.pageBorderRight ||
mergedOptions.pageBorderBottom ||
mergedOptions.pageBorderLeft
) {
if (pageBorders || pageBorderTop || pageBorderRight || pageBorderBottom || pageBorderLeft) {
this.root.push(
new PageBorders({
pageBorders: mergedOptions.pageBorders,
pageBorderTop: mergedOptions.pageBorderTop,
pageBorderRight: mergedOptions.pageBorderRight,
pageBorderBottom: mergedOptions.pageBorderBottom,
pageBorderLeft: mergedOptions.pageBorderLeft,
pageBorders: pageBorders,
pageBorderTop: pageBorderTop,
pageBorderRight: pageBorderRight,
pageBorderBottom: pageBorderBottom,
pageBorderLeft: pageBorderLeft,
}),
);
}
if (mergedOptions.titlePage) {
if (titlePage) {
this.root.push(new TitlePage());
}
}
this.options = mergedOptions;
private addHeaders(headers?: IHeaderFooterGroup<HeaderWrapper>): void {
if (headers) {
if (headers.default) {
this.root.push(
new HeaderReference({
headerType: HeaderReferenceType.DEFAULT,
headerId: headers.default.Header.ReferenceId,
}),
);
}
if (headers.first) {
this.root.push(
new HeaderReference({
headerType: HeaderReferenceType.FIRST,
headerId: headers.first.Header.ReferenceId,
}),
);
}
if (headers.even) {
this.root.push(
new HeaderReference({
headerType: HeaderReferenceType.EVEN,
headerId: headers.even.Header.ReferenceId,
}),
);
}
}
}
private addFooters(footers?: IHeaderFooterGroup<FooterWrapper>): void {
if (footers) {
if (footers.default) {
this.root.push(
new FooterReference({
footerType: FooterReferenceType.DEFAULT,
footerId: footers.default.Footer.ReferenceId,
}),
);
}
if (footers.first) {
this.root.push(
new FooterReference({
footerType: FooterReferenceType.FIRST,
footerId: footers.first.Footer.ReferenceId,
}),
);
}
if (footers.even) {
this.root.push(
new FooterReference({
footerType: FooterReferenceType.EVEN,
footerId: footers.even.Footer.ReferenceId,
}),
);
}
}
}
public get Options(): SectionPropertiesOptions {