diff --git a/demo/72-comment.ts b/demo/72-comment.ts index 787cfe0e63..0b2d1f4952 100644 --- a/demo/72-comment.ts +++ b/demo/72-comment.ts @@ -1,9 +1,10 @@ // Simple example to add text to a document // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, Packer, Paragraph, TextRun, CommentRangeStart, CommentRangeEnd, Comments, Comment, CommentReference } from "../build"; +import { Document, Packer, Paragraph, TextRun, CommentRangeStart, CommentRangeEnd, Comments, Comment, CommentReference, PrettityType } from "../build"; const doc = new Document({ + comments: new Comments([new Comment({ id: '0', author: 'Ray Chen', date: new Date().toISOString() }, 'comment text content')]), sections: [ { properties: {}, @@ -28,14 +29,8 @@ const doc = new Document({ ], }, ], -}, { - template: { - currentRelationshipId: 1, - // global comments data, every comment has a unique id - comments: new Comments([new Comment({ id: '0', author: 'Ray Chen', date: new Date().toISOString() }, 'comment text content')]), - } -} +}, ); -Packer.toBuffer(doc, ' ').then((buffer) => { +Packer.toBuffer(doc, PrettityType.WITH_2_BLANKS).then((buffer) => { fs.writeFileSync("document-comments.docx", buffer); }); diff --git a/src/export/packer/next-compiler.ts b/src/export/packer/next-compiler.ts index 88ec2cfa56..b571852b10 100644 --- a/src/export/packer/next-compiler.ts +++ b/src/export/packer/next-compiler.ts @@ -5,6 +5,7 @@ import { File } from "file"; import { Formatter } from "../formatter"; import { ImageReplacer } from "./image-replacer"; import { NumberingReplacer } from "./numbering-replacer"; +import { PrettityType } from "./packer"; interface IXmlifyedFile { readonly data: string; @@ -42,7 +43,7 @@ export class Compiler { this.numberingReplacer = new NumberingReplacer(); } - public compile(file: File, prettifyXml?: boolean | string): JSZip { + public compile(file: File, prettifyXml?: boolean | PrettityType): JSZip { const zip = new JSZip(); const xmlifiedFileMapping = this.xmlifyFile(file, prettifyXml); const map = new Map(Object.entries(xmlifiedFileMapping)); @@ -65,7 +66,7 @@ export class Compiler { return zip; } - private xmlifyFile(file: File, prettify?: boolean | string): IXmlifyedFileMapping { + private xmlifyFile(file: File, prettify?: boolean | PrettityType): IXmlifyedFileMapping { const documentRelationshipCount = file.Document.Relationships.RelationshipCount + 1; const documentXmlData = xml( diff --git a/src/export/packer/packer.ts b/src/export/packer/packer.ts index d9be2315aa..87ee47666b 100644 --- a/src/export/packer/packer.ts +++ b/src/export/packer/packer.ts @@ -1,8 +1,18 @@ import { File } from "file"; import { Compiler } from "./next-compiler"; +/** + * Use blanks to prettify + */ +export enum PrettityType { + NONE = "", + WITH_2_BLANKS = " ", + WITH_4_BLANKS = " ", + WITH_TAB = "\t" +} + export class Packer { - public static async toBuffer(file: File, prettify?: boolean | string): Promise { + public static async toBuffer(file: File, prettify?: boolean | PrettityType): Promise { const zip = this.compiler.compile(file, prettify); const zipData = await zip.generateAsync({ type: "nodebuffer", @@ -13,7 +23,7 @@ export class Packer { return zipData; } - public static async toBase64String(file: File, prettify?: boolean): Promise { + public static async toBase64String(file: File, prettify?: boolean | PrettityType): Promise { const zip = this.compiler.compile(file, prettify); const zipData = await zip.generateAsync({ type: "base64", @@ -24,7 +34,7 @@ export class Packer { return zipData; } - public static async toBlob(file: File, prettify?: boolean): Promise { + public static async toBlob(file: File, prettify?: boolean | PrettityType): Promise { const zip = this.compiler.compile(file, prettify); const zipData = await zip.generateAsync({ type: "blob", diff --git a/src/file/core-properties/properties.ts b/src/file/core-properties/properties.ts index f323c94981..5c3dbd42d8 100644 --- a/src/file/core-properties/properties.ts +++ b/src/file/core-properties/properties.ts @@ -1,3 +1,4 @@ +import { Comments } from './../paragraph/run/comment-run'; import { StringContainer, XmlComponent } from "file/xml-components"; import { ICustomPropertyOptions } from "../custom-properties"; import { IDocumentBackgroundOptions } from "../document"; @@ -21,6 +22,7 @@ export interface IPropertiesOptions { readonly externalStyles?: string; readonly styles?: IStylesOptions; readonly numbering?: INumberingOptions; + readonly comments?: Comments; readonly footnotes?: { readonly [key: string]: { readonly children: Paragraph[]; diff --git a/src/file/file.ts b/src/file/file.ts index 658514d1d0..48dd71b844 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -71,6 +71,10 @@ export class File { }, ); + if (options.comments) { + this.comments = options.comments; + } + this.fileRelationships = new Relationships(); this.customProperties = new CustomProperties(options.customProperties ?? []); this.appProperties = new AppProperties(); @@ -136,9 +140,6 @@ export class File { this.footnotesWrapper.View.createFootNote(parseFloat(key), options.footnotes[key].children); } } - if (fileProperties.template && fileProperties.template.comments) { - this.comments = fileProperties.template.comments; - } } private addSection({ headers = {}, footers = {}, children, properties }: ISectionOptions): void { diff --git a/src/file/paragraph/run/comment-run.ts b/src/file/paragraph/run/comment-run.ts index 6f5176f521..f65bb08c5b 100644 --- a/src/file/paragraph/run/comment-run.ts +++ b/src/file/paragraph/run/comment-run.ts @@ -10,7 +10,7 @@ interface ICommentOptions { } class CommentAttributes extends XmlAttributeComponent { - protected readonly xmlKeys = { id: "w:id", initias: "w:initials", author: "w:author", date: "w:date" }; + protected readonly xmlKeys = { id: "w:id", initials: "w:initials", author: "w:author", date: "w:date" }; } const COMMENT_ATTRIBUTES_MAP = { diff --git a/src/import-dotx/import-dotx.ts b/src/import-dotx/import-dotx.ts index 774f27a534..3ef73208d8 100644 --- a/src/import-dotx/import-dotx.ts +++ b/src/import-dotx/import-dotx.ts @@ -1,4 +1,3 @@ -import { Comments } from "./../file/paragraph/run/comment-run"; import * as JSZip from "jszip"; import { Element as XMLElement, ElementCompact as XMLElementCompact, xml2js } from "xml-js"; @@ -38,12 +37,11 @@ interface IRelationshipFileInfo { // https://fileinfo.com/extension/dotx export interface IDocumentTemplate { readonly currentRelationshipId: number; - readonly headers?: IDocumentHeader[]; - readonly footers?: IDocumentFooter[]; - readonly styles?: string; - readonly titlePageIsDefined?: boolean; - readonly media?: Media; - readonly comments?: Comments; + readonly headers: IDocumentHeader[]; + readonly footers: IDocumentFooter[]; + readonly styles: string; + readonly titlePageIsDefined: boolean; + readonly media: Media; } export class ImportDotx {