improved signature for deleted text runs, added demo 54 and added documentation for change tracking

This commit is contained in:
Thomas Jansen
2020-10-07 11:44:23 +02:00
parent 065c17de74
commit cae6405d9a
14 changed files with 796 additions and 138 deletions

View File

@ -0,0 +1,30 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { DeletedNumberOfPages, DeletedNumberOfPagesSection, DeletedPage } from "./deleted-page-number";
describe("Deleted Page", () => {
describe("#constructor()", () => {
it("uses the font name for both ascii and hAnsi", () => {
const tree = new Formatter().format(new DeletedPage());
expect(tree).to.deep.equal({ "w:delInstrText": [{ _attr: { "xml:space": "preserve" } }, "PAGE"] });
});
});
});
describe("Delted NumberOfPages", () => {
describe("#constructor()", () => {
it("uses the font name for both ascii and hAnsi", () => {
const tree = new Formatter().format(new DeletedNumberOfPages());
expect(tree).to.deep.equal({ "w:delInstrText": [{ _attr: { "xml:space": "preserve" } }, "NUMPAGES"] });
});
});
});
describe("Deleted NumberOfPagesSection", () => {
describe("#constructor()", () => {
it("uses the font name for both ascii and hAnsi", () => {
const tree = new Formatter().format(new DeletedNumberOfPagesSection());
expect(tree).to.deep.equal({ "w:delInstrText": [{ _attr: { "xml:space": "preserve" } }, "SECTIONPAGES"] });
});
});
});

View File

@ -0,0 +1,30 @@
import { SpaceType } from "file/space-type";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
class TextAttributes extends XmlAttributeComponent<{ readonly space: SpaceType }> {
protected readonly xmlKeys = { space: "xml:space" };
}
export class DeletedPage extends XmlComponent {
constructor() {
super("w:delInstrText");
this.root.push(new TextAttributes({ space: SpaceType.PRESERVE }));
this.root.push("PAGE");
}
}
export class DeletedNumberOfPages extends XmlComponent {
constructor() {
super("w:delInstrText");
this.root.push(new TextAttributes({ space: SpaceType.PRESERVE }));
this.root.push("NUMPAGES");
}
}
export class DeletedNumberOfPagesSection extends XmlComponent {
constructor() {
super("w:delInstrText");
this.root.push(new TextAttributes({ space: SpaceType.PRESERVE }));
this.root.push("SECTIONPAGES");
}
}

View File

