table of contents with all the options

This commit is contained in:
Sergio Mendonça
2018-09-25 01:18:47 -03:00
parent 4de6b51e76
commit 808c5b00a0
6 changed files with 300 additions and 56 deletions

View File

@ -1,61 +1,68 @@
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { TableOfContentsProperties } from "./table-of-contents-properties";
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;
private readonly properties: TableOfContentsProperties;
constructor(properties?: TableOfContentsInstructionProperties) {
constructor(properties?: TableOfContentsProperties) {
super("w:instrText");
this.properties = properties || new TableOfContentsInstructionProperties();
this.properties = properties || new TableOfContentsProperties();
this.root.push(new TextAttributes({ space: "preserve" }));
let instruction = "TOC";
if (this.properties.entriesFromSession) {
instruction = `${instruction} \\b "${this.properties.entriesFromSession}"`;
if (this.properties.captionLabel) {
instruction = `${instruction} \\a "${this.properties.captionLabel}"`;
}
if (this.properties.entriesFromBookmark) {
instruction = `${instruction} \\b "${this.properties.entriesFromBookmark}"`;
}
if (this.properties.captionLabelIncludingNumbers) {
instruction = `${instruction} \\c "${this.properties.captionLabelIncludingNumbers}"`;
}
if (this.properties.sequenceAndPageNumbersSeparator) {
instruction = `${instruction} \\d "${this.properties.sequenceAndPageNumbersSeparator}"`;
}
if (this.properties.tcFieldIdentifier) {
instruction = `${instruction} \\f "${this.properties.tcFieldIdentifier}"`;
}
if (this.properties.hiperlink) {
instruction = `${instruction} \\h`;
}
if (this.properties.entryLevelsRange) {
instruction = `${instruction} \\n "${this.properties.entryLevelsRange}"`;
if (this.properties.tcFieldLevelRange) {
instruction = `${instruction} \\l "${this.properties.tcFieldLevelRange}`;
}
if (this.properties.headerRange) {
instruction = `${instruction} \\o "${this.properties.headerRange}"`;
if (this.properties.pageNumbersEntryLevelsRange) {
instruction = `${instruction} \\n "${this.properties.pageNumbersEntryLevelsRange}`;
}
if (this.properties.styles && this.properties.styles.length) {
const styles = this.properties.styles.map((sl) => `${sl.styleName}, ${sl.level}`).join(", ");
if (this.properties.headingStyleRange) {
instruction = `${instruction} \\o "${this.properties.headingStyleRange}`;
}
if (this.properties.entryAndPageNumberSeparator) {
instruction = `${instruction} \\p "${this.properties.entryAndPageNumberSeparator}`;
}
if (this.properties.seqFieldIdentifierForPrefix) {
instruction = `${instruction} \\s "${this.properties.seqFieldIdentifierForPrefix}`;
}
if (this.properties.stylesWithLevels && this.properties.stylesWithLevels.length) {
const styles = this.properties.stylesWithLevels.map((sl) => `${sl.styleName},${sl.level}`).join(",");
instruction = `${instruction} \\t "${styles}"`;
}
if (this.properties.useAppliedParagraphOutlineLevel) {
instruction = `${instruction} \\u`;
}
if (this.properties.preserveTabInEntries) {
instruction = `${instruction} \\w`;
}
if (this.properties.preserveNewLineInEntries) {
instruction = `${instruction} \\x`;
}
if (this.properties.hideTabAndPageNumbersInWebView) {
instruction = `${instruction} \\z`;
}
this.root.push(instruction);
}
public getHeaderRange(): string {
return this.properties.headerRange;
}
}