From 099da3d90c5a1cf2318964c19be2a6e10c89bd90 Mon Sep 17 00:00:00 2001 From: Dolan Miu Date: Wed, 30 Mar 2016 00:28:05 +0100 Subject: [PATCH] added page break --- ts/docx/{ => paragraph}/border.ts | 2 +- ts/docx/{paragraph.ts => paragraph/index.ts} | 20 ++++++++++---------- ts/docx/paragraph/page-break.ts | 19 +++++++++++++++++++ ts/docx/paragraph/properties.ts | 14 ++++++++++++++ ts/docx/paragraph/text-run.ts | 11 +++++++++++ ts/docx/xml-components/index.ts | 20 +++----------------- ts/tests/borderTest.ts | 2 +- ts/tests/paragraphTest.ts | 16 +++++++++++++++- 8 files changed, 74 insertions(+), 30 deletions(-) rename ts/docx/{ => paragraph}/border.ts (89%) rename ts/docx/{paragraph.ts => paragraph/index.ts} (84%) create mode 100644 ts/docx/paragraph/page-break.ts create mode 100644 ts/docx/paragraph/properties.ts create mode 100644 ts/docx/paragraph/text-run.ts diff --git a/ts/docx/border.ts b/ts/docx/paragraph/border.ts similarity index 89% rename from ts/docx/border.ts rename to ts/docx/paragraph/border.ts index 9a0a84882e..272b20f747 100644 --- a/ts/docx/border.ts +++ b/ts/docx/paragraph/border.ts @@ -1,4 +1,4 @@ -import {XmlComponent, Attributes} from "./xml-components"; +import {XmlComponent, Attributes} from "../xml-components"; class Border implements XmlComponent { private bottom: Array; diff --git a/ts/docx/paragraph.ts b/ts/docx/paragraph/index.ts similarity index 84% rename from ts/docx/paragraph.ts rename to ts/docx/paragraph/index.ts index 7c0430a507..50b9d0a748 100644 --- a/ts/docx/paragraph.ts +++ b/ts/docx/paragraph/index.ts @@ -1,5 +1,8 @@ -import {XmlComponent, Attributes, ParagraphProperties, Run} from "./xml-components"; +import {XmlComponent, Attributes} from "../xml-components"; import {ThematicBreak} from "./border"; +import {PageBreak} from "./page-break"; +import {TextRun} from "./text-run"; +import {ParagraphProperties} from "./properties"; class Style { private pStyle: Array; @@ -32,10 +35,10 @@ export class Paragraph { this.p.push(new Attributes()); this.properties = new ParagraphProperties(); this.p.push(this.properties); - this.p.push(new Run(text)); + this.p.push(new TextRun(text)); } - addText(run: Run) { + addText(run: TextRun) { this.p.push(run); return this; } @@ -94,12 +97,9 @@ export class Paragraph { this.properties.push(new ThematicBreak()); return this; } - - pageBreak () { - this.properties.push(new ThematicBreak()); - - paragraphProperties.push(pBreak); - return this; - } + pageBreak() { + this.properties.push(new PageBreak()); + return this; + } } \ No newline at end of file diff --git a/ts/docx/paragraph/page-break.ts b/ts/docx/paragraph/page-break.ts new file mode 100644 index 0000000000..ab0444c2ad --- /dev/null +++ b/ts/docx/paragraph/page-break.ts @@ -0,0 +1,19 @@ +import {XmlComponent, Attributes, Run} from "../xml-components"; + +class Break implements XmlComponent { + private br: Array; + constructor() { + this.br = new Array(); + this.br.push(new Attributes({ + type: "page" + })) + } +} + +export class PageBreak extends Run { + + constructor() { + super(); + this.r.push(new Break()); + } +} \ No newline at end of file diff --git a/ts/docx/paragraph/properties.ts b/ts/docx/paragraph/properties.ts new file mode 100644 index 0000000000..5ba875a8b5 --- /dev/null +++ b/ts/docx/paragraph/properties.ts @@ -0,0 +1,14 @@ +import {XmlComponent, Attributes} from "../xml-components"; + +export class ParagraphProperties implements XmlComponent { + private pPr: Array; + + constructor() { + this.pPr = new Array(); + this.pPr.push(new Attributes()); + } + + push(item: XmlComponent) { + this.pPr.push(item); + } +} \ No newline at end of file diff --git a/ts/docx/paragraph/text-run.ts b/ts/docx/paragraph/text-run.ts new file mode 100644 index 0000000000..d619823830 --- /dev/null +++ b/ts/docx/paragraph/text-run.ts @@ -0,0 +1,11 @@ +import {Run, Text} from "../xml-components"; +import {ParagraphProperties} from "./properties"; + +export class TextRun extends Run { + + constructor(text: string) { + super(); + this.r.push(new ParagraphProperties()); + this.r.push(new Text(text)); + } +} \ No newline at end of file diff --git a/ts/docx/xml-components/index.ts b/ts/docx/xml-components/index.ts index f6816ccf75..3f074100ae 100644 --- a/ts/docx/xml-components/index.ts +++ b/ts/docx/xml-components/index.ts @@ -7,6 +7,7 @@ interface AttributesProperties { color?: string; space?: string; sz?: string; + type?: string; } export class Attributes implements XmlComponent { @@ -21,26 +22,11 @@ export class Attributes implements XmlComponent { } } -export class ParagraphProperties implements XmlComponent { - private pPr: Array; +export class Run implements XmlComponent { + protected r: Array; constructor() { - this.pPr = new Array(); - this.pPr.push(new Attributes()); - } - - push(item: XmlComponent) { - this.pPr.push(item); - } -} - -export class Run implements XmlComponent { - private r: Array; - - constructor(text: string) { this.r = new Array(); - this.r.push(new ParagraphProperties()); - this.r.push(new Text(text)); } } diff --git a/ts/tests/borderTest.ts b/ts/tests/borderTest.ts index a0e92102f3..64f21f83ee 100644 --- a/ts/tests/borderTest.ts +++ b/ts/tests/borderTest.ts @@ -1,6 +1,6 @@ /// /// -import {ThematicBreak} from "../docx/border"; +import {ThematicBreak} from "../docx/paragraph/border"; import {assert} from "chai"; function jsonify(obj: Object) { diff --git a/ts/tests/paragraphTest.ts b/ts/tests/paragraphTest.ts index a038eba293..d4adb850d9 100644 --- a/ts/tests/paragraphTest.ts +++ b/ts/tests/paragraphTest.ts @@ -81,8 +81,22 @@ describe('Paragraph', () => { it("should add thematic break to JSON", () => { paragraph.thematicBreak(); var newJson = jsonify(paragraph); - + assert.isDefined(newJson.p[1].pPr[1].pBdr); }); }); + + describe("#pageBreak()", () => { + it("should add page break to JSON", () => { + paragraph.pageBreak(); + var newJson = jsonify(paragraph); + assert.isDefined(newJson.p[1].pPr[1].r[0].br); + }); + + it("should add page break with 'page' type", () => { + paragraph.pageBreak(); + var newJson = jsonify(paragraph); + assert(newJson.p[1].pPr[1].r[0].br[0]._attrs.type === "page"); + }); + }); }); \ No newline at end of file