Files
docx-js/src/file/xml-components/xml-component.ts

43 lines
1.1 KiB
TypeScript
Raw Normal View History

2017-09-19 15:46:20 +01:00
import { BaseXmlComponent } from "./base";
import { IXmlableObject } from "./xmlable-object";
export { BaseXmlComponent };
export abstract class XmlComponent extends BaseXmlComponent {
2018-09-14 02:33:36 +01:00
protected root: Array<BaseXmlComponent | string>;
2017-09-19 15:46:20 +01:00
constructor(rootKey: string) {
2017-09-19 15:46:20 +01:00
super(rootKey);
this.root = new Array<BaseXmlComponent | string>();
2017-09-19 15:46:20 +01:00
}
public prepForXml(): IXmlableObject | undefined {
2018-01-23 01:33:12 +00:00
const children = this.root
2018-05-06 03:19:36 +01:00
.filter((c) => {
if (c instanceof BaseXmlComponent) {
2018-08-07 01:25:28 +01:00
return !c.IsDeleted;
}
return true;
})
2018-01-23 01:33:12 +00:00
.map((comp) => {
if (comp instanceof BaseXmlComponent) {
return comp.prepForXml();
}
return comp;
})
.filter((comp) => comp !== undefined); // Exclude undefined
2017-09-19 15:46:20 +01:00
return {
[this.rootKey]: children,
};
}
2018-04-23 11:49:57 +02:00
2018-05-06 03:19:36 +01:00
public addChildElement(child: XmlComponent | string): XmlComponent {
2018-04-23 11:49:57 +02:00
this.root.push(child);
2018-05-06 03:19:36 +01:00
return this;
2018-04-23 11:49:57 +02:00
}
2018-05-06 03:19:36 +01:00
public delete(): void {
this.deleted = true;
}
2017-09-19 15:46:20 +01:00
}