From 8d4a07302b57dd21f424864b09c2cb72f6317b6e Mon Sep 17 00:00:00 2001 From: Tom Hunkapiller Date: Tue, 24 Nov 2020 13:30:26 -0600 Subject: [PATCH 1/3] Add defaultStyles option to overwrite the core default styles (Heading 1-6, document, hyperlinks, etc) --- demo/11-declaritive-styles-2.ts | 120 ++++++++++--------------- demo/2-declaritive-styles.ts | 81 ++++++++--------- src/file/core-properties/properties.ts | 2 + src/file/file.ts | 4 +- src/file/styles/factory.ts | 41 +++++++-- 5 files changed, 121 insertions(+), 127 deletions(-) diff --git a/demo/11-declaritive-styles-2.ts b/demo/11-declaritive-styles-2.ts index 3a0154a0fb..10bc93c17e 100644 --- a/demo/11-declaritive-styles-2.ts +++ b/demo/11-declaritive-styles-2.ts @@ -17,74 +17,56 @@ import { } from "../build"; const doc = new Document({ + defaultStyles: { + heading1: { + run: { + font: "Calibri", + size: 52, + bold: true, + color: "000000", + underline: { + type: UnderlineType.SINGLE, + color: "000000", + }, + }, + paragraph: { + alignment: AlignmentType.CENTER, + spacing: { line: 340 }, + }, + }, + heading2: { + run: { + font: "Calibri", + size: 26, + bold: true, + }, + paragraph: { + spacing: { line: 340 }, + }, + }, + heading3: { + run: { + font: "Calibri", + size: 26, + bold: true, + }, + paragraph: { + spacing: { line: 276 }, + }, + }, + heading4: { + run: { + font: "Calibri", + size: 26, + bold: true, + }, + paragraph: { + alignment: AlignmentType.JUSTIFIED, + }, + }, + }, styles: { paragraphStyles: [ - { - id: "Heading1", - name: "Heading 1", - basedOn: "Normal", - next: "Normal", - quickFormat: true, - run: { - font: "Calibri", - size: 52, - bold: true, - color: "000000", - underline: { - type: UnderlineType.SINGLE, - color: "000000", - }, - }, - paragraph: { - alignment: AlignmentType.CENTER, - spacing: { line: 340 }, - }, - }, - { - id: "Heading2", - name: "Heading 2", - basedOn: "Normal", - next: "Normal", - quickFormat: true, - run: { - font: "Calibri", - size: 26, - bold: true, - }, - paragraph: { - spacing: { line: 340 }, - }, - }, - { - id: "Heading3", - name: "Heading 3", - basedOn: "Normal", - next: "Normal", - quickFormat: true, - run: { - font: "Calibri", - size: 26, - bold: true, - }, - paragraph: { - spacing: { line: 276 }, - }, - }, - { - id: "Heading4", - name: "Heading 4", - basedOn: "Normal", - next: "Normal", - quickFormat: true, - run: { - font: "Calibri", - size: 26, - bold: true, - }, - paragraph: { - alignment: AlignmentType.JUSTIFIED, - }, - }, { id: "normalPara", name: "Normal Para", @@ -139,12 +121,6 @@ const doc = new Document({ spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }, }, }, - { - id: "ListParagraph", - name: "List Paragraph", - basedOn: "Normal", - quickFormat: true, - }, ], }, }); diff --git a/demo/2-declaritive-styles.ts b/demo/2-declaritive-styles.ts index c34eae9628..f01877a8d5 100644 --- a/demo/2-declaritive-styles.ts +++ b/demo/2-declaritive-styles.ts @@ -7,47 +7,44 @@ const doc = new Document({ creator: "Clippy", title: "Sample Document", description: "A brief example of using docx", + defaultStyles: { + heading1: { + run: { + size: 28, + bold: true, + italics: true, + color: "red", + }, + paragraph: { + spacing: { + after: 120, + }, + }, + }, + heading2: { + run: { + size: 26, + bold: true, + underline: { + type: UnderlineType.DOUBLE, + color: "FF0000", + }, + }, + paragraph: { + spacing: { + before: 240, + after: 120, + }, + }, + }, + listParagraph: { + run: { + color: '#FF0000' + } + } + }, styles: { paragraphStyles: [ - { - id: "Heading1", - name: "Heading 1", - basedOn: "Normal", - next: "Normal", - quickFormat: true, - run: { - size: 28, - bold: true, - italics: true, - color: "red", - }, - paragraph: { - spacing: { - after: 120, - }, - }, - }, - { - id: "Heading2", - name: "Heading 2", - basedOn: "Normal", - next: "Normal", - quickFormat: true, - run: { - size: 26, - bold: true, - underline: { - type: UnderlineType.DOUBLE, - color: "FF0000", - }, - }, - paragraph: { - spacing: { - before: 240, - after: 120, - }, - }, - }, { id: "aside", name: "Aside", @@ -75,12 +72,6 @@ const doc = new Document({ spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }, }, }, - { - id: "ListParagraph", - name: "List Paragraph", - basedOn: "Normal", - quickFormat: true, - }, ], }, numbering: { diff --git a/src/file/core-properties/properties.ts b/src/file/core-properties/properties.ts index ae9f227ad5..3fb3dbdc7c 100644 --- a/src/file/core-properties/properties.ts +++ b/src/file/core-properties/properties.ts @@ -1,3 +1,4 @@ +import { IDefaultStylesOptions } from "docx/src/file/styles/factory"; import { XmlComponent } from "file/xml-components"; import { IDocumentBackgroundOptions } from "../document"; @@ -28,6 +29,7 @@ export interface IPropertiesOptions { readonly revision?: string; readonly externalStyles?: string; readonly styles?: IStylesOptions; + readonly defaultStyles?: IDefaultStylesOptions; readonly numbering?: INumberingOptions; readonly footnotes?: Paragraph[]; readonly hyperlinks?: { diff --git a/src/file/file.ts b/src/file/file.ts index 1c655253de..2c351f3df3 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -106,14 +106,14 @@ export class File { this.styles = stylesFactory.newInstance(options.externalStyles); } else if (options.styles) { const stylesFactory = new DefaultStylesFactory(); - const defaultStyles = stylesFactory.newInstance(); + const defaultStyles = stylesFactory.newInstance(options.defaultStyles); this.styles = new Styles({ ...defaultStyles, ...options.styles, }); } else { const stylesFactory = new DefaultStylesFactory(); - this.styles = new Styles(stylesFactory.newInstance()); + this.styles = new Styles(stylesFactory.newInstance(options.defaultStyles)); } this.addDefaultRelationships(); diff --git a/src/file/styles/factory.ts b/src/file/styles/factory.ts index f5bca8248d..da4c268447 100644 --- a/src/file/styles/factory.ts +++ b/src/file/styles/factory.ts @@ -1,7 +1,7 @@ import { DocumentAttributes } from "../document/document-attributes"; import { IStylesOptions } from "./styles"; -import { DocumentDefaults } from "./defaults"; +import { DocumentDefaults, IDocumentDefaultsOptions } from "./defaults"; import { FootnoteReferenceStyle, FootnoteText, @@ -13,12 +13,30 @@ import { Heading5Style, Heading6Style, HyperlinkStyle, + IBaseCharacterStyleOptions, + IBaseParagraphStyleOptions, ListParagraph, TitleStyle, } from "./style"; +export interface IDefaultStylesOptions { + readonly document?: IDocumentDefaultsOptions; + readonly title?: IBaseParagraphStyleOptions; + readonly heading1?: IBaseParagraphStyleOptions; + readonly heading2?: IBaseParagraphStyleOptions; + readonly heading3?: IBaseParagraphStyleOptions; + readonly heading4?: IBaseParagraphStyleOptions; + readonly heading5?: IBaseParagraphStyleOptions; + readonly heading6?: IBaseParagraphStyleOptions; + readonly listParagraph?: IBaseParagraphStyleOptions; + readonly hyperlink?: IBaseCharacterStyleOptions; + readonly footnoteReference?: IBaseCharacterStyleOptions; + readonly footnoteText?: IBaseParagraphStyleOptions; + readonly footnoteTextChar?: IBaseCharacterStyleOptions; +} + export class DefaultStylesFactory { - public newInstance(): IStylesOptions { + public newInstance(options: IDefaultStylesOptions = {}): IStylesOptions { const documentAttributes = new DocumentAttributes({ mc: "http://schemas.openxmlformats.org/markup-compatibility/2006", r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships", @@ -30,51 +48,58 @@ export class DefaultStylesFactory { return { initialStyles: documentAttributes, importedStyles: [ - new DocumentDefaults(), + new DocumentDefaults(options.document), new TitleStyle({ run: { size: 56, }, + ...options.title, }), new Heading1Style({ run: { color: "2E74B5", size: 32, }, + ...options.heading1, }), new Heading2Style({ run: { color: "2E74B5", size: 26, }, + ...options.heading2, }), new Heading3Style({ run: { color: "1F4D78", size: 24, }, + ...options.heading3, }), new Heading4Style({ run: { color: "2E74B5", italics: true, }, + ...options.heading4, }), new Heading5Style({ run: { color: "2E74B5", }, + ...options.heading5, }), new Heading6Style({ run: { color: "1F4D78", }, + ...options.heading6, }), - new ListParagraph({}), - new HyperlinkStyle({}), - new FootnoteReferenceStyle({}), - new FootnoteText({}), - new FootnoteTextChar({}), + new ListParagraph(options.listParagraph || {}), + new HyperlinkStyle(options.hyperlink || {}), + new FootnoteReferenceStyle(options.footnoteReference || {}), + new FootnoteText(options.footnoteText || {}), + new FootnoteTextChar(options.footnoteTextChar || {}), ], }; } From f3ba62fd881fb84a4c9d3b7a201538f489fa7030 Mon Sep 17 00:00:00 2001 From: Tom Hunkapiller Date: Tue, 24 Nov 2020 13:36:43 -0600 Subject: [PATCH 2/3] bugfix path --- src/file/core-properties/properties.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/file/core-properties/properties.ts b/src/file/core-properties/properties.ts index 3fb3dbdc7c..a36828aca6 100644 --- a/src/file/core-properties/properties.ts +++ b/src/file/core-properties/properties.ts @@ -1,4 +1,4 @@ -import { IDefaultStylesOptions } from "docx/src/file/styles/factory"; +import { IDefaultStylesOptions } from "file/styles/factory"; import { XmlComponent } from "file/xml-components"; import { IDocumentBackgroundOptions } from "../document"; From 30ab92652c6b32a93587e6cac0d020bb37b158e6 Mon Sep 17 00:00:00 2001 From: Tom Hunkapiller Date: Mon, 30 Nov 2020 10:25:58 -0600 Subject: [PATCH 3/3] move file options.defaultStyles to options.styles.default --- demo/11-declaritive-styles-2.ts | 88 +++++++++++++------------- demo/2-declaritive-styles.ts | 72 ++++++++++----------- src/file/core-properties/properties.ts | 2 - src/file/file.ts | 4 +- src/file/styles/styles.ts | 3 +- 5 files changed, 84 insertions(+), 85 deletions(-) diff --git a/demo/11-declaritive-styles-2.ts b/demo/11-declaritive-styles-2.ts index 10bc93c17e..5fe9e16ae6 100644 --- a/demo/11-declaritive-styles-2.ts +++ b/demo/11-declaritive-styles-2.ts @@ -17,55 +17,55 @@ import { } from "../build"; const doc = new Document({ - defaultStyles: { - heading1: { - run: { - font: "Calibri", - size: 52, - bold: true, - color: "000000", - underline: { - type: UnderlineType.SINGLE, + styles: { + default: { + heading1: { + run: { + font: "Calibri", + size: 52, + bold: true, color: "000000", + underline: { + type: UnderlineType.SINGLE, + color: "000000", + }, + }, + paragraph: { + alignment: AlignmentType.CENTER, + spacing: { line: 340 }, }, }, - paragraph: { - alignment: AlignmentType.CENTER, - spacing: { line: 340 }, + heading2: { + run: { + font: "Calibri", + size: 26, + bold: true, + }, + paragraph: { + spacing: { line: 340 }, + }, + }, + heading3: { + run: { + font: "Calibri", + size: 26, + bold: true, + }, + paragraph: { + spacing: { line: 276 }, + }, + }, + heading4: { + run: { + font: "Calibri", + size: 26, + bold: true, + }, + paragraph: { + alignment: AlignmentType.JUSTIFIED, + }, }, }, - heading2: { - run: { - font: "Calibri", - size: 26, - bold: true, - }, - paragraph: { - spacing: { line: 340 }, - }, - }, - heading3: { - run: { - font: "Calibri", - size: 26, - bold: true, - }, - paragraph: { - spacing: { line: 276 }, - }, - }, - heading4: { - run: { - font: "Calibri", - size: 26, - bold: true, - }, - paragraph: { - alignment: AlignmentType.JUSTIFIED, - }, - }, - }, - styles: { paragraphStyles: [ { id: "normalPara", diff --git a/demo/2-declaritive-styles.ts b/demo/2-declaritive-styles.ts index f01877a8d5..6f3add1f19 100644 --- a/demo/2-declaritive-styles.ts +++ b/demo/2-declaritive-styles.ts @@ -7,43 +7,43 @@ const doc = new Document({ creator: "Clippy", title: "Sample Document", description: "A brief example of using docx", - defaultStyles: { - heading1: { - run: { - size: 28, - bold: true, - italics: true, - color: "red", - }, - paragraph: { - spacing: { - after: 120, - }, - }, - }, - heading2: { - run: { - size: 26, - bold: true, - underline: { - type: UnderlineType.DOUBLE, - color: "FF0000", - }, - }, - paragraph: { - spacing: { - before: 240, - after: 120, - }, - }, - }, - listParagraph: { - run: { - color: '#FF0000' - } - } - }, styles: { + default: { + heading1: { + run: { + size: 28, + bold: true, + italics: true, + color: "red", + }, + paragraph: { + spacing: { + after: 120, + }, + }, + }, + heading2: { + run: { + size: 26, + bold: true, + underline: { + type: UnderlineType.DOUBLE, + color: "FF0000", + }, + }, + paragraph: { + spacing: { + before: 240, + after: 120, + }, + }, + }, + listParagraph: { + run: { + color: '#FF0000' + } + } + }, paragraphStyles: [ { id: "aside", diff --git a/src/file/core-properties/properties.ts b/src/file/core-properties/properties.ts index a36828aca6..ae9f227ad5 100644 --- a/src/file/core-properties/properties.ts +++ b/src/file/core-properties/properties.ts @@ -1,4 +1,3 @@ -import { IDefaultStylesOptions } from "file/styles/factory"; import { XmlComponent } from "file/xml-components"; import { IDocumentBackgroundOptions } from "../document"; @@ -29,7 +28,6 @@ export interface IPropertiesOptions { readonly revision?: string; readonly externalStyles?: string; readonly styles?: IStylesOptions; - readonly defaultStyles?: IDefaultStylesOptions; readonly numbering?: INumberingOptions; readonly footnotes?: Paragraph[]; readonly hyperlinks?: { diff --git a/src/file/file.ts b/src/file/file.ts index 2c351f3df3..90986d16d7 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -106,14 +106,14 @@ export class File { this.styles = stylesFactory.newInstance(options.externalStyles); } else if (options.styles) { const stylesFactory = new DefaultStylesFactory(); - const defaultStyles = stylesFactory.newInstance(options.defaultStyles); + const defaultStyles = stylesFactory.newInstance(options.styles.default); this.styles = new Styles({ ...defaultStyles, ...options.styles, }); } else { const stylesFactory = new DefaultStylesFactory(); - this.styles = new Styles(stylesFactory.newInstance(options.defaultStyles)); + this.styles = new Styles(stylesFactory.newInstance()); } this.addDefaultRelationships(); diff --git a/src/file/styles/styles.ts b/src/file/styles/styles.ts index 1fbc4b5e50..1906b45ba0 100644 --- a/src/file/styles/styles.ts +++ b/src/file/styles/styles.ts @@ -1,11 +1,12 @@ +import { IDefaultStylesOptions } from "file/styles/factory"; import { BaseXmlComponent, ImportedXmlComponent, XmlComponent } from "file/xml-components"; - import { StyleForCharacter, StyleForParagraph } from "./style"; import { ICharacterStyleOptions } from "./style/character-style"; import { IParagraphStyleOptions } from "./style/paragraph-style"; export * from "./border"; export interface IStylesOptions { + readonly default?: IDefaultStylesOptions; readonly initialStyles?: BaseXmlComponent; readonly paragraphStyles?: IParagraphStyleOptions[]; readonly characterStyles?: ICharacterStyleOptions[];