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)
This commit is contained in:
felipe
2017-04-14 21:13:11 +02:00
parent 94716e081b
commit 0453f28951
8 changed files with 111 additions and 0 deletions

View File

@ -1,6 +1,7 @@
export { Alignment } from "./alignment"; export { Alignment } from "./alignment";
export { ThematicBreak } from "./border"; export { ThematicBreak } from "./border";
export { Indent } from "./indent"; export { Indent } from "./indent";
export { KeepLines, KeepNext } from "./keep";
export { PageBreak } from "./page-break"; export { PageBreak } from "./page-break";
export { ParagraphProperties } from "./properties"; export { ParagraphProperties } from "./properties";
export { ISpacingProperties, Spacing } from "./spacing"; export { ISpacingProperties, Spacing } from "./spacing";

View File

@ -8,6 +8,7 @@ import { XmlComponent } from "../xml-components";
import { Alignment } from "./alignment"; import { Alignment } from "./alignment";
import { ThematicBreak } from "./border"; import { ThematicBreak } from "./border";
import { Indent } from "./indent"; import { Indent } from "./indent";
import { KeepLines, KeepNext } from "./keep";
import { PageBreak } from "./page-break"; import { PageBreak } from "./page-break";
import { ParagraphProperties } from "./properties"; import { ParagraphProperties } from "./properties";
import { ISpacingProperties, Spacing } from "./spacing"; import { ISpacingProperties, Spacing } from "./spacing";
@ -140,4 +141,14 @@ export class Paragraph extends XmlComponent {
this.properties.push(new Spacing(params)); this.properties.push(new Spacing(params));
return this; return this;
}; };
public keepNext(): Paragraph {
this.properties.push(new KeepNext());
return this;
}
public keepLines(): Paragraph {
this.properties.push(new KeepLines());
return this;
}
} }

13
ts/docx/paragraph/keep.ts Normal file
View File

@ -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");
}
}

View File

@ -205,6 +205,16 @@ class LevelBase extends XmlComponent {
this.addParagraphProperty(new paragraph.Spacing(params)); this.addParagraphProperty(new paragraph.Spacing(params));
return this; 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 { export class Level extends LevelBase {

View File

@ -180,6 +180,16 @@ export class ParagraphStyle extends Style {
this.addParagraphProperty(new paragraph.Spacing(params)); this.addParagraphProperty(new paragraph.Spacing(params));
return this; 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 { export class HeadingStyle extends ParagraphStyle {

View File

@ -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": []}]}],
});
});
});
}); });

View File

@ -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", () => { describe("formatting methods: run properties", () => {

View File

@ -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", () => { describe("formatting methods: run properties", () => {