From 3f8d23662e1b3cf9f61b88de4ac9b39d65c97bed Mon Sep 17 00:00:00 2001 From: Dolan Miu Date: Mon, 25 Apr 2016 03:28:02 +0100 Subject: [PATCH] made attributes able to be replaced --- ts/docx/xml-components/index.ts | 75 +++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 28 deletions(-) diff --git a/ts/docx/xml-components/index.ts b/ts/docx/xml-components/index.ts index 03b030d8fc..42c15ed5de 100644 --- a/ts/docx/xml-components/index.ts +++ b/ts/docx/xml-components/index.ts @@ -1,3 +1,5 @@ +import * as _ from "lodash"; + export abstract class BaseXmlComponent { protected rootKey: string; @@ -44,6 +46,28 @@ export abstract class XmlUnitComponent extends BaseXmlComponent { } } +export abstract class XmlAttributeComponent extends BaseXmlComponent { + protected root: Object; + private xmlKeys: Object; + + constructor(xmlKeys: Object) { + super("_attr"); + this.xmlKeys = xmlKeys; + } + + replaceKey(): void { + if (this.root !== undefined) { + _.forOwn(this.root, (value, key) => { + var newKey = this.xmlKeys[key]; + this.root[newKey] = value; + delete this.root[key]; + }); + this[this.rootKey] = this.root; + delete this.root; + } + } +} + interface AttributesProperties { val?: any; color?: string; @@ -65,39 +89,34 @@ interface AttributesProperties { linePitch?: string; } -export class Attributes extends XmlComponent { - private _attr: Object; - - xmlKeys = { - val: "w:val", - color: "w:color", - space: "w:space", - sz: "w:sz", - type: "w:type", - rsidR: "w:rsidR", - rsidRPr: "w:rsidRPr", - rsidSect: "w:rsidSect", - w: "w:w", - h: "w:h", - top: "w:top", - right: "w:right", - bottom: "w:bottom", - left: "w:left", - header: "w:header", - footer: "w:footer", - gutter: "w:gutter", - linePitch: "w:linePitch" - }; +export class Attributes extends XmlAttributeComponent { constructor(properties?: AttributesProperties) { - super("_attr"); - this._attr = properties + super({ + val: "w:val", + color: "w:color", + space: "w:space", + sz: "w:sz", + type: "w:type", + rsidR: "w:rsidR", + rsidRPr: "w:rsidRPr", + rsidSect: "w:rsidSect", + w: "w:w", + h: "w:h", + top: "w:top", + right: "w:right", + bottom: "w:bottom", + left: "w:left", + header: "w:header", + footer: "w:footer", + gutter: "w:gutter", + linePitch: "w:linePitch" + }); + this.root = properties if (!properties) { - this._attr = {}; + this.root = {}; } - - this._attr["xmlKeys"] = this.xmlKeys; } }