Add defaultStyles option to overwrite the core default styles (Heading 1-6, document, hyperlinks, etc)
This commit is contained in:
@ -17,74 +17,56 @@ import {
|
|||||||
} from "../build";
|
} from "../build";
|
||||||
|
|
||||||
const doc = new Document({
|
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: {
|
styles: {
|
||||||
paragraphStyles: [
|
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",
|
id: "normalPara",
|
||||||
name: "Normal Para",
|
name: "Normal Para",
|
||||||
@ -139,12 +121,6 @@ const doc = new Document({
|
|||||||
spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 },
|
spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
id: "ListParagraph",
|
|
||||||
name: "List Paragraph",
|
|
||||||
basedOn: "Normal",
|
|
||||||
quickFormat: true,
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -7,47 +7,44 @@ 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",
|
||||||
|
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: {
|
styles: {
|
||||||
paragraphStyles: [
|
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",
|
id: "aside",
|
||||||
name: "Aside",
|
name: "Aside",
|
||||||
@ -75,12 +72,6 @@ const doc = new Document({
|
|||||||
spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 },
|
spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
id: "ListParagraph",
|
|
||||||
name: "List Paragraph",
|
|
||||||
basedOn: "Normal",
|
|
||||||
quickFormat: true,
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
numbering: {
|
numbering: {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { IDefaultStylesOptions } from "docx/src/file/styles/factory";
|
||||||
import { XmlComponent } from "file/xml-components";
|
import { XmlComponent } from "file/xml-components";
|
||||||
import { IDocumentBackgroundOptions } from "../document";
|
import { IDocumentBackgroundOptions } from "../document";
|
||||||
|
|
||||||
@ -28,6 +29,7 @@ export interface IPropertiesOptions {
|
|||||||
readonly revision?: string;
|
readonly revision?: string;
|
||||||
readonly externalStyles?: string;
|
readonly externalStyles?: string;
|
||||||
readonly styles?: IStylesOptions;
|
readonly styles?: IStylesOptions;
|
||||||
|
readonly defaultStyles?: IDefaultStylesOptions;
|
||||||
readonly numbering?: INumberingOptions;
|
readonly numbering?: INumberingOptions;
|
||||||
readonly footnotes?: Paragraph[];
|
readonly footnotes?: Paragraph[];
|
||||||
readonly hyperlinks?: {
|
readonly hyperlinks?: {
|
||||||
|
@ -106,14 +106,14 @@ export class File {
|
|||||||
this.styles = stylesFactory.newInstance(options.externalStyles);
|
this.styles = stylesFactory.newInstance(options.externalStyles);
|
||||||
} else if (options.styles) {
|
} else if (options.styles) {
|
||||||
const stylesFactory = new DefaultStylesFactory();
|
const stylesFactory = new DefaultStylesFactory();
|
||||||
const defaultStyles = stylesFactory.newInstance();
|
const defaultStyles = stylesFactory.newInstance(options.defaultStyles);
|
||||||
this.styles = new Styles({
|
this.styles = new Styles({
|
||||||
...defaultStyles,
|
...defaultStyles,
|
||||||
...options.styles,
|
...options.styles,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const stylesFactory = new DefaultStylesFactory();
|
const stylesFactory = new DefaultStylesFactory();
|
||||||
this.styles = new Styles(stylesFactory.newInstance());
|
this.styles = new Styles(stylesFactory.newInstance(options.defaultStyles));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.addDefaultRelationships();
|
this.addDefaultRelationships();
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { DocumentAttributes } from "../document/document-attributes";
|
import { DocumentAttributes } from "../document/document-attributes";
|
||||||
import { IStylesOptions } from "./styles";
|
import { IStylesOptions } from "./styles";
|
||||||
|
|
||||||
import { DocumentDefaults } from "./defaults";
|
import { DocumentDefaults, IDocumentDefaultsOptions } from "./defaults";
|
||||||
import {
|
import {
|
||||||
FootnoteReferenceStyle,
|
FootnoteReferenceStyle,
|
||||||
FootnoteText,
|
FootnoteText,
|
||||||
@ -13,12 +13,30 @@ import {
|
|||||||
Heading5Style,
|
Heading5Style,
|
||||||
Heading6Style,
|
Heading6Style,
|
||||||
HyperlinkStyle,
|
HyperlinkStyle,
|
||||||
|
IBaseCharacterStyleOptions,
|
||||||
|
IBaseParagraphStyleOptions,
|
||||||
ListParagraph,
|
ListParagraph,
|
||||||
TitleStyle,
|
TitleStyle,
|
||||||
} from "./style";
|
} 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 {
|
export class DefaultStylesFactory {
|
||||||
public newInstance(): IStylesOptions {
|
public newInstance(options: IDefaultStylesOptions = {}): 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",
|
||||||
@ -30,51 +48,58 @@ export class DefaultStylesFactory {
|
|||||||
return {
|
return {
|
||||||
initialStyles: documentAttributes,
|
initialStyles: documentAttributes,
|
||||||
importedStyles: [
|
importedStyles: [
|
||||||
new DocumentDefaults(),
|
new DocumentDefaults(options.document),
|
||||||
new TitleStyle({
|
new TitleStyle({
|
||||||
run: {
|
run: {
|
||||||
size: 56,
|
size: 56,
|
||||||
},
|
},
|
||||||
|
...options.title,
|
||||||
}),
|
}),
|
||||||
new Heading1Style({
|
new Heading1Style({
|
||||||
run: {
|
run: {
|
||||||
color: "2E74B5",
|
color: "2E74B5",
|
||||||
size: 32,
|
size: 32,
|
||||||
},
|
},
|
||||||
|
...options.heading1,
|
||||||
}),
|
}),
|
||||||
new Heading2Style({
|
new Heading2Style({
|
||||||
run: {
|
run: {
|
||||||
color: "2E74B5",
|
color: "2E74B5",
|
||||||
size: 26,
|
size: 26,
|
||||||
},
|
},
|
||||||
|
...options.heading2,
|
||||||
}),
|
}),
|
||||||
new Heading3Style({
|
new Heading3Style({
|
||||||
run: {
|
run: {
|
||||||
color: "1F4D78",
|
color: "1F4D78",
|
||||||
size: 24,
|
size: 24,
|
||||||
},
|
},
|
||||||
|
...options.heading3,
|
||||||
}),
|
}),
|
||||||
new Heading4Style({
|
new Heading4Style({
|
||||||
run: {
|
run: {
|
||||||
color: "2E74B5",
|
color: "2E74B5",
|
||||||
italics: true,
|
italics: true,
|
||||||
},
|
},
|
||||||
|
...options.heading4,
|
||||||
}),
|
}),
|
||||||
new Heading5Style({
|
new Heading5Style({
|
||||||
run: {
|
run: {
|
||||||
color: "2E74B5",
|
color: "2E74B5",
|
||||||
},
|
},
|
||||||
|
...options.heading5,
|
||||||
}),
|
}),
|
||||||
new Heading6Style({
|
new Heading6Style({
|
||||||
run: {
|
run: {
|
||||||
color: "1F4D78",
|
color: "1F4D78",
|
||||||
},
|
},
|
||||||
|
...options.heading6,
|
||||||
}),
|
}),
|
||||||
new ListParagraph({}),
|
new ListParagraph(options.listParagraph || {}),
|
||||||
new HyperlinkStyle({}),
|
new HyperlinkStyle(options.hyperlink || {}),
|
||||||
new FootnoteReferenceStyle({}),
|
new FootnoteReferenceStyle(options.footnoteReference || {}),
|
||||||
new FootnoteText({}),
|
new FootnoteText(options.footnoteText || {}),
|
||||||
new FootnoteTextChar({}),
|
new FootnoteTextChar(options.footnoteTextChar || {}),
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user