added paragraph styles

This commit is contained in:
Dolan Miu
2016-03-29 04:10:33 +01:00
parent 18c92426ad
commit c2b215e81b
6 changed files with 214 additions and 46 deletions

View File

@ -1,12 +1,87 @@
import {P, Attributes, ParagraphProperties, Run} from "./xml-components/p";
class Style {
private pStyle: Array<P>;
constructor(type: string) {
this.pStyle = new Array<P>();
this.pStyle.push(new Attributes(type));
}
}
class Alignment {
private jc: Array<P>;
constructor(type: string) {
this.jc = new Array<P>();
this.jc.push(new Attributes(type));
}
}
export class Paragraph {
private p: Array<P>;
private properties: ParagraphProperties;
constructor(text?: string) {
this.p = new Array<P>();
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;
}
}

View File

@ -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<P>;
constructor() {
this.pPr = new Array<P>();
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<P>();
this.r.push(new ParagraphProperties());
this.r.push(new Text(text));
}
}

29
ts/tests/attributeTest.ts Normal file
View File

@ -0,0 +1,29 @@
/// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/chai/chai.d.ts" />
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");
});
});
});

28
ts/tests/documentTest.ts Normal file
View File

@ -0,0 +1,28 @@
/// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/chai/chai.d.ts" />
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);
});
});
});

View File

@ -1,41 +0,0 @@
/// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/chai/chai.d.ts" />
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);
});
});
});

70
ts/tests/paragraphTest.ts Normal file
View File

@ -0,0 +1,70 @@
/// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/chai/chai.d.ts" />
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");
});
});
});