feat: transfer comments property to File CoreProperties like numbering.

This commit is contained in:
Chen Yuncai
2022-03-07 20:02:14 +08:00
parent b2a09b512f
commit b81086ab7a
7 changed files with 32 additions and 25 deletions

View File

@ -1,9 +1,10 @@
// Simple example to add text to a document // Simple example to add text to a document
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; 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({ const doc = new Document({
comments: new Comments([new Comment({ id: '0', author: 'Ray Chen', date: new Date().toISOString() }, 'comment text content')]),
sections: [ sections: [
{ {
properties: {}, 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); fs.writeFileSync("document-comments.docx", buffer);
}); });

View File

@ -5,6 +5,7 @@ import { File } from "file";
import { Formatter } from "../formatter"; import { Formatter } from "../formatter";
import { ImageReplacer } from "./image-replacer"; import { ImageReplacer } from "./image-replacer";
import { NumberingReplacer } from "./numbering-replacer"; import { NumberingReplacer } from "./numbering-replacer";
import { PrettityType } from "./packer";
interface IXmlifyedFile { interface IXmlifyedFile {
readonly data: string; readonly data: string;
@ -42,7 +43,7 @@ export class Compiler {
this.numberingReplacer = new NumberingReplacer(); 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 zip = new JSZip();
const xmlifiedFileMapping = this.xmlifyFile(file, prettifyXml); const xmlifiedFileMapping = this.xmlifyFile(file, prettifyXml);
const map = new Map<string, IXmlifyedFile | IXmlifyedFile[]>(Object.entries(xmlifiedFileMapping)); const map = new Map<string, IXmlifyedFile | IXmlifyedFile[]>(Object.entries(xmlifiedFileMapping));
@ -65,7 +66,7 @@ export class Compiler {
return zip; 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 documentRelationshipCount = file.Document.Relationships.RelationshipCount + 1;
const documentXmlData = xml( const documentXmlData = xml(

View File

@ -1,8 +1,18 @@
import { File } from "file"; import { File } from "file";
import { Compiler } from "./next-compiler"; 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 { export class Packer {
public static async toBuffer(file: File, prettify?: boolean | string): Promise<Buffer> { public static async toBuffer(file: File, prettify?: boolean | PrettityType): Promise<Buffer> {
const zip = this.compiler.compile(file, prettify); const zip = this.compiler.compile(file, prettify);
const zipData = await zip.generateAsync({ const zipData = await zip.generateAsync({
type: "nodebuffer", type: "nodebuffer",
@ -13,7 +23,7 @@ export class Packer {
return zipData; return zipData;
} }
public static async toBase64String(file: File, prettify?: boolean): Promise<string> { public static async toBase64String(file: File, prettify?: boolean | PrettityType): Promise<string> {
const zip = this.compiler.compile(file, prettify); const zip = this.compiler.compile(file, prettify);
const zipData = await zip.generateAsync({ const zipData = await zip.generateAsync({
type: "base64", type: "base64",
@ -24,7 +34,7 @@ export class Packer {
return zipData; return zipData;
} }
public static async toBlob(file: File, prettify?: boolean): Promise<Blob> { public static async toBlob(file: File, prettify?: boolean | PrettityType): Promise<Blob> {
const zip = this.compiler.compile(file, prettify); const zip = this.compiler.compile(file, prettify);
const zipData = await zip.generateAsync({ const zipData = await zip.generateAsync({
type: "blob", type: "blob",

View File

@ -1,3 +1,4 @@
import { Comments } from './../paragraph/run/comment-run';
import { StringContainer, XmlComponent } from "file/xml-components"; import { StringContainer, XmlComponent } from "file/xml-components";
import { ICustomPropertyOptions } from "../custom-properties"; import { ICustomPropertyOptions } from "../custom-properties";
import { IDocumentBackgroundOptions } from "../document"; import { IDocumentBackgroundOptions } from "../document";
@ -21,6 +22,7 @@ export interface IPropertiesOptions {
readonly externalStyles?: string; readonly externalStyles?: string;
readonly styles?: IStylesOptions; readonly styles?: IStylesOptions;
readonly numbering?: INumberingOptions; readonly numbering?: INumberingOptions;
readonly comments?: Comments;
readonly footnotes?: { readonly footnotes?: {
readonly [key: string]: { readonly [key: string]: {
readonly children: Paragraph[]; readonly children: Paragraph[];

View File

@ -71,6 +71,10 @@ export class File {
}, },
); );
if (options.comments) {
this.comments = options.comments;
}
this.fileRelationships = new Relationships(); this.fileRelationships = new Relationships();
this.customProperties = new CustomProperties(options.customProperties ?? []); this.customProperties = new CustomProperties(options.customProperties ?? []);
this.appProperties = new AppProperties(); this.appProperties = new AppProperties();
@ -136,9 +140,6 @@ export class File {
this.footnotesWrapper.View.createFootNote(parseFloat(key), options.footnotes[key].children); 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 { private addSection({ headers = {}, footers = {}, children, properties }: ISectionOptions): void {

View File

@ -10,7 +10,7 @@ interface ICommentOptions {
} }
class CommentAttributes extends XmlAttributeComponent<ICommentOptions> { class CommentAttributes extends XmlAttributeComponent<ICommentOptions> {
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 = { const COMMENT_ATTRIBUTES_MAP = {

View File

@ -1,4 +1,3 @@
import { Comments } from "./../file/paragraph/run/comment-run";
import * as JSZip from "jszip"; import * as JSZip from "jszip";
import { Element as XMLElement, ElementCompact as XMLElementCompact, xml2js } from "xml-js"; import { Element as XMLElement, ElementCompact as XMLElementCompact, xml2js } from "xml-js";
@ -38,12 +37,11 @@ interface IRelationshipFileInfo {
// https://fileinfo.com/extension/dotx // https://fileinfo.com/extension/dotx
export interface IDocumentTemplate { export interface IDocumentTemplate {
readonly currentRelationshipId: number; readonly currentRelationshipId: number;
readonly headers?: IDocumentHeader[]; readonly headers: IDocumentHeader[];
readonly footers?: IDocumentFooter[]; readonly footers: IDocumentFooter[];
readonly styles?: string; readonly styles: string;
readonly titlePageIsDefined?: boolean; readonly titlePageIsDefined: boolean;
readonly media?: Media; readonly media: Media;
readonly comments?: Comments;
} }
export class ImportDotx { export class ImportDotx {