added Spacing for paragraphs and ParagraphStyle
This commit is contained in:
@ -6,6 +6,7 @@ import { ThematicBreak } from "./border";
|
||||
import { Indent } from "./indent";
|
||||
import { PageBreak } from "./page-break";
|
||||
import { ParagraphProperties } from "./properties";
|
||||
import { ISpacingProperties, Spacing } from "./spacing";
|
||||
import { Style } from "./style";
|
||||
import { LeftTabStop, MaxRightTabStop } from "./tab-stop";
|
||||
import { NumberProperties } from "./unordered-list";
|
||||
@ -128,4 +129,9 @@ export class Paragraph extends XmlComponent {
|
||||
this.properties.push(new Indent(start, hanging));
|
||||
return this;
|
||||
}
|
||||
|
||||
public spacing(params: ISpacingProperties): Paragraph {
|
||||
this.properties.push(new Spacing(params));
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
24
ts/docx/paragraph/spacing.ts
Normal file
24
ts/docx/paragraph/spacing.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { XmlAttributeComponent, XmlComponent } from "../xml-components";
|
||||
|
||||
export interface ISpacingProperties {
|
||||
after?: number;
|
||||
before?: number;
|
||||
line?: number;
|
||||
}
|
||||
|
||||
class SpacingAttributes extends XmlAttributeComponent {
|
||||
constructor(properties: ISpacingProperties) {
|
||||
super({
|
||||
after: "w:after",
|
||||
before: "w:before",
|
||||
line: "w:line",
|
||||
}, properties);
|
||||
}
|
||||
}
|
||||
|
||||
export class Spacing extends XmlComponent {
|
||||
constructor(opts: ISpacingProperties) {
|
||||
super("w:spacing");
|
||||
this.root.push(new SpacingAttributes(opts));
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
import { Indent } from "../../docx/paragraph/indent";
|
||||
import { ParagraphProperties } from "../../docx/paragraph/properties";
|
||||
import { ISpacingProperties, Spacing } from "../../docx/paragraph/spacing";
|
||||
import * as formatting from "../../docx/run/formatting";
|
||||
import { RunProperties } from "../../docx/run/properties";
|
||||
import { XmlAttributeComponent, XmlComponent } from "../../docx/xml-components";
|
||||
@ -108,6 +109,11 @@ export class ParagraphStyle extends Style {
|
||||
this.addParagraphProperty(new Indent(left, hanging));
|
||||
return this;
|
||||
}
|
||||
|
||||
public spacing(params: ISpacingProperties): ParagraphStyle {
|
||||
this.addParagraphProperty(new Spacing(params));
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
export class HeadingStyle extends ParagraphStyle {
|
||||
|
@ -188,4 +188,21 @@ describe("Paragraph", () => {
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
describe("#spacing", () => {
|
||||
it("should set the paragraph spacing to the given values", () => {
|
||||
paragraph.spacing({before: 90, line: 50});
|
||||
const tree = new Formatter().format(paragraph);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:p": [
|
||||
{
|
||||
"w:pPr": [
|
||||
{"_attr": {}},
|
||||
{"w:spacing": [{"_attr": {"w:before": 90, "w:line": 50}}]},
|
||||
],
|
||||
},
|
||||
]
|
||||
})
|
||||
});
|
||||
});
|
||||
});
|
||||
|
24
ts/tests/docx/paragraph/spacingTest.ts
Normal file
24
ts/tests/docx/paragraph/spacingTest.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Spacing } from "../../../docx/paragraph/spacing";
|
||||
import { Formatter } from "../../../export/formatter";
|
||||
|
||||
describe("Spacing", () => {
|
||||
describe("#constructor", () => {
|
||||
it("should set the properties given", () => {
|
||||
const spacing = new Spacing({before: 100, after: 120, line: 150});
|
||||
const tree = new Formatter().format(spacing);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:spacing": [{_attr: {"w:after": 120, "w:before": 100, "w:line": 150}}],
|
||||
});
|
||||
});
|
||||
|
||||
it("should only set the given properties", () => {
|
||||
const spacing = new Spacing({before: 100});
|
||||
const tree = new Formatter().format(spacing);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:spacing": [{_attr: {"w:before": 100}}],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
@ -171,6 +171,22 @@ describe("ParagraphStyle", () => {
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("#spacing", () => {
|
||||
const style = new ParagraphStyle("myStyleId")
|
||||
.spacing({before: 50, after: 150});
|
||||
const tree = new Formatter().format(style);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:style": [
|
||||
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
|
||||
{"w:pPr": [
|
||||
{_attr: {}},
|
||||
{"w:spacing": [{_attr: {"w:before": 50, "w:after": 150}}]},
|
||||
]},
|
||||
{"w:rPr": []},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatting methods: run properties", () => {
|
||||
|
Reference in New Issue
Block a user