Skip to content

Commit

Permalink
Merge pull request #1823 from anti-the-social/overrideLevels-impl
Browse files Browse the repository at this point in the history
Override levels impl
  • Loading branch information
dolanmiu authored Dec 10, 2022
2 parents 62d0767 + 8509bcc commit c8914a1
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"dependencies": {
"@types/node": "^18.0.0",
"jszip": "^3.1.5",
"nanoid": "^3.1.20",
"nanoid": "^3.3.4",
"xml": "^1.0.1",
"xml-js": "^1.6.8"
},
Expand Down
195 changes: 185 additions & 10 deletions src/file/numbering/concrete-numbering.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ describe("ConcreteNumbering", () => {
abstractNumId: 1,
reference: "1",
instance: 0,
overrideLevel: {
num: 3,
},
overrideLevels: [
{
num: 3,
},
],
});

const tree = new Formatter().format(concreteNumbering);
Expand Down Expand Up @@ -44,16 +46,113 @@ describe("ConcreteNumbering", () => {
});
});

it("sets a new override level for two different level numbers", () => {
const concreteNumbering = new ConcreteNumbering({
numId: 0,
abstractNumId: 1,
reference: "1",
instance: 0,
overrideLevels: [{ num: 3 }, { num: 5 }],
});

const tree = new Formatter().format(concreteNumbering);

expect(tree).to.deep.equal({
"w:num": [
{
_attr: {
"w:numId": 0,
},
},
{
"w:abstractNumId": {
_attr: {
"w:val": 1,
},
},
},
{
"w:lvlOverride": {
_attr: {
"w:ilvl": 3,
},
},
},
{
"w:lvlOverride": {
_attr: {
"w:ilvl": 5,
},
},
},
],
});
});

it("sets the startOverride element if start is given", () => {
const concreteNumbering = new ConcreteNumbering({
numId: 0,
abstractNumId: 1,
reference: "1",
instance: 0,
overrideLevel: {
num: 1,
start: 9,
},
overrideLevels: [
{
num: 1,
start: 9,
},
],
});
const tree = new Formatter().format(concreteNumbering);
expect(tree).to.deep.equal({
"w:num": [
{
_attr: {
"w:numId": 0,
},
},
{
"w:abstractNumId": {
_attr: {
"w:val": 1,
},
},
},
{
"w:lvlOverride": [
{
_attr: {
"w:ilvl": 1,
},
},
{
"w:startOverride": {
_attr: {
"w:val": 9,
},
},
},
],
},
],
});
});

it("sets the startOverride element for several levels if start is given", () => {
const concreteNumbering = new ConcreteNumbering({
numId: 0,
abstractNumId: 1,
reference: "1",
instance: 0,
overrideLevels: [
{
num: 1,
start: 9,
},
{
num: 3,
start: 10,
},
],
});
const tree = new Formatter().format(concreteNumbering);
expect(tree).to.deep.equal({
Expand Down Expand Up @@ -86,6 +185,80 @@ describe("ConcreteNumbering", () => {
},
],
},
{
"w:lvlOverride": [
{
_attr: {
"w:ilvl": 3,
},
},
{
"w:startOverride": {
_attr: {
"w:val": 10,
},
},
},
],
},
],
});
});

it("Mix of overrideLevels with start and without", () => {
const concreteNumbering = new ConcreteNumbering({
numId: 0,
abstractNumId: 1,
reference: "1",
instance: 0,
overrideLevels: [
{
num: 1,
start: 9,
},
{
num: 3,
},
],
});
const tree = new Formatter().format(concreteNumbering);
expect(tree).to.deep.equal({
"w:num": [
{
_attr: {
"w:numId": 0,
},
},
{
"w:abstractNumId": {
_attr: {
"w:val": 1,
},
},
},
{
"w:lvlOverride": [
{
_attr: {
"w:ilvl": 1,
},
},
{
"w:startOverride": {
_attr: {
"w:val": 9,
},
},
},
],
},
{
"w:lvlOverride": {
_attr: {
"w:ilvl": 3,
},
},
},
],
});
});
Expand All @@ -96,9 +269,11 @@ describe("ConcreteNumbering", () => {
abstractNumId: 1,
reference: "1",
instance: 0,
overrideLevel: {
num: 1,
},
overrideLevels: [
{
num: 1,
},
],
});
const tree = new Formatter().format(concreteNumbering);
expect(tree).to.deep.equal({
Expand Down
16 changes: 10 additions & 6 deletions src/file/numbering/num.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ class NumAttributes extends XmlAttributeComponent<{
protected readonly xmlKeys = { numId: "w:numId" };
}

interface IOverrideLevel {
readonly num: number;
readonly start?: number;
}

export interface IConcreteNumberingOptions {
readonly numId: number;
readonly abstractNumId: number;
readonly reference: string;
readonly instance: number;
readonly overrideLevel?: {
readonly num: number;
readonly start?: number;
};
readonly overrideLevels?: readonly IOverrideLevel[];
}

// <xsd:complexType name="CT_Numbering">
Expand Down Expand Up @@ -60,8 +62,10 @@ export class ConcreteNumbering extends XmlComponent {

this.root.push(new AbstractNumId(decimalNumber(options.abstractNumId)));

if (options.overrideLevel) {
this.root.push(new LevelOverride(options.overrideLevel.num, options.overrideLevel.start));
if (options.overrideLevels && options.overrideLevels.length) {
for (const level of options.overrideLevels) {
this.root.push(new LevelOverride(level.num, level.start));
}
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/file/numbering/numbering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,12 @@ export class Numbering extends XmlComponent {
abstractNumId: abstractNumbering.id,
reference: "default-bullet-numbering",
instance: 0,
overrideLevel: {
num: 0,
start: 1,
},
overrideLevels: [
{
num: 0,
start: 1,
},
],
}),
);

Expand Down
8 changes: 4 additions & 4 deletions src/file/paragraph/run/comment-run.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("Comment", () => {
let clock: sinon.SinonFakeTimers;

beforeEach(() => {
const now = new Date(1999, 0, 1);
const now = new Date("1999-01-01T00:00:00.000Z");
clock = sinon.useFakeTimers(now.getTime());
});

Expand All @@ -57,7 +57,7 @@ describe("Comment", () => {
const component = new Comment({
id: 0,
text: "test-comment",
date: new Date(1999, 0, 1),
date: new Date("1999-01-01T00:00:00.000Z"),
});
const tree = new Formatter().format(component);
expect(tree).to.deep.equal({
Expand Down Expand Up @@ -126,12 +126,12 @@ describe("Comments", () => {
{
id: 0,
text: "test-comment",
date: new Date(1999, 0, 1),
date: new Date("1999-01-01T00:00:00.000Z"),
},
{
id: 1,
text: "test-comment-2",
date: new Date(1999, 0, 1),
date: new Date("1999-01-01T00:00:00.000Z"),
},
],
});
Expand Down

0 comments on commit c8914a1

Please sign in to comment.