updated clone deep dependency and make fields dirty to be updated when word is opened

This commit is contained in:
Sergio Mendonça
2018-09-21 10:26:28 -03:00
parent 17d696e33a
commit 8b463b3bb6
9 changed files with 56 additions and 30 deletions

View File

@ -29,8 +29,8 @@ export class TabStopItem extends XmlComponent {
}
export class MaxRightTabStop extends TabStop {
constructor() {
super(new TabStopItem("right", 9026));
constructor(leader?: LeaderType) {
super(new TabStopItem("right", 9026, leader));
}
}

View File

@ -398,18 +398,38 @@ describe("Paragraph", () => {
it("changes in a cloned paragraph must not affect the original paragraph", () => {
paragraph.pageBreakBefore();
const clonedParagraph = paragraph.clone() as file.Paragraph;
clonedParagraph.clearPageBreaks();
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [{ "w:pPr": [{ "w:pageBreakBefore": [] }] }],
});
expect(tree).to.deep.equal({ "w:p": [{ "w:pPr": [{ "w:pageBreakBefore": [] }] }] }, "Paragraph with a page break before");
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);
expect(clonedTree).to.deep.equal({
"w:p": [{ "w:pPr": [] }],
});
expect(clonedTree).to.deep.equal(
{
"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.",
);
});
});
});

View File

@ -1,4 +1,6 @@
// http://officeopenxml.com/WPparagraph.php
import * as cloneDeep from "lodash.clonedeep";
import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run";
import { Image } from "file/media";
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 {
return this.properties.paragraphBorder;
}
@ -155,8 +161,8 @@ export class Paragraph extends XmlComponent {
return this;
}
public maxRightTabStop(): Paragraph {
this.properties.push(new MaxRightTabStop());
public maxRightTabStop(leader?: LeaderType): Paragraph {
this.properties.push(new MaxRightTabStop(leader));
return this;
}
@ -246,4 +252,8 @@ export class Paragraph extends XmlComponent {
this.properties.clearPageBreaks();
return this;
}
public clone(): Paragraph {
return cloneDeep(this, false);
}
}

View File

@ -1,26 +1,26 @@
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
class FidCharAttrs extends XmlAttributeComponent<{ type: "begin" | "end" | "separate" }> {
protected xmlKeys = { type: "w:fldCharType" };
class FidCharAttrs extends XmlAttributeComponent<{ type: "begin" | "end" | "separate"; dirty?: boolean }> {
protected xmlKeys = { type: "w:fldCharType", dirty: "w:dirty" };
}
export class Begin extends XmlComponent {
constructor() {
constructor(dirty?: boolean) {
super("w:fldChar");
this.root.push(new FidCharAttrs({ type: "begin" }));
this.root.push(new FidCharAttrs({ type: "begin", dirty }));
}
}
export class Separate extends XmlComponent {
constructor() {
constructor(dirty?: boolean) {
super("w:fldChar");
this.root.push(new FidCharAttrs({ type: "separate" }));
this.root.push(new FidCharAttrs({ type: "separate", dirty }));
}
}
export class End extends XmlComponent {
constructor() {
constructor(dirty?: boolean) {
super("w:fldChar");
this.root.push(new FidCharAttrs({ type: "end" }));
this.root.push(new FidCharAttrs({ type: "end", dirty }));
}
}