Files
docx-js/src/file/numbering/numbering.ts

228 lines
8.2 KiB
TypeScript
Raw Normal View History

2019-11-01 01:57:01 +00:00
// http://officeopenxml.com/WPnumbering.php
2021-03-12 03:58:05 +00:00
// https://stackoverflow.com/questions/58622437/purpose-of-abstractnum-and-numberinginstance
import { AlignmentType } from "@file/paragraph";
import { IContext, IXmlableObject, XmlComponent } from "@file/xml-components";
import { abstractNumUniqueNumericIdGen, concreteNumUniqueNumericIdGen, convertInchesToTwip } from "@util/convenience-functions";
2019-11-01 01:57:01 +00:00
2019-11-08 03:11:19 +00:00
import { AbstractNumbering } from "./abstract-numbering";
import { ILevelsOptions, LevelFormat } from "./level";
2019-11-01 01:57:01 +00:00
import { ConcreteNumbering } from "./num";
import { DocumentAttributes } from "../document/document-attributes";
2017-09-20 13:37:39 +01:00
export type INumberingOptions = {
readonly config: readonly {
readonly levels: readonly ILevelsOptions[];
2019-11-08 03:11:19 +00:00
readonly reference: string;
2020-08-01 17:58:16 +01:00
}[];
};
2019-11-08 03:11:19 +00:00
// <xsd:element name="numbering" type="CT_Numbering"/>
//
// <xsd:complexType name="CT_Numbering">
// <xsd:sequence>
// <xsd:element name="numPicBullet" type="CT_NumPicBullet" minOccurs="0" maxOccurs="unbounded"/>
// <xsd:element name="abstractNum" type="CT_AbstractNum" minOccurs="0" maxOccurs="unbounded"/>
// <xsd:element name="num" type="CT_Num" minOccurs="0" maxOccurs="unbounded"/>
// <xsd:element name="numIdMacAtCleanup" type="CT_DecimalNumber" minOccurs="0"/>
// </xsd:sequence>
// </xsd:complexType>
2017-09-20 13:37:39 +01:00
export class Numbering extends XmlComponent {
2021-03-12 03:58:05 +00:00
private readonly abstractNumberingMap = new Map<string, AbstractNumbering>();
private readonly concreteNumberingMap = new Map<string, ConcreteNumbering>();
2023-05-01 20:37:39 +01:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private readonly referenceConfigMap = new Map<string, Record<string, any>>();
private readonly abstractNumUniqueNumericId = abstractNumUniqueNumericIdGen();
private readonly concreteNumUniqueNumericId = concreteNumUniqueNumericIdGen();
2022-08-31 07:52:27 +01:00
public constructor(options: INumberingOptions) {
2017-09-20 13:37:39 +01:00
super("w:numbering");
2018-01-23 01:33:12 +00:00
this.root.push(
new DocumentAttributes(
["wpc", "mc", "o", "r", "m", "v", "wp14", "wp", "w10", "w", "w14", "w15", "wpg", "wpi", "wne", "wps"],
"w14 w15 wp14",
),
2018-01-23 01:33:12 +00:00
);
2017-09-20 13:37:39 +01:00
const abstractNumbering = new AbstractNumbering(this.abstractNumUniqueNumericId(), [
2019-11-08 03:11:19 +00:00
{
level: 0,
format: LevelFormat.BULLET,
2019-11-08 03:11:19 +00:00
text: "\u25CF",
alignment: AlignmentType.LEFT,
style: {
paragraph: {
2020-12-24 03:37:43 +00:00
indent: { left: convertInchesToTwip(0.5), hanging: convertInchesToTwip(0.25) },
2019-11-01 01:57:01 +00:00
},
},
2019-11-08 03:11:19 +00:00
},
{
level: 1,
format: LevelFormat.BULLET,
2019-11-08 03:11:19 +00:00
text: "\u25CB",
alignment: AlignmentType.LEFT,
style: {
paragraph: {
2020-12-24 03:37:43 +00:00
indent: { left: convertInchesToTwip(1), hanging: convertInchesToTwip(0.25) },
2019-11-01 01:57:01 +00:00
},
},
2019-11-08 03:11:19 +00:00
},
{
level: 2,
format: LevelFormat.BULLET,
2019-11-08 03:11:19 +00:00
text: "\u25A0",
alignment: AlignmentType.LEFT,
style: {
paragraph: {
2020-12-24 03:37:43 +00:00
indent: { left: 2160, hanging: convertInchesToTwip(0.25) },
2019-11-01 01:57:01 +00:00
},
},
2019-11-08 03:11:19 +00:00
},
{
level: 3,
format: LevelFormat.BULLET,
2019-11-08 03:11:19 +00:00
text: "\u25CF",
alignment: AlignmentType.LEFT,
style: {
paragraph: {
2020-12-24 03:37:43 +00:00
indent: { left: 2880, hanging: convertInchesToTwip(0.25) },
2019-11-01 01:57:01 +00:00
},
},
2019-11-08 03:11:19 +00:00
},
{
level: 4,
format: LevelFormat.BULLET,
2019-11-08 03:11:19 +00:00
text: "\u25CB",
alignment: AlignmentType.LEFT,
style: {
paragraph: {
2020-12-24 03:37:43 +00:00
indent: { left: 3600, hanging: convertInchesToTwip(0.25) },
2019-11-01 01:57:01 +00:00
},
},
2019-11-08 03:11:19 +00:00
},
{
level: 5,
format: LevelFormat.BULLET,
2019-11-08 03:11:19 +00:00
text: "\u25A0",
alignment: AlignmentType.LEFT,
style: {
paragraph: {
2020-12-24 03:37:43 +00:00
indent: { left: 4320, hanging: convertInchesToTwip(0.25) },
2019-11-01 01:57:01 +00:00
},
},
2019-11-08 03:11:19 +00:00
},
{
level: 6,
format: LevelFormat.BULLET,
2019-11-08 03:11:19 +00:00
text: "\u25CF",
alignment: AlignmentType.LEFT,
style: {
paragraph: {
2020-12-24 03:37:43 +00:00
indent: { left: 5040, hanging: convertInchesToTwip(0.25) },
2019-11-01 01:57:01 +00:00
},
},
2019-11-08 03:11:19 +00:00
},
{
level: 7,
format: LevelFormat.BULLET,
2019-11-08 03:11:19 +00:00
text: "\u25CF",
alignment: AlignmentType.LEFT,
style: {
paragraph: {
2020-12-24 03:37:43 +00:00
indent: { left: 5760, hanging: convertInchesToTwip(0.25) },
2019-11-01 01:57:01 +00:00
},
},
2019-11-08 03:11:19 +00:00
},
{
level: 8,
format: LevelFormat.BULLET,
2019-11-08 03:11:19 +00:00
text: "\u25CF",
alignment: AlignmentType.LEFT,
style: {
paragraph: {
2020-12-24 03:37:43 +00:00
indent: { left: 6480, hanging: convertInchesToTwip(0.25) },
2019-11-01 01:57:01 +00:00
},
},
2019-11-08 03:11:19 +00:00
},
]);
2018-05-06 02:57:40 +01:00
2021-03-12 03:58:05 +00:00
this.concreteNumberingMap.set(
"default-bullet-numbering",
new ConcreteNumbering({
numId: 1,
2021-03-12 03:58:05 +00:00
abstractNumId: abstractNumbering.id,
reference: "default-bullet-numbering",
instance: 0,
2022-12-07 23:03:42 +02:00
overrideLevels: [
{
num: 0,
start: 1,
},
],
2021-03-12 03:58:05 +00:00
}),
);
this.abstractNumberingMap.set("default-bullet-numbering", abstractNumbering);
2019-11-01 01:57:01 +00:00
2019-11-08 03:11:19 +00:00
for (const con of options.config) {
this.abstractNumberingMap.set(con.reference, new AbstractNumbering(this.abstractNumUniqueNumericId(), con.levels));
this.referenceConfigMap.set(con.reference, con.levels);
2019-11-08 03:11:19 +00:00
}
2017-09-20 13:37:39 +01:00
}
2021-03-11 01:06:55 +00:00
public prepForXml(context: IContext): IXmlableObject | undefined {
2021-03-12 03:58:05 +00:00
for (const numbering of this.abstractNumberingMap.values()) {
this.root.push(numbering);
}
for (const numbering of this.concreteNumberingMap.values()) {
this.root.push(numbering);
}
2021-03-11 01:06:55 +00:00
return super.prepForXml(context);
2017-09-20 13:37:39 +01:00
}
2021-03-12 03:58:05 +00:00
public createConcreteNumberingInstance(reference: string, instance: number): void {
const abstractNumbering = this.abstractNumberingMap.get(reference);
if (!abstractNumbering) {
return;
}
2021-03-12 03:58:05 +00:00
const fullReference = `${reference}-${instance}`;
if (this.concreteNumberingMap.has(fullReference)) {
return;
}
const referenceConfigLevels = this.referenceConfigMap.get(reference);
const firstLevelStartNumber = referenceConfigLevels && referenceConfigLevels[0].start;
const concreteNumberingSettings = {
numId: this.concreteNumUniqueNumericId(),
abstractNumId: abstractNumbering.id,
reference,
instance,
overrideLevels: [
firstLevelStartNumber && Number.isInteger(firstLevelStartNumber)
? {
num: 0,
start: firstLevelStartNumber,
}
: {
num: 0,
start: 1,
},
],
};
this.concreteNumberingMap.set(fullReference, new ConcreteNumbering(concreteNumberingSettings));
}
2019-11-08 03:11:19 +00:00
public get ConcreteNumbering(): readonly ConcreteNumbering[] {
2021-03-12 03:58:05 +00:00
return Array.from(this.concreteNumberingMap.values());
2019-11-08 03:11:19 +00:00
}
2023-05-01 20:37:39 +01:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public get ReferenceConfig(): readonly Record<string, any>[] {
2021-07-30 11:59:49 +03:00
return Array.from(this.referenceConfigMap.values());
}
2017-09-20 13:37:39 +01:00
}