diff --git a/src/docx/document/document.ts b/src/docx/document/document.ts index fa13145b94..74f602eb5e 100644 --- a/src/docx/document/document.ts +++ b/src/docx/document/document.ts @@ -52,5 +52,4 @@ export class Document extends XmlComponent { this.addTable(table); return table; } - } diff --git a/src/docx/file.ts b/src/docx/file.ts index e0053cc691..2b11926fdb 100644 --- a/src/docx/file.ts +++ b/src/docx/file.ts @@ -1,8 +1,10 @@ import { Media } from "../media"; import { Numbering } from "../numbering"; -import { Properties } from "../properties"; +import { IPropertiesOptions, Properties } from "../properties"; import { Styles } from "../styles"; +import { DefaultStylesFactory } from "../styles/factory"; import { Document } from "./document"; +import { Paragraph, Table } from "./index"; export class File { @@ -12,15 +14,57 @@ export class File { private numbering: Numbering; private media: Media; - constructor() { + constructor(options?: IPropertiesOptions) { this.document = new Document(); - this.styles = new Styles(); - this.properties = new Properties({ - creator: "Un-named", - revision: "1", - lastModifiedBy: "Un-named", - }); + const stylesFactory = new DefaultStylesFactory(); + this.styles = stylesFactory.newInstance(); + + if (!options) { + options = { + creator: "Un-named", + revision: "1", + lastModifiedBy: "Un-named", + }; + } + + this.properties = new Properties(options); this.numbering = new Numbering(); this.media = new Media(); } + + public addParagraph(paragraph: Paragraph): void { + this.document.addParagraph(paragraph); + } + + public createParagraph(text?: string): Paragraph { + return this.document.createParagraph(); + } + + public addTable(table: Table): void { + return this.document.addTable(table); + } + + public createTable(rows: number, cols: number): Table { + return this.document.createTable(rows, cols); + } + + public get Document(): Document { + return this.document; + } + + public get Styles(): Styles { + return this.styles; + } + + public get Properties(): Properties { + return this.properties; + } + + public get Numbering(): Numbering { + return this.numbering; + } + + public get Media(): Media { + return this.media; + } } diff --git a/src/export/packer/compiler.ts b/src/export/packer/compiler.ts index b1e3f0a1d7..66765906fd 100644 --- a/src/export/packer/compiler.ts +++ b/src/export/packer/compiler.ts @@ -4,12 +4,7 @@ import * as fs from "fs"; import * as path from "path"; import * as xml from "xml"; -import { Document } from "../../docx"; -import { Media } from "../../media"; -import { Numbering } from "../../numbering"; -import { Properties } from "../../properties"; -import { Styles } from "../../styles"; -import { DefaultStylesFactory } from "../../styles/factory"; +import { File } from "../../docx"; import { Formatter } from "../formatter"; const TEMPLATE_PATH = path.resolve(__dirname, "../../../template"); @@ -17,29 +12,11 @@ const TEMPLATE_PATH = path.resolve(__dirname, "../../../template"); export class Compiler { protected archive: archiver.Archiver; private formatter: Formatter; - private style: Styles; - constructor( - protected document: Document, - style?: Styles, - private properties: Properties = new Properties({ - creator: "Un-named", - revision: "1", - lastModifiedBy: "Un-named", - }), - private numbering: Numbering = new Numbering(), - private media: Media = new Media(), - ) { + constructor(private file: File) { this.formatter = new Formatter(); this.archive = archiver.create("zip", {}); - if (style) { - this.style = style; - } else { - const stylesFactory = new DefaultStylesFactory(); - this.style = stylesFactory.newInstance(); - } - this.archive.on("error", (err) => { throw err; }); @@ -55,15 +32,15 @@ export class Compiler { cwd: TEMPLATE_PATH, }); - const xmlDocument = xml(this.formatter.format(this.document)); - const xmlStyles = xml(this.formatter.format(this.style)); - const xmlProperties = xml(this.formatter.format(this.properties), { + const xmlDocument = xml(this.formatter.format(this.file.Document)); + const xmlStyles = xml(this.formatter.format(this.file.Styles)); + const xmlProperties = xml(this.formatter.format(this.file.Properties), { declaration: { standalone: "yes", encoding: "UTF-8", }, }); - const xmlNumbering = xml(this.formatter.format(this.numbering)); + const xmlNumbering = xml(this.formatter.format(this.file.Numbering)); this.archive.append(xmlDocument, { name: "word/document.xml", @@ -81,7 +58,7 @@ export class Compiler { name: "word/numbering.xml", }); - for (const data of this.media.array) { + for (const data of this.file.Media.array) { this.archive.append(data.stream, { name: `media/${data.fileName}`, }); diff --git a/src/export/packer/express.ts b/src/export/packer/express.ts index f5f2789f1d..5c065e60b5 100644 --- a/src/export/packer/express.ts +++ b/src/export/packer/express.ts @@ -1,10 +1,6 @@ import * as express from "express"; -import { Document } from "../../docx/document"; -import { Media } from "../../media"; -import { Numbering } from "../../numbering"; -import { Properties } from "../../properties"; -import { Styles } from "../../styles"; +import { File } from "../../docx"; import { Compiler } from "./compiler"; import { IPacker } from "./packer"; @@ -12,8 +8,8 @@ export class ExpressPacker implements IPacker { private res: express.Response; private packer: Compiler; - constructor(document: Document, res: express.Response, styles?: Styles, properties?: Properties, numbering?: Numbering, media?: Media) { - this.packer = new Compiler(document, styles, properties, numbering, media); + constructor(file: File, res: express.Response) { + this.packer = new Compiler(file); this.res = res; diff --git a/src/export/packer/local.spec.ts b/src/export/packer/local.spec.ts index 79caf6dd9c..092fe97a6b 100644 --- a/src/export/packer/local.spec.ts +++ b/src/export/packer/local.spec.ts @@ -1,31 +1,27 @@ /* tslint:disable:typedef space-before-function-paren */ import * as fs from "fs"; -import { Document } from "../../docx/document"; +import { File } from "../../docx"; import { Paragraph } from "../../docx/paragraph"; import { LocalPacker } from "../../export/packer/local"; -import { Properties } from "../../properties"; -import { DefaultStylesFactory } from "../../styles/factory"; -describe("LocalPacker", () => { +describe.only("LocalPacker", () => { let packer: LocalPacker; - let stylesFactory: DefaultStylesFactory; beforeEach(() => { - const document = new Document(); - const paragraph = new Paragraph("test text"); - const heading = new Paragraph("Hello world").heading1(); - document.addParagraph(new Paragraph("title").title()); - document.addParagraph(heading); - document.addParagraph(new Paragraph("heading 2").heading2()); - document.addParagraph(paragraph); - const properties = new Properties({ + const file = new File({ creator: "Dolan Miu", revision: "1", lastModifiedBy: "Dolan Miu", }); - stylesFactory = new DefaultStylesFactory(); - packer = new LocalPacker(document, stylesFactory.newInstance(), properties); + const paragraph = new Paragraph("test text"); + const heading = new Paragraph("Hello world").heading1(); + file.addParagraph(new Paragraph("title").title()); + file.addParagraph(heading); + file.addParagraph(new Paragraph("heading 2").heading2()); + file.addParagraph(paragraph); + + packer = new LocalPacker(file); }); describe("#pack()", () => { diff --git a/src/export/packer/local.ts b/src/export/packer/local.ts index 1fefb6cb20..a1d293e0e0 100644 --- a/src/export/packer/local.ts +++ b/src/export/packer/local.ts @@ -2,11 +2,7 @@ import * as fs from "fs"; import * as os from "os"; import * as path from "path"; -import { Document } from "../../docx/document"; -import { Media } from "../../media"; -import { Numbering } from "../../numbering"; -import { Properties } from "../../properties"; -import { Styles } from "../../styles"; +import { File } from "../../docx"; import { Compiler } from "./compiler"; import { IPacker } from "./packer"; import { PdfConvertWrapper } from "./pdf-convert-wrapper"; @@ -16,9 +12,9 @@ export class LocalPacker implements IPacker { private pdfConverter: PdfConvertWrapper; private packer: Compiler; - constructor(document: Document, styles?: Styles, properties?: Properties, numbering?: Numbering, media?: Media) { + constructor(file: File) { this.pdfConverter = new PdfConvertWrapper(); - this.packer = new Compiler(document, styles, properties, numbering, media); + this.packer = new Compiler(file); } public async pack(filePath: string): Promise {