From 58346a8937ead91899476e933f3690371e58efb0 Mon Sep 17 00:00:00 2001 From: Dolan Date: Sun, 7 Jul 2019 03:54:29 +0100 Subject: [PATCH] Section WIP --- demo/demo16.ts | 87 ++++++++++++++++++++++++------------------- src/file/file.spec.ts | 6 +-- src/file/file.ts | 16 +++++++- 3 files changed, 66 insertions(+), 43 deletions(-) diff --git a/demo/demo16.ts b/demo/demo16.ts index cd1507d747..ee77ce7ebb 100644 --- a/demo/demo16.ts +++ b/demo/demo16.ts @@ -15,68 +15,77 @@ const footer = doc.createFooter(); footer.add(new Paragraph("Footer on another page")); doc.addSection({ - headers: { - default: header, + properties: { + headers: { + default: header, + }, + footers: { + default: footer, + }, + pageNumberStart: 1, + pageNumberFormatType: PageNumberFormat.DECIMAL, }, - footers: { - default: footer, - }, - pageNumberStart: 1, - pageNumberFormatType: PageNumberFormat.DECIMAL, + children: [new Paragraph("hello")], }); -doc.add(new Paragraph("hello")); - doc.addSection({ - headers: { - default: header, + properties: { + headers: { + default: header, + }, + footers: { + default: footer, + }, + pageNumberStart: 1, + pageNumberFormatType: PageNumberFormat.DECIMAL, + orientation: PageOrientation.LANDSCAPE, }, - footers: { - default: footer, - }, - pageNumberStart: 1, - pageNumberFormatType: PageNumberFormat.DECIMAL, - orientation: PageOrientation.LANDSCAPE, + children: [new Paragraph("hello in landscape")], }); -doc.add(new Paragraph("hello in landscape")); - const header2 = doc.createHeader(); const pageNumber = new TextRun("Page number: ").pageNumber(); header2.add(new Paragraph({}).addRun(pageNumber)); doc.addSection({ - headers: { - default: header2, + properties: { + headers: { + default: header2, + }, + orientation: PageOrientation.PORTRAIT, }, - orientation: PageOrientation.PORTRAIT, + children: [new Paragraph("Page number in the header must be 2, because it continues from the previous section.")], }); -doc.add(new Paragraph("Page number in the header must be 2, because it continues from the previous section.")); - doc.addSection({ - headers: { - default: header2, + properties: { + headers: { + default: header2, + }, + pageNumberFormatType: PageNumberFormat.UPPER_ROMAN, + orientation: PageOrientation.PORTRAIT, }, - pageNumberFormatType: PageNumberFormat.UPPER_ROMAN, - orientation: PageOrientation.PORTRAIT, + children: [ + new Paragraph( + "Page number in the header must be III, because it continues from the previous section, but is defined as upper roman.", + ), + ], }); -doc.add(new Paragraph( - "Page number in the header must be III, because it continues from the previous section, but is defined as upper roman.", -)); - doc.addSection({ - headers: { - default: header2, + properties: { + headers: { + default: header2, + }, + pageNumberFormatType: PageNumberFormat.DECIMAL, + pageNumberStart: 25, + orientation: PageOrientation.PORTRAIT, }, - pageNumberFormatType: PageNumberFormat.DECIMAL, - pageNumberStart: 25, - orientation: PageOrientation.PORTRAIT, + children: [ + new Paragraph("Page number in the header must be 25, because it is defined to start at 25 and to be decimal in this section."), + ], }); -doc.add(new Paragraph("Page number in the header must be 25, because it is defined to start at 25 and to be decimal in this section.")); - const packer = new Packer(); packer.toBuffer(doc).then((buffer) => { diff --git a/src/file/file.spec.ts b/src/file/file.spec.ts index b11a5f61ec..00e0b7d02b 100644 --- a/src/file/file.spec.ts +++ b/src/file/file.spec.ts @@ -15,7 +15,7 @@ describe("File", () => { const header = doc.createHeader(); const footer = doc.createFooter(); - doc.addSection({ + doc.addSectionOld({ headers: { default: header, }, @@ -35,7 +35,7 @@ describe("File", () => { const header = doc.createHeader(); const footer = doc.createFooter(); - doc.addSection({ + doc.addSectionOld({ headers: { first: header, }, @@ -55,7 +55,7 @@ describe("File", () => { const header = doc.createHeader(); const footer = doc.createFooter(); - doc.addSection({ + doc.addSectionOld({ headers: { default: header, first: header, diff --git a/src/file/file.ts b/src/file/file.ts index a8fba1d512..0f4c77c82b 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -27,6 +27,11 @@ import { DefaultStylesFactory } from "./styles/factory"; import { Table } from "./table"; import { TableOfContents } from "./table-of-contents"; +export interface ISectionOptions { + readonly properties: SectionPropertiesOptions; + readonly children: Array; +} + export class File { // tslint:disable-next-line:readonly-keyword private currentRelationshipId: number = 1; @@ -54,6 +59,7 @@ export class File { }, sectionPropertiesOptions: SectionPropertiesOptions = {}, fileProperties: IFileProperties = {}, + sections: ISectionOptions[] = [], ) { this.coreProperties = new CoreProperties(options); this.numbering = new Numbering(); @@ -110,6 +116,14 @@ export class File { this.document = new Document(newSectionPropertiesOptions); this.settings = new Settings(); + + for (const section of sections) { + this.document.Body.addSection(section.properties); + + for (const child of section.children) { + this.add(child); + } + } } public add(item: Paragraph | Table | TableOfContents): File { @@ -167,7 +181,7 @@ export class File { return bookmark; } - public addSection(sectionPropertiesOptions: SectionPropertiesOptions): void { + public addSectionOld(sectionPropertiesOptions: SectionPropertiesOptions): void { this.document.Body.addSection(sectionPropertiesOptions); }