Skip to content

Commit

Permalink
Implemented comments parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Volodymyr Baydalka committed Dec 22, 2023
1 parent 9b92722 commit 02a9892
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 19 deletions.
113 changes: 105 additions & 8 deletions dist/docx-preview.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/docx-preview.js.map

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions src/comments/comments-part.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Part } from "../common/part";
import { OpenXmlPackage } from "../common/open-xml-package";
import { DocumentParser } from "../document-parser";
import { WmlComment } from "./elements";
import { keyBy } from "../utils";

export class CommentsPart extends Part {
protected _documentParser: DocumentParser;

comments: WmlComment[]
commentMap: Record<string, WmlComment>;

constructor(pkg: OpenXmlPackage, path: string, parser: DocumentParser) {
super(pkg, path);
this._documentParser = parser;
}

parseXml(root: Element) {
this.comments = this._documentParser.parseComments(root);
this.commentMap = keyBy(this.comments, x => x.id);
}
}
32 changes: 32 additions & 0 deletions src/comments/elements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { DomType, OpenXmlElementBase } from "../document/dom";

export class WmlComment extends OpenXmlElementBase {
type = DomType.Comment;
id: string;
author: string;
initials: string;
date: string;
}

export class WmlCommentReference extends OpenXmlElementBase {
type = DomType.CommentReference;

constructor(public id?: string) {
super();
}
}

export class WmlCommentRangeStart extends OpenXmlElementBase {
type = DomType.CommentRangeStart;

constructor(public id?: string) {
super();
}
}
export class WmlCommentRangeEnd extends OpenXmlElementBase {
type = DomType.CommentRangeEnd;

constructor(public id?: string) {
super();
}
}
1 change: 1 addition & 0 deletions src/common/relationship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export enum RelationshipTypes {
ExtendedProperties = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties",
CoreProperties = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties",
CustomProperties = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/custom-properties",
Comments = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"
}

export function parseRelationships(root: Element, xml: XmlParser): Relationship[] {
Expand Down
29 changes: 29 additions & 0 deletions src/document-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { IDomStyle, IDomSubStyle } from './document/style';
import { WmlFieldChar, WmlFieldSimple, WmlInstructionText } from './document/fields';
import { convertLength, LengthUsage, LengthUsageType } from './document/common';
import { parseVmlElement } from './vml/vml';
import { WmlComment, WmlCommentRangeEnd, WmlCommentRangeStart, WmlCommentReference } from './comments/elements';

export var autos = {
shd: "inherit",
Expand Down Expand Up @@ -81,6 +82,22 @@ export class DocumentParser {
return result;
}

parseComments(xmlDoc: Element): any[] {
var result = [];

for (let el of xml.elements(xmlDoc, "comment")) {
const item = new WmlComment();
item.id = xml.attr(el, "id");
item.author = xml.attr(el, "author");
item.initials = xml.attr(el, "initials");
item.date = xml.attr(el, "date");
item.children = this.parseBodyElements(el);
result.push(item);
}

return result;
}

parseDocumentFile(xmlDoc: Element): DocumentElement {
var xbody = xml.element(xmlDoc, "body");
var background = xml.element(xmlDoc, "background");
Expand Down Expand Up @@ -494,6 +511,14 @@ export class DocumentParser {
result.children.push(parseBookmarkEnd(el, xml));
break;

case "commentRangeStart":
result.children.push(new WmlCommentRangeStart(xml.attr(el, "id")));
break;

case "commentRangeEnd":
result.children.push(new WmlCommentRangeEnd(xml.attr(el, "id")));
break;

case "oMath":
case "oMathPara":
result.children.push(this.parseMathElement(el));
Expand Down Expand Up @@ -596,6 +621,10 @@ export class DocumentParser {
});
break;

case "commentReference":
result.children.push(new WmlCommentReference(xml.attr(c, "id")));
break;

case "fldSimple":
result.children.push(<WmlFieldSimple>{
type: DomType.SimpleField,
Expand Down
6 changes: 5 additions & 1 deletion src/document/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ export enum DomType {
VmlElement = "vmlElement",
Inserted = "inserted",
Deleted = "deleted",
DeletedText = "deletedText"
DeletedText = "deletedText",
Comment = "comment",
CommentReference = "commentReference",
CommentRangeStart = "commentRangeStart",
CommentRangeEnd = "commentRangeEnd"
}

export interface OpenXmlElement {
Expand Down
Loading

0 comments on commit 02a9892

Please sign in to comment.