@ -0,0 +1,371 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { DeletedTextRun } from "./deleted-text-run";
import { FootnoteReferenceRun, PageNumber } from "../../index";
describe("DeletedTextRun", () => {
describe("#constructor", () => {
it("should create a deleted text run", () => {
const deletedTextRun = new DeletedTextRun({ text: "some text", id: 0, date: "123", author: "Author" });
const tree = new Formatter().format(deletedTextRun);
expect(tree).to.deep.equal({
"w:del": [
{
_attr: {
"w:author": "Author",
"w:date": "123",
"w:id": 0,
},
},
{
"w:r": [
{
"w:delText": [
{
_attr: {
"xml:space": "preserve",
},
},
"some text",
],
},
],
},
],
});
});
});
describe("#constructor with formatting", () => {
it("should create a deleted text run", () => {
const deletedTextRun = new DeletedTextRun({ text: "some text", bold: true, id: 0, date: "123", author: "Author" });
const tree = new Formatter().format(deletedTextRun);
expect(tree).to.deep.equal({
"w:del": [
{
_attr: {
"w:author": "Author",
"w:date": "123",
"w:id": 0,
},
},
{
"w:r": [
{
"w:rPr": [
{
"w:b": {
_attr: {
"w:val": true,
},
},
},
{
"w:bCs": {
_attr: {
"w:val": true,
},
},
},
],
},
{
"w:delText": [
{
_attr: {
"xml:space": "preserve",
},
},
"some text",
],
},
],
},
],
});
});
});
describe("#break()", () => {
it("should add a break", () => {
const deletedTextRun = new DeletedTextRun({
children: ["some text"],
id: 0,
date: "123",
author: "Author",
}).break();
const tree = new Formatter().format(deletedTextRun);
expect(tree).to.deep.equal({
"w:del": [
{
_attr: {
"w:author": "Author",
"w:date": "123",
"w:id": 0,
},
},
{
"w:r": [
{
"w:br": {},
},
{
"w:delText": [
{
_attr: {
"xml:space": "preserve",
},
},
"some text",
],
},
],
},
],
});
});
});
describe("page numbering", () => {
it("should be able to delete the total pages", () => {
const deletedTextRun = new DeletedTextRun({
children: [" to ", PageNumber.TOTAL_PAGES],
id: 0,
date: "123",
author: "Author",
});
const tree = new Formatter().format(deletedTextRun);
expect(tree).to.deep.equal({
"w:del": [
{
_attr: {
"w:author": "Author",
"w:date": "123",
"w:id": 0,
},
},
{
"w:r": [
{
"w:delText": [
{
_attr: {
"xml:space": "preserve",
},
},
" to ",
],
},
{
"w:fldChar": {
_attr: {
"w:fldCharType": "begin",
},
},
},
{
"w:delInstrText": [
{
_attr: {
"xml:space": "preserve",
},
},
"NUMPAGES",
],
},
{
"w:fldChar": {
_attr: {
"w:fldCharType": "separate",
},
},
},
{
"w:fldChar": {
_attr: {
"w:fldCharType": "end",
},
},
},
],
},
],
});
});
it("should be able to delete the total pages in section", () => {
const deletedTextRun = new DeletedTextRun({
children: [" to ", PageNumber.TOTAL_PAGES_IN_SECTION],
id: 0,
date: "123",
author: "Author",
});
const tree = new Formatter().format(deletedTextRun);
expect(tree).to.deep.equal({
"w:del": [
{
_attr: {
"w:author": "Author",
"w:date": "123",
"w:id": 0,
},
},
{
"w:r": [
{
"w:delText": [
{
_attr: {
"xml:space": "preserve",
},
},
" to ",
],
},
{
"w:fldChar": {
_attr: {
"w:fldCharType": "begin",
},
},
},
{
"w:delInstrText": [
{
_attr: {
"xml:space": "preserve",
},
},
"SECTIONPAGES",
],
},
{
"w:fldChar": {
_attr: {
"w:fldCharType": "separate",
},
},
},
{
"w:fldChar": {
_attr: {
"w:fldCharType": "end",
},
},
},
],
},
],
});
});
it("should be able to delete the current page", () => {
const deletedTextRun = new DeletedTextRun({
children: [" to ", PageNumber.CURRENT],
id: 0,
date: "123",
author: "Author",
});
const tree = new Formatter().format(deletedTextRun);
expect(tree).to.deep.equal({
"w:del": [
{
_attr: {
"w:author": "Author",
"w:date": "123",
"w:id": 0,
},
},
{
"w:r": [
{
"w:delText": [
{
_attr: {
"xml:space": "preserve",
},
},
" to ",
],
},
{
"w:fldChar": {
_attr: {
"w:fldCharType": "begin",
},
},
},
{
"w:delInstrText": [
{
_attr: {
"xml:space": "preserve",
},
},
"PAGE",
],
},
{
"w:fldChar": {
_attr: {
"w:fldCharType": "separate",
},
},
},
{
"w:fldChar": {
_attr: {
"w:fldCharType": "end",
},
},
},
],
},
],
});
});
});
describe("footnote references", () => {
it("should add a valid footnote reference", () => {
const deletedTextRun = new DeletedTextRun({
children: ["some text", new FootnoteReferenceRun(1)],
id: 0,
date: "123",
author: "Author",
});
const tree = new Formatter().format(deletedTextRun);
expect(tree).to.deep.equal({
"w:del": [
{
_attr: {
"w:author": "Author",
"w:date": "123",
"w:id": 0,
},
},
{
"w:r": [
{
"w:delText": [
{
_attr: {
"xml:space": "preserve",
},
},
"some text",
],
},
{
"w:r": [
{ "w:rPr": [{ "w:rStyle": { _attr: { "w:val": "FootnoteReference" } } }] },
{ "w:footnoteReference": { _attr: { "w:id": 1 } } },
],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,83 @@
import { IChangedAttributesProperties, ChangeAttributes } from "../track-revision";
import { XmlComponent } from "file/xml-components";
import { IRunOptions, RunProperties, IRunPropertiesOptions, FootnoteReferenceRun } from "../../index";
import { Break } from "../../paragraph/run/break";
import { Begin, Separate, End } from "../../paragraph/run/field";
import { PageNumber } from "../../paragraph/run/run";
import { DeletedPage, DeletedNumberOfPages, DeletedNumberOfPagesSection } from "./deleted-page-number";
import { DeletedText } from "./deleted-text";
interface IDeletedRunOptions extends IRunPropertiesOptions, IChangedAttributesProperties {
readonly children?: (Begin | Separate | End | PageNumber | FootnoteReferenceRun | string)[];
readonly text?: string;
}
export class DeletedTextRun extends XmlComponent {
protected readonly deletedTextRunWrapper: DeletedTextRunWrapper;
constructor(options: IDeletedRunOptions) {
super("w:del");
this.root.push(
new ChangeAttributes({
id: options.id,
author: options.author,
date: options.date,
}),
);
this.deletedTextRunWrapper = new DeletedTextRunWrapper(options as IRunOptions);
this.addChildElement(this.deletedTextRunWrapper);
}
public break(): DeletedTextRun {
this.deletedTextRunWrapper.break();
return this;
}
}
class DeletedTextRunWrapper extends XmlComponent {
constructor(options: IRunOptions) {
super("w:r");
this.root.push(new RunProperties(options));
if (options.children) {
for (const child of options.children) {
if (typeof child === "string") {
switch (child) {
case PageNumber.CURRENT:
this.root.push(new Begin());
this.root.push(new DeletedPage());
this.root.push(new Separate());
this.root.push(new End());
break;
case PageNumber.TOTAL_PAGES:
this.root.push(new Begin());
this.root.push(new DeletedNumberOfPages());
this.root.push(new Separate());
this.root.push(new End());
break;
case PageNumber.TOTAL_PAGES_IN_SECTION:
this.root.push(new Begin());
this.root.push(new DeletedNumberOfPagesSection());
this.root.push(new Separate());
this.root.push(new End());
break;
default:
this.root.push(new DeletedText(child));
break;
}
continue;
}
this.root.push(child);
}
} else if (options.text) {
this.root.push(new DeletedText(options.text));
}
}
public break(): void {
this.root.splice(1, 0, new Break());
}
}

View File

@ -0,0 +1,15 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { DeletedText } from "./deleted-text";
describe("Deleted Text", () => {
describe("#constructor", () => {
it("adds the passed in text to the component", () => {
const t = new DeletedText(" this is\n text");
const f = new Formatter().format(t);
expect(f).to.deep.equal({
"w:delText": [{ _attr: { "xml:space": "preserve" } }, " this is\n text"],
});
});
});
});

View File

@ -0,0 +1,15 @@
import { SpaceType } from "file/space-type";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
class TextAttributes extends XmlAttributeComponent<{ readonly space: SpaceType }> {
protected readonly xmlKeys = { space: "xml:space" };
}
export class DeletedText extends XmlComponent {
constructor(text: string) {
super("w:delText");
this.root.push(new TextAttributes({ space: SpaceType.PRESERVE }));
this.root.push(text);
}
}

View File

@ -0,0 +1,37 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { InsertedTextRun } from "./inserted-text-run";
describe("InsertedTextRun", () => {
describe("#constructor", () => {
it("should create a inserted text run", () => {
const insertedTextRun = new InsertedTextRun({ text: "some text", id: 0, date: "123", author: "Author" });
const tree = new Formatter().format(insertedTextRun);
expect(tree).to.deep.equal({
"w:ins": [
{
_attr: {
"w:author": "Author",
"w:date": "123",
"w:id": 0,
},
},
{
"w:r": [
{
"w:t": [
{
_attr: {
"xml:space": "preserve",
},
},
"some text",
],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,19 @@
import { IChangedAttributesProperties, ChangeAttributes } from "../track-revision";
import { XmlComponent } from "file/xml-components";
import { TextRun, IRunOptions } from "../../index";
interface IInsertedRunOptions extends IChangedAttributesProperties, IRunOptions {}
export class InsertedTextRun extends XmlComponent {
constructor(options: IInsertedRunOptions) {
super("w:ins");
this.root.push(
new ChangeAttributes({
id: options.id,
author: options.author,
date: options.date,
}),
);
this.addChildElement(new TextRun(options as IRunOptions));
}
}