diff --git a/src/file/paragraph/links/index.ts b/src/file/paragraph/links/index.ts index 09084aa8c7..82715b62e8 100644 --- a/src/file/paragraph/links/index.ts +++ b/src/file/paragraph/links/index.ts @@ -1,2 +1,3 @@ export * from "./hyperlink"; export * from "./bookmark"; +export * from "./outline-level"; diff --git a/src/file/paragraph/links/outline-level.spec.ts b/src/file/paragraph/links/outline-level.spec.ts new file mode 100644 index 0000000000..6ba188fd9d --- /dev/null +++ b/src/file/paragraph/links/outline-level.spec.ts @@ -0,0 +1,23 @@ +import { assert } from "chai"; + +import { Utility } from "tests/utility"; + +import { OutlineLevel } from "./outline-level"; + +describe("ParagraphOutlineLevel", () => { + let outlineLevel: OutlineLevel; + + describe("#constructor()", () => { + it("should create an outlineLevel with given value", () => { + outlineLevel = new OutlineLevel("0"); + const newJson = Utility.jsonify(outlineLevel); + assert.equal(newJson.root[0].root.val, "0"); + }); + + it("should create an outlineLevel with blank val", () => { + outlineLevel = new OutlineLevel(""); + const newJson = Utility.jsonify(outlineLevel); + assert.equal(newJson.root[0].root.val, ""); + }); + }); +}); diff --git a/src/file/paragraph/links/outline-level.ts b/src/file/paragraph/links/outline-level.ts new file mode 100644 index 0000000000..512638bcc7 --- /dev/null +++ b/src/file/paragraph/links/outline-level.ts @@ -0,0 +1,16 @@ +// http://officeopenxml.com/WPparagraph.php +import { Attributes, XmlComponent } from "file/xml-components"; + +export class OutlineLevel extends XmlComponent { + public readonly level: string; + + constructor(level: string) { + super("w:outlineLvl"); + this.level = level; + this.root.push( + new Attributes({ + val: level, + }), + ); + } +} diff --git a/src/file/paragraph/paragraph.spec.ts b/src/file/paragraph/paragraph.spec.ts index 2fa6dfedc2..f47850ac4d 100644 --- a/src/file/paragraph/paragraph.spec.ts +++ b/src/file/paragraph/paragraph.spec.ts @@ -666,4 +666,18 @@ describe("Paragraph", () => { }); }); }); + + describe("#outlineLevel", () => { + it("should set paragraph outline level to the given value", () => { + paragraph.outlineLevel("0"); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [ + { + "w:pPr": [{ "w:outlineLvl": [{ _attr: { "w:val": "0" } }] }], + }, + ], + }); + }); + }); }); diff --git a/src/file/paragraph/paragraph.ts b/src/file/paragraph/paragraph.ts index fdcc5ed83b..cfd32322c2 100644 --- a/src/file/paragraph/paragraph.ts +++ b/src/file/paragraph/paragraph.ts @@ -14,7 +14,7 @@ import { ContextualSpacing, ISpacingProperties, Spacing } from "./formatting/spa import { Style } from "./formatting/style"; import { CenterTabStop, LeaderType, LeftTabStop, MaxRightTabStop, RightTabStop } from "./formatting/tab-stop"; import { NumberProperties } from "./formatting/unordered-list"; -import { Bookmark, Hyperlink } from "./links"; +import { Bookmark, Hyperlink, OutlineLevel } from "./links"; import { ParagraphProperties } from "./properties"; import { PictureRun, Run, SequentialIdentifier, TextRun } from "./run"; @@ -245,4 +245,9 @@ export class Paragraph extends XmlComponent { this.root.push(new SequentialIdentifier(identifier)); return this; } + + public outlineLevel(level: string): Paragraph { + this.properties.push(new OutlineLevel(level)); + return this; + } }