Moved addTableOfContents to File and creating a settings.xml and applying updateFields=true when there is a table of contents
This commit is contained in:
2
src/file/settings/index.ts
Normal file
2
src/file/settings/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from "./settings";
|
||||
export * from "./update-fields";
|
78
src/file/settings/settings.spec.ts
Normal file
78
src/file/settings/settings.spec.ts
Normal file
@ -0,0 +1,78 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "../../export/formatter";
|
||||
import { Settings } from "./";
|
||||
|
||||
describe("Settings", () => {
|
||||
describe("#constructor", () => {
|
||||
it("should create a empty Settings with correct rootKey", () => {
|
||||
const settings = new Settings();
|
||||
const tree = new Formatter().format(settings);
|
||||
let keys = Object.keys(tree);
|
||||
|
||||
expect(keys).is.an.instanceof(Array);
|
||||
expect(keys).has.length(1);
|
||||
expect(keys[0]).to.be.equal("w:settings");
|
||||
|
||||
expect(tree["w:settings"]).is.an.instanceof(Array);
|
||||
expect(tree["w:settings"]).has.length(1);
|
||||
|
||||
keys = Object.keys(tree["w:settings"][0]);
|
||||
expect(keys).is.an.instanceof(Array);
|
||||
expect(keys).has.length(1);
|
||||
expect(keys[0]).to.be.equal("_attr");
|
||||
});
|
||||
});
|
||||
|
||||
describe("#addUpdateFields", () => {
|
||||
const assertSettingsWithUpdateFields = (settings: Settings) => {
|
||||
const tree = new Formatter().format(settings);
|
||||
let keys = Object.keys(tree);
|
||||
|
||||
expect(keys).is.an.instanceof(Array);
|
||||
expect(keys).has.length(1);
|
||||
expect(keys[0]).to.be.equal("w:settings");
|
||||
|
||||
const rootArray = tree["w:settings"];
|
||||
expect(rootArray).is.an.instanceof(Array);
|
||||
expect(rootArray).has.length(2);
|
||||
|
||||
keys = Object.keys(rootArray[0]);
|
||||
expect(keys).is.an.instanceof(Array);
|
||||
expect(keys).has.length(1);
|
||||
expect(keys[0]).to.be.equal("_attr");
|
||||
|
||||
keys = Object.keys(rootArray[1]);
|
||||
expect(keys).is.an.instanceof(Array);
|
||||
expect(keys).has.length(1);
|
||||
expect(keys[0]).to.be.equal("w:updateFields");
|
||||
|
||||
const updateFieldsArray = rootArray[1]["w:updateFields"];
|
||||
keys = Object.keys(updateFieldsArray[0]);
|
||||
expect(keys).is.an.instanceof(Array);
|
||||
expect(keys).has.length(1);
|
||||
expect(keys[0]).to.be.equal("_attr");
|
||||
|
||||
const updateFieldsAttr = updateFieldsArray[0]._attr;
|
||||
expect(updateFieldsAttr["w:val"]).to.be.equal(true);
|
||||
};
|
||||
|
||||
it("should add a UpdateFields with value true", () => {
|
||||
const settings = new Settings();
|
||||
settings.addUpdateFields();
|
||||
|
||||
assertSettingsWithUpdateFields(settings);
|
||||
});
|
||||
|
||||
it("should add a UpdateFields with value true only once", () => {
|
||||
const settings = new Settings();
|
||||
settings.addUpdateFields();
|
||||
|
||||
assertSettingsWithUpdateFields(settings);
|
||||
|
||||
settings.addUpdateFields();
|
||||
|
||||
assertSettingsWithUpdateFields(settings);
|
||||
});
|
||||
});
|
||||
});
|
77
src/file/settings/settings.ts
Normal file
77
src/file/settings/settings.ts
Normal file
@ -0,0 +1,77 @@
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
import { UpdateFields } from "./update-fields";
|
||||
|
||||
export interface ISettingsAttributesProperties {
|
||||
wpc?: string;
|
||||
mc?: string;
|
||||
o?: string;
|
||||
r?: string;
|
||||
m?: string;
|
||||
v?: string;
|
||||
wp14?: string;
|
||||
wp?: string;
|
||||
w10?: string;
|
||||
w?: string;
|
||||
w14?: string;
|
||||
w15?: string;
|
||||
wpg?: string;
|
||||
wpi?: string;
|
||||
wne?: string;
|
||||
wps?: string;
|
||||
Ignorable?: string;
|
||||
}
|
||||
|
||||
export class SettingsAttributes extends XmlAttributeComponent<ISettingsAttributesProperties> {
|
||||
protected xmlKeys = {
|
||||
wpc: "xmlns:wpc",
|
||||
mc: "xmlns:mc",
|
||||
o: "xmlns:o",
|
||||
r: "xmlns:r",
|
||||
m: "xmlns:m",
|
||||
v: "xmlns:v",
|
||||
wp14: "xmlns:wp14",
|
||||
wp: "xmlns:wp",
|
||||
w10: "xmlns:w10",
|
||||
w: "xmlns:w",
|
||||
w14: "xmlns:w14",
|
||||
w15: "xmlns:w15",
|
||||
wpg: "xmlns:wpg",
|
||||
wpi: "xmlns:wpi",
|
||||
wne: "xmlns:wne",
|
||||
wps: "xmlns:wps",
|
||||
Ignorable: "mc:Ignorable",
|
||||
};
|
||||
}
|
||||
|
||||
export class Settings extends XmlComponent {
|
||||
constructor() {
|
||||
super("w:settings");
|
||||
this.root.push(
|
||||
new SettingsAttributes({
|
||||
wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
|
||||
mc: "http://schemas.openxmlformats.org/markup-compatibility/2006",
|
||||
o: "urn:schemas-microsoft-com:office:office",
|
||||
r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
|
||||
m: "http://schemas.openxmlformats.org/officeDocument/2006/math",
|
||||
v: "urn:schemas-microsoft-com:vml",
|
||||
wp14: "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
|
||||
wp: "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
|
||||
w10: "urn:schemas-microsoft-com:office:word",
|
||||
w: "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
|
||||
w14: "http://schemas.microsoft.com/office/word/2010/wordml",
|
||||
w15: "http://schemas.microsoft.com/office/word/2012/wordml",
|
||||
wpg: "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",
|
||||
wpi: "http://schemas.microsoft.com/office/word/2010/wordprocessingInk",
|
||||
wne: "http://schemas.microsoft.com/office/word/2006/wordml",
|
||||
wps: "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
|
||||
Ignorable: "w14 w15 wp14",
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
public addUpdateFields(): void {
|
||||
if (!this.root.find((child) => child instanceof UpdateFields)) {
|
||||
this.addChildElement(new UpdateFields());
|
||||
}
|
||||
}
|
||||
}
|
46
src/file/settings/update-fields.spec.ts
Normal file
46
src/file/settings/update-fields.spec.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "../../export/formatter";
|
||||
import { UpdateFields } from "./";
|
||||
|
||||
const UF_TRUE = {
|
||||
"w:updateFields": [
|
||||
{
|
||||
_attr: {
|
||||
"w:val": true,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const UF_FALSE = {
|
||||
"w:updateFields": [
|
||||
{
|
||||
_attr: {
|
||||
"w:val": false,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
describe("Update Fields", () => {
|
||||
describe("#constructor", () => {
|
||||
it("should construct a Update Fields with TRUE value by default", () => {
|
||||
const uf = new UpdateFields();
|
||||
const tree = new Formatter().format(uf);
|
||||
expect(tree).to.be.deep.equal(UF_TRUE);
|
||||
});
|
||||
|
||||
it("should construct a Update Fields with TRUE value", () => {
|
||||
const uf = new UpdateFields(true);
|
||||
const tree = new Formatter().format(uf);
|
||||
expect(tree).to.be.deep.equal(UF_TRUE);
|
||||
});
|
||||
|
||||
it("should construct a Update Fields with FALSE value", () => {
|
||||
const uf = new UpdateFields(false);
|
||||
const tree = new Formatter().format(uf);
|
||||
expect(tree).to.be.deep.equal(UF_FALSE);
|
||||
});
|
||||
});
|
||||
});
|
22
src/file/settings/update-fields.ts
Normal file
22
src/file/settings/update-fields.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
export interface IUpdateFieldsAttributesProperties {
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
export class UpdateFieldsAttributes extends XmlAttributeComponent<IUpdateFieldsAttributesProperties> {
|
||||
protected xmlKeys = {
|
||||
enabled: "w:val",
|
||||
};
|
||||
}
|
||||
|
||||
export class UpdateFields extends XmlComponent {
|
||||
constructor(enabled: boolean = true) {
|
||||
super("w:updateFields");
|
||||
this.root.push(
|
||||
new UpdateFieldsAttributes({
|
||||
enabled,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user