generating the content for a table of contents

This commit is contained in:
Sergio Mendonça
2018-09-18 05:24:19 -03:00
parent 0eb36be053
commit 8e911698a5
13 changed files with 168 additions and 38 deletions

View File

@ -0,0 +1,61 @@
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
class TextAttributes extends XmlAttributeComponent<{ space: "default" | "preserve" }> {
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: string;
// \o option
public headerRange = "1-6";
// \t option
public styles: StyleLevel[];
// \z option
}
export class TableOfContentsInstruction extends XmlComponent {
private readonly properties: TableOfContentsInstructionProperties;
constructor(properties?: TableOfContentsInstructionProperties) {
super("w:instrText");
this.properties = properties || new TableOfContentsInstructionProperties();
this.root.push(new TextAttributes({ space: "preserve" }));
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);
}
public getHeaderRange(): string {
return this.properties.headerRange;
}
}