2022-06-26 23:26:42 +01:00
|
|
|
import { IContext, IXmlableObject, XmlComponent } from "@file/xml-components";
|
2020-08-03 14:58:30 +12:00
|
|
|
import { CustomPropertiesAttributes } from "./custom-properties-attributes";
|
|
|
|
import { CustomProperty, ICustomPropertyOptions } from "./custom-property";
|
|
|
|
|
|
|
|
export class CustomProperties extends XmlComponent {
|
2022-09-15 20:00:50 +01:00
|
|
|
// eslint-disable-next-line functional/prefer-readonly-type
|
2020-08-03 14:58:30 +12:00
|
|
|
private nextId: number;
|
2022-09-15 20:00:50 +01:00
|
|
|
// eslint-disable-next-line functional/prefer-readonly-type
|
2020-08-03 14:58:30 +12:00
|
|
|
private readonly properties: CustomProperty[] = [];
|
|
|
|
|
2022-09-15 20:00:50 +01:00
|
|
|
public constructor(properties: readonly ICustomPropertyOptions[]) {
|
2020-08-03 14:58:30 +12:00
|
|
|
super("Properties");
|
|
|
|
|
|
|
|
this.root.push(
|
|
|
|
new CustomPropertiesAttributes({
|
|
|
|
xmlns: "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties",
|
|
|
|
vt: "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
// I'm not sure why, but every example I have seen starts with 2
|
|
|
|
// https://docs.microsoft.com/en-us/office/open-xml/how-to-set-a-custom-property-in-a-word-processing-document
|
|
|
|
this.nextId = 2;
|
|
|
|
|
|
|
|
for (const property of properties) {
|
|
|
|
this.addCustomProperty(property);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 01:06:55 +00:00
|
|
|
public prepForXml(context: IContext): IXmlableObject | undefined {
|
2020-08-03 14:58:30 +12:00
|
|
|
this.properties.forEach((x) => this.root.push(x));
|
2021-03-11 01:06:55 +00:00
|
|
|
return super.prepForXml(context);
|
2020-08-03 14:58:30 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
public addCustomProperty(property: ICustomPropertyOptions): void {
|
2022-09-19 20:48:50 +01:00
|
|
|
// eslint-disable-next-line functional/immutable-data
|
2020-08-03 14:58:30 +12:00
|
|
|
this.properties.push(new CustomProperty(this.nextId++, property));
|
|
|
|
}
|
|
|
|
}
|