TOC content generation aborted
This commit is contained in:
@ -8,16 +8,14 @@ import { FootNotes } from "./footnotes";
|
||||
import { HeaderWrapper } from "./header-wrapper";
|
||||
import { Image, Media } from "./media";
|
||||
import { Numbering } from "./numbering";
|
||||
import { Bookmark, Hyperlink, Paragraph, Run, TextRun } from "./paragraph";
|
||||
import { Begin, End, Separate } from "./paragraph/run/field";
|
||||
import { Tab } from "./paragraph/run/tab";
|
||||
import { Bookmark, Hyperlink, Paragraph } from "./paragraph";
|
||||
import { Relationships } from "./relationships";
|
||||
import { Settings } from "./settings";
|
||||
import { Styles } from "./styles";
|
||||
import { ExternalStylesFactory } from "./styles/external-styles-factory";
|
||||
import { DefaultStylesFactory } from "./styles/factory";
|
||||
import { Table } from "./table";
|
||||
import { PageReferenceInstruction, TableOfContents } from "./table-of-contents";
|
||||
import { TableOfContents } from "./table-of-contents";
|
||||
|
||||
export class File {
|
||||
private readonly document: Document;
|
||||
@ -36,7 +34,6 @@ export class File {
|
||||
private readonly appProperties: AppProperties;
|
||||
|
||||
private currentRelationshipId: number = 1;
|
||||
private currentTocBookmarkId: number = 1;
|
||||
|
||||
constructor(options?: IPropertiesOptions, sectionPropertiesOptions?: SectionPropertiesOptions) {
|
||||
if (!options) {
|
||||
@ -297,76 +294,9 @@ export class File {
|
||||
return this.settings;
|
||||
}
|
||||
|
||||
public generateTablesOfContents(): void {
|
||||
// console.log("generateTablesOfContents");
|
||||
const TOCs = this.document.getTablesOfContents();
|
||||
if (TOCs && TOCs.length) {
|
||||
public verifyUpdateFields(): void {
|
||||
if (this.document.getTablesOfContents().length) {
|
||||
this.settings.addUpdateFields();
|
||||
TOCs.forEach((child) => this.generateContent(child));
|
||||
}
|
||||
}
|
||||
|
||||
private generateContent(toc: TableOfContents): void {
|
||||
// console.log("TOC", JSON.stringify(toc));
|
||||
if (toc.getHeaderRange()) {
|
||||
this.generateContentForHeaderRange(toc);
|
||||
}
|
||||
}
|
||||
|
||||
private generateContentForHeaderRange(toc: TableOfContents): void {
|
||||
const headerRange = toc.getHeaderRange();
|
||||
const hyphenIndex = headerRange.indexOf("-");
|
||||
// console.log("Hyphen Index: ", hyphenIndex);
|
||||
if (hyphenIndex !== -1) {
|
||||
const rangeBegin = parseInt(headerRange.substring(0, hyphenIndex), 10);
|
||||
const rangeEnd = parseInt(headerRange.substring(hyphenIndex + 1), 10);
|
||||
const styles = new Array<string>();
|
||||
for (let i = rangeBegin; i <= rangeEnd; i++) {
|
||||
styles.push(`Heading${i}`);
|
||||
}
|
||||
// console.log("Find Headers for range ", rangeBegin, " - ", rangeEnd, styles.join(","));
|
||||
this.document
|
||||
.getParagraphs()
|
||||
.filter((paragraph) => this.paragraphContainAnyStyle(paragraph, styles))
|
||||
.forEach((paragraph) => this.generateContentForParagraph(paragraph, toc));
|
||||
} else {
|
||||
throw new Error(`Invalid headerRange: '${headerRange}'`);
|
||||
}
|
||||
}
|
||||
|
||||
private paragraphContainAnyStyle(paragraph: Paragraph, styles: string[]): boolean {
|
||||
return paragraph.getStyles().some((style) => styles.indexOf(style.styleId) !== -1);
|
||||
}
|
||||
|
||||
private generateContentForParagraph(paragraph: Paragraph, toc: TableOfContents): void {
|
||||
const bookmarkId = `_TOC_${this.currentTocBookmarkId}`;
|
||||
// console.log("Generating content for paragraph: ", bookmarkId);
|
||||
|
||||
// deep clone the original paragraph
|
||||
const generatedParagraph = paragraph.clone() as Paragraph;
|
||||
|
||||
generatedParagraph.clearPageBreaks().maxRightTabStop("dot");
|
||||
|
||||
const tabRun = new Run();
|
||||
tabRun.addChildElement(new Tab());
|
||||
generatedParagraph.addChildElement(tabRun);
|
||||
|
||||
const beginRun = new Run();
|
||||
beginRun.addChildElement(new Begin(true));
|
||||
beginRun.addChildElement(new PageReferenceInstruction(bookmarkId));
|
||||
beginRun.addChildElement(new Separate());
|
||||
generatedParagraph.addRun(beginRun);
|
||||
|
||||
generatedParagraph.addRun(new TextRun("?"));
|
||||
|
||||
const endRun = new Run();
|
||||
endRun.addChildElement(new End());
|
||||
generatedParagraph.addRun(endRun);
|
||||
|
||||
toc.addGeneratedContent(generatedParagraph);
|
||||
|
||||
paragraph.addBookmark(this.createBookmark(bookmarkId, ""));
|
||||
// console.log("Paragraph after content generation", JSON.stringify(paragraph, null, 2));
|
||||
this.currentTocBookmarkId++;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user