Files
docx-js/src/file/table-of-contents/field-instruction.ts

77 lines
3.1 KiB
TypeScript
Raw Normal View History

2018-09-26 02:17:39 +01:00
// http://officeopenxml.com/WPfieldInstructions.php
2018-09-03 11:20:36 -03:00
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { ITableOfContentsOptions } from "./table-of-contents-properties";
2018-09-03 11:20:36 -03:00
enum SpaceType {
DEFAULT = "default",
PRESERVE = "preserve",
}
class TextAttributes extends XmlAttributeComponent<{ space: SpaceType }> {
2018-09-03 11:20:36 -03:00
protected xmlKeys = { space: "xml:space" };
}
2018-09-26 02:17:39 +01:00
export class FieldInstruction extends XmlComponent {
private readonly properties: ITableOfContentsOptions;
constructor(properties: ITableOfContentsOptions = {}) {
2018-09-03 11:20:36 -03:00
super("w:instrText");
2018-09-25 20:05:35 +01:00
this.properties = properties;
this.root.push(new TextAttributes({ space: SpaceType.PRESERVE }));
let instruction = "TOC";
2018-09-25 20:05:35 +01:00
2018-09-25 01:18:47 -03:00
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.hyperlink) {
2018-09-04 17:29:24 -03:00
instruction = `${instruction} \\h`;
}
2018-09-25 01:18:47 -03:00
if (this.properties.tcFieldLevelRange) {
instruction = `${instruction} \\l "${this.properties.tcFieldLevelRange}"`;
2018-09-25 01:18:47 -03:00
}
if (this.properties.pageNumbersEntryLevelsRange) {
instruction = `${instruction} \\n "${this.properties.pageNumbersEntryLevelsRange}"`;
}
2018-09-25 01:18:47 -03:00
if (this.properties.headingStyleRange) {
instruction = `${instruction} \\o "${this.properties.headingStyleRange}"`;
}
2018-09-25 01:18:47 -03:00
if (this.properties.entryAndPageNumberSeparator) {
instruction = `${instruction} \\p "${this.properties.entryAndPageNumberSeparator}"`;
2018-09-25 01:18:47 -03:00
}
if (this.properties.seqFieldIdentifierForPrefix) {
instruction = `${instruction} \\s "${this.properties.seqFieldIdentifierForPrefix}"`;
2018-09-25 01:18:47 -03:00
}
if (this.properties.stylesWithLevels && this.properties.stylesWithLevels.length) {
const styles = this.properties.stylesWithLevels.map((sl) => `${sl.styleName},${sl.level}`).join(",");
2018-09-04 17:29:24 -03:00
instruction = `${instruction} \\t "${styles}"`;
}
2018-09-25 01:18:47 -03:00
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);
2018-09-03 11:20:36 -03:00
}
}