diff --git a/ts/styles/index.ts b/ts/styles/index.ts index ee9f2bc784..519ad925e2 100644 --- a/ts/styles/index.ts +++ b/ts/styles/index.ts @@ -4,6 +4,7 @@ import { DocumentDefaults } from "./defaults"; import { LatentStyles } from "./latent-styles"; import { LatentStyleException } from "./latent-styles/exceptions"; import { LatentStyleExceptionAttributes } from "./latent-styles/exceptions/attributes"; +import { ParagraphStyle } from "./style"; export class Styles extends XmlComponent { @@ -34,4 +35,10 @@ export class Styles extends XmlComponent { element.clearVariables(); }); } + + public createParagraphStyle(styleId: string, name?: string): ParagraphStyle { + const para = new ParagraphStyle(styleId, name); + this.push(para); + return para; + } } diff --git a/ts/tests/stylesTest.ts b/ts/tests/stylesTest.ts index 22441ec769..0922627528 100644 --- a/ts/tests/stylesTest.ts +++ b/ts/tests/stylesTest.ts @@ -17,6 +17,33 @@ describe("Styles", () => { assert.equal(newJson.rootKey, "w:styles"); }); }); + + describe("#createParagraphStyle", () => { + it("should create a new paragraph style and push it onto this collection", () => { + const ps = styles.createParagraphStyle("pStyleId"); + const tree = new Formatter().format(styles)["w:styles"].filter((x) => !x._attr); + expect(tree).to.deep.equal([{ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "pStyleId"}}, + {"w:pPr": [{_attr: {}}]}, + {"w:rPr": []}, + ], + }]); + }); + + it("should set the paragraph name if given", () => { + const ps = styles.createParagraphStyle("pStyleId", "Paragraph Style"); + const tree = new Formatter().format(styles)["w:styles"].filter((x) => !x._attr); + expect(tree).to.deep.equal([{ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "pStyleId"}}, + {"w:name": [{_attr: {"w:val": "Paragraph Style"}}]}, + {"w:pPr": [{_attr: {}}]}, + {"w:rPr": []}, + ], + }]); + }); + }); }); describe("Style", () => {