From 0453f289516cc0ec51b2788bccee884f346d8b31 Mon Sep 17 00:00:00 2001 From: felipe Date: Fri, 14 Apr 2017 21:13:11 +0200 Subject: [PATCH] add keepLines and keepNext support for paragraph formatting This commit adds it to the three places where paragraph formatting can be applied right now (directly on the paragraph, paragraph styles, and bullets/numbering styles). I'm separately adding an entry to the wiki regarding what these methods do (widow/orphan control) --- ts/docx/paragraph/formatting.ts | 1 + ts/docx/paragraph/index.ts | 11 ++++++++++ ts/docx/paragraph/keep.ts | 13 ++++++++++++ ts/numbering/level.ts | 10 +++++++++ ts/styles/style/index.ts | 10 +++++++++ ts/tests/docx/paragraph/paragraphTests.ts | 20 +++++++++++++++++ ts/tests/numberingTest.ts | 20 +++++++++++++++++ ts/tests/stylesTest.ts | 26 +++++++++++++++++++++++ 8 files changed, 111 insertions(+) create mode 100644 ts/docx/paragraph/keep.ts diff --git a/ts/docx/paragraph/formatting.ts b/ts/docx/paragraph/formatting.ts index 8b76c0fa32..204afb528d 100644 --- a/ts/docx/paragraph/formatting.ts +++ b/ts/docx/paragraph/formatting.ts @@ -1,6 +1,7 @@ export { Alignment } from "./alignment"; export { ThematicBreak } from "./border"; export { Indent } from "./indent"; +export { KeepLines, KeepNext } from "./keep"; export { PageBreak } from "./page-break"; export { ParagraphProperties } from "./properties"; export { ISpacingProperties, Spacing } from "./spacing"; diff --git a/ts/docx/paragraph/index.ts b/ts/docx/paragraph/index.ts index 5c42381311..4657fcf016 100644 --- a/ts/docx/paragraph/index.ts +++ b/ts/docx/paragraph/index.ts @@ -8,6 +8,7 @@ import { XmlComponent } from "../xml-components"; import { Alignment } from "./alignment"; import { ThematicBreak } from "./border"; import { Indent } from "./indent"; +import { KeepLines, KeepNext } from "./keep"; import { PageBreak } from "./page-break"; import { ParagraphProperties } from "./properties"; import { ISpacingProperties, Spacing } from "./spacing"; @@ -140,4 +141,14 @@ export class Paragraph extends XmlComponent { this.properties.push(new Spacing(params)); return this; }; + + public keepNext(): Paragraph { + this.properties.push(new KeepNext()); + return this; + } + + public keepLines(): Paragraph { + this.properties.push(new KeepLines()); + return this; + } } diff --git a/ts/docx/paragraph/keep.ts b/ts/docx/paragraph/keep.ts new file mode 100644 index 0000000000..76f378faf0 --- /dev/null +++ b/ts/docx/paragraph/keep.ts @@ -0,0 +1,13 @@ +import { XmlComponent } from "../xml-components"; + +export class KeepLines extends XmlComponent { + constructor() { + super("w:keepLines"); + } +} + +export class KeepNext extends XmlComponent { + constructor() { + super("w:keepNext"); + } +} diff --git a/ts/numbering/level.ts b/ts/numbering/level.ts index 8f8cf7bce6..55f4080385 100644 --- a/ts/numbering/level.ts +++ b/ts/numbering/level.ts @@ -205,6 +205,16 @@ class LevelBase extends XmlComponent { this.addParagraphProperty(new paragraph.Spacing(params)); return this; }; + + public keepNext(): Level { + this.addParagraphProperty(new paragraph.KeepNext()); + return this; + } + + public keepLines(): Level { + this.addParagraphProperty(new paragraph.KeepLines()); + return this; + } } export class Level extends LevelBase { diff --git a/ts/styles/style/index.ts b/ts/styles/style/index.ts index e70936655b..f4775c7e3a 100644 --- a/ts/styles/style/index.ts +++ b/ts/styles/style/index.ts @@ -180,6 +180,16 @@ export class ParagraphStyle extends Style { this.addParagraphProperty(new paragraph.Spacing(params)); return this; }; + + public keepNext(): ParagraphStyle { + this.addParagraphProperty(new paragraph.KeepNext()); + return this; + } + + public keepLines(): ParagraphStyle { + this.addParagraphProperty(new paragraph.KeepLines()); + return this; + } } export class HeadingStyle extends ParagraphStyle { diff --git a/ts/tests/docx/paragraph/paragraphTests.ts b/ts/tests/docx/paragraph/paragraphTests.ts index 8562965d61..1209fecea1 100644 --- a/ts/tests/docx/paragraph/paragraphTests.ts +++ b/ts/tests/docx/paragraph/paragraphTests.ts @@ -269,4 +269,24 @@ describe("Paragraph", () => { }); }); }); + + describe("#keepLines", () => { + it("should set the paragraph keepLines sub-component", () => { + paragraph.keepLines(); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [{"w:pPr": [{"w:keepLines": []}]}], + }); + }); + }); + + describe("#keepNext", () => { + it("should set the paragraph keepNext sub-component", () => { + paragraph.keepNext(); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [{"w:pPr": [{"w:keepNext": []}]}], + }); + }); + }); }); diff --git a/ts/tests/numberingTest.ts b/ts/tests/numberingTest.ts index f280a00dc4..81f24068d2 100644 --- a/ts/tests/numberingTest.ts +++ b/ts/tests/numberingTest.ts @@ -222,6 +222,26 @@ describe("AbstractNumbering", () => { ], }); }); + + it("#keepLines", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .keepLines(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:pPr": [{"w:keepLines": []}], + }); + }); + + it("#keepNext", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .keepNext(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:pPr": [{"w:keepNext": []}], + }); + }); }); describe("formatting methods: run properties", () => { diff --git a/ts/tests/stylesTest.ts b/ts/tests/stylesTest.ts index d00fb4cc0d..8709d752fa 100644 --- a/ts/tests/stylesTest.ts +++ b/ts/tests/stylesTest.ts @@ -326,6 +326,32 @@ describe("ParagraphStyle", () => { ], }); }); + + it("#keepLines", () => { + const style = new ParagraphStyle("myStyleId") + .keepLines(); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:pPr": [{"w:keepLines": []}]}, + {"w:rPr": []}, + ], + }); + }); + + it("#keepNext", () => { + const style = new ParagraphStyle("myStyleId") + .keepNext(); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:pPr": [{"w:keepNext": []}]}, + {"w:rPr": []}, + ], + }); + }); }); describe("formatting methods: run properties", () => {