diff --git a/ts/docx/paragraph.ts b/ts/docx/paragraph.ts index d5ed0c7c1d..720b69d3e9 100644 --- a/ts/docx/paragraph.ts +++ b/ts/docx/paragraph.ts @@ -1,12 +1,87 @@ import {P, Attributes, ParagraphProperties, Run} from "./xml-components/p"; +class Style { + private pStyle: Array

; + + constructor(type: string) { + this.pStyle = new Array

(); + this.pStyle.push(new Attributes(type)); + } +} + +class Alignment { + private jc: Array

; + + constructor(type: string) { + this.jc = new Array

(); + this.jc.push(new Attributes(type)); + } +} + export class Paragraph { private p: Array

; - + private properties: ParagraphProperties; + constructor(text?: string) { this.p = new Array

(); this.p.push(new Attributes()); - this.p.push(new ParagraphProperties()); + this.properties = new ParagraphProperties(); + this.p.push(this.properties); this.p.push(new Run(text)); } + + addText(run: Run) { + this.p.push(run); + return this; + } + + heading1() { + this.properties.push(new Style("Heading1")); + return this; + } + + heading2() { + this.properties.push(new Style("Heading2")); + return this; + } + + heading3() { + this.properties.push(new Style("Heading3")); + return this; + } + + heading4() { + this.properties.push(new Style("Heading4")); + return this; + } + + heading5() { + this.properties.push(new Style("Heading5")); + return this; + } + + title() { + this.properties.push(new Style("Title")); + return this; + } + + center() { + this.properties.push(new Alignment("center")); + return this; + } + + left() { + this.properties.push(new Alignment("left")); + return this; + } + + right() { + this.properties.push(new Alignment("right")); + return this; + } + + justified() { + this.properties.push(new Alignment("both")); + return this; + } } \ No newline at end of file diff --git a/ts/docx/xml-components/p.ts b/ts/docx/xml-components/p.ts index 4e409fea15..c2f18b077a 100644 --- a/ts/docx/xml-components/p.ts +++ b/ts/docx/xml-components/p.ts @@ -5,18 +5,24 @@ export interface P { export class Attributes implements P { private _attrs: Object; - constructor() { - this._attrs = {}; + constructor(value?: string) { + this._attrs = { + val: value + }; } } -export class ParagraphProperties implements P { +export class ParagraphProperties implements P{ private pPr: Array

; constructor() { this.pPr = new Array

(); this.pPr.push(new Attributes()); } + + push(item: P) { + this.pPr.push(item); + } } export class Run implements P { @@ -24,6 +30,7 @@ export class Run implements P { constructor(text: string) { this.r = new Array

(); + this.r.push(new ParagraphProperties()); this.r.push(new Text(text)); } } diff --git a/ts/tests/attributeTest.ts b/ts/tests/attributeTest.ts new file mode 100644 index 0000000000..9436e51bff --- /dev/null +++ b/ts/tests/attributeTest.ts @@ -0,0 +1,29 @@ +/// +/// +import {Attributes} from "../docx/xml-components/p"; +import {assert} from "chai"; + +describe('Attribute', () => { + var attributes: Attributes; + + beforeEach(() => { + attributes = new Attributes(); + }); + + describe('#constructor()', () => { + + it("should not add val with empty constructor", () => { + var newAttrs = new Attributes(); + var stringifiedJson = JSON.stringify(newAttrs); + var newJson = JSON.parse(stringifiedJson); + assert.isUndefined(newJson._attrs.val); + }); + + it("should have val as defined with populated constructor", () => { + var newAttrs = new Attributes("test"); + var stringifiedJson = JSON.stringify(newAttrs); + var newJson = JSON.parse(stringifiedJson); + assert(newJson._attrs.val === "test"); + }); + }); +}); \ No newline at end of file diff --git a/ts/tests/documentTest.ts b/ts/tests/documentTest.ts new file mode 100644 index 0000000000..9b8dc70e16 --- /dev/null +++ b/ts/tests/documentTest.ts @@ -0,0 +1,28 @@ +/// +/// +import * as docx from "../docx"; +import {assert} from "chai"; + +describe('Document', () => { + var document: docx.Document; + + beforeEach(() => { + document = new docx.Document(); + }); + + describe('#constructor()', () => { + + it("should create valid JSON", () => { + console.log(JSON.stringify(document, null, " ")); + var stringifiedJson = JSON.stringify(document); + var newJson; + + try { + newJson = JSON.parse(stringifiedJson); + } catch (e) { + assert.isTrue(false); + } + assert.isTrue(true); + }); + }); +}); \ No newline at end of file diff --git a/ts/tests/docxTest.ts b/ts/tests/docxTest.ts deleted file mode 100644 index f62a408e26..0000000000 --- a/ts/tests/docxTest.ts +++ /dev/null @@ -1,41 +0,0 @@ -/// -/// -import * as docx from "../docx"; -import {assert, } from "chai"; - -describe('Calculator', () => { - var document: docx.Document; - - /*beforeEach(function () { - subject = new Calculator(); - }); - - describe('#add', () => { - it('should add two numbers together', () => { - var result : number = subject.add(2, 3); - if (result !== 5) { - throw new Error('Expected 2 + 3 = 5 but was ' + result); - } - }); - });*/ - describe('#test', () => { - var document = new docx.Document(); - var paragraph = new docx.Paragraph(); - //var body = new docx.Body(); - console.log(paragraph); - console.log(JSON.stringify(paragraph, null, " ")); - console.log(document.test()); - - it("should create valid JSON", () => { - var stringifiedJson = JSON.stringify(document); - var newJson; - - try { - newJson = JSON.parse(stringifiedJson); - } catch (e) { - assert.isTrue(false); - } - assert.isTrue(true); - }); - }); -}); \ No newline at end of file diff --git a/ts/tests/paragraphTest.ts b/ts/tests/paragraphTest.ts new file mode 100644 index 0000000000..9494f679db --- /dev/null +++ b/ts/tests/paragraphTest.ts @@ -0,0 +1,70 @@ +/// +/// +import * as docx from "../docx"; +import {assert} from "chai"; + +describe('Paragraph', () => { + var paragraph: docx.Paragraph; + + beforeEach(() => { + paragraph = new docx.Paragraph(); + }); + + function jsonify(obj: Object) { + var stringifiedJson = JSON.stringify(obj); + return JSON.parse(stringifiedJson); + } + + describe('#constructor()', () => { + + it("should create valid JSON", () => { + console.log(JSON.stringify(paragraph, null, " ")); + + var stringifiedJson = JSON.stringify(paragraph); + var newJson; + + try { + newJson = JSON.parse(stringifiedJson); + } catch (e) { + assert.isTrue(false); + } + assert.isTrue(true); + }); + }); + + describe("#heading1()", () => { + it("should add heading style to JSON", () => { + paragraph.heading1(); + var newJson = jsonify(paragraph); + + assert(newJson.p[1].pPr[1].pStyle[0]._attrs.val === "Heading1"); + }); + }); + + describe("#heading2()", () => { + it("should add heading style to JSON", () => { + paragraph.heading2(); + var newJson = jsonify(paragraph); + + assert(newJson.p[1].pPr[1].pStyle[0]._attrs.val === "Heading2"); + }); + }); + + describe("#heading3()", () => { + it("should add heading style to JSON", () => { + paragraph.heading3(); + var newJson = jsonify(paragraph); + + assert(newJson.p[1].pPr[1].pStyle[0]._attrs.val === "Heading3"); + }); + }); + + describe("#title()", () => { + it("should add title style to JSON", () => { + paragraph.title(); + var newJson = jsonify(paragraph); + + assert(newJson.p[1].pPr[1].pStyle[0]._attrs.val === "Title"); + }); + }); +}); \ No newline at end of file