Merge pull request #2087 from lentyaevpk/fix/myoffice-numbering-render
fix: added unique numeric id creator to avoid numbering render errors
This commit is contained in:
@ -41,11 +41,11 @@ const createAnchor = (drawingOptions: IDrawingOptions): Anchor =>
|
||||
|
||||
describe("Anchor", () => {
|
||||
before(() => {
|
||||
stub(convenienceFunctions, "uniqueNumericId").callsFake(() => 0);
|
||||
stub(convenienceFunctions, "docPropertiesUniqueNumericId").callsFake(() => 0);
|
||||
});
|
||||
|
||||
after(() => {
|
||||
(convenienceFunctions.uniqueNumericId as SinonStub).restore();
|
||||
(convenienceFunctions.docPropertiesUniqueNumericId as SinonStub).restore();
|
||||
});
|
||||
|
||||
let anchor: Anchor;
|
||||
|
@ -2,7 +2,7 @@
|
||||
import { IContext, IXmlableObject, NextAttributeComponent, XmlComponent } from "@file/xml-components";
|
||||
import { ConcreteHyperlink } from "@file/paragraph";
|
||||
|
||||
import { uniqueNumericId } from "@util/convenience-functions";
|
||||
import { docPropertiesUniqueNumericId } from "@util/convenience-functions";
|
||||
|
||||
import { createHyperlinkClick } from "./doc-properties-children";
|
||||
|
||||
@ -32,7 +32,7 @@ export class DocProperties extends XmlComponent {
|
||||
new NextAttributeComponent({
|
||||
id: {
|
||||
key: "id",
|
||||
value: uniqueNumericId(),
|
||||
value: docPropertiesUniqueNumericId(),
|
||||
},
|
||||
name: {
|
||||
key: "name",
|
||||
|
@ -31,11 +31,11 @@ const createDrawing = (drawingOptions?: IDrawingOptions): Drawing =>
|
||||
|
||||
describe("Drawing", () => {
|
||||
before(() => {
|
||||
stub(convenienceFunctions, "uniqueNumericId").callsFake(() => 0);
|
||||
stub(convenienceFunctions, "docPropertiesUniqueNumericId").callsFake(() => 0);
|
||||
});
|
||||
|
||||
after(() => {
|
||||
(convenienceFunctions.uniqueNumericId as SinonStub).restore();
|
||||
(convenienceFunctions.docPropertiesUniqueNumericId as SinonStub).restore();
|
||||
});
|
||||
|
||||
let currentBreak: Drawing;
|
||||
|
@ -8,11 +8,13 @@ import { Numbering } from "./numbering";
|
||||
|
||||
describe("Numbering", () => {
|
||||
before(() => {
|
||||
stub(convenienceFunctions, "uniqueNumericId").callsFake(() => 0);
|
||||
stub(convenienceFunctions, "abstractNumUniqueNumericId").callsFake(() => 0);
|
||||
stub(convenienceFunctions, "concreteNumUniqueNumericId").callsFake(() => 0);
|
||||
});
|
||||
|
||||
after(() => {
|
||||
(convenienceFunctions.uniqueNumericId as SinonStub).restore();
|
||||
(convenienceFunctions.abstractNumUniqueNumericId as SinonStub).restore();
|
||||
(convenienceFunctions.concreteNumUniqueNumericId as SinonStub).restore();
|
||||
});
|
||||
|
||||
describe("#constructor", () => {
|
||||
|
@ -2,7 +2,7 @@
|
||||
// https://stackoverflow.com/questions/58622437/purpose-of-abstractnum-and-numberinginstance
|
||||
import { AlignmentType } from "@file/paragraph";
|
||||
import { IContext, IXmlableObject, XmlComponent } from "@file/xml-components";
|
||||
import { convertInchesToTwip, uniqueNumericId } from "@util/convenience-functions";
|
||||
import { abstractNumUniqueNumericId, concreteNumUniqueNumericId, convertInchesToTwip } from "@util/convenience-functions";
|
||||
|
||||
import { DocumentAttributes } from "../document/document-attributes";
|
||||
import { AbstractNumbering } from "./abstract-numbering";
|
||||
@ -55,7 +55,7 @@ export class Numbering extends XmlComponent {
|
||||
}),
|
||||
);
|
||||
|
||||
const abstractNumbering = new AbstractNumbering(uniqueNumericId(), [
|
||||
const abstractNumbering = new AbstractNumbering(abstractNumUniqueNumericId(), [
|
||||
{
|
||||
level: 0,
|
||||
format: LevelFormat.BULLET,
|
||||
@ -176,7 +176,7 @@ export class Numbering extends XmlComponent {
|
||||
this.abstractNumberingMap.set("default-bullet-numbering", abstractNumbering);
|
||||
|
||||
for (const con of options.config) {
|
||||
this.abstractNumberingMap.set(con.reference, new AbstractNumbering(uniqueNumericId(), con.levels));
|
||||
this.abstractNumberingMap.set(con.reference, new AbstractNumbering(abstractNumUniqueNumericId(), con.levels));
|
||||
this.referenceConfigMap.set(con.reference, con.levels);
|
||||
}
|
||||
}
|
||||
@ -209,7 +209,7 @@ export class Numbering extends XmlComponent {
|
||||
const firstLevelStartNumber = referenceConfigLevels && referenceConfigLevels[0].start;
|
||||
|
||||
const concreteNumberingSettings = {
|
||||
numId: uniqueNumericId(),
|
||||
numId: concreteNumUniqueNumericId(),
|
||||
abstractNumId: abstractNumbering.id,
|
||||
reference,
|
||||
instance,
|
||||
|
@ -1,6 +1,6 @@
|
||||
// http://officeopenxml.com/WPbookmark.php
|
||||
import { XmlComponent } from "@file/xml-components";
|
||||
import { uniqueNumericId } from "@util/convenience-functions";
|
||||
import { bookmarkUniqueNumericId } from "@util/convenience-functions";
|
||||
|
||||
import { ParagraphChild } from "../paragraph";
|
||||
import { BookmarkEndAttributes, BookmarkStartAttributes } from "./bookmark-attributes";
|
||||
@ -11,7 +11,7 @@ export class Bookmark {
|
||||
public readonly end: BookmarkEnd;
|
||||
|
||||
public constructor(options: { readonly id: string; readonly children: readonly ParagraphChild[] }) {
|
||||
const linkId = uniqueNumericId();
|
||||
const linkId = bookmarkUniqueNumericId();
|
||||
|
||||
this.start = new BookmarkStart(options.id, linkId);
|
||||
this.children = options.children;
|
||||
|
@ -20,12 +20,12 @@ import { TextRun } from "./run";
|
||||
describe("Paragraph", () => {
|
||||
before(() => {
|
||||
stub(convenienceFunctions, "uniqueId").callsFake(() => "test-unique-id");
|
||||
stub(convenienceFunctions, "uniqueNumericId").callsFake(() => -101);
|
||||
stub(convenienceFunctions, "bookmarkUniqueNumericId").callsFake(() => -101);
|
||||
});
|
||||
|
||||
after(() => {
|
||||
(convenienceFunctions.uniqueId as SinonStub).restore();
|
||||
(convenienceFunctions.uniqueNumericId as SinonStub).restore();
|
||||
(convenienceFunctions.bookmarkUniqueNumericId as SinonStub).restore();
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
|
@ -11,12 +11,12 @@ import { ImageRun } from "./image-run";
|
||||
describe("ImageRun", () => {
|
||||
before(() => {
|
||||
stub(convenienceFunctions, "uniqueId").callsFake(() => "test-unique-id");
|
||||
stub(convenienceFunctions, "uniqueNumericId").callsFake(() => 0);
|
||||
stub(convenienceFunctions, "docPropertiesUniqueNumericId").callsFake(() => 0);
|
||||
});
|
||||
|
||||
after(() => {
|
||||
(convenienceFunctions.uniqueId as SinonStub).restore();
|
||||
(convenienceFunctions.uniqueNumericId as SinonStub).restore();
|
||||
(convenienceFunctions.docPropertiesUniqueNumericId as SinonStub).restore();
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { convertInchesToTwip, convertMillimetersToTwip, uniqueId, uniqueNumericId } from "./convenience-functions";
|
||||
import { convertInchesToTwip, convertMillimetersToTwip, uniqueId, uniqueNumericIdCreator } from "./convenience-functions";
|
||||
|
||||
describe("Utility", () => {
|
||||
describe("#convertMillimetersToTwip", () => {
|
||||
@ -17,8 +17,9 @@ describe("Utility", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("#uniqueNumericId", () => {
|
||||
describe("#uniqueNumericIdCreator", () => {
|
||||
it("should generate a unique incrementing ID", () => {
|
||||
const uniqueNumericId = uniqueNumericIdCreator();
|
||||
expect(uniqueNumericId()).to.not.be.undefined;
|
||||
});
|
||||
});
|
||||
|
@ -1,12 +1,19 @@
|
||||
import { nanoid } from "nanoid/non-secure";
|
||||
|
||||
let currentCount = 0;
|
||||
|
||||
// Twip - twentieths of a point
|
||||
export const convertMillimetersToTwip = (millimeters: number): number => Math.floor((millimeters / 25.4) * 72 * 20);
|
||||
|
||||
export const convertInchesToTwip = (inches: number): number => Math.floor(inches * 72 * 20);
|
||||
|
||||
export const uniqueNumericId = (): number => ++currentCount;
|
||||
export const uniqueNumericIdCreator = (initial = 0): (() => number) => {
|
||||
let currentCount = initial;
|
||||
|
||||
return () => ++currentCount;
|
||||
};
|
||||
|
||||
export const abstractNumUniqueNumericId = uniqueNumericIdCreator();
|
||||
export const concreteNumUniqueNumericId = uniqueNumericIdCreator(1); // Setting initial to 1 as we have numId = 1 for "default-bullet-numbering"
|
||||
export const docPropertiesUniqueNumericId = uniqueNumericIdCreator();
|
||||
export const bookmarkUniqueNumericId = uniqueNumericIdCreator();
|
||||
|
||||
export const uniqueId = (): string => nanoid().toLowerCase();
|
||||
|
Reference in New Issue
Block a user