Allow disabling numbering inherited from a paragraph style (#2531)

This commit is contained in:
Max Lay
2024-05-20 14:15:11 +12:00
committed by GitHub
parent e379a7fe04
commit f98f852a55
3 changed files with 96 additions and 6 deletions

View File

@ -130,6 +130,62 @@ new Paragraph({
}), }),
``` ```
## Disabling numbering inherited from paragraph style
If the numbering is set on a paragraph style, you may wish to disable it for a specific paragraph:
```ts
const doc = new Document({
...
numbering: {
config: [
{
reference: "my-bullet-points",
levels: [
{
level: 0,
format: LevelFormat.BULLET,
text: "\u1F60",
alignment: AlignmentType.LEFT,
style: {
paragraph: {
indent: { left: convertInchesToTwip(0.5), hanging: convertInchesToTwip(0.25) },
},
},
},
],
},
],
},
styles: {
paragraphStyles: [
{
id: 'bullet',
name: 'Bullet',
basedOn: 'Normal',
next: 'Normal',
run: {},
paragraph: {
numbering: {
reference: 'my-bullet-points',
level: 0,
},
},
},
],
},
...
});
```
```ts
new Paragraph({
text: "No bullet points!",
style: "Bullet",
numbering: false,
}),
```
## Full Example ## Full Example
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/3-numbering-and-bullet-points.ts ":include") [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/3-numbering-and-bullet-points.ts ":include")

View File

@ -65,6 +65,36 @@ describe("ParagraphProperties", () => {
}); });
}); });
it("should create with numbering disabled", () => {
const properties = new ParagraphProperties({
numbering: false,
});
const tree = new Formatter().format(properties);
expect(tree).to.deep.equal({
"w:pPr": [
{
"w:numPr": [
{
"w:ilvl": {
_attr: {
"w:val": 0,
},
},
},
{
"w:numId": {
_attr: {
"w:val": 0,
},
},
},
],
},
],
});
});
it("should create with widowControl", () => { it("should create with widowControl", () => {
const properties = new ParagraphProperties({ const properties = new ParagraphProperties({
widowControl: true, widowControl: true,

View File

@ -37,12 +37,14 @@ export interface ILevelParagraphStylePropertiesOptions {
} }
export interface IParagraphStylePropertiesOptions extends ILevelParagraphStylePropertiesOptions { export interface IParagraphStylePropertiesOptions extends ILevelParagraphStylePropertiesOptions {
readonly numbering?: { readonly numbering?:
readonly reference: string; | {
readonly level: number; readonly reference: string;
readonly instance?: number; readonly level: number;
readonly custom?: boolean; readonly instance?: number;
}; readonly custom?: boolean;
}
| false;
} }
export interface IParagraphPropertiesOptions extends IParagraphStylePropertiesOptions { export interface IParagraphPropertiesOptions extends IParagraphStylePropertiesOptions {
@ -135,6 +137,8 @@ export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
}); });
this.push(new NumberProperties(`${options.numbering.reference}-${options.numbering.instance ?? 0}`, options.numbering.level)); this.push(new NumberProperties(`${options.numbering.reference}-${options.numbering.instance ?? 0}`, options.numbering.level));
} else if (options.numbering === false) {
this.push(new NumberProperties(0, 0));
} }
if (options.border) { if (options.border) {