import * as fastXmlParser from "fast-xml-parser"; import { ImportedXmlComponent, ImportedRootElementAttributes, parseOptions, convertToXmlComponent } from "./../../file/xml-components"; import { convertToXmlComponent, ImportedRootElementAttributes, ImportedXmlComponent, parseOptions } from "file/xml-components"; import { ImportedRootElementAttributes, parseOptions, convertToXmlComponent } from "./../../file/xml-components"; import { Styles } from "./"; export class ExternalStylesFactory { /** * Creates new Style based on the given styles. * Parses the styles and convert them to XmlComponent. * Example content from styles.xml: * * * * * * ..... * * * * * ..... * * * Or any other element will be parsed to * * * @param externalStyles context from styles.xml */ public newInstance(externalStyles: string): Styles { const xmlStyles = fastXmlParser.parse(externalStyles, parseOptions)["w:styles"]; // create styles with attributes from the parsed xml const importedStyle = new Styles(new ImportedRootElementAttributes(xmlStyles._attr)); // convert other elements (not styles definitions, but default styles and so on ...) Object.keys(xmlStyles) .filter((element) => element !== "_attr" && element !== "w:style") .forEach((element) => { const converted = convertToXmlComponent(element, xmlStyles[element]); if (Array.isArray(converted)) { converted.forEach((c) => importedStyle.push(c)); } else { importedStyle.push(converted); } }); // convert the styles one by one xmlStyles["w:style"].map((style) => convertToXmlComponent("w:style", style)).forEach(importedStyle.push.bind(importedStyle)); return importedStyle; } }