added InsertedTextRun and DeletedTextRun for Revision Tracking

This commit is contained in:
Thomas Jansen
2020-09-24 10:24:00 +02:00
parent 2adfe532dd
commit 09db2c528a
5 changed files with 158 additions and 0 deletions

View File

@ -13,3 +13,4 @@ export * from "./header-wrapper";
export * from "./footer-wrapper";
export * from "./header";
export * from "./footnotes";
export * from "./track-revision"

View File

@ -3,6 +3,7 @@ import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run"
import { IXmlableObject, XmlComponent } from "file/xml-components";
import { File } from "../file";
import { InsertedTextRun, DeletedTextRun } from "../track-revision";
import { PageBreak } from "./formatting/page-break";
import { Bookmark, HyperlinkRef } from "./links";
import { IParagraphPropertiesOptions, ParagraphProperties } from "./properties";
@ -19,6 +20,8 @@ export interface IParagraphOptions extends IParagraphPropertiesOptions {
| SequentialIdentifier
| FootnoteReferenceRun
| HyperlinkRef
| InsertedTextRun
| DeletedTextRun
)[];
}

View File

@ -0,0 +1 @@
export * from "./track-revision";

View File

@ -0,0 +1,81 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { InsertedTextRun, DeletedTextRun } from "./track-revision";
import { TextRun } from "../paragraph";
describe("InsertedTestRun", () => {
describe("#constructor", () => {
it("should create a inserted text run", () => {
const textRun = new TextRun({
text: "some text"
});
const insertedTextRun = new InsertedTextRun({ child: textRun, 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"
]
}
],
}
]
});
});
});
});
describe("DeletedTestRun", () => {
describe("#constructor", () => {
it("should create a deleted text run", () => {
const insertedParagraph = new DeletedTextRun({ text: 'some text', id: 0, date: "123", author: "Author" })
const tree = new Formatter().format(insertedParagraph);
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"
]
}
],
}
]
});
});
});
});

View File

@ -0,0 +1,72 @@
import { SpaceType } from "file/space-type";
import { XmlComponent, XmlAttributeComponent } from "file/xml-components";
import { TextRun } from "../index";
export interface ITrackRevisionAttributesProperties {
readonly id: number;
readonly author: string;
readonly date: string;
}
export class TrackRevisionAttributes extends XmlAttributeComponent<ITrackRevisionAttributesProperties> {
protected readonly xmlKeys = {
id: "w:id",
author: "w:author",
date: "w:date",
};
}
export interface IInsertedTextRunOptions extends ITrackRevisionAttributesProperties {
readonly child: TextRun
}
export interface IDeletedTextRunOptions extends ITrackRevisionAttributesProperties {
readonly text: string
}
export class InsertedTextRun extends XmlComponent {
constructor(options: IInsertedTextRunOptions) {
super("w:ins");
this.root.push(
new TrackRevisionAttributes({
id: options.id,
author: options.author,
date: options.date,
})
);
this.addChildElement(options.child);
}
}
export class DeletedTextRunWrapper extends XmlComponent {
constructor(text: string) {
super("w:r");
this.root.push(new DeletedText(text));
}
}
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);
}
}
export class DeletedTextRun extends XmlComponent {
constructor(options: IDeletedTextRunOptions) {
super("w:del");
this.root.push(
new TrackRevisionAttributes({
id: options.id,
author: options.author,
date: options.date,
})
);
this.addChildElement(new DeletedTextRunWrapper(options.text));
}
}