Allow custom text properties to be included

This commit is contained in:
Max Lay
2020-08-03 14:58:30 +12:00
parent a6eb8e01df
commit 870c222dd5
12 changed files with 203 additions and 10 deletions

View File

@ -0,0 +1,13 @@
import { XmlAttributeComponent } from "file/xml-components";
export interface ICustomPropertiesAttributes {
readonly xmlns: string;
readonly vt: string;
}
export class CustomPropertiesAttributes extends XmlAttributeComponent<ICustomPropertiesAttributes> {
protected readonly xmlKeys = {
xmlns: "xmlns",
vt: "xmlns:vt",
};
}

View File

@ -0,0 +1,67 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { CustomProperties } from "./custom-properties";
describe("CustomProperties", () => {
describe("#constructor()", () => {
it("sets the appropriate attributes on the top-level", () => {
const properties = new CustomProperties([]);
const tree = new Formatter().format(properties);
expect(tree).to.deep.equal({
Properties: {
_attr: {
"xmlns": "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties",
"xmlns:vt": "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes",
},
},
});
});
it("should create custom properties with all the attributes given", () => {
const properties = new CustomProperties([{ name: "Address", value: "123" }, { name: "Author", value: "456" }]);
const tree = new Formatter().format(properties);
expect(tree).to.deep.equal({
Properties: [
{
_attr: {
"xmlns": "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties",
"xmlns:vt": "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes",
},
},
{
property: [
{
_attr: {
fmtid: "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}",
pid: "2",
name: "Address",
},
},
{
"vt:lpwstr": [
"123",
],
},
],
},
{
property: [
{
_attr: {
fmtid: "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}",
pid: "3",
name: "Author",
},
},
{
"vt:lpwstr": [
"456",
],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,41 @@
import { IXmlableObject, XmlComponent } from "file/xml-components";
import { CustomPropertiesAttributes } from "./custom-properties-attributes";
import { CustomProperty, ICustomPropertyOptions } from "./custom-property";
export class CustomProperties extends XmlComponent {
// tslint:disable-next-line:readonly-keyword
private nextId: number;
private readonly properties: CustomProperty[] = [];
constructor(properties: ICustomPropertyOptions[]) {
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);
}
}
public prepForXml(): IXmlableObject | undefined {
this.properties.forEach((x) => this.root.push(x));
return super.prepForXml();
}
public addCustomProperty(property: ICustomPropertyOptions): void {
this.properties.push(new CustomProperty(this.nextId++, property));
}
public get Properties(): CustomProperty[] {
return this.properties;
}
}

View File

@ -0,0 +1,15 @@
import { XmlAttributeComponent } from "file/xml-components";
export interface ICustomPropertyAttributes {
readonly fmtid: string;
readonly pid: string;
readonly name: string;
}
export class CustomPropertyAttributes extends XmlAttributeComponent<ICustomPropertyAttributes> {
protected readonly xmlKeys = {
fmtid: "fmtid",
pid: "pid",
name: "name",
};
}

View File

@ -0,0 +1,26 @@
import { XmlComponent } from "file/xml-components";
import { CustomPropertyAttributes } from "./custom-property-attributes";
export interface ICustomPropertyOptions {
readonly name: string;
readonly value: string;
}
export class CustomProperty extends XmlComponent {
constructor(id: number, properties: ICustomPropertyOptions) {
super("property");
this.root.push(new CustomPropertyAttributes({
fmtid: "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}",
pid: id.toString(),
name: properties.name,
}));
this.root.push(new CustomPropertyValue(properties.value));
}
}
export class CustomPropertyValue extends XmlComponent {
constructor(value: string) {
super("vt:lpwstr");
this.root.push(value);
}
}

View File

@ -0,0 +1,2 @@
export * from "./custom-properties";
export * from "./custom-property";