Merge pull request #1 from h4buli/feature/external-styles-support

(styles): add support to provide external styles (as complete file co…
This commit is contained in:
h4buli
2018-03-23 12:21:00 +01:00
committed by GitHub
4 changed files with 85 additions and 1 deletions

View File

@ -55,6 +55,7 @@
"awesome-typescript-loader": "^3.4.1", "awesome-typescript-loader": "^3.4.1",
"chai": "^3.5.0", "chai": "^3.5.0",
"glob": "^7.1.2", "glob": "^7.1.2",
"jszip": "^3.1.5",
"mocha": "^3.2.0", "mocha": "^3.2.0",
"mocha-webpack": "^1.0.1", "mocha-webpack": "^1.0.1",
"prettier": "^1.10.2", "prettier": "^1.10.2",

View File

@ -0,0 +1,65 @@
/* tslint:disable:typedef space-before-function-paren */
import * as fs from "fs";
import * as os from 'os';
import { expect } from "chai";
import { File, Paragraph } from "../../file";
import {Compiler} from './compiler';
import * as jszip from 'jszip';
async function getDocxXmlFileContent(filePath: string, xmlFileName: string): Promise<any> {
let zipFile = fs.readFileSync(filePath);
const zipData = await jszip.loadAsync(zipFile).then(zip => zip);
return zipData.files[xmlFileName].async('text');
}
describe("compiler", () => {
let compiler: Compiler;
let file: File;
let externalStyles: string;
beforeEach(() => {
file = new File({
creator: "Dolan Miu",
revision: "1",
lastModifiedBy: "Dolan Miu",
});
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);
file.Styles.createParagraphStyle("testStyle").basedOn("Normal").bold();
externalStyles = "Some external styles";
file.setExternalStyles(externalStyles);
compiler = new Compiler(file);
});
describe("#compile()", () => {
it("should use document styles when they are no external styles provided", async function() {
file.setExternalStyles('');
const filePath = `${os.tmpdir()}/test-compile.zip`;
let stream = fs.createWriteStream(filePath);
await compiler.compile(stream);
const styles = await getDocxXmlFileContent(filePath, 'word/styles.xml')
expect(styles).not.to.equal(externalStyles);
});
it("should use provided external styles", async function() {
const filePath = `${os.tmpdir()}/test-compile.zip`;
let stream = fs.createWriteStream(filePath);
await compiler.compile(stream);
const styles = await getDocxXmlFileContent(filePath, 'word/styles.xml')
expect(styles).to.equal(externalStyles);
});
});
});

View File

@ -23,7 +23,7 @@ export class Compiler {
this.archive.pipe(output); this.archive.pipe(output);
const xmlDocument = xml(this.formatter.format(this.file.Document), true); const xmlDocument = xml(this.formatter.format(this.file.Document), true);
const xmlStyles = xml(this.formatter.format(this.file.Styles)); const xmlStyles = this.resolveStyles();
const xmlProperties = xml(this.formatter.format(this.file.CoreProperties), { const xmlProperties = xml(this.formatter.format(this.file.CoreProperties), {
declaration: { declaration: {
standalone: "yes", standalone: "yes",
@ -102,4 +102,13 @@ export class Compiler {
}); });
}); });
} }
private resolveStyles(): string {
if (this.file.ExternalStyles) {
return this.file.ExternalStyles;
} else {
return xml(this.formatter.format(this.file.Styles));
}
}
} }

View File

@ -25,6 +25,8 @@ export class File {
private readonly footerWrapper: FooterWrapper; private readonly footerWrapper: FooterWrapper;
private readonly contentTypes: ContentTypes; private readonly contentTypes: ContentTypes;
private readonly appProperties: AppProperties; private readonly appProperties: AppProperties;
private externalStyles: string;
constructor(options?: IPropertiesOptions, sectionPropertiesOptions?: SectionPropertiesOptions) { constructor(options?: IPropertiesOptions, sectionPropertiesOptions?: SectionPropertiesOptions) {
this.document = new Document(sectionPropertiesOptions); this.document = new Document(sectionPropertiesOptions);
@ -111,6 +113,10 @@ export class File {
this.document.createDrawing(mediaData); this.document.createDrawing(mediaData);
} }
public setExternalStyles(styles: string): void {
this.externalStyles = styles;
}
public get Document(): Document { public get Document(): Document {
return this.document; return this.document;
} }
@ -154,4 +160,7 @@ export class File {
public get AppProperties(): AppProperties { public get AppProperties(): AppProperties {
return this.appProperties; return this.appProperties;
} }
public get ExternalStyles(): string {
return this.externalStyles;
}
} }