From d85c6ce56af142ccb8c9f10a797024687d6b6fba Mon Sep 17 00:00:00 2001 From: felipe Date: Sun, 12 Mar 2017 17:28:52 +0100 Subject: [PATCH 01/13] allow Run#underline to take style and color arguments --- ts/docx/run/index.ts | 4 ++-- ts/tests/docx/run/runTest.ts | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/ts/docx/run/index.ts b/ts/docx/run/index.ts index 6585de6c01..80417d05fb 100644 --- a/ts/docx/run/index.ts +++ b/ts/docx/run/index.ts @@ -29,8 +29,8 @@ export class Run extends XmlComponent { return this; } - public underline(): Run { - this.properties.push(new Underline()); + public underline(underlineType?: string, color?: string): Run { + this.properties.push(new Underline(underlineType, color)); return this; } diff --git a/ts/tests/docx/run/runTest.ts b/ts/tests/docx/run/runTest.ts index 1bdc1718f0..a4587a05c8 100644 --- a/ts/tests/docx/run/runTest.ts +++ b/ts/tests/docx/run/runTest.ts @@ -32,6 +32,26 @@ describe("Run", () => { const newJson = Utility.jsonify(run); assert.equal(newJson.root[0].root[0].rootKey, "w:u"); }); + + it("should default to 'single' and no color", () => { + run.underline(); + const tree = new Formatter().format(run); + expect(tree).to.deep.equal({ + "w:r": [ + {"w:rPr": [{"w:u": [{_attr: {"w:val": "single"}}]}]}, + ], + }); + }); + + it("should set the style type and color if given", () => { + run.underline("double", "990011"); + const tree = new Formatter().format(run); + expect(tree).to.deep.equal({ + "w:r": [ + {"w:rPr": [{"w:u": [{_attr: {"w:val": "double", "w:color": "990011"}}]}]}, + ], + }); + }); }); describe("#smallCaps()", () => { From 3dbd917667a6b3d229ca37f73893f3da8e09f964 Mon Sep 17 00:00:00 2001 From: felipe Date: Sun, 12 Mar 2017 17:31:36 +0100 Subject: [PATCH 02/13] added #color and #size methods to Run --- ts/docx/run/index.ts | 12 +++++++++++- ts/tests/docx/run/runTest.ts | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/ts/docx/run/index.ts b/ts/docx/run/index.ts index 80417d05fb..bf974ed29b 100644 --- a/ts/docx/run/index.ts +++ b/ts/docx/run/index.ts @@ -1,6 +1,6 @@ import { Break } from "./break"; import { Caps, SmallCaps } from "./caps"; -import { Bold, Italics } from "./formatting"; +import { Bold, Color, Italics, Size } from "./formatting"; import { RunProperties } from "./properties"; import { RunFonts } from "./run-fonts"; import { SubScript, SuperScript } from "./script"; @@ -34,6 +34,16 @@ export class Run extends XmlComponent { return this; } + public color(color: string): Run { + this.properties.push(new Color(color)); + return this; + } + + public size(size: number): Run { + this.properties.push(new Size(size)); + return this; + } + public break(): Run { this.root.splice(1, 0, new Break()); return this; diff --git a/ts/tests/docx/run/runTest.ts b/ts/tests/docx/run/runTest.ts index a4587a05c8..42e180bba7 100644 --- a/ts/tests/docx/run/runTest.ts +++ b/ts/tests/docx/run/runTest.ts @@ -117,4 +117,28 @@ describe("Run", () => { }); }); }); + + describe("#color", () => { + it("should set the run to the color given", () => { + run.color("001122"); + const tree = new Formatter().format(run); + expect(tree).to.deep.equal({ + "w:r": [ + {"w:rPr": [{"w:color": [{_attr: {"w:val": "001122"}}]}]}, + ], + }); + }); + }); + + describe("#size", () => { + it("should set the run to the given size", () => { + run.size(24); + const tree = new Formatter().format(run); + expect(tree).to.deep.equal({ + "w:r": [ + {"w:rPr": [{"w:sz": [{_attr: {"w:val": 24}}]}]}, + ], + }); + }); + }); }); From 90049a31ee5af6cef29565d3adc7bbc37db3c129 Mon Sep 17 00:00:00 2001 From: felipe Date: Sun, 12 Mar 2017 17:35:15 +0100 Subject: [PATCH 03/13] added #style method to runs --- ts/docx/run/index.ts | 6 ++++++ ts/docx/run/style.ts | 13 +++++++++++++ ts/tests/docx/run/runTest.ts | 12 ++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 ts/docx/run/style.ts 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"}}]}]}, + ], + }); + }); + }); }); From 9a90818729c3c355f4a0a07384a51725f53b2c1e Mon Sep 17 00:00:00 2001 From: felipe Date: Sun, 12 Mar 2017 17:43:00 +0100 Subject: [PATCH 04/13] added #smallCaps and #allCaps methods to ParagraphStyle --- ts/styles/style/index.ts | 12 ++++++++++++ ts/tests/stylesTest.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/ts/styles/style/index.ts b/ts/styles/style/index.ts index e5e3561eef..6b9b64fdd9 100644 --- a/ts/styles/style/index.ts +++ b/ts/styles/style/index.ts @@ -74,6 +74,8 @@ export class ParagraphStyle extends Style { return this; } + // ---------- Run formatting ---------------------- // + public size(twips: number): ParagraphStyle { this.addRunProperty(new formatting.Size(twips)); return this; @@ -89,6 +91,16 @@ export class ParagraphStyle extends Style { return this; } + public smallCaps(): ParagraphStyle { + this.addRunProperty(new formatting.SmallCaps()); + return this; + } + + public allCaps(): ParagraphStyle { + this.addRunProperty(new formatting.Caps()); + return this; + } + public underline(underlineType?: string, color?: string): ParagraphStyle { this.addRunProperty(new formatting.Underline(underlineType, color)); return this; diff --git a/ts/tests/stylesTest.ts b/ts/tests/stylesTest.ts index dfbd1ca2d3..03214ec701 100644 --- a/ts/tests/stylesTest.ts +++ b/ts/tests/stylesTest.ts @@ -230,6 +230,36 @@ describe("ParagraphStyle", () => { }); }); + it("#smallCaps", () => { + const style = new ParagraphStyle("myStyleId") + .smallCaps(); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:pPr": []}, + {"w:rPr": [ + {"w:smallCaps": [{_attr: {"w:val": true}}]}, + ]}, + ], + }); + }); + + it("#allCaps", () => { + const style = new ParagraphStyle("myStyleId") + .allCaps(); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:pPr": []}, + {"w:rPr": [ + {"w:caps": [{_attr: {"w:val": true}}]}, + ]}, + ], + }); + }); + it("#bold", () => { const style = new ParagraphStyle("myStyleId") .bold(); From 371398bc2c9dd6101dac876b9105ff49f294d017 Mon Sep 17 00:00:00 2001 From: felipe Date: Sun, 12 Mar 2017 17:44:42 +0100 Subject: [PATCH 05/13] added #strike and #doubleStrike methods to paragraphStyle --- ts/styles/style/index.ts | 10 ++++++++++ ts/tests/stylesTest.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/ts/styles/style/index.ts b/ts/styles/style/index.ts index 6b9b64fdd9..af37157bd9 100644 --- a/ts/styles/style/index.ts +++ b/ts/styles/style/index.ts @@ -101,6 +101,16 @@ export class ParagraphStyle extends Style { return this; } + public strike(): ParagraphStyle { + this.addRunProperty(new formatting.Strike()); + return this; + } + + public doubleStrike(): ParagraphStyle { + this.addRunProperty(new formatting.DoubleStrike()); + return this; + } + public underline(underlineType?: string, color?: string): ParagraphStyle { this.addRunProperty(new formatting.Underline(underlineType, color)); return this; diff --git a/ts/tests/stylesTest.ts b/ts/tests/stylesTest.ts index 03214ec701..c7a87f6b40 100644 --- a/ts/tests/stylesTest.ts +++ b/ts/tests/stylesTest.ts @@ -260,6 +260,36 @@ describe("ParagraphStyle", () => { }); }); + it("#strike", () => { + const style = new ParagraphStyle("myStyleId") + .strike(); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:pPr": []}, + {"w:rPr": [ + {"w:strike": [{_attr: {"w:val": true}}]}, + ]}, + ], + }); + }); + + it("#doubleStrike", () => { + const style = new ParagraphStyle("myStyleId") + .doubleStrike(); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:pPr": []}, + {"w:rPr": [ + {"w:dstrike": [{_attr: {"w:val": true}}]}, + ]}, + ], + }); + }); + it("#bold", () => { const style = new ParagraphStyle("myStyleId") .bold(); From 8c6cd012e7eba97ef1bb85b7b6d3c33235d15751 Mon Sep 17 00:00:00 2001 From: felipe Date: Sun, 12 Mar 2017 17:51:01 +0100 Subject: [PATCH 06/13] eliminated duplicate implementation of strikethrough --- ts/docx/run/index.ts | 3 +-- ts/docx/run/strike.ts | 15 --------------- ts/tests/docx/run/strikeTests.ts | 2 +- 3 files changed, 2 insertions(+), 18 deletions(-) delete mode 100644 ts/docx/run/strike.ts diff --git a/ts/docx/run/index.ts b/ts/docx/run/index.ts index 090bbf04eb..bf5465306a 100644 --- a/ts/docx/run/index.ts +++ b/ts/docx/run/index.ts @@ -1,10 +1,9 @@ import { Break } from "./break"; import { Caps, SmallCaps } from "./caps"; -import { Bold, Color, Italics, Size } from "./formatting"; +import { Bold, Color, DoubleStrike, Italics, Size, Strike } from "./formatting"; 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"; diff --git a/ts/docx/run/strike.ts b/ts/docx/run/strike.ts deleted file mode 100644 index 7c8c575f0b..0000000000 --- a/ts/docx/run/strike.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { XmlComponent } from "../xml-components"; - -export class Strike extends XmlComponent { - - constructor() { - super("w:strike"); - } -} - -export class DoubleStrike extends XmlComponent { - - constructor() { - super("w:dstrike"); - } -} diff --git a/ts/tests/docx/run/strikeTests.ts b/ts/tests/docx/run/strikeTests.ts index ef591566a6..44dcece140 100644 --- a/ts/tests/docx/run/strikeTests.ts +++ b/ts/tests/docx/run/strikeTests.ts @@ -1,5 +1,5 @@ import { assert } from "chai"; -import { DoubleStrike, Strike } from "../../../docx/run/strike"; +import { DoubleStrike, Strike } from "../../../docx/run/formatting"; import { Utility } from "../../utility"; describe("Strike", () => { From 7f4d1bf3e7447cce62636caef95faf217773c379 Mon Sep 17 00:00:00 2001 From: felipe Date: Sun, 12 Mar 2017 17:52:03 +0100 Subject: [PATCH 07/13] added #SubScript and #SuperScript methods to ParagraphStyle --- ts/docx/run/formatting.ts | 1 + ts/styles/style/index.ts | 10 ++++++++++ ts/tests/stylesTest.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/ts/docx/run/formatting.ts b/ts/docx/run/formatting.ts index d8f0257e03..7236c24f5e 100644 --- a/ts/docx/run/formatting.ts +++ b/ts/docx/run/formatting.ts @@ -1,5 +1,6 @@ import { Attributes, XmlComponent } from "../xml-components"; export { Underline } from "./underline"; +export { SubScript, SuperScript } from "./script"; export class Bold extends XmlComponent { diff --git a/ts/styles/style/index.ts b/ts/styles/style/index.ts index af37157bd9..5d9e221410 100644 --- a/ts/styles/style/index.ts +++ b/ts/styles/style/index.ts @@ -111,6 +111,16 @@ export class ParagraphStyle extends Style { return this; } + public subScript(): ParagraphStyle { + this.addRunProperty(new formatting.SubScript()); + return this; + } + + public superScript(): ParagraphStyle { + this.addRunProperty(new formatting.SuperScript()); + return this; + } + public underline(underlineType?: string, color?: string): ParagraphStyle { this.addRunProperty(new formatting.Underline(underlineType, color)); return this; diff --git a/ts/tests/stylesTest.ts b/ts/tests/stylesTest.ts index c7a87f6b40..b56cb687fd 100644 --- a/ts/tests/stylesTest.ts +++ b/ts/tests/stylesTest.ts @@ -290,6 +290,36 @@ describe("ParagraphStyle", () => { }); }); + it("#subScript", () => { + const style = new ParagraphStyle("myStyleId") + .subScript(); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:pPr": []}, + {"w:rPr": [ + {"w:vertAlign": [{_attr: {"w:val": "subscript"}}]}, + ]}, + ], + }); + }); + + it("#superScript", () => { + const style = new ParagraphStyle("myStyleId") + .superScript(); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:pPr": []}, + {"w:rPr": [ + {"w:vertAlign": [{_attr: {"w:val": "superscript"}}]}, + ]}, + ], + }); + }); + it("#bold", () => { const style = new ParagraphStyle("myStyleId") .bold(); From 1cd681bc2d77bd57c449b368ceb0686d12e62aff Mon Sep 17 00:00:00 2001 From: felipe Date: Sun, 12 Mar 2017 17:53:32 +0100 Subject: [PATCH 08/13] added #font method to paragraph styles --- ts/docx/run/formatting.ts | 1 + ts/styles/style/index.ts | 7 +++++++ ts/tests/stylesTest.ts | 13 +++++++++++++ 3 files changed, 21 insertions(+) diff --git a/ts/docx/run/formatting.ts b/ts/docx/run/formatting.ts index 7236c24f5e..2685f07fda 100644 --- a/ts/docx/run/formatting.ts +++ b/ts/docx/run/formatting.ts @@ -1,6 +1,7 @@ import { Attributes, XmlComponent } from "../xml-components"; export { Underline } from "./underline"; export { SubScript, SuperScript } from "./script"; +export { RunFonts } from "./run-fonts"; export class Bold extends XmlComponent { diff --git a/ts/styles/style/index.ts b/ts/styles/style/index.ts index 5d9e221410..3cf06cefb8 100644 --- a/ts/styles/style/index.ts +++ b/ts/styles/style/index.ts @@ -131,6 +131,13 @@ export class ParagraphStyle extends Style { return this; } + public font(fontName: string): ParagraphStyle { + this.addRunProperty(new formatting.RunFonts(fontName)); + return this; + } + + // --------------------- Paragraph formatting ------------------------ // + public indent(left: number, hanging?: number): ParagraphStyle { this.addParagraphProperty(new Indent(left, hanging)); return this; diff --git a/ts/tests/stylesTest.ts b/ts/tests/stylesTest.ts index b56cb687fd..4180d182fd 100644 --- a/ts/tests/stylesTest.ts +++ b/ts/tests/stylesTest.ts @@ -320,6 +320,19 @@ describe("ParagraphStyle", () => { }); }); + it("#font", () => { + const style = new ParagraphStyle("myStyleId") + .font("Times"); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:pPr": []}, + {"w:rPr": [{"w:rFonts": [{_attr: {"w:ascii": "Times", "w:hAnsi": "Times"}}]}]}, + ], + }); + }); + it("#bold", () => { const style = new ParagraphStyle("myStyleId") .bold(); From b3524971ace3778b0852334bec806e1ffa5fcba4 Mon Sep 17 00:00:00 2001 From: felipe Date: Sun, 12 Mar 2017 21:35:30 +0100 Subject: [PATCH 09/13] clean up imports into paragraph and paragraph style (this prep for importing it from styles) --- ts/docx/paragraph/alignment.ts | 15 +++++++++++++++ ts/docx/paragraph/formatting.ts | 9 +++++++++ ts/docx/paragraph/index.ts | 13 ++----------- ts/styles/style/index.ts | 14 ++++++-------- 4 files changed, 32 insertions(+), 19 deletions(-) create mode 100644 ts/docx/paragraph/alignment.ts create mode 100644 ts/docx/paragraph/formatting.ts diff --git a/ts/docx/paragraph/alignment.ts b/ts/docx/paragraph/alignment.ts new file mode 100644 index 0000000000..94e0568515 --- /dev/null +++ b/ts/docx/paragraph/alignment.ts @@ -0,0 +1,15 @@ +import { XmlAttributeComponent, XmlComponent } from "../xml-components"; + +type alignmentOptions = "left" | "center" | "right" | "both"; + +class AlignmentAttributes extends XmlAttributeComponent<{val: alignmentOptions}> { + protected xmlKeys = {val: "w:val"}; +} + +export class Alignment extends XmlComponent { + + constructor(type: alignmentOptions) { + super("w:jc"); + this.root.push(new AlignmentAttributes({val: type})); + } +} diff --git a/ts/docx/paragraph/formatting.ts b/ts/docx/paragraph/formatting.ts new file mode 100644 index 0000000000..8b76c0fa32 --- /dev/null +++ b/ts/docx/paragraph/formatting.ts @@ -0,0 +1,9 @@ +export { Alignment } from "./alignment"; +export { ThematicBreak } from "./border"; +export { Indent } from "./indent"; +export { PageBreak } from "./page-break"; +export { ParagraphProperties } from "./properties"; +export { ISpacingProperties, Spacing } from "./spacing"; +export { Style } from "./style"; +export { LeftTabStop, MaxRightTabStop } from "./tab-stop"; +export { NumberProperties } from "./unordered-list"; diff --git a/ts/docx/paragraph/index.ts b/ts/docx/paragraph/index.ts index 5bf0912d03..a85c014764 100644 --- a/ts/docx/paragraph/index.ts +++ b/ts/docx/paragraph/index.ts @@ -1,7 +1,8 @@ import { Num } from "../../numbering/num"; import { TextRun } from "../run/text-run"; -import { Attributes, XmlComponent } from "../xml-components"; +import { XmlComponent } from "../xml-components"; +import { Alignment } from "./alignment"; import { ThematicBreak } from "./border"; import { Indent } from "./indent"; import { PageBreak } from "./page-break"; @@ -11,16 +12,6 @@ import { Style } from "./style"; import { LeftTabStop, MaxRightTabStop } from "./tab-stop"; import { NumberProperties } from "./unordered-list"; -class Alignment extends XmlComponent { - - constructor(type: string) { - super("w:jc"); - this.root.push(new Attributes({ - val: type, - })); - } -} - export class Paragraph extends XmlComponent { private properties: ParagraphProperties; diff --git a/ts/styles/style/index.ts b/ts/styles/style/index.ts index 3cf06cefb8..db4b1c1924 100644 --- a/ts/styles/style/index.ts +++ b/ts/styles/style/index.ts @@ -1,6 +1,4 @@ -import { Indent } from "../../docx/paragraph/indent"; -import { ParagraphProperties } from "../../docx/paragraph/properties"; -import { ISpacingProperties, Spacing } from "../../docx/paragraph/spacing"; +import * as paragraph from "../../docx/paragraph/formatting"; import * as formatting from "../../docx/run/formatting"; import { RunProperties } from "../../docx/run/properties"; import { XmlAttributeComponent, XmlComponent } from "../../docx/xml-components"; @@ -40,12 +38,12 @@ export class Style extends XmlComponent { export class ParagraphStyle extends Style { - private paragraphProperties: ParagraphProperties; + private paragraphProperties: paragraph.ParagraphProperties; private runProperties: RunProperties; constructor(styleId: string, name?: string) { super({type: "paragraph", styleId: styleId}, name); - this.paragraphProperties = new ParagraphProperties(); + this.paragraphProperties = new paragraph.ParagraphProperties(); this.runProperties = new RunProperties(); this.root.push(this.paragraphProperties); this.root.push(this.runProperties); @@ -139,12 +137,12 @@ export class ParagraphStyle extends Style { // --------------------- Paragraph formatting ------------------------ // public indent(left: number, hanging?: number): ParagraphStyle { - this.addParagraphProperty(new Indent(left, hanging)); + this.addParagraphProperty(new paragraph.Indent(left, hanging)); return this; } - public spacing(params: ISpacingProperties): ParagraphStyle { - this.addParagraphProperty(new Spacing(params)); + public spacing(params: paragraph.ISpacingProperties): ParagraphStyle { + this.addParagraphProperty(new paragraph.Spacing(params)); return this; }; } From df8ba1e8b8ab6361926f5e85e8c019926303bf85 Mon Sep 17 00:00:00 2001 From: felipe Date: Sun, 12 Mar 2017 21:40:01 +0100 Subject: [PATCH 10/13] added alignment methods to paragraph style --- ts/styles/style/index.ts | 20 ++++++++++++++ ts/tests/stylesTest.ts | 60 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/ts/styles/style/index.ts b/ts/styles/style/index.ts index db4b1c1924..41493c9188 100644 --- a/ts/styles/style/index.ts +++ b/ts/styles/style/index.ts @@ -136,6 +136,26 @@ export class ParagraphStyle extends Style { // --------------------- Paragraph formatting ------------------------ // + public center(): ParagraphStyle { + this.addParagraphProperty(new paragraph.Alignment("center")); + return this; + } + + public left(): ParagraphStyle { + this.addParagraphProperty(new paragraph.Alignment("left")); + return this; + } + + public right(): ParagraphStyle { + this.addParagraphProperty(new paragraph.Alignment("right")); + return this; + } + + public justified(): ParagraphStyle { + this.addParagraphProperty(new paragraph.Alignment("both")); + return this; + } + public indent(left: number, hanging?: number): ParagraphStyle { this.addParagraphProperty(new paragraph.Indent(left, hanging)); return this; diff --git a/ts/tests/stylesTest.ts b/ts/tests/stylesTest.ts index 4180d182fd..68eeb6915d 100644 --- a/ts/tests/stylesTest.ts +++ b/ts/tests/stylesTest.ts @@ -212,6 +212,66 @@ describe("ParagraphStyle", () => { ], }); }); + + it("#center", () => { + const style = new ParagraphStyle("myStyleId") + .center(); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:pPr": [ + {"w:jc": [{_attr: {"w:val": "center"}}]}, + ]}, + {"w:rPr": []}, + ], + }); + }); + + it("#left", () => { + const style = new ParagraphStyle("myStyleId") + .left(); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:pPr": [ + {"w:jc": [{_attr: {"w:val": "left"}}]}, + ]}, + {"w:rPr": []}, + ], + }); + }); + + it("#right", () => { + const style = new ParagraphStyle("myStyleId") + .right(); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:pPr": [ + {"w:jc": [{_attr: {"w:val": "right"}}]}, + ]}, + {"w:rPr": []}, + ], + }); + }); + + it("#justified", () => { + const style = new ParagraphStyle("myStyleId") + .justified(); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:pPr": [ + {"w:jc": [{_attr: {"w:val": "both"}}]}, + ]}, + {"w:rPr": []}, + ], + }); + }); }); describe("formatting methods: run properties", () => { From 6689f76c2809373f73c84c968b16302c6aadb758 Mon Sep 17 00:00:00 2001 From: felipe Date: Sun, 12 Mar 2017 21:58:12 +0100 Subject: [PATCH 11/13] added #thematicBreak method to paragraph styles --- ts/styles/style/index.ts | 5 +++++ ts/tests/stylesTest.ts | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/ts/styles/style/index.ts b/ts/styles/style/index.ts index 41493c9188..35e456d937 100644 --- a/ts/styles/style/index.ts +++ b/ts/styles/style/index.ts @@ -156,6 +156,11 @@ export class ParagraphStyle extends Style { return this; } + public thematicBreak(): ParagraphStyle { + this.addParagraphProperty(new paragraph.ThematicBreak()); + return this; + } + public indent(left: number, hanging?: number): ParagraphStyle { this.addParagraphProperty(new paragraph.Indent(left, hanging)); return this; diff --git a/ts/tests/stylesTest.ts b/ts/tests/stylesTest.ts index 68eeb6915d..821ff2a9f3 100644 --- a/ts/tests/stylesTest.ts +++ b/ts/tests/stylesTest.ts @@ -272,6 +272,26 @@ describe("ParagraphStyle", () => { ], }); }); + + it("#thematicBreak", () => { + const style = new ParagraphStyle("myStyleId") + .thematicBreak(); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:pPr": [ + {"w:pBdr": [{"w:bottom": [{_attr: { + "w:color": "auto", + "w:space": "1", + "w:val": "single", + "w:sz": "6", + }}]}]}, + ]}, + {"w:rPr": []}, + ], + }); + }); }); describe("formatting methods: run properties", () => { From 0b78e33444909888cc6870c06b49e744f9d92aad Mon Sep 17 00:00:00 2001 From: felipe Date: Sun, 12 Mar 2017 22:04:57 +0100 Subject: [PATCH 12/13] added #leftTabStop and #maxRightTabStop methods to paragraph styles --- ts/styles/style/index.ts | 10 ++++++++++ ts/tests/stylesTest.ts | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/ts/styles/style/index.ts b/ts/styles/style/index.ts index 35e456d937..e70936655b 100644 --- a/ts/styles/style/index.ts +++ b/ts/styles/style/index.ts @@ -161,6 +161,16 @@ export class ParagraphStyle extends Style { return this; } + public maxRightTabStop(): ParagraphStyle { + this.addParagraphProperty(new paragraph.MaxRightTabStop()); + return this; + } + + public leftTabStop(position: number): ParagraphStyle { + this.addParagraphProperty(new paragraph.LeftTabStop(position)); + return this; + } + public indent(left: number, hanging?: number): ParagraphStyle { this.addParagraphProperty(new paragraph.Indent(left, hanging)); return this; diff --git a/ts/tests/stylesTest.ts b/ts/tests/stylesTest.ts index 821ff2a9f3..d00fb4cc0d 100644 --- a/ts/tests/stylesTest.ts +++ b/ts/tests/stylesTest.ts @@ -292,6 +292,40 @@ describe("ParagraphStyle", () => { ], }); }); + + it("#leftTabStop", () => { + const style = new ParagraphStyle("myStyleId") + .leftTabStop(1200); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:pPr": [ + {"w:tabs": [ + {"w:tab": [{_attr: {"w:val": "left", "w:pos": 1200}}]}, + ]}, + ]}, + {"w:rPr": []}, + ], + }); + }); + + it("#maxRightTabStop", () => { + const style = new ParagraphStyle("myStyleId") + .maxRightTabStop(); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + {_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}}, + {"w:pPr": [ + {"w:tabs": [ + {"w:tab": [{_attr: {"w:val": "right", "w:pos": 9026}}]}, + ]}, + ]}, + {"w:rPr": []}, + ], + }); + }); }); describe("formatting methods: run properties", () => { From a6e40d9d922ce2d32f28db182ee52c6c7120516b Mon Sep 17 00:00:00 2001 From: felipe Date: Sun, 12 Mar 2017 22:06:11 +0100 Subject: [PATCH 13/13] tightened type checking for tab stops --- ts/docx/paragraph/tab-stop.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ts/docx/paragraph/tab-stop.ts b/ts/docx/paragraph/tab-stop.ts index f531f6ba84..6154c33d2a 100644 --- a/ts/docx/paragraph/tab-stop.ts +++ b/ts/docx/paragraph/tab-stop.ts @@ -1,4 +1,4 @@ -import { Attributes, XmlComponent } from "../xml-components"; +import { XmlAttributeComponent, XmlComponent } from "../xml-components"; class TabStop extends XmlComponent { @@ -8,11 +8,17 @@ class TabStop extends XmlComponent { } } +export type tabOptions = "left" | "right"; + +class TabAttributes extends XmlAttributeComponent<{val: tabOptions, pos: string | number}> { + protected xmlKeys = {val: "w:val", pos: "w:pos"}; +} + class Tab extends XmlComponent { - constructor(value: string, position: string | number) { + constructor(value: tabOptions, position: string | number) { super("w:tab"); - this.root.push(new Attributes({ + this.root.push(new TabAttributes({ val: value, pos: position, }));