Turn Packer static

This commit is contained in:
Dolan
2019-08-07 22:12:14 +01:00
parent 65c5177daf
commit d8b60d82f3
51 changed files with 118 additions and 183 deletions

View File

@ -30,17 +30,15 @@ interface IXmlifyedFileMapping {
export class Compiler {
private readonly formatter: Formatter;
private readonly imageReplacer: ImageReplacer;
private readonly prettifyXml?: boolean;
constructor(prettifyXml?: boolean) {
constructor() {
this.formatter = new Formatter();
this.imageReplacer = new ImageReplacer();
this.prettifyXml = prettifyXml;
}
public compile(file: File): JSZip {
public compile(file: File, prettifyXml?: boolean): JSZip {
const zip = new JSZip();
const xmlifiedFileMapping = this.xmlifyFile(file);
const xmlifiedFileMapping = this.xmlifyFile(file, prettifyXml);
for (const key in xmlifiedFileMapping) {
if (!xmlifiedFileMapping[key]) {
@ -66,14 +64,14 @@ export class Compiler {
return zip;
}
private xmlifyFile(file: File): IXmlifyedFileMapping {
private xmlifyFile(file: File, prettify?: boolean): IXmlifyedFileMapping {
file.verifyUpdateFields();
const documentRelationshipCount = file.DocumentRelationships.RelationshipCount + 1;
return {
Relationships: {
data: (() => {
const xmlData = xml(this.formatter.format(file.Document), this.prettifyXml);
const xmlData = xml(this.formatter.format(file.Document), prettify);
const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media);
mediaDatas.forEach((mediaData, i) => {
@ -84,13 +82,13 @@ export class Compiler {
);
});
return xml(this.formatter.format(file.DocumentRelationships), this.prettifyXml);
return xml(this.formatter.format(file.DocumentRelationships), prettify);
})(),
path: "word/_rels/document.xml.rels",
},
Document: {
data: (() => {
const tempXmlData = xml(this.formatter.format(file.Document), this.prettifyXml);
const tempXmlData = xml(this.formatter.format(file.Document), prettify);
const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media);
const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, documentRelationshipCount);
@ -99,7 +97,7 @@ export class Compiler {
path: "word/document.xml",
},
Styles: {
data: xml(this.formatter.format(file.Styles), this.prettifyXml),
data: xml(this.formatter.format(file.Styles), prettify),
path: "word/styles.xml",
},
Properties: {
@ -112,15 +110,15 @@ export class Compiler {
path: "docProps/core.xml",
},
Numbering: {
data: xml(this.formatter.format(file.Numbering), this.prettifyXml),
data: xml(this.formatter.format(file.Numbering), prettify),
path: "word/numbering.xml",
},
FileRelationships: {
data: xml(this.formatter.format(file.FileRelationships), this.prettifyXml),
data: xml(this.formatter.format(file.FileRelationships), prettify),
path: "_rels/.rels",
},
HeaderRelationships: file.Headers.map((headerWrapper, index) => {
const xmlData = xml(this.formatter.format(headerWrapper.Header), this.prettifyXml);
const xmlData = xml(this.formatter.format(headerWrapper.Header), prettify);
const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media);
mediaDatas.forEach((mediaData, i) => {
@ -132,12 +130,12 @@ export class Compiler {
});
return {
data: xml(this.formatter.format(headerWrapper.Relationships), this.prettifyXml),
data: xml(this.formatter.format(headerWrapper.Relationships), prettify),
path: `word/_rels/header${index + 1}.xml.rels`,
};
}),
FooterRelationships: file.Footers.map((footerWrapper, index) => {
const xmlData = xml(this.formatter.format(footerWrapper.Footer), this.prettifyXml);
const xmlData = xml(this.formatter.format(footerWrapper.Footer), prettify);
const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media);
mediaDatas.forEach((mediaData, i) => {
@ -149,12 +147,12 @@ export class Compiler {
});
return {
data: xml(this.formatter.format(footerWrapper.Relationships), this.prettifyXml),
data: xml(this.formatter.format(footerWrapper.Relationships), prettify),
path: `word/_rels/footer${index + 1}.xml.rels`,
};
}),
Headers: file.Headers.map((headerWrapper, index) => {
const tempXmlData = xml(this.formatter.format(headerWrapper.Header), this.prettifyXml);
const tempXmlData = xml(this.formatter.format(headerWrapper.Header), prettify);
const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media);
// TODO: 0 needs to be changed when headers get relationships of their own
const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, 0);
@ -165,7 +163,7 @@ export class Compiler {
};
}),
Footers: file.Footers.map((footerWrapper, index) => {
const tempXmlData = xml(this.formatter.format(footerWrapper.Footer), this.prettifyXml);
const tempXmlData = xml(this.formatter.format(footerWrapper.Footer), prettify);
const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media);
// TODO: 0 needs to be changed when headers get relationships of their own
const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, 0);
@ -176,19 +174,19 @@ export class Compiler {
};
}),
ContentTypes: {
data: xml(this.formatter.format(file.ContentTypes), this.prettifyXml),
data: xml(this.formatter.format(file.ContentTypes), prettify),
path: "[Content_Types].xml",
},
AppProperties: {
data: xml(this.formatter.format(file.AppProperties), this.prettifyXml),
data: xml(this.formatter.format(file.AppProperties), prettify),
path: "docProps/app.xml",
},
FootNotes: {
data: xml(this.formatter.format(file.FootNotes), this.prettifyXml),
data: xml(this.formatter.format(file.FootNotes), prettify),
path: "word/footnotes.xml",
},
Settings: {
data: xml(this.formatter.format(file.Settings), this.prettifyXml),
data: xml(this.formatter.format(file.Settings), prettify),
path: "word/settings.xml",
},
};

View File

@ -7,7 +7,6 @@ import { File, HeadingLevel, Paragraph } from "file";
import { Packer } from "./packer";
describe("Packer", () => {
let packer: Packer;
let file: File;
beforeEach(() => {
@ -34,14 +33,12 @@ describe("Packer", () => {
new Paragraph("test text"),
],
});
packer = new Packer();
});
describe("#toBuffer()", () => {
it("should create a standard docx file", async function() {
this.timeout(99999999);
const buffer = await packer.toBuffer(file);
const buffer = await Packer.toBuffer(file);
assert.isDefined(buffer);
assert.isTrue(buffer.byteLength > 0);
@ -49,10 +46,10 @@ describe("Packer", () => {
it("should handle exception if it throws any", () => {
// tslint:disable-next-line:no-any
const compiler = stub((packer as any).compiler, "compile");
const compiler = stub((Packer as any).compiler, "compile");
compiler.throwsException();
return packer.toBuffer(file).catch((error) => {
return Packer.toBuffer(file).catch((error) => {
assert.isDefined(error);
});
});
@ -61,7 +58,7 @@ describe("Packer", () => {
describe("#toBase64String()", () => {
it("should create a standard docx file", async function() {
this.timeout(99999999);
const str = await packer.toBase64String(file);
const str = await Packer.toBase64String(file);
assert.isDefined(str);
assert.isTrue(str.length > 0);
@ -69,10 +66,10 @@ describe("Packer", () => {
it("should handle exception if it throws any", () => {
// tslint:disable-next-line:no-any
const compiler = stub((packer as any).compiler, "compile");
const compiler = stub((Packer as any).compiler, "compile");
compiler.throwsException();
return packer.toBase64String(file).catch((error) => {
return Packer.toBase64String(file).catch((error) => {
assert.isDefined(error);
});
});

View File

@ -2,14 +2,8 @@ import { File } from "file";
import { Compiler } from "./next-compiler";
export class Packer {
private readonly compiler: Compiler;
constructor(prettifyXml?: boolean) {
this.compiler = new Compiler(prettifyXml);
}
public async toBuffer(file: File): Promise<Buffer> {
const zip = this.compiler.compile(file);
public static async toBuffer(file: File, prettify?: boolean): Promise<Buffer> {
const zip = this.compiler.compile(file, prettify);
const zipData = (await zip.generateAsync({
type: "nodebuffer",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
@ -18,8 +12,8 @@ export class Packer {
return zipData;
}
public async toBase64String(file: File): Promise<string> {
const zip = this.compiler.compile(file);
public static async toBase64String(file: File, prettify?: boolean): Promise<string> {
const zip = this.compiler.compile(file, prettify);
const zipData = (await zip.generateAsync({
type: "base64",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
@ -28,8 +22,8 @@ export class Packer {
return zipData;
}
public async toBlob(file: File): Promise<Blob> {
const zip = this.compiler.compile(file);
public static async toBlob(file: File, prettify?: boolean): Promise<Blob> {
const zip = this.compiler.compile(file, prettify);
const zipData = (await zip.generateAsync({
type: "blob",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
@ -37,4 +31,6 @@ export class Packer {
return zipData;
}
private static readonly compiler = new Compiler();
}