diff --git a/demo/demo27.ts b/demo/demo27.ts index c4fe6c44b3..bbba325052 100644 --- a/demo/demo27.ts +++ b/demo/demo27.ts @@ -24,5 +24,5 @@ doc.addParagraph(new Paragraph("I'm a another text very nicely written.'")); const packer = new Packer(); packer.toBuffer(doc).then((buffer) => { - fs.writeFileSync("My Document.docx", buffer); + fs.writeFileSync("tmp/My Document.docx", buffer); }); diff --git a/src/file/file.ts b/src/file/file.ts index 484f69bd2d..9e45e885ac 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -9,7 +9,7 @@ import { FootNotes } from "./footnotes"; import { HeaderWrapper } from "./header-wrapper"; import { Image, Media } from "./media"; import { Numbering } from "./numbering"; -import { Bookmark, Hyperlink, Paragraph, Run } from "./paragraph"; +import { Bookmark, Hyperlink, Paragraph, Run, TextRun } from "./paragraph"; import { Begin, End, Separate } from "./paragraph/run/field"; import { Tab } from "./paragraph/run/tab"; import { Relationships } from "./relationships"; @@ -341,6 +341,8 @@ export class File { beginRun.addChildElement(new Separate()); generatedParagraph.addRun(beginRun); + generatedParagraph.addRun(new TextRun("?")); + const endRun = new Run(); endRun.addChildElement(new End()); generatedParagraph.addRun(endRun); diff --git a/src/file/table-of-contents/alias.ts b/src/file/table-of-contents/alias.ts new file mode 100644 index 0000000000..d3a3235391 --- /dev/null +++ b/src/file/table-of-contents/alias.ts @@ -0,0 +1,12 @@ +import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; + +class AliasAttributes extends XmlAttributeComponent<{ alias: string }> { + protected xmlKeys = { alias: "w:val" }; +} + +export class Alias extends XmlComponent { + constructor(alias: string) { + super("w:alias"); + this.root.push(new AliasAttributes({ alias })); + } +} diff --git a/src/file/table-of-contents/std-content.ts b/src/file/table-of-contents/std-content.ts new file mode 100644 index 0000000000..cbab4c8440 --- /dev/null +++ b/src/file/table-of-contents/std-content.ts @@ -0,0 +1,12 @@ +import { Paragraph } from "file/paragraph"; +import { XmlComponent } from "file/xml-components"; + +export class StdContent extends XmlComponent { + constructor() { + super("w:stdContent"); + } + + public addGeneratedContent(paragraph: Paragraph): void { + this.root.splice(this.root.length - 1, 0, paragraph); + } +} diff --git a/src/file/table-of-contents/std-properties.ts b/src/file/table-of-contents/std-properties.ts new file mode 100644 index 0000000000..628e92840c --- /dev/null +++ b/src/file/table-of-contents/std-properties.ts @@ -0,0 +1,9 @@ +import { XmlComponent } from "file/xml-components"; +import { Alias } from "./alias"; + +export class StdProperties extends XmlComponent { + constructor(alias: string) { + super("w:stdPr"); + this.root.push(new Alias(alias)); + } +} diff --git a/src/file/table-of-contents/table-of-contents.ts b/src/file/table-of-contents/table-of-contents.ts index e9984f073e..7a31373143 100644 --- a/src/file/table-of-contents/table-of-contents.ts +++ b/src/file/table-of-contents/table-of-contents.ts @@ -1,21 +1,27 @@ // import { TableOfContentsProperties } from "./properties"; -import { Paragraph, ParagraphProperties } from "file/paragraph"; +import { Paragraph } from "file/paragraph"; import { Run } from "file/paragraph/run"; import { Begin, End, Separate } from "file/paragraph/run/field"; import { XmlComponent } from "file/xml-components"; +import { StdContent } from "./std-content"; +import { StdProperties } from "./std-properties"; import { TableOfContentsInstruction } from "./table-of-contents-instruction"; export class TableOfContents extends XmlComponent { // private readonly tocProperties: TableOfContentsProperties; - private readonly properties: ParagraphProperties; + private readonly properties: StdProperties; + + private readonly content: StdContent; private readonly instruction: TableOfContentsInstruction; constructor(/*tocProperties?: TableOfContentsProperties*/) { super("w:sdt"); - this.properties = new ParagraphProperties(); + this.properties = new StdProperties("Table of Contents"); + this.content = new StdContent(); this.instruction = new TableOfContentsInstruction(); this.root.push(this.properties); + this.root.push(this.content); // this.tocProperties = tocProperties || new TableOfContentsProperties(); const beginParagraph = new Paragraph(); const beginRun = new Run(); @@ -23,13 +29,13 @@ export class TableOfContents extends XmlComponent { beginRun.addChildElement(this.instruction); beginRun.addChildElement(new Separate()); beginParagraph.addRun(beginRun); - this.root.push(beginParagraph); + this.content.addChildElement(beginParagraph); const endParagraph = new Paragraph(); const endRun = new Run(); endRun.addChildElement(new End()); endParagraph.addRun(endRun); - this.root.push(endParagraph); + this.content.addChildElement(endParagraph); } public getHeaderRange(): string { @@ -37,6 +43,6 @@ export class TableOfContents extends XmlComponent { } public addGeneratedContent(paragraph: Paragraph): void { - this.root.splice(this.root.length - 1, 0, paragraph); + this.content.addGeneratedContent(paragraph); } }