From ad229e91ad95385b9f8a624508a091884a05b894 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Thu, 2 Dec 2021 00:39:50 +0900 Subject: [PATCH] fix: Add test and support table (#379) --- README.md | 1 + docx-wasm/js/footer.ts | 11 +++- docx-wasm/js/header.ts | 11 +++- docx-wasm/js/index.ts | 56 ++++++++++++++++-- .../test/__snapshots__/index.test.js.snap | 57 +++++++++++++++++++ docx-wasm/test/index.test.js | 32 +++++++++++ 6 files changed, 160 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6ac46b4..de35821 100644 --- a/README.md +++ b/README.md @@ -173,4 +173,5 @@ $ yarn test -- --updateSnapshot - [x] HIstory - [ ] Table of contents - [ ] Section +- [ ] Textbox diff --git a/docx-wasm/js/footer.ts b/docx-wasm/js/footer.ts index f8dad67..1c58839 100644 --- a/docx-wasm/js/footer.ts +++ b/docx-wasm/js/footer.ts @@ -1,8 +1,9 @@ import { Paragraph } from "./paragraph"; +import { Table } from "./table"; export class Footer { hasNumberings = false; - children: Paragraph[] = []; + children: (Paragraph | Table)[] = []; addParagraph(p: Paragraph) { if (p.hasNumberings) { @@ -11,4 +12,12 @@ export class Footer { this.children.push(p); return this; } + + addTable(t: Table) { + if (t.hasNumberings) { + this.hasNumberings = true; + } + this.children.push(t); + return this; + } } diff --git a/docx-wasm/js/header.ts b/docx-wasm/js/header.ts index 193b10a..da2d614 100644 --- a/docx-wasm/js/header.ts +++ b/docx-wasm/js/header.ts @@ -1,8 +1,9 @@ import { Paragraph } from "./paragraph"; +import { Table } from "./table"; export class Header { hasNumberings = false; - children: Paragraph[] = []; + children: (Paragraph | Table)[] = []; addParagraph(p: Paragraph) { if (p.hasNumberings) { @@ -11,4 +12,12 @@ export class Header { this.children.push(p); return this; } + + addTable(t: Table) { + if (t.hasNumberings) { + this.hasNumberings = true; + } + this.children.push(t); + return this; + } } diff --git a/docx-wasm/js/index.ts b/docx-wasm/js/index.ts index cc7bfe1..7cd1b8a 100644 --- a/docx-wasm/js/index.ts +++ b/docx-wasm/js/index.ts @@ -157,11 +157,31 @@ export class Docx { return this; } + firstHeader(h: Header) { + this.sectionProperty._firstHeader = h; + return this; + } + + evenHeader(h: Header) { + this.sectionProperty._evenHeader = h; + return this; + } + footer(f: Footer) { this.sectionProperty._footer = f; return this; } + firstFooter(f: Footer) { + this.sectionProperty._firstFooter = f; + return this; + } + + evenFooter(f: Footer) { + this.sectionProperty._evenFooter = f; + return this; + } + pageSize(w: number, h: number) { this.sectionProperty.pageSize(w, h); return this; @@ -858,7 +878,11 @@ export class Docx { if (this.sectionProperty._header) { let header = wasm.createHeader(); this.sectionProperty._header.children.forEach((c) => { - header = header.add_paragraph(this.buildParagraph(c)); + if (c instanceof Paragraph) { + header = header.add_paragraph(this.buildParagraph(c)); + } else { + header = header.add_table(this.buildTable(c)); + } }); docx = docx.header(header); } @@ -866,7 +890,11 @@ export class Docx { if (this.sectionProperty._firstHeader) { let header = wasm.createHeader(); this.sectionProperty._firstHeader.children.forEach((c) => { - header = header.add_paragraph(this.buildParagraph(c)); + if (c instanceof Paragraph) { + header = header.add_paragraph(this.buildParagraph(c)); + } else { + header = header.add_table(this.buildTable(c)); + } }); docx = docx.first_header(header); } @@ -874,7 +902,11 @@ export class Docx { if (this.sectionProperty._evenHeader) { let header = wasm.createHeader(); this.sectionProperty._evenHeader.children.forEach((c) => { - header = header.add_paragraph(this.buildParagraph(c)); + if (c instanceof Paragraph) { + header = header.add_paragraph(this.buildParagraph(c)); + } else { + header = header.add_table(this.buildTable(c)); + } }); docx = docx.even_header(header); } @@ -882,7 +914,11 @@ export class Docx { if (this.sectionProperty._footer) { let footer = wasm.createFooter(); this.sectionProperty._footer.children.forEach((c) => { - footer = footer.add_paragraph(this.buildParagraph(c)); + if (c instanceof Paragraph) { + footer = footer.add_paragraph(this.buildParagraph(c)); + } else { + footer = footer.add_table(this.buildTable(c)); + } }); docx = docx.footer(footer); } @@ -890,7 +926,11 @@ export class Docx { if (this.sectionProperty._firstFooter) { let footer = wasm.createFooter(); this.sectionProperty._firstFooter.children.forEach((c) => { - footer = footer.add_paragraph(this.buildParagraph(c)); + if (c instanceof Paragraph) { + footer = footer.add_paragraph(this.buildParagraph(c)); + } else { + footer = footer.add_table(this.buildTable(c)); + } }); docx = docx.first_footer(footer); } @@ -898,7 +938,11 @@ export class Docx { if (this.sectionProperty._evenFooter) { let footer = wasm.createFooter(); this.sectionProperty._evenFooter.children.forEach((c) => { - footer = footer.add_paragraph(this.buildParagraph(c)); + if (c instanceof Paragraph) { + footer = footer.add_paragraph(this.buildParagraph(c)); + } else { + footer = footer.add_table(this.buildTable(c)); + } }); docx = docx.even_footer(footer); } diff --git a/docx-wasm/test/__snapshots__/index.test.js.snap b/docx-wasm/test/__snapshots__/index.test.js.snap index ebdd5aa..6d27071 100644 --- a/docx-wasm/test/__snapshots__/index.test.js.snap +++ b/docx-wasm/test/__snapshots__/index.test.js.snap @@ -25396,6 +25396,63 @@ exports[`writer should write doc vars 3`] = ` " `; +exports[`writer should write evenFooter with table for default section 1`] = ` +" + + + + + + +" +`; + +exports[`writer should write evenFooter with table for default section 2`] = ` +" + + +" +`; + +exports[`writer should write evenFooter with table for default section 3`] = ` +" + + + + + +Hello Footer!!" +`; + +exports[`writer should write firstHeader with table for default section 1`] = ` +" + + + + + + +" +`; + +exports[`writer should write firstHeader with table for default section 2`] = ` +" + + + +" +`; + +exports[`writer should write firstHeader with table for default section 3`] = ` +" + + + + + +Hello Header!!" +`; + exports[`writer should write footer for default section 1`] = ` " diff --git a/docx-wasm/test/index.test.js b/docx-wasm/test/index.test.js index efdd9aa..8818b5a 100644 --- a/docx-wasm/test/index.test.js +++ b/docx-wasm/test/index.test.js @@ -404,4 +404,36 @@ describe("writer", () => { } } }); + + test("should write firstHeader with table for default section", () => { + const p = new w.Paragraph().addRun(new w.Run().addText("Hello Header!!")); + const table = new w.Table().addRow( + new w.TableRow().addCell(new w.TableCell().addParagraph(p)) + ); + const header = new w.Header().addTable(table); + const buffer = new w.Docx().firstHeader(header).build(); + writeFileSync("../output/first_header_with_table.docx", buffer); + const z = new Zip(Buffer.from(buffer)); + for (const e of z.getEntries()) { + if (e.entryName.match(/document.xml|header[1-9].xml/)) { + expect(z.readAsText(e)).toMatchSnapshot(); + } + } + }); + + test("should write evenFooter with table for default section", () => { + const p = new w.Paragraph().addRun(new w.Run().addText("Hello Footer!!")); + const table = new w.Table().addRow( + new w.TableRow().addCell(new w.TableCell().addParagraph(p)) + ); + const footer = new w.Footer().addTable(table); + const buffer = new w.Docx().evenFooter(footer).build(); + writeFileSync("../output/even_footer_with_table.docx", buffer); + const z = new Zip(Buffer.from(buffer)); + for (const e of z.getEntries()) { + if (e.entryName.match(/document.xml|footer[1-9].xml/)) { + expect(z.readAsText(e)).toMatchSnapshot(); + } + } + }); });