added page break

This commit is contained in:
Dolan Miu
2016-03-30 00:28:05 +01:00
parent e04d9e0c97
commit 099da3d90c
8 changed files with 74 additions and 30 deletions

View File

@ -1,4 +1,4 @@
import {XmlComponent, Attributes} from "./xml-components"; import {XmlComponent, Attributes} from "../xml-components";
class Border implements XmlComponent { class Border implements XmlComponent {
private bottom: Array<XmlComponent>; private bottom: Array<XmlComponent>;

View File

@ -1,5 +1,8 @@
import {XmlComponent, Attributes, ParagraphProperties, Run} from "./xml-components"; import {XmlComponent, Attributes} from "../xml-components";
import {ThematicBreak} from "./border"; import {ThematicBreak} from "./border";
import {PageBreak} from "./page-break";
import {TextRun} from "./text-run";
import {ParagraphProperties} from "./properties";
class Style { class Style {
private pStyle: Array<XmlComponent>; private pStyle: Array<XmlComponent>;
@ -32,10 +35,10 @@ export class Paragraph {
this.p.push(new Attributes()); this.p.push(new Attributes());
this.properties = new ParagraphProperties(); this.properties = new ParagraphProperties();
this.p.push(this.properties); 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); this.p.push(run);
return this; return this;
} }
@ -96,10 +99,7 @@ export class Paragraph {
} }
pageBreak() { pageBreak() {
this.properties.push(new ThematicBreak()); this.properties.push(new PageBreak());
paragraphProperties.push(pBreak);
return this; return this;
} }
} }

View File

@ -0,0 +1,19 @@
import {XmlComponent, Attributes, Run} from "../xml-components";
class Break implements XmlComponent {
private br: Array<XmlComponent>;
constructor() {
this.br = new Array<XmlComponent>();
this.br.push(new Attributes({
type: "page"
}))
}
}
export class PageBreak extends Run {
constructor() {
super();
this.r.push(new Break());
}
}

View File

@ -0,0 +1,14 @@
import {XmlComponent, Attributes} from "../xml-components";
export class ParagraphProperties implements XmlComponent {
private pPr: Array<XmlComponent>;
constructor() {
this.pPr = new Array<XmlComponent>();
this.pPr.push(new Attributes());
}
push(item: XmlComponent) {
this.pPr.push(item);
}
}

View File

@ -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));
}
}

View File

@ -7,6 +7,7 @@ interface AttributesProperties {
color?: string; color?: string;
space?: string; space?: string;
sz?: string; sz?: string;
type?: string;
} }
export class Attributes implements XmlComponent { export class Attributes implements XmlComponent {
@ -21,26 +22,11 @@ export class Attributes implements XmlComponent {
} }
} }
export class ParagraphProperties implements XmlComponent { export class Run implements XmlComponent {
private pPr: Array<XmlComponent>; protected r: Array<XmlComponent>;
constructor() { constructor() {
this.pPr = new Array<XmlComponent>();
this.pPr.push(new Attributes());
}
push(item: XmlComponent) {
this.pPr.push(item);
}
}
export class Run implements XmlComponent {
private r: Array<XmlComponent>;
constructor(text: string) {
this.r = new Array<XmlComponent>(); this.r = new Array<XmlComponent>();
this.r.push(new ParagraphProperties());
this.r.push(new Text(text));
} }
} }

View File

@ -1,6 +1,6 @@
/// <reference path="../typings/mocha/mocha.d.ts" /> /// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/chai/chai.d.ts" /> /// <reference path="../typings/chai/chai.d.ts" />
import {ThematicBreak} from "../docx/border"; import {ThematicBreak} from "../docx/paragraph/border";
import {assert} from "chai"; import {assert} from "chai";
function jsonify(obj: Object) { function jsonify(obj: Object) {

View File

@ -85,4 +85,18 @@ describe('Paragraph', () => {
assert.isDefined(newJson.p[1].pPr[1].pBdr); 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");
});
});
}); });