refactored out xml components into multiple files
This commit is contained in:
49
ts/docx/xml-components/attributes.ts
Normal file
49
ts/docx/xml-components/attributes.ts
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import {XmlAttributeComponent} from "./default-attributes";
|
||||||
|
|
||||||
|
interface AttributesProperties {
|
||||||
|
val?: any;
|
||||||
|
color?: string;
|
||||||
|
space?: string;
|
||||||
|
sz?: string;
|
||||||
|
type?: string;
|
||||||
|
rsidR?: string;
|
||||||
|
rsidRPr?: string;
|
||||||
|
rsidSect?: string;
|
||||||
|
w?: string;
|
||||||
|
h?: string;
|
||||||
|
top?: string;
|
||||||
|
right?: string;
|
||||||
|
bottom?: string;
|
||||||
|
left?: string;
|
||||||
|
header?: string;
|
||||||
|
footer?: string;
|
||||||
|
gutter?: string;
|
||||||
|
linePitch?: string;
|
||||||
|
pos?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Attributes extends XmlAttributeComponent {
|
||||||
|
|
||||||
|
constructor(properties?: AttributesProperties) {
|
||||||
|
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"
|
||||||
|
}, properties);
|
||||||
|
}
|
||||||
|
}
|
11
ts/docx/xml-components/base.ts
Normal file
11
ts/docx/xml-components/base.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
export abstract class BaseXmlComponent {
|
||||||
|
protected rootKey: string;
|
||||||
|
|
||||||
|
constructor(rootKey: string) {
|
||||||
|
this.rootKey = rootKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract replaceKey(): void;
|
||||||
|
clearVariables(): void {
|
||||||
|
};
|
||||||
|
}
|
29
ts/docx/xml-components/default-attributes.ts
Normal file
29
ts/docx/xml-components/default-attributes.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import {BaseXmlComponent} from "./base";
|
||||||
|
|
||||||
|
export abstract class XmlAttributeComponent extends BaseXmlComponent {
|
||||||
|
protected root: Object;
|
||||||
|
private xmlKeys: Object;
|
||||||
|
|
||||||
|
constructor(xmlKeys: Object, properties: Object) {
|
||||||
|
super("_attr");
|
||||||
|
this.xmlKeys = xmlKeys;
|
||||||
|
|
||||||
|
this.root = properties
|
||||||
|
|
||||||
|
if (!properties) {
|
||||||
|
this.root = {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,18 +1,6 @@
|
|||||||
import * as _ from "lodash";
|
import * as _ from "lodash";
|
||||||
import {ParagraphProperties} from "../paragraph/properties";
|
import {BaseXmlComponent} from "./base";
|
||||||
import {RunProperties} from "../run/properties";
|
|
||||||
|
|
||||||
export abstract class BaseXmlComponent {
|
|
||||||
protected rootKey: string;
|
|
||||||
|
|
||||||
constructor(rootKey: string) {
|
|
||||||
this.rootKey = rootKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract replaceKey(): void;
|
|
||||||
clearVariables(): void {
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export abstract class XmlComponent extends BaseXmlComponent {
|
export abstract class XmlComponent extends BaseXmlComponent {
|
||||||
protected root: Array<BaseXmlComponent>;
|
protected root: Array<BaseXmlComponent>;
|
||||||
@ -37,147 +25,7 @@ export abstract class XmlComponent extends BaseXmlComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export abstract class XmlUnitComponent extends BaseXmlComponent {
|
export * from "./attributes"
|
||||||
protected root: string;
|
export * from "./default-attributes";
|
||||||
|
export * from "./unit";
|
||||||
constructor(rootKey: string) {
|
export * from "./property";
|
||||||
super(rootKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
replaceKey(): void {
|
|
||||||
if (this.root !== undefined) {
|
|
||||||
this[this.rootKey] = this.root;
|
|
||||||
delete this.root;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export abstract class XmlAttributeComponent extends BaseXmlComponent {
|
|
||||||
protected root: Object;
|
|
||||||
private xmlKeys: Object;
|
|
||||||
|
|
||||||
constructor(xmlKeys: Object, properties: Object) {
|
|
||||||
super("_attr");
|
|
||||||
this.xmlKeys = xmlKeys;
|
|
||||||
|
|
||||||
this.root = properties
|
|
||||||
|
|
||||||
if (!properties) {
|
|
||||||
this.root = {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
space?: string;
|
|
||||||
sz?: string;
|
|
||||||
type?: string;
|
|
||||||
rsidR?: string;
|
|
||||||
rsidRPr?: string;
|
|
||||||
rsidSect?: string;
|
|
||||||
w?: string;
|
|
||||||
h?: string;
|
|
||||||
top?: string;
|
|
||||||
right?: string;
|
|
||||||
bottom?: string;
|
|
||||||
left?: string;
|
|
||||||
header?: string;
|
|
||||||
footer?: string;
|
|
||||||
gutter?: string;
|
|
||||||
linePitch?: string;
|
|
||||||
pos?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Attributes extends XmlAttributeComponent {
|
|
||||||
|
|
||||||
constructor(properties?: AttributesProperties) {
|
|
||||||
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"
|
|
||||||
}, properties);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class ParagraphPropertyXmlComponent extends XmlComponent {
|
|
||||||
private paragraphProperties: ParagraphProperties;
|
|
||||||
|
|
||||||
constructor(rootKey) {
|
|
||||||
super(rootKey);
|
|
||||||
this.paragraphProperties = new ParagraphProperties();
|
|
||||||
this.root.push(this.paragraphProperties);
|
|
||||||
}
|
|
||||||
|
|
||||||
clearVariables(): void {
|
|
||||||
this.paragraphProperties.clearVariables();
|
|
||||||
|
|
||||||
delete this.paragraphProperties;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class RunPropertyXmlComponent extends XmlComponent {
|
|
||||||
private runProperties: RunProperties;
|
|
||||||
|
|
||||||
constructor(rootKey) {
|
|
||||||
super(rootKey);
|
|
||||||
this.runProperties = new RunProperties();
|
|
||||||
this.root.push(this.runProperties);
|
|
||||||
}
|
|
||||||
|
|
||||||
clearVariables(): void {
|
|
||||||
this.runProperties.clearVariables();
|
|
||||||
|
|
||||||
delete this.runProperties;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class MultiPropertyXmlComponent extends XmlComponent {
|
|
||||||
private runProperties: RunProperties;
|
|
||||||
private paragraphProperties: ParagraphProperties;
|
|
||||||
|
|
||||||
constructor(rootKey) {
|
|
||||||
super(rootKey);
|
|
||||||
this.runProperties = new RunProperties();
|
|
||||||
this.root.push(this.runProperties);
|
|
||||||
|
|
||||||
this.paragraphProperties = new ParagraphProperties();
|
|
||||||
this.root.push(this.paragraphProperties);
|
|
||||||
}
|
|
||||||
|
|
||||||
clearVariables(): void {
|
|
||||||
this.runProperties.clearVariables();
|
|
||||||
this.paragraphProperties.clearVariables();
|
|
||||||
|
|
||||||
delete this.runProperties;
|
|
||||||
delete this.paragraphProperties;
|
|
||||||
}
|
|
||||||
}
|
|
57
ts/docx/xml-components/property.ts
Normal file
57
ts/docx/xml-components/property.ts
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import {XmlComponent} from "./"
|
||||||
|
import {ParagraphProperties} from "../paragraph/properties";
|
||||||
|
import {RunProperties} from "../run/properties";
|
||||||
|
|
||||||
|
export class ParagraphPropertyXmlComponent extends XmlComponent {
|
||||||
|
private paragraphProperties: ParagraphProperties;
|
||||||
|
|
||||||
|
constructor(rootKey) {
|
||||||
|
super(rootKey);
|
||||||
|
this.paragraphProperties = new ParagraphProperties();
|
||||||
|
this.root.push(this.paragraphProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
clearVariables(): void {
|
||||||
|
this.paragraphProperties.clearVariables();
|
||||||
|
|
||||||
|
delete this.paragraphProperties;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class RunPropertyXmlComponent extends XmlComponent {
|
||||||
|
private runProperties: RunProperties;
|
||||||
|
|
||||||
|
constructor(rootKey) {
|
||||||
|
super(rootKey);
|
||||||
|
this.runProperties = new RunProperties();
|
||||||
|
this.root.push(this.runProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
clearVariables(): void {
|
||||||
|
this.runProperties.clearVariables();
|
||||||
|
|
||||||
|
delete this.runProperties;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class MultiPropertyXmlComponent extends XmlComponent {
|
||||||
|
private runProperties: RunProperties;
|
||||||
|
private paragraphProperties: ParagraphProperties;
|
||||||
|
|
||||||
|
constructor(rootKey) {
|
||||||
|
super(rootKey);
|
||||||
|
this.runProperties = new RunProperties();
|
||||||
|
this.root.push(this.runProperties);
|
||||||
|
|
||||||
|
this.paragraphProperties = new ParagraphProperties();
|
||||||
|
this.root.push(this.paragraphProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
clearVariables(): void {
|
||||||
|
this.runProperties.clearVariables();
|
||||||
|
this.paragraphProperties.clearVariables();
|
||||||
|
|
||||||
|
delete this.runProperties;
|
||||||
|
delete this.paragraphProperties;
|
||||||
|
}
|
||||||
|
}
|
16
ts/docx/xml-components/unit.ts
Normal file
16
ts/docx/xml-components/unit.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import {BaseXmlComponent} from "./base";
|
||||||
|
|
||||||
|
export abstract class XmlUnitComponent extends BaseXmlComponent {
|
||||||
|
protected root: string;
|
||||||
|
|
||||||
|
constructor(rootKey: string) {
|
||||||
|
super(rootKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
replaceKey(): void {
|
||||||
|
if (this.root !== undefined) {
|
||||||
|
this[this.rootKey] = this.root;
|
||||||
|
delete this.root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user