first simple version of TOC working
This commit is contained in:
13
src/file/table-of-contents/instruction.ts
Normal file
13
src/file/table-of-contents/instruction.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
class TextAttributes extends XmlAttributeComponent<{ space: "default" | "preserve" }> {
|
||||
protected xmlKeys = { space: "xml:space" };
|
||||
}
|
||||
|
||||
export class TableOfContentsInstruction extends XmlComponent {
|
||||
constructor() {
|
||||
super("w:instrText");
|
||||
this.root.push(new TextAttributes({ space: "preserve" }));
|
||||
this.root.push("TOC");
|
||||
}
|
||||
}
|
@ -9,9 +9,39 @@ const DEFAULT_TOC = {
|
||||
"w:pPr": [],
|
||||
},
|
||||
{
|
||||
"w:r": [
|
||||
"w:fldChar": [
|
||||
{
|
||||
"w:fldChar": [],
|
||||
_attr: {
|
||||
"w:fldCharType": "begin",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"w:fldChar": [
|
||||
{
|
||||
_attr: {
|
||||
"w:fldCharType": "separate",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"w:instrText": [
|
||||
{
|
||||
_attr: {
|
||||
"xml:space": "preserve",
|
||||
},
|
||||
},
|
||||
"TOC",
|
||||
],
|
||||
},
|
||||
{
|
||||
"w:fldChar": [
|
||||
{
|
||||
_attr: {
|
||||
"w:fldCharType": "end",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
@ -23,6 +53,7 @@ describe("Table of Contents", () => {
|
||||
it("should construct a TOC with default options", () => {
|
||||
const toc = new TableOfContents();
|
||||
const tree = new Formatter().format(toc);
|
||||
// console.log(JSON.stringify(tree, null, 2));
|
||||
expect(tree).to.be.deep.equal(DEFAULT_TOC);
|
||||
});
|
||||
});
|
||||
|
@ -1,5 +1,7 @@
|
||||
// import { TableOfContentsProperties } from "./properties";
|
||||
import { Paragraph } from "../paragraph";
|
||||
import { Paragraph } from "file/paragraph";
|
||||
import { Begin, End, Separate } from "file/paragraph/run/field";
|
||||
import { TableOfContentsInstruction } from "./instruction";
|
||||
|
||||
export class TableOfContents extends Paragraph {
|
||||
// private readonly tocProperties: TableOfContentsProperties;
|
||||
@ -7,5 +9,9 @@ export class TableOfContents extends Paragraph {
|
||||
constructor(/*tocProperties?: TableOfContentsProperties*/) {
|
||||
super();
|
||||
// this.tocProperties = tocProperties || new TableOfContentsProperties();
|
||||
this.root.push(new Begin());
|
||||
this.root.push(new Separate());
|
||||
this.root.push(new TableOfContentsInstruction());
|
||||
this.root.push(new End());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user