Finish making numbering declarative

This commit is contained in:
Dolan
2019-11-08 03:11:19 +00:00
parent 9b40b5e55e
commit 643e3c2f84
14 changed files with 530 additions and 501 deletions

View File

@ -1,7 +1,7 @@
// Example on how to customise the look at feel using Styles // Example on how to customise the look at feel using Styles
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { Document, HeadingLevel, Packer, Paragraph, TextRun, UnderlineType } from "../build"; import { AlignmentType, Document, HeadingLevel, Packer, Paragraph, TextRun, UnderlineType } from "../build";
const doc = new Document({ const doc = new Document({
creator: "Clippy", creator: "Clippy",
@ -83,15 +83,23 @@ const doc = new Document({
}, },
], ],
}, },
numbering: {
config: [
{
reference: "my-crazy-numbering",
levels: [
{
level: 0,
format: "lowerLetter",
text: "%1)",
alignment: AlignmentType.LEFT,
},
],
},
],
},
}); });
const numberedAbstract = doc.Numbering.createAbstractNumbering();
numberedAbstract.createLevel(0, "lowerLetter", "%1)", "left");
const letterNumbering = doc.Numbering.createConcreteNumbering(numberedAbstract);
const letterNumbering5 = doc.Numbering.createConcreteNumbering(numberedAbstract);
letterNumbering5.overrideLevel(0, 5);
doc.addSection({ doc.addSection({
children: [ children: [
new Paragraph({ new Paragraph({
@ -106,21 +114,21 @@ doc.addSection({
new Paragraph({ new Paragraph({
text: "Option1", text: "Option1",
numbering: { numbering: {
num: letterNumbering, reference: "my-crazy-numbering",
level: 0, level: 0,
}, },
}), }),
new Paragraph({ new Paragraph({
text: "Option5 -- override 2 to 5", text: "Option5 -- override 2 to 5",
numbering: { numbering: {
num: letterNumbering, reference: "my-crazy-numbering",
level: 0, level: 0,
}, },
}), }),
new Paragraph({ new Paragraph({
text: "Option3", text: "Option3",
numbering: { numbering: {
num: letterNumbering, reference: "my-crazy-numbering",
level: 0, level: 0,
}, },
}), }),

View File

@ -1,23 +1,37 @@
// Numbered lists // Numbered lists
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { Document, Numbering, Packer, Paragraph } from "../build"; import { AlignmentType, Document, Packer, Paragraph } from "../build";
const doc = new Document(); const doc = new Document({
numbering: {
const numbering = new Numbering(); config: [
{
const abstractNum = numbering.createAbstractNumbering(); levels: [
abstractNum.createLevel(0, "upperRoman", "%1", "start").indent({ left: 720, hanging: 260 }); {
level: 0,
const concrete = numbering.createConcreteNumbering(abstractNum); format: "upperRoman",
text: "%1",
alignment: AlignmentType.START,
style: {
paragraph: {
indent: { left: 720, hanging: 260 },
},
},
},
],
reference: "my-crazy-reference",
},
],
},
});
doc.addSection({ doc.addSection({
children: [ children: [
new Paragraph({ new Paragraph({
text: "line with contextual spacing", text: "line with contextual spacing",
numbering: { numbering: {
num: concrete, reference: "my-crazy-reference",
level: 0, level: 0,
}, },
contextualSpacing: true, contextualSpacing: true,
@ -28,7 +42,7 @@ doc.addSection({
new Paragraph({ new Paragraph({
text: "line with contextual spacing", text: "line with contextual spacing",
numbering: { numbering: {
num: concrete, reference: "my-crazy-reference",
level: 0, level: 0,
}, },
contextualSpacing: true, contextualSpacing: true,
@ -39,7 +53,7 @@ doc.addSection({
new Paragraph({ new Paragraph({
text: "line without contextual spacing", text: "line without contextual spacing",
numbering: { numbering: {
num: concrete, reference: "my-crazy-reference",
level: 0, level: 0,
}, },
contextualSpacing: false, contextualSpacing: false,
@ -50,7 +64,7 @@ doc.addSection({
new Paragraph({ new Paragraph({
text: "line without contextual spacing", text: "line without contextual spacing",
numbering: { numbering: {
num: concrete, reference: "my-crazy-reference",
level: 0, level: 0,
}, },
contextualSpacing: false, contextualSpacing: false,

View File

@ -1,46 +1,80 @@
// Numbering and bullet points example // Numbering and bullet points example
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { Document, Numbering, Packer, Paragraph } from "../build"; import { AlignmentType, Document, Packer, Paragraph } from "../build";
const doc = new Document(); const doc = new Document({
numbering: {
const numbering = new Numbering(); config: [
{
const abstractNum = numbering.createAbstractNumbering(); reference: "my-crazy-numbering",
abstractNum.createLevel(0, "upperRoman", "%1", "start").indent({ left: 720, hanging: 260 }); levels: [
abstractNum.createLevel(1, "decimal", "%2.", "start").indent({ left: 1440, hanging: 980 }); {
abstractNum.createLevel(2, "lowerLetter", "%3)", "start").indent({ left: 2160, hanging: 1700 }); level: 0,
format: "upperRoman",
const concrete = numbering.createConcreteNumbering(abstractNum); text: "%1",
alignment: AlignmentType.START,
style: {
paragraph: {
indent: { left: 720, hanging: 260 },
},
},
},
{
level: 1,
format: "decimal",
text: "%2.",
alignment: AlignmentType.START,
style: {
paragraph: {
indent: { left: 1440, hanging: 980 },
},
},
},
{
level: 2,
format: "lowerLetter",
text: "%3)",
alignment: AlignmentType.START,
style: {
paragraph: {
indent: { left: 2160, hanging: 1700 },
},
},
},
],
},
],
},
});
doc.addSection({ doc.addSection({
children: [ children: [
new Paragraph({ new Paragraph({
text: "Hey you", text: "Hey you",
numbering: { numbering: {
num: concrete, reference: "my-crazy-numbering",
level: 0, level: 0,
}, },
}), }),
new Paragraph({ new Paragraph({
text: "What's up fam", text: "What's up fam",
numbering: { numbering: {
num: concrete, reference: "my-crazy-numbering",
level: 1, level: 1,
}, },
}), }),
new Paragraph({ new Paragraph({
text: "Hello World 2", text: "Hello World 2",
numbering: { numbering: {
num: concrete, reference: "my-crazy-numbering",
level: 1, level: 1,
}, },
}), }),
new Paragraph({ new Paragraph({
text: "Yeah boi", text: "Yeah boi",
numbering: { numbering: {
num: concrete, reference: "my-crazy-numbering",
level: 2, level: 2,
}, },
}), }),

View File

@ -4,6 +4,7 @@ import * as xml from "xml";
import { File } from "file"; import { File } from "file";
import { Formatter } from "../formatter"; import { Formatter } from "../formatter";
import { ImageReplacer } from "./image-replacer"; import { ImageReplacer } from "./image-replacer";
import { NumberingReplacer } from "./numbering-replacer";
interface IXmlifyedFile { interface IXmlifyedFile {
readonly data: string; readonly data: string;
@ -30,10 +31,12 @@ interface IXmlifyedFileMapping {
export class Compiler { export class Compiler {
private readonly formatter: Formatter; private readonly formatter: Formatter;
private readonly imageReplacer: ImageReplacer; private readonly imageReplacer: ImageReplacer;
private readonly numberingReplacer: NumberingReplacer;
constructor() { constructor() {
this.formatter = new Formatter(); this.formatter = new Formatter();
this.imageReplacer = new ImageReplacer(); this.imageReplacer = new ImageReplacer();
this.numberingReplacer = new NumberingReplacer();
} }
public compile(file: File, prettifyXml?: boolean): JSZip { public compile(file: File, prettifyXml?: boolean): JSZip {
@ -89,8 +92,9 @@ export class Compiler {
Document: { Document: {
data: (() => { data: (() => {
const xmlData = this.imageReplacer.replace(documentXmlData, documentMediaDatas, documentRelationshipCount); const xmlData = this.imageReplacer.replace(documentXmlData, documentMediaDatas, documentRelationshipCount);
const referenedXmlData = this.numberingReplacer.replace(xmlData, file.Numbering.ConcreteNumbering);
return xmlData; return referenedXmlData;
})(), })(),
path: "word/document.xml", path: "word/document.xml",
}, },

View File

@ -0,0 +1,17 @@
import { ConcreteNumbering } from "file";
export class NumberingReplacer {
public replace(xmlData: string, concreteNumberings: ConcreteNumbering[]): string {
let currentXmlData = xmlData;
for (const concreteNumbering of concreteNumberings) {
if (!concreteNumbering.reference) {
continue;
}
currentXmlData = currentXmlData.replace(new RegExp(`{${concreteNumbering.reference}}`, "g"), concreteNumbering.id.toString());
}
return currentXmlData;
}
}

View File

@ -1,7 +1,7 @@
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { DocumentAttributes } from "../document/document-attributes"; import { DocumentAttributes } from "../document/document-attributes";
import { IAbstractNumberingOptions } from "../numbering"; import { INumberingOptions } from "../numbering";
import { IStylesOptions } from "../styles"; import { IStylesOptions } from "../styles";
import { Created, Creator, Description, Keywords, LastModifiedBy, Modified, Revision, Subject, Title } from "./components"; import { Created, Creator, Description, Keywords, LastModifiedBy, Modified, Revision, Subject, Title } from "./components";
@ -15,7 +15,7 @@ export interface IPropertiesOptions {
readonly revision?: string; readonly revision?: string;
readonly externalStyles?: string; readonly externalStyles?: string;
readonly styles?: IStylesOptions; readonly styles?: IStylesOptions;
readonly numbering?: IAbstractNumberingOptions; readonly numbering?: INumberingOptions;
} }
export class CoreProperties extends XmlComponent { export class CoreProperties extends XmlComponent {

View File

@ -71,9 +71,13 @@ export class File {
sections: ISectionOptions[] = [], sections: ISectionOptions[] = [],
) { ) {
this.coreProperties = new CoreProperties(options); this.coreProperties = new CoreProperties(options);
this.numbering = new Numbering({ this.numbering = new Numbering(
levels: options.numbering ? options.numbering.levels : [], options.numbering
}); ? options.numbering
: {
config: [],
},
);
this.docRelationships = new Relationships(); this.docRelationships = new Relationships();
this.fileRelationships = new Relationships(); this.fileRelationships = new Relationships();
this.appProperties = new AppProperties(); this.appProperties = new AppProperties();

View File

@ -10,24 +10,20 @@ import { AbstractNumbering } from "./abstract-numbering";
describe("AbstractNumbering", () => { describe("AbstractNumbering", () => {
it("stores its ID at its .id property", () => { it("stores its ID at its .id property", () => {
const abstractNumbering = new AbstractNumbering(5, { const abstractNumbering = new AbstractNumbering(5, []);
levels: [],
});
expect(abstractNumbering.id).to.equal(5); expect(abstractNumbering.id).to.equal(5);
}); });
describe("#createLevel", () => { describe("#createLevel", () => {
it("creates a level with the given characteristics", () => { it("creates a level with the given characteristics", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 3,
level: 3, format: "lowerLetter",
format: "lowerLetter", text: "%1)",
text: "%1)", alignment: AlignmentType.END,
alignment: AlignmentType.END, },
}, ]);
],
});
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ _attr: { "w:ilvl": 3, "w15:tentative": 1 } }); expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ _attr: { "w:ilvl": 3, "w15:tentative": 1 } });
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:start": { _attr: { "w:val": 1 } } }); expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:start": { _attr: { "w:val": 1 } } });
@ -37,15 +33,13 @@ describe("AbstractNumbering", () => {
}); });
it("uses 'start' as the default alignment", () => { it("uses 'start' as the default alignment", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 3,
level: 3, format: "lowerLetter",
format: "lowerLetter", text: "%1)",
text: "%1)", },
}, ]);
],
});
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ _attr: { "w:ilvl": 3, "w15:tentative": 1 } }); expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ _attr: { "w:ilvl": 3, "w15:tentative": 1 } });
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:start": { _attr: { "w:val": 1 } } }); expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:start": { _attr: { "w:val": 1 } } });
@ -56,20 +50,18 @@ describe("AbstractNumbering", () => {
describe("formatting methods: paragraph properties", () => { describe("formatting methods: paragraph properties", () => {
it("#indent", () => { it("#indent", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { paragraph: {
paragraph: { indent: { left: 720 },
indent: { left: 720 },
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:pPr": [{ "w:ind": { _attr: { "w:left": 720 } } }], "w:pPr": [{ "w:ind": { _attr: { "w:left": 720 } } }],
@ -77,20 +69,18 @@ describe("AbstractNumbering", () => {
}); });
it("#spacing", () => { it("#spacing", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { paragraph: {
paragraph: { spacing: { before: 50, after: 150 },
spacing: { before: 50, after: 150 },
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:pPr": [{ "w:spacing": { _attr: { "w:before": 50, "w:after": 150 } } }], "w:pPr": [{ "w:spacing": { _attr: { "w:before": 50, "w:after": 150 } } }],
@ -98,20 +88,18 @@ describe("AbstractNumbering", () => {
}); });
it("#center", () => { it("#center", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { paragraph: {
paragraph: { alignment: AlignmentType.CENTER,
alignment: AlignmentType.CENTER,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:pPr": [{ "w:jc": { _attr: { "w:val": "center" } } }], "w:pPr": [{ "w:jc": { _attr: { "w:val": "center" } } }],
@ -119,20 +107,18 @@ describe("AbstractNumbering", () => {
}); });
it("#left", () => { it("#left", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { paragraph: {
paragraph: { alignment: AlignmentType.LEFT,
alignment: AlignmentType.LEFT,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:pPr": [{ "w:jc": { _attr: { "w:val": "left" } } }], "w:pPr": [{ "w:jc": { _attr: { "w:val": "left" } } }],
@ -140,20 +126,18 @@ describe("AbstractNumbering", () => {
}); });
it("#right", () => { it("#right", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { paragraph: {
paragraph: { alignment: AlignmentType.RIGHT,
alignment: AlignmentType.RIGHT,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:pPr": [{ "w:jc": { _attr: { "w:val": "right" } } }], "w:pPr": [{ "w:jc": { _attr: { "w:val": "right" } } }],
@ -161,20 +145,18 @@ describe("AbstractNumbering", () => {
}); });
it("#justified", () => { it("#justified", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { paragraph: {
paragraph: { alignment: AlignmentType.JUSTIFIED,
alignment: AlignmentType.JUSTIFIED,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:pPr": [{ "w:jc": { _attr: { "w:val": "both" } } }], "w:pPr": [{ "w:jc": { _attr: { "w:val": "both" } } }],
@ -182,20 +164,18 @@ describe("AbstractNumbering", () => {
}); });
it("#thematicBreak", () => { it("#thematicBreak", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { paragraph: {
paragraph: { thematicBreak: true,
thematicBreak: true,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:pPr": [ "w:pPr": [
@ -218,20 +198,18 @@ describe("AbstractNumbering", () => {
}); });
it("#leftTabStop", () => { it("#leftTabStop", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { paragraph: {
paragraph: { leftTabStop: 1200,
leftTabStop: 1200,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:pPr": [ "w:pPr": [
@ -243,20 +221,18 @@ describe("AbstractNumbering", () => {
}); });
it("#maxRightTabStop", () => { it("#maxRightTabStop", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { paragraph: {
paragraph: { rightTabStop: TabStopPosition.MAX,
rightTabStop: TabStopPosition.MAX,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:pPr": [ "w:pPr": [
@ -268,20 +244,18 @@ describe("AbstractNumbering", () => {
}); });
it("#keepLines", () => { it("#keepLines", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { paragraph: {
paragraph: { keepLines: true,
keepLines: true,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:pPr": [{ "w:keepLines": EMPTY_OBJECT }], "w:pPr": [{ "w:keepLines": EMPTY_OBJECT }],
@ -289,20 +263,18 @@ describe("AbstractNumbering", () => {
}); });
it("#keepNext", () => { it("#keepNext", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { paragraph: {
paragraph: { keepNext: true,
keepNext: true,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:pPr": [{ "w:keepNext": EMPTY_OBJECT }], "w:pPr": [{ "w:keepNext": EMPTY_OBJECT }],
@ -312,20 +284,18 @@ describe("AbstractNumbering", () => {
describe("formatting methods: run properties", () => { describe("formatting methods: run properties", () => {
it("#size", () => { it("#size", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { run: {
run: { size: 24,
size: 24,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:rPr": [{ "w:sz": { _attr: { "w:val": 24 } } }], "w:rPr": [{ "w:sz": { _attr: { "w:val": 24 } } }],
@ -333,20 +303,18 @@ describe("AbstractNumbering", () => {
}); });
it("#smallCaps", () => { it("#smallCaps", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { run: {
run: { smallCaps: true,
smallCaps: true,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:rPr": [{ "w:smallCaps": { _attr: { "w:val": true } } }], "w:rPr": [{ "w:smallCaps": { _attr: { "w:val": true } } }],
@ -354,20 +322,18 @@ describe("AbstractNumbering", () => {
}); });
it("#allCaps", () => { it("#allCaps", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { run: {
run: { allCaps: true,
allCaps: true,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:rPr": [{ "w:caps": { _attr: { "w:val": true } } }], "w:rPr": [{ "w:caps": { _attr: { "w:val": true } } }],
@ -375,20 +341,18 @@ describe("AbstractNumbering", () => {
}); });
it("#strike", () => { it("#strike", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { run: {
run: { strike: true,
strike: true,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
@ -397,20 +361,18 @@ describe("AbstractNumbering", () => {
}); });
it("#doubleStrike", () => { it("#doubleStrike", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { run: {
run: { doubleStrike: true,
doubleStrike: true,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:rPr": [{ "w:dstrike": { _attr: { "w:val": true } } }], "w:rPr": [{ "w:dstrike": { _attr: { "w:val": true } } }],
@ -418,20 +380,18 @@ describe("AbstractNumbering", () => {
}); });
it("#subScript", () => { it("#subScript", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { run: {
run: { subScript: true,
subScript: true,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:rPr": [{ "w:vertAlign": { _attr: { "w:val": "subscript" } } }], "w:rPr": [{ "w:vertAlign": { _attr: { "w:val": "subscript" } } }],
@ -439,20 +399,18 @@ describe("AbstractNumbering", () => {
}); });
it("#superScript", () => { it("#superScript", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { run: {
run: { superScript: true,
superScript: true,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:rPr": [{ "w:vertAlign": { _attr: { "w:val": "superscript" } } }], "w:rPr": [{ "w:vertAlign": { _attr: { "w:val": "superscript" } } }],
@ -460,20 +418,18 @@ describe("AbstractNumbering", () => {
}); });
it("#font", () => { it("#font", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { run: {
run: { font: "Times",
font: "Times",
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:rPr": [ "w:rPr": [
@ -483,20 +439,18 @@ describe("AbstractNumbering", () => {
}); });
it("#bold", () => { it("#bold", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { run: {
run: { bold: true,
bold: true,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:rPr": [{ "w:b": { _attr: { "w:val": true } } }], "w:rPr": [{ "w:b": { _attr: { "w:val": true } } }],
@ -504,20 +458,18 @@ describe("AbstractNumbering", () => {
}); });
it("#italics", () => { it("#italics", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { run: {
run: { italics: true,
italics: true,
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:rPr": [{ "w:i": { _attr: { "w:val": true } } }], "w:rPr": [{ "w:i": { _attr: { "w:val": true } } }],
@ -525,20 +477,18 @@ describe("AbstractNumbering", () => {
}); });
it("#highlight", () => { it("#highlight", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { run: {
run: { highlight: "005599",
highlight: "005599",
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:rPr": [{ "w:highlight": { _attr: { "w:val": "005599" } } }], "w:rPr": [{ "w:highlight": { _attr: { "w:val": "005599" } } }],
@ -546,24 +496,22 @@ describe("AbstractNumbering", () => {
}); });
it("#shadow", () => { it("#shadow", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { run: {
run: { shadow: {
shadow: { type: ShadingType.PERCENT_10,
type: ShadingType.PERCENT_10, fill: "00FFFF",
fill: "00FFFF", color: "FF0000",
color: "FF0000",
},
}, },
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:rPr": [{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } }], "w:rPr": [{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } }],
@ -572,20 +520,18 @@ describe("AbstractNumbering", () => {
describe("#underline", () => { describe("#underline", () => {
it("should set underline to 'single' if no arguments are given", () => { it("should set underline to 'single' if no arguments are given", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { run: {
run: { underline: {},
underline: {},
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:rPr": [{ "w:u": { _attr: { "w:val": "single" } } }], "w:rPr": [{ "w:u": { _attr: { "w:val": "single" } } }],
@ -593,22 +539,20 @@ describe("AbstractNumbering", () => {
}); });
it("should set the style if given", () => { it("should set the style if given", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { run: {
run: { underline: {
underline: { type: UnderlineType.DOUBLE,
type: UnderlineType.DOUBLE,
},
}, },
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:rPr": [{ "w:u": { _attr: { "w:val": "double" } } }], "w:rPr": [{ "w:u": { _attr: { "w:val": "double" } } }],
@ -616,23 +560,21 @@ describe("AbstractNumbering", () => {
}); });
it("should set the style and color if given", () => { it("should set the style and color if given", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { run: {
run: { underline: {
underline: { type: UnderlineType.DOUBLE,
type: UnderlineType.DOUBLE, color: "005599",
color: "005599",
},
}, },
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:rPr": [{ "w:u": { _attr: { "w:val": "double", "w:color": "005599" } } }], "w:rPr": [{ "w:u": { _attr: { "w:val": "double", "w:color": "005599" } } }],
@ -641,20 +583,18 @@ describe("AbstractNumbering", () => {
}); });
it("#color", () => { it("#color", () => {
const abstractNumbering = new AbstractNumbering(1, { const abstractNumbering = new AbstractNumbering(1, [
levels: [ {
{ level: 0,
level: 0, format: "lowerRoman",
format: "lowerRoman", text: "%0.",
text: "%0.", style: {
style: { run: {
run: { color: "123456",
color: "123456",
},
}, },
}, },
], },
}); ]);
const tree = new Formatter().format(abstractNumbering); const tree = new Formatter().format(abstractNumbering);
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
"w:rPr": [{ "w:color": { _attr: { "w:val": "123456" } } }], "w:rPr": [{ "w:color": { _attr: { "w:val": "123456" } } }],

View File

@ -15,14 +15,10 @@ class AbstractNumberingAttributes extends XmlAttributeComponent<IAbstractNumberi
}; };
} }
export interface IAbstractNumberingOptions {
readonly levels: ILevelsOptions[];
}
export class AbstractNumbering extends XmlComponent { export class AbstractNumbering extends XmlComponent {
public readonly id: number; public readonly id: number;
constructor(id: number, options: IAbstractNumberingOptions) { constructor(id: number, levelOptions: ILevelsOptions[]) {
super("w:abstractNum"); super("w:abstractNum");
this.root.push( this.root.push(
new AbstractNumberingAttributes({ new AbstractNumberingAttributes({
@ -33,7 +29,7 @@ export class AbstractNumbering extends XmlComponent {
this.root.push(new MultiLevelType("hybridMultilevel")); this.root.push(new MultiLevelType("hybridMultilevel"));
this.id = id; this.id = id;
for (const option of options.levels) { for (const option of levelOptions) {
this.root.push(new Level(option)); this.root.push(new Level(option));
} }
} }

View File

@ -23,7 +23,7 @@ class NumAttributes extends XmlAttributeComponent<INumAttributesProperties> {
export class ConcreteNumbering extends XmlComponent { export class ConcreteNumbering extends XmlComponent {
public readonly id: number; public readonly id: number;
constructor(numId: number, abstractNumId: number) { constructor(numId: number, abstractNumId: number, public readonly reference?: string) {
super("w:num"); super("w:num");
this.root.push( this.root.push(
new NumAttributes({ new NumAttributes({

View File

@ -8,7 +8,7 @@ describe("Numbering", () => {
describe("#constructor", () => { describe("#constructor", () => {
it("creates a default numbering with one abstract and one concrete instance", () => { it("creates a default numbering with one abstract and one concrete instance", () => {
const numbering = new Numbering({ const numbering = new Numbering({
levels: [], config: [],
}); });
const tree = new Formatter().format(numbering); const tree = new Formatter().format(numbering);

View File

@ -3,17 +3,25 @@ import { AlignmentType } from "file/paragraph";
import { IXmlableObject, XmlComponent } from "file/xml-components"; import { IXmlableObject, XmlComponent } from "file/xml-components";
import { DocumentAttributes } from "../document/document-attributes"; import { DocumentAttributes } from "../document/document-attributes";
import { AbstractNumbering, IAbstractNumberingOptions } from "./abstract-numbering"; import { AbstractNumbering } from "./abstract-numbering";
import { ILevelsOptions } from "./level";
import { ConcreteNumbering } from "./num"; import { ConcreteNumbering } from "./num";
export interface INumberingOptions {
readonly config: Array<{
readonly levels: ILevelsOptions[];
readonly reference: string;
}>;
}
export class Numbering extends XmlComponent { export class Numbering extends XmlComponent {
// tslint:disable-next-line:readonly-keyword // tslint:disable-next-line:readonly-keyword
private nextId: number; private nextId: number;
private readonly abstractNumbering: XmlComponent[] = []; private readonly abstractNumbering: AbstractNumbering[] = [];
private readonly concreteNumbering: XmlComponent[] = []; private readonly concreteNumbering: ConcreteNumbering[] = [];
constructor(options: IAbstractNumberingOptions) { constructor(options: INumberingOptions) {
super("w:numbering"); super("w:numbering");
this.root.push( this.root.push(
new DocumentAttributes({ new DocumentAttributes({
@ -39,114 +47,114 @@ export class Numbering extends XmlComponent {
this.nextId = 0; this.nextId = 0;
const abstractNumbering = this.createAbstractNumbering({ const abstractNumbering = this.createAbstractNumbering([
levels: [ {
{ level: 0,
level: 0, format: "bullet",
format: "bullet", text: "\u25CF",
text: "\u25CF", alignment: AlignmentType.LEFT,
alignment: AlignmentType.LEFT, style: {
style: { paragraph: {
paragraph: { indent: { left: 720, hanging: 360 },
indent: { left: 720, hanging: 360 },
},
}, },
}, },
{ },
level: 1, {
format: "bullet", level: 1,
text: "\u25CB", format: "bullet",
alignment: AlignmentType.LEFT, text: "\u25CB",
style: { alignment: AlignmentType.LEFT,
paragraph: { style: {
indent: { left: 1440, hanging: 360 }, paragraph: {
}, indent: { left: 1440, hanging: 360 },
}, },
}, },
{ },
level: 2, {
format: "bullet", level: 2,
text: "\u25A0", format: "bullet",
alignment: AlignmentType.LEFT, text: "\u25A0",
style: { alignment: AlignmentType.LEFT,
paragraph: { style: {
indent: { left: 2160, hanging: 360 }, paragraph: {
}, indent: { left: 2160, hanging: 360 },
}, },
}, },
{ },
level: 3, {
format: "bullet", level: 3,
text: "\u25CF", format: "bullet",
alignment: AlignmentType.LEFT, text: "\u25CF",
style: { alignment: AlignmentType.LEFT,
paragraph: { style: {
indent: { left: 2880, hanging: 360 }, paragraph: {
}, indent: { left: 2880, hanging: 360 },
}, },
}, },
{ },
level: 4, {
format: "bullet", level: 4,
text: "\u25CB", format: "bullet",
alignment: AlignmentType.LEFT, text: "\u25CB",
style: { alignment: AlignmentType.LEFT,
paragraph: { style: {
indent: { left: 3600, hanging: 360 }, paragraph: {
}, indent: { left: 3600, hanging: 360 },
}, },
}, },
{ },
level: 5, {
format: "bullet", level: 5,
text: "\u25A0", format: "bullet",
alignment: AlignmentType.LEFT, text: "\u25A0",
style: { alignment: AlignmentType.LEFT,
paragraph: { style: {
indent: { left: 4320, hanging: 360 }, paragraph: {
}, indent: { left: 4320, hanging: 360 },
}, },
}, },
{ },
level: 6, {
format: "bullet", level: 6,
text: "\u25CF", format: "bullet",
alignment: AlignmentType.LEFT, text: "\u25CF",
style: { alignment: AlignmentType.LEFT,
paragraph: { style: {
indent: { left: 5040, hanging: 360 }, paragraph: {
}, indent: { left: 5040, hanging: 360 },
}, },
}, },
{ },
level: 7, {
format: "bullet", level: 7,
text: "\u25CF", format: "bullet",
alignment: AlignmentType.LEFT, text: "\u25CF",
style: { alignment: AlignmentType.LEFT,
paragraph: { style: {
indent: { left: 5760, hanging: 360 }, paragraph: {
}, indent: { left: 5760, hanging: 360 },
}, },
}, },
{ },
level: 8, {
format: "bullet", level: 8,
text: "\u25CF", format: "bullet",
alignment: AlignmentType.LEFT, text: "\u25CF",
style: { alignment: AlignmentType.LEFT,
paragraph: { style: {
indent: { left: 6480, hanging: 360 }, paragraph: {
}, indent: { left: 6480, hanging: 360 },
}, },
}, },
], },
}); ]);
this.createConcreteNumbering(abstractNumbering); this.createConcreteNumbering(abstractNumbering);
const currentAbstractNumbering = this.createAbstractNumbering(options); for (const con of options.config) {
this.createConcreteNumbering(currentAbstractNumbering); const currentAbstractNumbering = this.createAbstractNumbering(con.levels);
this.createConcreteNumbering(currentAbstractNumbering, con.reference);
}
} }
public prepForXml(): IXmlableObject | undefined { public prepForXml(): IXmlableObject | undefined {
@ -155,15 +163,19 @@ export class Numbering extends XmlComponent {
return super.prepForXml(); return super.prepForXml();
} }
private createConcreteNumbering(abstractNumbering: AbstractNumbering): ConcreteNumbering { private createConcreteNumbering(abstractNumbering: AbstractNumbering, reference?: string): ConcreteNumbering {
const num = new ConcreteNumbering(this.nextId++, abstractNumbering.id); const num = new ConcreteNumbering(this.nextId++, abstractNumbering.id, reference);
this.concreteNumbering.push(num); this.concreteNumbering.push(num);
return num; return num;
} }
private createAbstractNumbering(options: IAbstractNumberingOptions): AbstractNumbering { private createAbstractNumbering(options: ILevelsOptions[]): AbstractNumbering {
const num = new AbstractNumbering(this.nextId++, options); const num = new AbstractNumbering(this.nextId++, options);
this.abstractNumbering.push(num); this.abstractNumbering.push(num);
return num; return num;
} }
public get ConcreteNumbering(): ConcreteNumbering[] {
return this.concreteNumbering;
}
} }

View File

@ -24,7 +24,7 @@ class NumberId extends XmlComponent {
super("w:numId"); super("w:numId");
this.root.push( this.root.push(
new Attributes({ new Attributes({
val: typeof id === "string" ? `__${id}__` : id, val: typeof id === "string" ? `{${id}}` : id,
}), }),
); );
} }

View File

@ -629,7 +629,7 @@ describe("Paragraph", () => {
"w:pPr": [ "w:pPr": [
{ "w:pStyle": { _attr: { "w:val": "ListParagraph" } } }, { "w:pStyle": { _attr: { "w:val": "ListParagraph" } } },
{ {
"w:numPr": [{ "w:ilvl": { _attr: { "w:val": 0 } } }, { "w:numId": { _attr: { "w:val": "__test id__" } } }], "w:numPr": [{ "w:ilvl": { _attr: { "w:val": 0 } } }, { "w:numId": { _attr: { "w:val": "{test id}" } } }],
}, },
], ],
}, },