Skip to content

Commit 09fb2d0

Browse files
committed
chore(table-of-contents): clean up
This commit can be squashed with the previous one if necessary
1 parent 22876f9 commit 09fb2d0

File tree

1 file changed

+59
-53
lines changed

1 file changed

+59
-53
lines changed

src/file/table-of-contents/table-of-contents.ts

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -25,70 +25,76 @@ export class TableOfContents extends FileChild {
2525

2626
const content = new StructuredDocumentTagContent();
2727

28-
const beginParagraphChildren =
29-
cachedContent.length >= 1 ? [this.buildCachedContentParagraphChild(cachedContent[0], properties)] : [];
30-
31-
const getTabStops = (level: number): readonly TabStopDefinition[] => {
32-
const levelSpace = 720;
33-
const levelPosition = 9026 - (level - 1) * levelSpace; // TODO: should be equal to page width + 1 - level margin
34-
return [
35-
{
36-
type: "clear",
37-
position: levelPosition,
38-
},
39-
{
40-
type: "right",
41-
position: 9025, // TODO: should be equal to page width
42-
leader: "dot",
43-
},
44-
];
45-
};
46-
47-
const beginParagraph = new Paragraph({
48-
style: cachedContent.length >= 1 ? `TOC${cachedContent[0].level}` : undefined,
49-
tabStops: getTabStops(cachedContent.length >= 1 ? cachedContent[0].level : 1),
50-
children: [
51-
new Run({
52-
children: [new Begin(true), new FieldInstruction(properties), new Separate()],
53-
}),
54-
...beginParagraphChildren,
55-
],
28+
const beginParagraphMandatoryChidlren = [
29+
new Run({
30+
children: [new Begin(true), new FieldInstruction(properties), new Separate()],
31+
}),
32+
];
33+
34+
const endParagraphMandatoryChildren = [
35+
new Run({
36+
children: [new End()],
37+
}),
38+
];
39+
40+
const cachedParagraphs = cachedContent.map((entry, i) => {
41+
const contentChild = this.buildCachedContentParagraphChild(entry, properties);
42+
const children =
43+
i === 0
44+
? [...beginParagraphMandatoryChidlren, contentChild]
45+
: i === cachedContent.length - 1
46+
? [contentChild, ...endParagraphMandatoryChildren]
47+
: [contentChild];
48+
49+
return new Paragraph({
50+
style: `TOC${entry.level}`,
51+
tabStops: this.getTabStopsForLevel(entry.level),
52+
children,
53+
});
5654
});
5755

58-
content.addChildElement(beginParagraph);
59-
60-
const middleParagraphs = cachedContent.slice(1, -1).map(
61-
(entry) =>
56+
let paragraphs = cachedParagraphs;
57+
if (cachedContent.length <= 0) {
58+
paragraphs = [
59+
new Paragraph({
60+
children: beginParagraphMandatoryChidlren,
61+
}),
62+
new Paragraph({
63+
children: endParagraphMandatoryChildren,
64+
}),
65+
];
66+
} else if (cachedContent.length <= 1) {
67+
paragraphs = [
68+
...cachedParagraphs,
6269
new Paragraph({
63-
style: `TOC${entry.level}`,
64-
tabStops: getTabStops(entry.level),
65-
children: [this.buildCachedContentParagraphChild(entry, properties)],
70+
children: endParagraphMandatoryChildren,
6671
}),
67-
);
72+
];
73+
}
6874

69-
for (const paragraph of middleParagraphs) {
75+
for (const paragraph of paragraphs) {
7076
content.addChildElement(paragraph);
7177
}
7278

73-
const endParagraphChildren =
74-
cachedContent.length > 1 ? [this.buildCachedContentParagraphChild(cachedContent[cachedContent.length - 1], properties)] : [];
75-
76-
const endParagraph = new Paragraph({
77-
style: cachedContent.length > 1 ? `TOC${cachedContent[cachedContent.length - 1].level}` : undefined,
78-
tabStops: getTabStops(cachedContent.length > 1 ? cachedContent[cachedContent.length - 1].level : 1),
79-
children: [
80-
...endParagraphChildren,
81-
new Run({
82-
children: [new End()],
83-
}),
84-
],
85-
});
86-
87-
content.addChildElement(endParagraph);
88-
8979
this.root.push(content);
9080
}
9181

82+
private getTabStopsForLevel(level: number, pageWidth: number = 9025): readonly TabStopDefinition[] {
83+
const levelSpace = 720;
84+
const levelPosition = pageWidth + 1 - (level - 1) * levelSpace; // TODO: should be equal to page width + 1 - level margin
85+
return [
86+
{
87+
type: "clear",
88+
position: levelPosition,
89+
},
90+
{
91+
type: "right",
92+
position: pageWidth,
93+
leader: "dot",
94+
},
95+
];
96+
}
97+
9298
private buildCachedContentRun(entry: ToCEntry, properties?: ITableOfContentsOptions): Run {
9399
return new Run({
94100
style: properties?.hyperlink && entry.href !== undefined ? "IndexLink" : undefined,

0 commit comments

Comments
 (0)