diff --git a/src/file/paragraph/formatting/spacing.ts b/src/file/paragraph/formatting/spacing.ts index a40391c761..4f646b33a6 100644 --- a/src/file/paragraph/formatting/spacing.ts +++ b/src/file/paragraph/formatting/spacing.ts @@ -18,9 +18,9 @@ class SpacingAttributes extends XmlAttributeComponent { } export class Spacing extends XmlComponent { - constructor(opts: ISpacingProperties) { + constructor(options: ISpacingProperties) { super("w:spacing"); - this.root.push(new SpacingAttributes(opts)); + this.root.push(new SpacingAttributes(options)); } } diff --git a/src/file/paragraph/paragraph.spec.ts b/src/file/paragraph/paragraph.spec.ts index cafe231325..2fa6dfedc2 100644 --- a/src/file/paragraph/paragraph.spec.ts +++ b/src/file/paragraph/paragraph.spec.ts @@ -4,6 +4,7 @@ import { Formatter } from "export/formatter"; import * as file from "file"; import { Numbering } from "../numbering"; +import { LeaderType } from "./formatting"; describe("Paragraph", () => { let paragraph: file.Paragraph; @@ -86,6 +87,48 @@ describe("Paragraph", () => { }); }); + describe("#heading4()", () => { + it("should add heading style to JSON", () => { + paragraph.heading4(); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [ + { + "w:pPr": [{ "w:pStyle": [{ _attr: { "w:val": "Heading4" } }] }], + }, + ], + }); + }); + }); + + describe("#heading5()", () => { + it("should add heading style to JSON", () => { + paragraph.heading5(); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [ + { + "w:pPr": [{ "w:pStyle": [{ _attr: { "w:val": "Heading5" } }] }], + }, + ], + }); + }); + }); + + describe("#heading6()", () => { + it("should add heading style to JSON", () => { + paragraph.heading6(); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [ + { + "w:pPr": [{ "w:pStyle": [{ _attr: { "w:val": "Heading6" } }] }], + }, + ], + }); + }); + }); + describe("#title()", () => { it("should add title style to JSON", () => { paragraph.title(); @@ -114,6 +157,235 @@ describe("Paragraph", () => { }); }); + describe("#left()", () => { + it("should add left alignment to JSON", () => { + paragraph.left(); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [ + { + "w:pPr": [{ "w:jc": [{ _attr: { "w:val": "left" } }] }], + }, + ], + }); + }); + }); + + describe("#right()", () => { + it("should add right alignment to JSON", () => { + paragraph.right(); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [ + { + "w:pPr": [{ "w:jc": [{ _attr: { "w:val": "right" } }] }], + }, + ], + }); + }); + }); + + describe("#start()", () => { + it("should add start alignment to JSON", () => { + paragraph.start(); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [ + { + "w:pPr": [{ "w:jc": [{ _attr: { "w:val": "start" } }] }], + }, + ], + }); + }); + }); + + describe("#end()", () => { + it("should add end alignment to JSON", () => { + paragraph.end(); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [ + { + "w:pPr": [{ "w:jc": [{ _attr: { "w:val": "end" } }] }], + }, + ], + }); + }); + }); + + describe("#distribute()", () => { + it("should add distribute alignment to JSON", () => { + paragraph.distribute(); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [ + { + "w:pPr": [{ "w:jc": [{ _attr: { "w:val": "distribute" } }] }], + }, + ], + }); + }); + }); + + describe("#justified()", () => { + it("should add justified alignment to JSON", () => { + paragraph.justified(); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [ + { + "w:pPr": [{ "w:jc": [{ _attr: { "w:val": "both" } }] }], + }, + ], + }); + }); + }); + + describe("#maxRightTabStop()", () => { + it("should add maxRightTabStop to JSON", () => { + paragraph.maxRightTabStop(); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [ + { + "w:pPr": [ + { + "w:tabs": [ + { + "w:tab": [ + { + _attr: { + "w:pos": 9026, + "w:val": "right", + }, + }, + ], + }, + ], + }, + ], + }, + ], + }); + }); + }); + + describe("#leftTabStop()", () => { + it("should add leftTabStop to JSON", () => { + paragraph.leftTabStop(100, LeaderType.HYPHEN); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [ + { + "w:pPr": [ + { + "w:tabs": [ + { + "w:tab": [ + { + _attr: { + "w:pos": 100, + "w:val": "left", + "w:leader": "hyphen", + }, + }, + ], + }, + ], + }, + ], + }, + ], + }); + }); + }); + + describe("#rightTabStop()", () => { + it("should add rightTabStop to JSON", () => { + paragraph.rightTabStop(100, LeaderType.DOT); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [ + { + "w:pPr": [ + { + "w:tabs": [ + { + "w:tab": [ + { + _attr: { + "w:pos": 100, + "w:val": "right", + "w:leader": "dot", + }, + }, + ], + }, + ], + }, + ], + }, + ], + }); + }); + }); + + describe("#centerTabStop()", () => { + it("should add centerTabStop to JSON", () => { + paragraph.centerTabStop(100, LeaderType.MIDDLE_DOT); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [ + { + "w:pPr": [ + { + "w:tabs": [ + { + "w:tab": [ + { + _attr: { + "w:pos": 100, + "w:val": "center", + "w:leader": "middleDot", + }, + }, + ], + }, + ], + }, + ], + }, + ], + }); + }); + }); + + describe("#contextualSpacing()", () => { + it("should add contextualSpacing to JSON, and set 1 if true", () => { + paragraph.contextualSpacing(true); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [ + { + "w:pPr": [{ "w:contextualSpacing": [{ _attr: { "w:val": 1 } }] }], + }, + ], + }); + }); + + it("should add contextualSpacing to JSON, and set 0 if false", () => { + paragraph.contextualSpacing(false); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [ + { + "w:pPr": [{ "w:contextualSpacing": [{ _attr: { "w:val": 0 } }] }], + }, + ], + }); + }); + }); + describe("#thematicBreak()", () => { it("should add thematic break to JSON", () => { paragraph.thematicBreak(); diff --git a/src/file/paragraph/paragraph.ts b/src/file/paragraph/paragraph.ts index fc6c46d50b..fdcc5ed83b 100644 --- a/src/file/paragraph/paragraph.ts +++ b/src/file/paragraph/paragraph.ts @@ -241,11 +241,6 @@ export class Paragraph extends XmlComponent { return this; } - public addTabStop(run: Run): Paragraph { - this.root.splice(1, 0, run); - return this; - } - public addSequentialIdentifier(identifier: string): Paragraph { this.root.push(new SequentialIdentifier(identifier)); return this;