Add back default styles

This commit is contained in:
Dolan
2019-10-04 02:37:22 +01:00
parent 591b2f4e04
commit 10ab3c70bf
5 changed files with 24 additions and 28 deletions

View File

@ -1,13 +1,13 @@
// Example on how to customise the look at feel using Styles // Example on how to customise the look at feel using Styles
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { Document, HeadingLevel, Packer, Paragraph, Styles, TextRun, UnderlineType } from "../build"; import { Document, HeadingLevel, Packer, Paragraph, TextRun, UnderlineType } from "../build";
const doc = new Document({ const doc = new Document({
creator: "Clippy", creator: "Clippy",
title: "Sample Document", title: "Sample Document",
description: "A brief example of using docx", description: "A brief example of using docx",
styles: new Styles({ styles: {
paragraphStyles: [ paragraphStyles: [
{ {
id: "Heading1", id: "Heading1",
@ -81,7 +81,7 @@ const doc = new Document({
quickFormat: true, quickFormat: true,
}, },
], ],
}), },
}); });
const numberedAbstract = doc.Numbering.createAbstractNumbering(); const numberedAbstract = doc.Numbering.createAbstractNumbering();

View File

@ -1,6 +1,7 @@
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { DocumentAttributes } from "../document/document-attributes"; import { DocumentAttributes } from "../document/document-attributes";
import { Styles } from "../styles"; import { IStylesOptions } from "../styles";
import { Created, Creator, Description, Keywords, LastModifiedBy, Modified, Revision, Subject, Title } from "./components"; import { Created, Creator, Description, Keywords, LastModifiedBy, Modified, Revision, Subject, Title } from "./components";
export interface IPropertiesOptions { export interface IPropertiesOptions {
@ -12,7 +13,7 @@ export interface IPropertiesOptions {
readonly lastModifiedBy?: string; readonly lastModifiedBy?: string;
readonly revision?: string; readonly revision?: string;
readonly externalStyles?: string; readonly externalStyles?: string;
readonly styles?: Styles; readonly styles?: IStylesOptions;
} }
export class CoreProperties extends XmlComponent { export class CoreProperties extends XmlComponent {

View File

@ -97,10 +97,15 @@ export class File {
const stylesFactory = new ExternalStylesFactory(); const stylesFactory = new ExternalStylesFactory();
this.styles = stylesFactory.newInstance(options.externalStyles); this.styles = stylesFactory.newInstance(options.externalStyles);
} else if (options.styles) { } else if (options.styles) {
this.styles = options.styles; const stylesFactory = new DefaultStylesFactory();
const defaultStyles = stylesFactory.newInstance();
this.styles = new Styles({
...defaultStyles,
...options.styles,
});
} else { } else {
const stylesFactory = new DefaultStylesFactory(); const stylesFactory = new DefaultStylesFactory();
this.styles = stylesFactory.newInstance(); this.styles = new Styles(stylesFactory.newInstance());
} }
this.addDefaultRelationships(); this.addDefaultRelationships();

View File

@ -1,5 +1,5 @@
import { DocumentAttributes } from "../document/document-attributes"; import { DocumentAttributes } from "../document/document-attributes";
import { Styles } from "./styles"; import { IStylesOptions } from "./styles";
import { DocumentDefaults } from "./defaults"; import { DocumentDefaults } from "./defaults";
import { import {
@ -18,7 +18,7 @@ import {
} from "./style"; } from "./style";
export class DefaultStylesFactory { export class DefaultStylesFactory {
public newInstance(): Styles { public newInstance(): IStylesOptions {
const documentAttributes = new DocumentAttributes({ const documentAttributes = new DocumentAttributes({
mc: "http://schemas.openxmlformats.org/markup-compatibility/2006", mc: "http://schemas.openxmlformats.org/markup-compatibility/2006",
r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships", r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
@ -27,7 +27,7 @@ export class DefaultStylesFactory {
w15: "http://schemas.microsoft.com/office/word/2012/wordml", w15: "http://schemas.microsoft.com/office/word/2012/wordml",
Ignorable: "w14 w15", Ignorable: "w14 w15",
}); });
const styles = new Styles({ return {
initialStyles: documentAttributes, initialStyles: documentAttributes,
importedStyles: [ importedStyles: [
new DocumentDefaults(), new DocumentDefaults(),
@ -76,9 +76,6 @@ export class DefaultStylesFactory {
new FootnoteText({}), new FootnoteText({}),
new FootnoteTextChar({}), new FootnoteTextChar({}),
], ],
}); };
styles.createDocumentDefaults();
return styles;
} }
} }

View File

@ -1,12 +1,11 @@
import { BaseXmlComponent, ImportedXmlComponent, XmlComponent } from "file/xml-components"; import { BaseXmlComponent, ImportedXmlComponent, XmlComponent } from "file/xml-components";
import { DocumentDefaults } from "./defaults";
import { CharacterStyle, ParagraphStyle } from "./style"; import { CharacterStyle, ParagraphStyle } from "./style";
import { ICharacterStyleOptions } from "./style/character-style"; import { ICharacterStyleOptions } from "./style/character-style";
import { IParagraphStyleOptions } from "./style/paragraph-style"; import { IParagraphStyleOptions } from "./style/paragraph-style";
export * from "./border"; export * from "./border";
interface IStylesOptions { export interface IStylesOptions {
readonly initialStyles?: BaseXmlComponent; readonly initialStyles?: BaseXmlComponent;
readonly paragraphStyles?: IParagraphStyleOptions[]; readonly paragraphStyles?: IParagraphStyleOptions[];
readonly characterStyles?: ICharacterStyleOptions[]; readonly characterStyles?: ICharacterStyleOptions[];
@ -21,6 +20,12 @@ export class Styles extends XmlComponent {
this.root.push(options.initialStyles); this.root.push(options.initialStyles);
} }
if (options.importedStyles) {
for (const style of options.importedStyles) {
this.root.push(style);
}
}
if (options.paragraphStyles) { if (options.paragraphStyles) {
for (const style of options.paragraphStyles) { for (const style of options.paragraphStyles) {
this.root.push(new ParagraphStyle(style)); this.root.push(new ParagraphStyle(style));
@ -32,17 +37,5 @@ export class Styles extends XmlComponent {
this.root.push(new CharacterStyle(style)); this.root.push(new CharacterStyle(style));
} }
} }
if (options.importedStyles) {
for (const style of options.importedStyles) {
this.root.push(style);
}
}
}
public createDocumentDefaults(): DocumentDefaults {
const defaults = new DocumentDefaults();
this.root.push(defaults);
return defaults;
} }
} }