added xmlKeys to xmlcomponent

This commit is contained in:
Dolan Miu
2016-04-03 01:44:18 +01:00
parent a70e82a7d0
commit 69edfcc0c9
24 changed files with 137 additions and 59 deletions

View File

@ -1,7 +1,11 @@
import {XmlComponent, Attributes} from "../../xml-components"; import {XmlComponent, Attributes} from "../../xml-components";
export class Columns { export class Columns implements XmlComponent {
private cols: Array<XmlComponent>; private cols: Array<XmlComponent>;
xmlKeys = {
cols: 'w:cols'
}
constructor() { constructor() {
this.cols = new Array<XmlComponent>(); this.cols = new Array<XmlComponent>();

View File

@ -1,7 +1,11 @@
import {XmlComponent, Attributes} from "../../xml-components"; import {XmlComponent, Attributes} from "../../xml-components";
export class DocumentGrid { export class DocumentGrid implements XmlComponent {
private docGrid: Array<XmlComponent>; private docGrid: Array<XmlComponent>;
xmlKeys = {
docGrid: 'w:docGrid'
}
constructor() { constructor() {
this.docGrid = new Array<XmlComponent>(); this.docGrid = new Array<XmlComponent>();

View File

@ -5,9 +5,13 @@ import {PageMargin} from "./page-margin";
import {Columns} from "./columns"; import {Columns} from "./columns";
import {DocumentGrid} from "./doc-grid"; import {DocumentGrid} from "./doc-grid";
export class Body { export class Body implements XmlComponent {
private body: Array<XmlComponent>; private body: Array<XmlComponent>;
xmlKeys = {
body: 'w:body'
}
constructor() { constructor() {
this.body = new Array<XmlComponent>(); this.body = new Array<XmlComponent>();
this.body.push(new SectionProperties()); this.body.push(new SectionProperties());
@ -16,7 +20,7 @@ export class Body {
this.body.push(new Columns()); this.body.push(new Columns());
this.body.push(new DocumentGrid()); this.body.push(new DocumentGrid());
} }
push(component: XmlComponent) { push(component: XmlComponent) {
this.body.push(component); this.body.push(component);
} }

View File

@ -1,7 +1,11 @@
import {XmlComponent, Attributes} from "../../xml-components"; import {XmlComponent, Attributes} from "../../xml-components";
export class PageMargin { export class PageMargin implements XmlComponent {
private pgMar: Array<XmlComponent>; private pgMar: Array<XmlComponent>;
xmlKeys = {
pgMar: 'w:pgMar'
}
constructor() { constructor() {
this.pgMar = new Array<XmlComponent>(); this.pgMar = new Array<XmlComponent>();

View File

@ -1,7 +1,11 @@
import {XmlComponent, Attributes} from "../../xml-components"; import {XmlComponent, Attributes} from "../../xml-components";
export class PageSize { export class PageSize implements XmlComponent {
private pgSz: Array<XmlComponent>; private pgSz: Array<XmlComponent>;
xmlKeys = {
pgSz: 'w:pgSz'
}
constructor() { constructor() {
this.pgSz = new Array<XmlComponent>(); this.pgSz = new Array<XmlComponent>();

View File

@ -1,8 +1,12 @@
import {XmlComponent, Attributes} from "../../xml-components"; import {XmlComponent, Attributes} from "../../xml-components";
export class SectionProperties { export class SectionProperties implements XmlComponent {
private sectPr: Array<XmlComponent>; private sectPr: Array<XmlComponent>;
xmlKeys = {
sectPr: 'sectPr'
}
constructor() { constructor() {
this.sectPr = new Array<XmlComponent>(); this.sectPr = new Array<XmlComponent>();
this.sectPr.push(new Attributes({ this.sectPr.push(new Attributes({

View File

@ -3,6 +3,10 @@ import {XmlComponent, Attributes} from "../xml-components";
class Border implements XmlComponent { class Border implements XmlComponent {
private bottom: Array<XmlComponent>; private bottom: Array<XmlComponent>;
xmlKeys = {
bottom: 'w:bottom'
}
constructor() { constructor() {
this.bottom = new Array<XmlComponent>(); this.bottom = new Array<XmlComponent>();
this.bottom.push(new Attributes({ this.bottom.push(new Attributes({
@ -14,9 +18,13 @@ class Border implements XmlComponent {
} }
} }
export class ThematicBreak { export class ThematicBreak implements XmlComponent {
private pBdr: Array<XmlComponent>; private pBdr: Array<XmlComponent>;
xmlKeys = {
pBdr: 'w:pBdr'
}
constructor() { constructor() {
this.pBdr = new Array<XmlComponent>(); this.pBdr = new Array<XmlComponent>();
this.pBdr.push(new Border()); this.pBdr.push(new Border());

View File

@ -7,8 +7,12 @@ import {TabStop} from "../tab-stop";
import {Style} from "./style"; import {Style} from "./style";
import {NumberProperties} from "./unordered-list"; import {NumberProperties} from "./unordered-list";
class Alignment { class Alignment implements XmlComponent {
private jc: Array<XmlComponent>; private jc: Array<XmlComponent>;
xmlKeys = {
jc: 'w:jc'
}
constructor(type: string) { constructor(type: string) {
this.jc = new Array<XmlComponent>(); this.jc = new Array<XmlComponent>();
@ -18,9 +22,13 @@ class Alignment {
} }
} }
export class Paragraph { export class Paragraph implements XmlComponent {
private p: Array<XmlComponent>; private p: Array<XmlComponent>;
private properties: ParagraphProperties; private properties: ParagraphProperties;
xmlKeys = {
p: 'w:p'
}
constructor(text?: string) { constructor(text?: string) {
this.p = new Array<XmlComponent>(); this.p = new Array<XmlComponent>();

View File

@ -3,6 +3,11 @@ import {Run} from "../run";
class Break implements XmlComponent { class Break implements XmlComponent {
private br: Array<XmlComponent>; private br: Array<XmlComponent>;
xmlKeys = {
br: 'w:br'
}
constructor() { constructor() {
this.br = new Array<XmlComponent>(); this.br = new Array<XmlComponent>();
this.br.push(new Attributes({ this.br.push(new Attributes({

View File

@ -2,6 +2,10 @@ import {XmlComponent, Attributes} from "../xml-components";
export class ParagraphProperties implements XmlComponent { export class ParagraphProperties implements XmlComponent {
private pPr: Array<XmlComponent>; private pPr: Array<XmlComponent>;
xmlKeys = {
pPr: 'w:rPr'
}
constructor() { constructor() {
this.pPr = new Array<XmlComponent>(); this.pPr = new Array<XmlComponent>();

View File

@ -2,6 +2,10 @@ import {XmlComponent, Attributes} from "../xml-components";
export class Style implements XmlComponent { export class Style implements XmlComponent {
private pStyle: Array<XmlComponent>; private pStyle: Array<XmlComponent>;
xmlKeys = {
pStyle: 'w:pStyle'
}
constructor(type: string) { constructor(type: string) {
this.pStyle = new Array<XmlComponent>(); this.pStyle = new Array<XmlComponent>();

View File

@ -4,6 +4,10 @@ import {Style} from "./style";
export class NumberProperties implements XmlComponent { export class NumberProperties implements XmlComponent {
private numPr: Array<XmlComponent>; private numPr: Array<XmlComponent>;
xmlKeys = {
numPr: 'w:numPr'
}
constructor() { constructor() {
this.numPr = new Array<XmlComponent>(); this.numPr = new Array<XmlComponent>();
this.numPr.push(new IndentLevel(0)); this.numPr.push(new IndentLevel(0));
@ -13,7 +17,11 @@ export class NumberProperties implements XmlComponent {
export class IndentLevel implements XmlComponent { export class IndentLevel implements XmlComponent {
private ilvl: Array<XmlComponent>; private ilvl: Array<XmlComponent>;
xmlKeys = {
ilvl: 'w:ilvl'
}
constructor(level: number) { constructor(level: number) {
this.ilvl = new Array<XmlComponent>(); this.ilvl = new Array<XmlComponent>();
this.ilvl.push(new Attributes({ this.ilvl.push(new Attributes({
@ -24,7 +32,11 @@ export class IndentLevel implements XmlComponent {
export class NumberId implements XmlComponent { export class NumberId implements XmlComponent {
private ilvl: Array<XmlComponent>; private ilvl: Array<XmlComponent>;
xmlKeys = {
ilvl: 'w:ilvl'
}
constructor(id: number) { constructor(id: number) {
this.ilvl = new Array<XmlComponent>(); this.ilvl = new Array<XmlComponent>();
this.ilvl.push(new Attributes({ this.ilvl.push(new Attributes({

View File

@ -1,8 +1,12 @@
import {XmlComponent, Attributes} from "../xml-components"; import {XmlComponent, Attributes} from "../xml-components";
export class Bold { export class Bold implements XmlComponent {
private b: Array<XmlComponent>; private b: Array<XmlComponent>;
xmlKeys = {
b: 'w:b'
}
constructor() { constructor() {
this.b = new Array<XmlComponent>(); this.b = new Array<XmlComponent>();
this.b.push(new Attributes({ this.b.push(new Attributes({
@ -13,7 +17,11 @@ export class Bold {
export class Italics { export class Italics {
private i: Array<XmlComponent>; private i: Array<XmlComponent>;
xmlKeys = {
i: 'w:i'
}
constructor() { constructor() {
this.i = new Array<XmlComponent>(); this.i = new Array<XmlComponent>();
this.i.push(new Attributes({ this.i.push(new Attributes({
@ -24,7 +32,11 @@ export class Italics {
export class Underline { export class Underline {
private u: Array<XmlComponent>; private u: Array<XmlComponent>;
xmlKeys = {
u: 'w:u'
}
constructor() { constructor() {
this.u = new Array<XmlComponent>(); this.u = new Array<XmlComponent>();
this.u.push(new Attributes({ this.u.push(new Attributes({

View File

@ -5,6 +5,10 @@ import {Bold, Italics, Underline} from "./emphasis";
export class Run implements XmlComponent { export class Run implements XmlComponent {
protected r: Array<XmlComponent>; protected r: Array<XmlComponent>;
private properties: RunProperties; private properties: RunProperties;
xmlKeys = {
r: 'w:r'
}
constructor() { constructor() {
this.r = new Array<XmlComponent>(); this.r = new Array<XmlComponent>();

View File

@ -1,7 +1,11 @@
import {XmlComponent, Attributes} from "../xml-components"; import {XmlComponent, Attributes} from "../xml-components";
export class RunProperties { export class RunProperties implements XmlComponent {
private rPr: Array<XmlComponent>; private rPr: Array<XmlComponent>;
xmlKeys = {
rPr: 'w:rPr'
}
constructor() { constructor() {
this.rPr = new Array<XmlComponent>(); this.rPr = new Array<XmlComponent>();

View File

@ -1,3 +1,5 @@
export class TabStop { import {XmlComponent, Attributes} from "./xml-components";
export class TabStop implements XmlComponent{
xmlKeys = {}
} }

View File

@ -1,5 +1,5 @@
export interface XmlComponent { export interface XmlComponent {
xmlKeys: Object;
} }
interface AttributesProperties { interface AttributesProperties {
@ -26,6 +26,8 @@ interface AttributesProperties {
export class Attributes implements XmlComponent { export class Attributes implements XmlComponent {
private _attrs: Object; private _attrs: Object;
xmlKeys = {};
constructor(properties?: AttributesProperties) { constructor(properties?: AttributesProperties) {
this._attrs = properties this._attrs = properties
@ -38,6 +40,10 @@ export class Attributes implements XmlComponent {
export class Text implements XmlComponent { export class Text implements XmlComponent {
private t: string; private t: string;
xmlKeys = {
t: 'w:t'
}
constructor(text: string) { constructor(text: string) {
this.t = text; this.t = text;
} }

View File

@ -2,57 +2,36 @@ import * as _ from "lodash";
export class Formatter { export class Formatter {
private xmlKeyDictionary = {
p: 'w:p',
t: 'w:t',
color: 'w:color',
space: 'w:space',
sz: 'w:sz',
val: 'w:val',
type: 'w:type',
ilvl: 'w:ilvl',
numId: 'w:numId',
pBdr: 'w:pBdr',
jc: 'w:jc',
r: 'w:r',
pPr: 'w:pPr',
pStyle: 'w:pStyle',
numPr: 'w:numPr',
b: 'w:b',
i: 'w:i',
u: 'w:u',
rPr: 'w:rPr'
};
format(input: any): Object { format(input: any): Object {
var newJson = this.jsonify(input); this.deepTraverseJson(input, (parent, value, key) => {
this.deepTraverseJson(newJson, (parent, value, key) => {
if (isNaN(key)) { if (isNaN(key)) {
var newKey = this.getReplacementKey(key); var newKey = this.getReplacementKey(parent, key);
parent[newKey] = parent[key];
if (newKey !== key) { if (newKey !== key) {
parent[newKey] = parent[key];
delete parent[key]; delete parent[key];
} else { } else {
console.error("Key is not in dictionary:" + key); //console.error("Key is not in dictionary:" + key);
} }
} }
}); });
newJson = this.clenseProperties(newJson); var newJson = this.clense(input);
return newJson; return newJson;
} }
private clenseProperties(input: any): Object { private clense(input: any): Object {
var newJson = this.jsonify(input); var newJson = this.jsonify(input);
this.deepTraverseJson(newJson, (parent, value, key) => { this.deepTraverseJson(newJson, (parent, value, key) => {
if (key === "properties") { if (key === "properties") {
delete parent[key]; delete parent[key];
} }
if (key === "xmlKeys") {
delete parent[key];
}
}); });
return newJson return newJson
} }
@ -63,15 +42,15 @@ export class Formatter {
private deepTraverseJson(json: Object, lambda: (json: any, value: any, key: any) => void): void { private deepTraverseJson(json: Object, lambda: (json: any, value: any, key: any) => void): void {
_.forOwn(json, (value, key) => { _.forOwn(json, (value, key) => {
if (_.isObject(value)) { if (_.isObject(value) && key !== "xmlKeys") {
this.deepTraverseJson(value, lambda); this.deepTraverseJson(value, lambda);
} }
lambda(json, value, key); lambda(json, value, key);
}); });
} }
private getReplacementKey(key: string): string { private getReplacementKey(input: any, key: string): string {
var newKey = this.xmlKeyDictionary[key]; var newKey = input.xmlKeys[key];
if (newKey !== undefined) { if (newKey !== undefined) {
return newKey; return newKey;

View File

@ -7,7 +7,7 @@ export class ExpressPacker extends Packer {
private res: express.Response; private res: express.Response;
constructor(document: Document, res: express.Response) { constructor(document: Document, res: express.Response) {
super(document); super(document, null, null);
this.res = res; this.res = res;
this.res.on('close', () => { this.res.on('close', () => {

View File

@ -6,7 +6,7 @@ export class LocalPacker extends Packer {
private stream: fs.WriteStream private stream: fs.WriteStream
constructor(document: Document, path: string) { constructor(document: Document, path: string) {
super(document); super(document, null, null);
this.stream = fs.createWriteStream(path); this.stream = fs.createWriteStream(path);
} }

View File

@ -0,0 +1,3 @@
export class Properties {
}

View File

@ -0,0 +1,3 @@
export class Style {
}

View File

@ -8,7 +8,7 @@ function jsonify(obj: Object) {
return JSON.parse(stringifiedJson); return JSON.parse(stringifiedJson);
} }
describe.only('Body', () => { describe('Body', () => {
var body: Body; var body: Body;
beforeEach(() => { beforeEach(() => {

View File

@ -19,7 +19,7 @@ describe('Formatter', () => {
}); });
describe('#format()', () => { describe('#format()', () => {
it("should format simple paragraph", () => { it.only("should format simple paragraph", () => {
var paragraph = new docx.Paragraph(); var paragraph = new docx.Paragraph();
var newJson = formatter.format(paragraph); var newJson = formatter.format(paragraph);
newJson = jsonify(newJson); newJson = jsonify(newJson);