diff --git a/ts/docx/run/index.ts b/ts/docx/run/index.ts index bf974ed29b..090bbf04eb 100644 --- a/ts/docx/run/index.ts +++ b/ts/docx/run/index.ts @@ -5,6 +5,7 @@ import { RunProperties } from "./properties"; import { RunFonts } from "./run-fonts"; import { SubScript, SuperScript } from "./script"; import { DoubleStrike, Strike } from "./strike"; +import { Style } from "./style"; import { Tab } from "./tab"; import { Underline } from "./underline"; @@ -88,4 +89,9 @@ export class Run extends XmlComponent { this.properties.push(new RunFonts(fontName)); return this; } + + public style(styleId: string): Run { + this.properties.push(new Style(styleId)); + return this; + } } diff --git a/ts/docx/run/style.ts b/ts/docx/run/style.ts new file mode 100644 index 0000000000..713752377e --- /dev/null +++ b/ts/docx/run/style.ts @@ -0,0 +1,13 @@ +import { XmlAttributeComponent, XmlComponent } from "../xml-components"; + +class StyleAttributes extends XmlAttributeComponent<{val: string}> { + protected xmlKeys = {val: "w:val"}; +} + +export class Style extends XmlComponent { + + constructor(styleId: string) { + super("w:rStyle"); + this.root.push(new StyleAttributes({val: styleId})); + } +} diff --git a/ts/tests/docx/run/runTest.ts b/ts/tests/docx/run/runTest.ts index 42e180bba7..f8f7826def 100644 --- a/ts/tests/docx/run/runTest.ts +++ b/ts/tests/docx/run/runTest.ts @@ -141,4 +141,16 @@ describe("Run", () => { }); }); }); + + describe("#style", () => { + it("should set the style to the given styleId", () => { + run.style("myRunStyle"); + const tree = new Formatter().format(run); + expect(tree).to.deep.equal({ + "w:r": [ + {"w:rPr": [{"w:rStyle": [{_attr: {"w:val": "myRunStyle"}}]}]}, + ], + }); + }); + }); });