From 048ae6a58c1016f0cd3f76b65bfb6f1952bc21df Mon Sep 17 00:00:00 2001 From: amitm02 Date: Wed, 26 Sep 2018 14:33:05 +0300 Subject: [PATCH] extract styles from dotx --- src/file/file.ts | 8 +++++++- src/import-dotx/import-dotx.ts | 10 +++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/file/file.ts b/src/file/file.ts index 81920a2ad2..33065765f0 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -57,7 +57,13 @@ export class File { this.currentRelationshipId = fileProperties.template.currentRelationshipId + 1; } - if (options.externalStyles) { + // set up styles + if (fileProperties.template && options.externalStyles) { + throw Error("can not use both template and external styles"); + } + if (fileProperties.template) { + this.styles = fileProperties.template.styles; + } else if (options.externalStyles) { const stylesFactory = new ExternalStylesFactory(); this.styles = stylesFactory.newInstance(options.externalStyles); } else { diff --git a/src/import-dotx/import-dotx.ts b/src/import-dotx/import-dotx.ts index cb0a90b0fc..940ecd2f10 100644 --- a/src/import-dotx/import-dotx.ts +++ b/src/import-dotx/import-dotx.ts @@ -7,6 +7,9 @@ import { FooterWrapper, IDocumentFooter } from "file/footer-wrapper"; import { HeaderWrapper, IDocumentHeader } from "file/header-wrapper"; import { convertToXmlComponent, ImportedXmlComponent, parseOptions } from "file/xml-components"; +import { Styles } from "file/styles"; +import { ExternalStylesFactory } from "file/styles/external-styles-factory"; + const importParseOptions = { ...parseOptions, textNodeName: "", @@ -36,6 +39,7 @@ export interface IDocumentTemplate { currentRelationshipId: number; headers: IDocumentHeader[]; footers: IDocumentFooter[]; + styles: Styles; } export class ImportDotx { @@ -48,6 +52,10 @@ export class ImportDotx { public async extract(data: Buffer): Promise { const zipContent = await JSZip.loadAsync(data); + const stylesContent = await zipContent.files["word/styles.xml"].async("text"); + const stylesFactory = new ExternalStylesFactory(); + const styles = stylesFactory.newInstance(stylesContent); + const documentContent = zipContent.files["word/document.xml"]; const documentRefs: IDocumentRefs = this.extractDocumentRefs(await documentContent.async("text")); @@ -88,7 +96,7 @@ export class ImportDotx { footers.push({ type: footerRef.type, footer }); } - const templateDocument: IDocumentTemplate = { headers, footers, currentRelationshipId: this.currentRelationshipId }; + const templateDocument: IDocumentTemplate = { headers, footers, currentRelationshipId: this.currentRelationshipId, styles }; return templateDocument; }