feat: transfer comments property to File CoreProperties like numbering.
This commit is contained in:
@ -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);
|
||||
});
|
||||
|
@ -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<string, IXmlifyedFile | IXmlifyedFile[]>(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(
|
||||
|
@ -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<Buffer> {
|
||||
public static async toBuffer(file: File, prettify?: boolean | PrettityType): Promise<Buffer> {
|
||||
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<string> {
|
||||
public static async toBase64String(file: File, prettify?: boolean | PrettityType): Promise<string> {
|
||||
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<Blob> {
|
||||
public static async toBlob(file: File, prettify?: boolean | PrettityType): Promise<Blob> {
|
||||
const zip = this.compiler.compile(file, prettify);
|
||||
const zipData = await zip.generateAsync({
|
||||
type: "blob",
|
||||
|
@ -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[];
|
||||
|
@ -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 {
|
||||
|
@ -10,7 +10,7 @@ interface 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 = {
|
||||
|
@ -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 {
|
||||
|
Reference in New Issue
Block a user