added fluent formatting methods to ParagraphStyle
This commit is contained in:
@ -1,4 +1,6 @@
|
|||||||
|
import { Indent } from "../../docx/paragraph/indent";
|
||||||
import { ParagraphProperties } from "../../docx/paragraph/properties";
|
import { ParagraphProperties } from "../../docx/paragraph/properties";
|
||||||
|
import * as formatting from "../../docx/run/formatting";
|
||||||
import { RunProperties } from "../../docx/run/properties";
|
import { RunProperties } from "../../docx/run/properties";
|
||||||
import { XmlAttributeComponent, XmlComponent } from "../../docx/xml-components";
|
import { XmlAttributeComponent, XmlComponent } from "../../docx/xml-components";
|
||||||
|
|
||||||
@ -66,16 +68,55 @@ export class ParagraphStyle extends Style {
|
|||||||
public addRunProperty(property: XmlComponent): void {
|
public addRunProperty(property: XmlComponent): void {
|
||||||
this.runProperties.push(property);
|
this.runProperties.push(property);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public basedOn(parentId: string): ParagraphStyle {
|
||||||
|
this.root.push(new BasedOn(parentId));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public quickFormat(): ParagraphStyle {
|
||||||
|
this.root.push(new QuickFormat());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public next(nextId: string): ParagraphStyle {
|
||||||
|
this.root.push(new Next(nextId));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public size(twips: number): ParagraphStyle {
|
||||||
|
this.addRunProperty(new formatting.Size(twips));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bold(): ParagraphStyle {
|
||||||
|
this.addRunProperty(new formatting.Bold());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public italics(): ParagraphStyle {
|
||||||
|
this.addRunProperty(new formatting.Italics());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public color(color: string): ParagraphStyle {
|
||||||
|
this.addRunProperty(new formatting.Color(color));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public indent(left: number, hanging?: number): ParagraphStyle {
|
||||||
|
this.addParagraphProperty(new Indent(left, hanging));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class HeadingStyle extends ParagraphStyle {
|
export class HeadingStyle extends ParagraphStyle {
|
||||||
|
|
||||||
constructor(styleId: string, name: string) {
|
constructor(styleId: string, name: string) {
|
||||||
super(styleId);
|
super(styleId, name);
|
||||||
this.root.push(new Name(name));
|
this.basedOn("Normal");
|
||||||
this.root.push(new BasedOn("Normal"));
|
this.next("Normal");
|
||||||
this.root.push(new Next("Normal"));
|
this.quickFormat();
|
||||||
this.root.push(new QuickFormat());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,7 +83,6 @@ describe("Style components", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
describe("ParagraphStyle", () => {
|
describe("ParagraphStyle", () => {
|
||||||
describe("#constructor", () => {
|
describe("#constructor", () => {
|
||||||
it("should set the style type to paragraph and use the given style id", () => {
|
it("should set the style type to paragraph and use the given style id", () => {
|
||||||
@ -111,4 +110,128 @@ describe("ParagraphStyle", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("formatting methods: style attributes", () => {
|
||||||
|
it("#basedOn", () => {
|
||||||
|
const style = new ParagraphStyle("myStyleId")
|
||||||
|
.basedOn("otherId");
|
||||||
|
const tree = new Formatter().format(style);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"w:style": [
|
||||||
|
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
|
||||||
|
{"w:pPr": [{_attr: {}}]},
|
||||||
|
{"w:rPr": []},
|
||||||
|
{"w:basedOn": [{_attr: {"w:val": "otherId"}}]},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("#quickFormat", () => {
|
||||||
|
const style = new ParagraphStyle("myStyleId")
|
||||||
|
.quickFormat();
|
||||||
|
const tree = new Formatter().format(style);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"w:style": [
|
||||||
|
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
|
||||||
|
{"w:pPr": [{_attr: {}}]},
|
||||||
|
{"w:rPr": []},
|
||||||
|
{"w:qFormat": []},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("#next", () => {
|
||||||
|
const style = new ParagraphStyle("myStyleId")
|
||||||
|
.next("otherId");
|
||||||
|
const tree = new Formatter().format(style);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"w:style": [
|
||||||
|
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
|
||||||
|
{"w:pPr": [{_attr: {}}]},
|
||||||
|
{"w:rPr": []},
|
||||||
|
{"w:next": [{_attr: {"w:val": "otherId"}}]},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("formatting methods: paragraph properties", () => {
|
||||||
|
it("#indent", () => {
|
||||||
|
const style = new ParagraphStyle("myStyleId")
|
||||||
|
.indent(720);
|
||||||
|
const tree = new Formatter().format(style);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"w:style": [
|
||||||
|
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
|
||||||
|
{"w:pPr": [
|
||||||
|
{_attr: {}},
|
||||||
|
{"w:ind": [{_attr: {"w:left": 720}}]},
|
||||||
|
]},
|
||||||
|
{"w:rPr": []},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("formatting methods: run properties", () => {
|
||||||
|
it("#size", () => {
|
||||||
|
const style = new ParagraphStyle("myStyleId")
|
||||||
|
.size(24);
|
||||||
|
const tree = new Formatter().format(style);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"w:style": [
|
||||||
|
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
|
||||||
|
{"w:pPr": [{_attr: {}}]},
|
||||||
|
{"w:rPr": [
|
||||||
|
{"w:sz": [{_attr: {"w:val": 24}}]},
|
||||||
|
]},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("#bold", () => {
|
||||||
|
const style = new ParagraphStyle("myStyleId")
|
||||||
|
.bold();
|
||||||
|
const tree = new Formatter().format(style);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"w:style": [
|
||||||
|
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
|
||||||
|
{"w:pPr": [{_attr: {}}]},
|
||||||
|
{"w:rPr": [
|
||||||
|
{"w:b": [{_attr: {"w:val": true}}]},
|
||||||
|
]},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("#italics", () => {
|
||||||
|
const style = new ParagraphStyle("myStyleId")
|
||||||
|
.italics();
|
||||||
|
const tree = new Formatter().format(style);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"w:style": [
|
||||||
|
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
|
||||||
|
{"w:pPr": [{_attr: {}}]},
|
||||||
|
{"w:rPr": [
|
||||||
|
{"w:i": [{_attr: {"w:val": true}}]},
|
||||||
|
]},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("#color", () => {
|
||||||
|
const style = new ParagraphStyle("myStyleId")
|
||||||
|
.color("123456");
|
||||||
|
const tree = new Formatter().format(style);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"w:style": [
|
||||||
|
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
|
||||||
|
{"w:pPr": [{_attr: {}}]},
|
||||||
|
{"w:rPr": [
|
||||||
|
{"w:color": [{_attr: {"w:val": "123456"}}]},
|
||||||
|
]},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user