updated clone deep dependency and make fields dirty to be updated when word is opened
This commit is contained in:
@ -52,6 +52,7 @@
|
|||||||
"fast-xml-parser": "^3.3.6",
|
"fast-xml-parser": "^3.3.6",
|
||||||
"image-size": "^0.6.2",
|
"image-size": "^0.6.2",
|
||||||
"jszip": "^3.1.5",
|
"jszip": "^3.1.5",
|
||||||
|
"lodash.clonedeep": "^4.5.0",
|
||||||
"xml": "^1.0.1"
|
"xml": "^1.0.1"
|
||||||
},
|
},
|
||||||
"author": "Dolan Miu",
|
"author": "Dolan Miu",
|
||||||
|
@ -345,14 +345,14 @@ export class File {
|
|||||||
// deep clone the original paragraph
|
// deep clone the original paragraph
|
||||||
const generatedParagraph = paragraph.clone() as Paragraph;
|
const generatedParagraph = paragraph.clone() as Paragraph;
|
||||||
|
|
||||||
generatedParagraph.clearPageBreaks().rightTabStop(9016, "dot");
|
generatedParagraph.clearPageBreaks().maxRightTabStop("dot");
|
||||||
|
|
||||||
const tabRun = new Run();
|
const tabRun = new Run();
|
||||||
tabRun.addChildElement(new Tab());
|
tabRun.addChildElement(new Tab());
|
||||||
generatedParagraph.addChildElement(tabRun);
|
generatedParagraph.addChildElement(tabRun);
|
||||||
|
|
||||||
const beginRun = new Run();
|
const beginRun = new Run();
|
||||||
beginRun.addChildElement(new Begin());
|
beginRun.addChildElement(new Begin(true));
|
||||||
beginRun.addChildElement(new PageReferenceInstruction(bookmarkId));
|
beginRun.addChildElement(new PageReferenceInstruction(bookmarkId));
|
||||||
beginRun.addChildElement(new Separate());
|
beginRun.addChildElement(new Separate());
|
||||||
generatedParagraph.addRun(beginRun);
|
generatedParagraph.addRun(beginRun);
|
||||||
|
@ -29,8 +29,8 @@ export class TabStopItem extends XmlComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class MaxRightTabStop extends TabStop {
|
export class MaxRightTabStop extends TabStop {
|
||||||
constructor() {
|
constructor(leader?: LeaderType) {
|
||||||
super(new TabStopItem("right", 9026));
|
super(new TabStopItem("right", 9026, leader));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -398,18 +398,38 @@ describe("Paragraph", () => {
|
|||||||
it("changes in a cloned paragraph must not affect the original paragraph", () => {
|
it("changes in a cloned paragraph must not affect the original paragraph", () => {
|
||||||
paragraph.pageBreakBefore();
|
paragraph.pageBreakBefore();
|
||||||
|
|
||||||
const clonedParagraph = paragraph.clone() as file.Paragraph;
|
|
||||||
clonedParagraph.clearPageBreaks();
|
|
||||||
|
|
||||||
const tree = new Formatter().format(paragraph);
|
const tree = new Formatter().format(paragraph);
|
||||||
expect(tree).to.deep.equal({
|
expect(tree).to.deep.equal({ "w:p": [{ "w:pPr": [{ "w:pageBreakBefore": [] }] }] }, "Paragraph with a page break before");
|
||||||
"w:p": [{ "w:pPr": [{ "w:pageBreakBefore": [] }] }],
|
|
||||||
});
|
const clonedParagraph = paragraph.clone();
|
||||||
|
expect(clonedParagraph).to.be.instanceof(file.Paragraph);
|
||||||
|
expect(clonedParagraph.paragraphProperties).to.be.instanceof(file.ParagraphProperties);
|
||||||
|
|
||||||
const clonedTree = new Formatter().format(clonedParagraph);
|
const clonedTree = new Formatter().format(clonedParagraph);
|
||||||
expect(clonedTree).to.deep.equal({
|
expect(clonedTree).to.deep.equal(
|
||||||
"w:p": [{ "w:pPr": [] }],
|
{
|
||||||
});
|
"w:p": [{ "w:pPr": [{ "w:pageBreakBefore": [] }] }],
|
||||||
|
},
|
||||||
|
"Cloned Paragraph with page break before",
|
||||||
|
);
|
||||||
|
|
||||||
|
clonedParagraph.clearPageBreaks();
|
||||||
|
|
||||||
|
const clonedTreeAfter = new Formatter().format(clonedParagraph);
|
||||||
|
expect(clonedTreeAfter).to.deep.equal(
|
||||||
|
{
|
||||||
|
"w:p": [{ "w:pPr": [] }],
|
||||||
|
},
|
||||||
|
"Cloned Paragraph after clearPageBreaks must have no properties",
|
||||||
|
);
|
||||||
|
|
||||||
|
const treeAfter = new Formatter().format(paragraph);
|
||||||
|
expect(treeAfter).to.deep.equal(
|
||||||
|
{
|
||||||
|
"w:p": [{ "w:pPr": [{ "w:pageBreakBefore": [] }] }],
|
||||||
|
},
|
||||||
|
"Paragraph after clearPageBreaks in Cloned Paragraph must keep the properties.",
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
// http://officeopenxml.com/WPparagraph.php
|
// http://officeopenxml.com/WPparagraph.php
|
||||||
|
import * as cloneDeep from "lodash.clonedeep";
|
||||||
|
|
||||||
import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run";
|
import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run";
|
||||||
import { Image } from "file/media";
|
import { Image } from "file/media";
|
||||||
import { Num } from "file/numbering/num";
|
import { Num } from "file/numbering/num";
|
||||||
@ -30,6 +32,10 @@ export class Paragraph extends XmlComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public get paragraphProperties(): ParagraphProperties {
|
||||||
|
return this.properties;
|
||||||
|
}
|
||||||
|
|
||||||
public get Borders(): Border {
|
public get Borders(): Border {
|
||||||
return this.properties.paragraphBorder;
|
return this.properties.paragraphBorder;
|
||||||
}
|
}
|
||||||
@ -155,8 +161,8 @@ export class Paragraph extends XmlComponent {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public maxRightTabStop(): Paragraph {
|
public maxRightTabStop(leader?: LeaderType): Paragraph {
|
||||||
this.properties.push(new MaxRightTabStop());
|
this.properties.push(new MaxRightTabStop(leader));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,4 +252,8 @@ export class Paragraph extends XmlComponent {
|
|||||||
this.properties.clearPageBreaks();
|
this.properties.clearPageBreaks();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public clone(): Paragraph {
|
||||||
|
return cloneDeep(this, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,26 +1,26 @@
|
|||||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||||
|
|
||||||
class FidCharAttrs extends XmlAttributeComponent<{ type: "begin" | "end" | "separate" }> {
|
class FidCharAttrs extends XmlAttributeComponent<{ type: "begin" | "end" | "separate"; dirty?: boolean }> {
|
||||||
protected xmlKeys = { type: "w:fldCharType" };
|
protected xmlKeys = { type: "w:fldCharType", dirty: "w:dirty" };
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Begin extends XmlComponent {
|
export class Begin extends XmlComponent {
|
||||||
constructor() {
|
constructor(dirty?: boolean) {
|
||||||
super("w:fldChar");
|
super("w:fldChar");
|
||||||
this.root.push(new FidCharAttrs({ type: "begin" }));
|
this.root.push(new FidCharAttrs({ type: "begin", dirty }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Separate extends XmlComponent {
|
export class Separate extends XmlComponent {
|
||||||
constructor() {
|
constructor(dirty?: boolean) {
|
||||||
super("w:fldChar");
|
super("w:fldChar");
|
||||||
this.root.push(new FidCharAttrs({ type: "separate" }));
|
this.root.push(new FidCharAttrs({ type: "separate", dirty }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class End extends XmlComponent {
|
export class End extends XmlComponent {
|
||||||
constructor() {
|
constructor(dirty?: boolean) {
|
||||||
super("w:fldChar");
|
super("w:fldChar");
|
||||||
this.root.push(new FidCharAttrs({ type: "end" }));
|
this.root.push(new FidCharAttrs({ type: "end", dirty }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,7 @@ const DEFAULT_TOC = {
|
|||||||
{
|
{
|
||||||
_attr: {
|
_attr: {
|
||||||
"w:fldCharType": "begin",
|
"w:fldCharType": "begin",
|
||||||
|
"w:dirty": true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
@ -25,7 +25,7 @@ export class TableOfContents extends XmlComponent {
|
|||||||
// this.tocProperties = tocProperties || new TableOfContentsProperties();
|
// this.tocProperties = tocProperties || new TableOfContentsProperties();
|
||||||
const beginParagraph = new Paragraph();
|
const beginParagraph = new Paragraph();
|
||||||
const beginRun = new Run();
|
const beginRun = new Run();
|
||||||
beginRun.addChildElement(new Begin());
|
beginRun.addChildElement(new Begin(true));
|
||||||
beginRun.addChildElement(this.instruction);
|
beginRun.addChildElement(this.instruction);
|
||||||
beginRun.addChildElement(new Separate());
|
beginRun.addChildElement(new Separate());
|
||||||
beginParagraph.addRun(beginRun);
|
beginParagraph.addRun(beginRun);
|
||||||
|
@ -39,10 +39,4 @@ export abstract class XmlComponent extends BaseXmlComponent {
|
|||||||
public delete(): void {
|
public delete(): void {
|
||||||
this.deleted = true;
|
this.deleted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public clone(): XmlComponent {
|
|
||||||
const newXmlComponent = Object.assign(Object.create(Object.getPrototypeOf(this)), this);
|
|
||||||
newXmlComponent.root = newXmlComponent.root.map((child) => (child instanceof XmlComponent ? child.clone() : child));
|
|
||||||
return newXmlComponent as XmlComponent;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user