Add back default styles
This commit is contained in:
@ -1,13 +1,13 @@
|
||||
// Example on how to customise the look at feel using Styles
|
||||
// Import from 'docx' rather than '../build' if you install from npm
|
||||
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({
|
||||
creator: "Clippy",
|
||||
title: "Sample Document",
|
||||
description: "A brief example of using docx",
|
||||
styles: new Styles({
|
||||
styles: {
|
||||
paragraphStyles: [
|
||||
{
|
||||
id: "Heading1",
|
||||
@ -81,7 +81,7 @@ const doc = new Document({
|
||||
quickFormat: true,
|
||||
},
|
||||
],
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
const numberedAbstract = doc.Numbering.createAbstractNumbering();
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
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";
|
||||
|
||||
export interface IPropertiesOptions {
|
||||
@ -12,7 +13,7 @@ export interface IPropertiesOptions {
|
||||
readonly lastModifiedBy?: string;
|
||||
readonly revision?: string;
|
||||
readonly externalStyles?: string;
|
||||
readonly styles?: Styles;
|
||||
readonly styles?: IStylesOptions;
|
||||
}
|
||||
|
||||
export class CoreProperties extends XmlComponent {
|
||||
|
@ -97,10 +97,15 @@ export class File {
|
||||
const stylesFactory = new ExternalStylesFactory();
|
||||
this.styles = stylesFactory.newInstance(options.externalStyles);
|
||||
} else if (options.styles) {
|
||||
this.styles = options.styles;
|
||||
const stylesFactory = new DefaultStylesFactory();
|
||||
const defaultStyles = stylesFactory.newInstance();
|
||||
this.styles = new Styles({
|
||||
...defaultStyles,
|
||||
...options.styles,
|
||||
});
|
||||
} else {
|
||||
const stylesFactory = new DefaultStylesFactory();
|
||||
this.styles = stylesFactory.newInstance();
|
||||
this.styles = new Styles(stylesFactory.newInstance());
|
||||
}
|
||||
|
||||
this.addDefaultRelationships();
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { DocumentAttributes } from "../document/document-attributes";
|
||||
import { Styles } from "./styles";
|
||||
import { IStylesOptions } from "./styles";
|
||||
|
||||
import { DocumentDefaults } from "./defaults";
|
||||
import {
|
||||
@ -18,7 +18,7 @@ import {
|
||||
} from "./style";
|
||||
|
||||
export class DefaultStylesFactory {
|
||||
public newInstance(): Styles {
|
||||
public newInstance(): IStylesOptions {
|
||||
const documentAttributes = new DocumentAttributes({
|
||||
mc: "http://schemas.openxmlformats.org/markup-compatibility/2006",
|
||||
r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
|
||||
@ -27,7 +27,7 @@ export class DefaultStylesFactory {
|
||||
w15: "http://schemas.microsoft.com/office/word/2012/wordml",
|
||||
Ignorable: "w14 w15",
|
||||
});
|
||||
const styles = new Styles({
|
||||
return {
|
||||
initialStyles: documentAttributes,
|
||||
importedStyles: [
|
||||
new DocumentDefaults(),
|
||||
@ -76,9 +76,6 @@ export class DefaultStylesFactory {
|
||||
new FootnoteText({}),
|
||||
new FootnoteTextChar({}),
|
||||
],
|
||||
});
|
||||
styles.createDocumentDefaults();
|
||||
|
||||
return styles;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,11 @@
|
||||
import { BaseXmlComponent, ImportedXmlComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
import { DocumentDefaults } from "./defaults";
|
||||
import { CharacterStyle, ParagraphStyle } from "./style";
|
||||
import { ICharacterStyleOptions } from "./style/character-style";
|
||||
import { IParagraphStyleOptions } from "./style/paragraph-style";
|
||||
export * from "./border";
|
||||
|
||||
interface IStylesOptions {
|
||||
export interface IStylesOptions {
|
||||
readonly initialStyles?: BaseXmlComponent;
|
||||
readonly paragraphStyles?: IParagraphStyleOptions[];
|
||||
readonly characterStyles?: ICharacterStyleOptions[];
|
||||
@ -21,6 +20,12 @@ export class Styles extends XmlComponent {
|
||||
this.root.push(options.initialStyles);
|
||||
}
|
||||
|
||||
if (options.importedStyles) {
|
||||
for (const style of options.importedStyles) {
|
||||
this.root.push(style);
|
||||
}
|
||||
}
|
||||
|
||||
if (options.paragraphStyles) {
|
||||
for (const style of options.paragraphStyles) {
|
||||
this.root.push(new ParagraphStyle(style));
|
||||
@ -32,17 +37,5 @@ export class Styles extends XmlComponent {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user