diff --git a/demo/demo27.ts b/demo/demo27.ts index 34798a377f..23cf91165f 100644 --- a/demo/demo27.ts +++ b/demo/demo27.ts @@ -5,6 +5,8 @@ import { Document, Packer, Paragraph, TableOfContents } from "../build"; const doc = new Document(); +// WordprocessingML docs for TableOfContents can be found here: +// http://officeopenxml.com/WPtableOfContents.php // Creates an table of contents with default properties const toc = new TableOfContents(); diff --git a/src/file/table-of-contents/instruction.ts b/src/file/table-of-contents/instruction.ts index eb67cbd49b..fae0339313 100644 --- a/src/file/table-of-contents/instruction.ts +++ b/src/file/table-of-contents/instruction.ts @@ -4,10 +4,54 @@ class TextAttributes extends XmlAttributeComponent<{ space: "default" | "preserv protected xmlKeys = { space: "xml:space" }; } +/** + * Options according to this docs: + * http://officeopenxml.com/WPtableOfContents.php + */ + +export class StyleLevel { + public styleName: string; + public level: number; +} +export class TableOfContentsInstructionProperties { + // \b option + public entriesFromSession: string; + // \h option + public hiperlink: true; + // \n option + public entryLevelsRange = "1-6"; + // \o option + public headerRange: string; + // \t option + public styles: StyleLevel[]; + // \z option +} + export class TableOfContentsInstruction extends XmlComponent { - constructor() { + private readonly properties: TableOfContentsInstructionProperties; + + constructor(properties?: TableOfContentsInstructionProperties) { super("w:instrText"); + this.properties = properties || new TableOfContentsInstructionProperties(); + this.root.push(new TextAttributes({ space: "preserve" })); - this.root.push("TOC"); + let instruction = "TOC"; + if (this.properties.entriesFromSession) { + instruction = `${instruction} \b "${this.properties.entriesFromSession}"`; + } + if (this.properties.hiperlink) { + instruction = `${instruction} \h`; + } + if (this.properties.entryLevelsRange) { + instruction = `${instruction} \n "${this.properties.entryLevelsRange}"`; + } + if (this.properties.headerRange) { + instruction = `${instruction} \o "${this.properties.headerRange}"`; + } + if (this.properties.styles && this.properties.styles.length) { + const styles = this.properties.styles.map((sl) => `${sl.styleName}, ${sl.level}`).join(", "); + instruction = `${instruction} \t "${styles}"`; + } + this.root.push(instruction); } } diff --git a/src/file/table-of-contents/table-of-contents.spec.ts b/src/file/table-of-contents/table-of-contents.spec.ts index 98560fd00a..864f1c36cd 100644 --- a/src/file/table-of-contents/table-of-contents.spec.ts +++ b/src/file/table-of-contents/table-of-contents.spec.ts @@ -33,7 +33,7 @@ const DEFAULT_TOC = { "xml:space": "preserve", }, }, - "TOC", + 'TOC \n "1-6"', ], }, {