Declarative styles

This commit is contained in:
Dolan
2019-10-04 01:20:41 +01:00
parent 2536fbe752
commit 591b2f4e04
20 changed files with 920 additions and 484 deletions

View File

@ -1,48 +1,89 @@
// 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, TextRun } from "../build"; import { Document, HeadingLevel, Packer, Paragraph, Styles, 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({
paragraphStyles: [
{
id: "Heading1",
name: "Heading 1",
basedOn: "Normal",
next: "Normal",
quickFormat: true,
run: {
size: 28,
bold: true,
italics: true,
},
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",
basedOn: "Normal",
next: "Normal",
run: {
color: "999999",
italics: true,
},
paragraph: {
indent: {
left: 720,
},
spacing: {
line: 276,
},
},
},
{
id: "wellSpaced",
name: "Well Spaced",
basedOn: "Normal",
quickFormat: true,
paragraph: {
spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 },
},
},
{
id: "ListParagraph",
name: "List Paragraph",
basedOn: "Normal",
quickFormat: true,
},
],
}),
}); });
doc.Styles.createParagraphStyle("Heading1", "Heading 1")
.basedOn("Normal")
.next("Normal")
.quickFormat()
.size(28)
.bold()
.italics()
.spacing({ after: 120 });
doc.Styles.createParagraphStyle("Heading2", "Heading 2")
.basedOn("Normal")
.next("Normal")
.quickFormat()
.size(26)
.bold()
.underline("double", "FF0000")
.spacing({ before: 240, after: 120 });
doc.Styles.createParagraphStyle("aside", "Aside")
.basedOn("Normal")
.next("Normal")
.color("999999")
.italics()
.indent({ left: 720 })
.spacing({ line: 276 });
doc.Styles.createParagraphStyle("wellSpaced", "Well Spaced")
.basedOn("Normal")
.spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 });
doc.Styles.createParagraphStyle("ListParagraph", "List Paragraph")
.quickFormat()
.basedOn("Normal");
const numberedAbstract = doc.Numbering.createAbstractNumbering(); const numberedAbstract = doc.Numbering.createAbstractNumbering();
numberedAbstract.createLevel(0, "lowerLetter", "%1)", "left"); numberedAbstract.createLevel(0, "lowerLetter", "%1)", "left");

View File

@ -1,5 +1,6 @@
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 { 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 {
@ -11,6 +12,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;
} }
export class CoreProperties extends XmlComponent { export class CoreProperties extends XmlComponent {

View File

@ -46,8 +46,6 @@ export interface ISectionOptions {
export class File { export class File {
// tslint:disable-next-line:readonly-keyword // tslint:disable-next-line:readonly-keyword
private currentRelationshipId: number = 1; private currentRelationshipId: number = 1;
// tslint:disable-next-line:readonly-keyword
private styles: Styles;
private readonly document: Document; private readonly document: Document;
private readonly headers: IDocumentHeader[] = []; private readonly headers: IDocumentHeader[] = [];
@ -61,6 +59,7 @@ export class File {
private readonly settings: Settings; private readonly settings: Settings;
private readonly contentTypes: ContentTypes; private readonly contentTypes: ContentTypes;
private readonly appProperties: AppProperties; private readonly appProperties: AppProperties;
private readonly styles: Styles;
constructor( constructor(
options: IPropertiesOptions = { options: IPropertiesOptions = {
@ -97,6 +96,8 @@ export class File {
} else if (options.externalStyles) { } else if (options.externalStyles) {
const stylesFactory = new ExternalStylesFactory(); const stylesFactory = new ExternalStylesFactory();
this.styles = stylesFactory.newInstance(options.externalStyles); this.styles = stylesFactory.newInstance(options.externalStyles);
} else if (options.styles) {
this.styles = options.styles;
} else { } else {
const stylesFactory = new DefaultStylesFactory(); const stylesFactory = new DefaultStylesFactory();
this.styles = stylesFactory.newInstance(); this.styles = stylesFactory.newInstance();
@ -277,10 +278,6 @@ export class File {
return this.styles; return this.styles;
} }
public set Styles(styles: Styles) {
this.styles = styles;
}
public get CoreProperties(): CoreProperties { public get CoreProperties(): CoreProperties {
return this.coreProperties; return this.coreProperties;
} }

View File

@ -10,12 +10,12 @@ import {
LeftTabStop, LeftTabStop,
RightTabStop, RightTabStop,
Spacing, Spacing,
TabStopPosition,
ThematicBreak, ThematicBreak,
} from "../paragraph/formatting"; } from "../paragraph/formatting";
import { ParagraphProperties } from "../paragraph/properties"; import { ParagraphProperties } from "../paragraph/properties";
import * as formatting from "../paragraph/run/formatting"; import * as formatting from "../paragraph/run/formatting";
import { RunProperties } from "../paragraph/run/properties"; import { RunProperties } from "../paragraph/run/properties";
import { UnderlineType } from "../paragraph/run/underline";
interface ILevelAttributesProperties { interface ILevelAttributesProperties {
readonly ilvl?: number; readonly ilvl?: number;
@ -185,7 +185,7 @@ export class LevelBase extends XmlComponent {
return this; return this;
} }
public underline(underlineType?: string, color?: string): Level { public underline(underlineType?: UnderlineType, color?: string): Level {
this.addRunProperty(new formatting.Underline(underlineType, color)); this.addRunProperty(new formatting.Underline(underlineType, color));
return this; return this;
} }

View File

@ -9,6 +9,7 @@ import { Numbering } from "./numbering";
import { EMPTY_OBJECT } from "file/xml-components"; import { EMPTY_OBJECT } from "file/xml-components";
import { TabStopPosition } from "../paragraph"; import { TabStopPosition } from "../paragraph";
import { UnderlineType } from "../paragraph/run/underline";
describe("Numbering", () => { describe("Numbering", () => {
let numbering: Numbering; let numbering: Numbering;
@ -356,7 +357,7 @@ describe("AbstractNumbering", () => {
it("should set the style if given", () => { it("should set the style if given", () => {
const abstractNumbering = new AbstractNumbering(1); const abstractNumbering = new AbstractNumbering(1);
const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.").underline("double"); const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.").underline(UnderlineType.DOUBLE);
const tree = new Formatter().format(level); const tree = new Formatter().format(level);
expect(tree["w:lvl"]).to.include({ expect(tree["w:lvl"]).to.include({
"w:rPr": [{ "w:u": { _attr: { "w:val": "double" } } }], "w:rPr": [{ "w:u": { _attr: { "w:val": "double" } } }],
@ -365,7 +366,7 @@ describe("AbstractNumbering", () => {
it("should set the style and color if given", () => { it("should set the style and color if given", () => {
const abstractNumbering = new AbstractNumbering(1); const abstractNumbering = new AbstractNumbering(1);
const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.").underline("double", "005599"); const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.").underline(UnderlineType.DOUBLE, "005599");
const tree = new Formatter().format(level); const tree = new Formatter().format(level);
expect(tree["w:lvl"]).to.include({ expect(tree["w:lvl"]).to.include({
"w:rPr": [{ "w:u": { _attr: { "w:val": "double", "w:color": "005599" } } }], "w:rPr": [{ "w:u": { _attr: { "w:val": "double", "w:color": "005599" } } }],

View File

@ -4,3 +4,4 @@ export * from "./symbol-run";
export * from "./picture-run"; export * from "./picture-run";
export * from "./run-fonts"; export * from "./run-fonts";
export * from "./sequential-identifier"; export * from "./sequential-identifier";
export * from "./underline";

View File

@ -27,7 +27,7 @@ describe("Underline", () => {
}); });
it("should use the given style type and color", () => { it("should use the given style type and color", () => {
const underline = new u.Underline("double", "FF00CC"); const underline = new u.Underline(u.UnderlineType.DOUBLE, "FF00CC");
const tree = new Formatter().format(underline); const tree = new Formatter().format(underline);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:u": { _attr: { "w:val": "double", "w:color": "FF00CC" } }, "w:u": { _attr: { "w:val": "double", "w:color": "FF00CC" } },

View File

@ -33,7 +33,7 @@ export abstract class BaseUnderline extends XmlComponent {
} }
export class Underline extends BaseUnderline { export class Underline extends BaseUnderline {
constructor(underlineType: string = "single", color?: string) { constructor(underlineType: UnderlineType = UnderlineType.SINGLE, color?: string) {
super(underlineType, color); super(underlineType, color);
} }
} }

View File

@ -38,11 +38,13 @@ export class ExternalStylesFactory {
throw new Error("can not find styles element"); throw new Error("can not find styles element");
} }
const importedStyle = new Styles(new ImportedRootElementAttributes(stylesXmlElement.attributes));
const stylesElements = stylesXmlElement.elements || []; const stylesElements = stylesXmlElement.elements || [];
for (const childElm of stylesElements) {
importedStyle.push(convertToXmlComponent(childElm) as ImportedXmlComponent); const importedStyle = new Styles({
} initialStyles: new ImportedRootElementAttributes(stylesXmlElement.attributes),
importedStyles: stylesElements.map((childElm) => convertToXmlComponent(childElm) as ImportedXmlComponent),
});
return importedStyle; return importedStyle;
} }
} }

View File

@ -1,7 +1,7 @@
import { DocumentAttributes } from "../document/document-attributes"; import { DocumentAttributes } from "../document/document-attributes";
import { Color, Italics, Size } from "../paragraph/run/formatting"; import { Styles } from "./styles";
import { Styles } from "./";
import { DocumentDefaults } from "./defaults";
import { import {
FootnoteReferenceStyle, FootnoteReferenceStyle,
FootnoteText, FootnoteText,
@ -27,57 +27,58 @@ 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(documentAttributes); const styles = new Styles({
initialStyles: documentAttributes,
importedStyles: [
new DocumentDefaults(),
new TitleStyle({
run: {
size: 56,
},
}),
new Heading1Style({
run: {
color: "2E74B5",
size: 32,
},
}),
new Heading2Style({
run: {
color: "2E74B5",
size: 26,
},
}),
new Heading3Style({
run: {
color: "1F4D78",
size: 24,
},
}),
new Heading4Style({
run: {
color: "2E74B5",
italics: true,
},
}),
new Heading5Style({
run: {
color: "2E74B5",
},
}),
new Heading6Style({
run: {
color: "1F4D78",
},
}),
new ListParagraph({}),
new HyperlinkStyle({}),
new FootnoteReferenceStyle({}),
new FootnoteText({}),
new FootnoteTextChar({}),
],
});
styles.createDocumentDefaults(); styles.createDocumentDefaults();
const titleStyle = new TitleStyle();
titleStyle.addRunProperty(new Size(56));
styles.push(titleStyle);
const heading1Style = new Heading1Style();
heading1Style.addRunProperty(new Color("2E74B5"));
heading1Style.addRunProperty(new Size(32));
styles.push(heading1Style);
const heading2Style = new Heading2Style();
heading2Style.addRunProperty(new Color("2E74B5"));
heading2Style.addRunProperty(new Size(26));
styles.push(heading2Style);
const heading3Style = new Heading3Style();
heading3Style.addRunProperty(new Color("1F4D78"));
heading3Style.addRunProperty(new Size(24));
styles.push(heading3Style);
const heading4Style = new Heading4Style();
heading4Style.addRunProperty(new Color("2E74B5"));
heading4Style.addRunProperty(new Italics());
styles.push(heading4Style);
const heading5Style = new Heading5Style();
heading5Style.addRunProperty(new Color("2E74B5"));
styles.push(heading5Style);
const heading6Style = new Heading6Style();
heading6Style.addRunProperty(new Color("1F4D78"));
styles.push(heading6Style);
const listParagraph = new ListParagraph();
// listParagraph.addParagraphProperty();
styles.push(listParagraph);
const hyperLinkStyle = new HyperlinkStyle();
styles.push(hyperLinkStyle);
const footnoteReferenceStyle = new FootnoteReferenceStyle();
styles.push(footnoteReferenceStyle);
const footnoteTextStyle = new FootnoteText();
styles.push(footnoteTextStyle);
const footnoteTextCharStyle = new FootnoteTextChar();
styles.push(footnoteTextCharStyle);
return styles; return styles;
} }
} }

View File

@ -1,15 +1,16 @@
import { expect } from "chai"; import { expect } from "chai";
import { Formatter } from "export/formatter"; import { Formatter } from "export/formatter";
import { UnderlineType } from "file/paragraph/run/underline";
import { ShadingType } from "file/table";
import { EMPTY_OBJECT } from "file/xml-components";
import { CharacterStyle } from "./character-style"; import { CharacterStyle } from "./character-style";
import { EMPTY_OBJECT } from "file/xml-components";
describe("CharacterStyle", () => { describe("CharacterStyle", () => {
describe("#constructor", () => { describe("#constructor", () => {
it("should set the style type to character and use the given style id", () => { it("should set the style type to character and use the given style id", () => {
const style = new CharacterStyle("myStyleId"); const style = new CharacterStyle({ id: "myStyleId" });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -17,7 +18,7 @@ describe("CharacterStyle", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -29,7 +30,10 @@ describe("CharacterStyle", () => {
}); });
it("should set the name of the style, if given", () => { it("should set the name of the style, if given", () => {
const style = new CharacterStyle("myStyleId", "Style Name"); const style = new CharacterStyle({
id: "myStyleId",
name: "Style Name",
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -38,7 +42,7 @@ describe("CharacterStyle", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -52,7 +56,7 @@ describe("CharacterStyle", () => {
describe("formatting methods: style attributes", () => { describe("formatting methods: style attributes", () => {
it("#basedOn", () => { it("#basedOn", () => {
const style = new CharacterStyle("myStyleId").basedOn("otherId"); const style = new CharacterStyle({ id: "myStyleId", basedOn: "otherId" });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -60,7 +64,7 @@ describe("CharacterStyle", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -75,7 +79,12 @@ describe("CharacterStyle", () => {
describe("formatting methods: run properties", () => { describe("formatting methods: run properties", () => {
it("#size", () => { it("#size", () => {
const style = new CharacterStyle("myStyleId").size(24); const style = new CharacterStyle({
id: "myStyleId",
run: {
size: 24,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -86,7 +95,7 @@ describe("CharacterStyle", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -99,7 +108,12 @@ describe("CharacterStyle", () => {
describe("#underline", () => { describe("#underline", () => {
it("should set underline to 'single' if no arguments are given", () => { it("should set underline to 'single' if no arguments are given", () => {
const style = new CharacterStyle("myStyleId").underline(); const style = new CharacterStyle({
id: "myStyleId",
run: {
underline: {},
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -110,7 +124,7 @@ describe("CharacterStyle", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -122,7 +136,14 @@ describe("CharacterStyle", () => {
}); });
it("should set the style if given", () => { it("should set the style if given", () => {
const style = new CharacterStyle("myStyleId").underline("double"); const style = new CharacterStyle({
id: "myStyleId",
run: {
underline: {
type: UnderlineType.DOUBLE,
},
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -133,7 +154,7 @@ describe("CharacterStyle", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -145,7 +166,15 @@ describe("CharacterStyle", () => {
}); });
it("should set the style and color if given", () => { it("should set the style and color if given", () => {
const style = new CharacterStyle("myStyleId").underline("double", "005599"); const style = new CharacterStyle({
id: "myStyleId",
run: {
underline: {
type: UnderlineType.DOUBLE,
color: "005599",
},
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -156,7 +185,7 @@ describe("CharacterStyle", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -169,7 +198,12 @@ describe("CharacterStyle", () => {
}); });
it("#superScript", () => { it("#superScript", () => {
const style = new CharacterStyle("myStyleId").superScript(); const style = new CharacterStyle({
id: "myStyleId",
run: {
superScript: true,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -188,7 +222,7 @@ describe("CharacterStyle", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -200,7 +234,12 @@ describe("CharacterStyle", () => {
}); });
it("#color", () => { it("#color", () => {
const style = new CharacterStyle("myStyleId").color("123456"); const style = new CharacterStyle({
id: "myStyleId",
run: {
color: "123456",
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -211,7 +250,7 @@ describe("CharacterStyle", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -223,7 +262,12 @@ describe("CharacterStyle", () => {
}); });
it("#bold", () => { it("#bold", () => {
const style = new CharacterStyle("myStyleId").bold(); const style = new CharacterStyle({
id: "myStyleId",
run: {
bold: true,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -234,7 +278,7 @@ describe("CharacterStyle", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -246,7 +290,12 @@ describe("CharacterStyle", () => {
}); });
it("#italics", () => { it("#italics", () => {
const style = new CharacterStyle("myStyleId").italics(); const style = new CharacterStyle({
id: "myStyleId",
run: {
italics: true,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -257,7 +306,7 @@ describe("CharacterStyle", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -269,7 +318,7 @@ describe("CharacterStyle", () => {
}); });
it("#link", () => { it("#link", () => {
const style = new CharacterStyle("myStyleId").link("MyLink"); const style = new CharacterStyle({ id: "myStyleId", link: "MyLink" });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -277,7 +326,7 @@ describe("CharacterStyle", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -290,7 +339,7 @@ describe("CharacterStyle", () => {
}); });
it("#semiHidden", () => { it("#semiHidden", () => {
const style = new CharacterStyle("myStyleId").semiHidden(); const style = new CharacterStyle({ id: "myStyleId", semiHidden: true });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -298,7 +347,7 @@ describe("CharacterStyle", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -309,7 +358,12 @@ describe("CharacterStyle", () => {
}); });
it("#highlight", () => { it("#highlight", () => {
const style = new CharacterStyle("myStyleId").highlight("005599"); const style = new CharacterStyle({
id: "myStyleId",
run: {
highlight: "005599",
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -320,7 +374,7 @@ describe("CharacterStyle", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -332,7 +386,16 @@ describe("CharacterStyle", () => {
}); });
it("#shadow", () => { it("#shadow", () => {
const style = new CharacterStyle("myStyleId").shadow("pct10", "00FFFF", "FF0000"); const style = new CharacterStyle({
id: "myStyleId",
run: {
shadow: {
type: ShadingType.PERCENT_10,
fill: "00FFFF",
color: "FF0000",
},
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -343,7 +406,7 @@ describe("CharacterStyle", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },

View File

@ -1,69 +1,128 @@
import * as formatting from "file/paragraph/run/formatting"; import * as formatting from "file/paragraph/run/formatting";
import { RunProperties } from "file/paragraph/run/properties"; import { RunProperties } from "file/paragraph/run/properties";
import { XmlComponent } from "file/xml-components"; import { UnderlineType } from "file/paragraph/run/underline";
import { BasedOn, Link, SemiHidden, UiPriority, UnhideWhenUsed } from "./components"; import { BasedOn, Link, SemiHidden, UiPriority, UnhideWhenUsed } from "./components";
import { Style } from "./style"; import { Style } from "./style";
export interface IBaseCharacterStyleOptions {
readonly basedOn?: string;
readonly link?: string;
readonly semiHidden?: boolean;
readonly run?: {
readonly size?: number;
readonly bold?: boolean;
readonly italics?: boolean;
readonly smallCaps?: boolean;
readonly allCaps?: boolean;
readonly strike?: boolean;
readonly doubleStrike?: boolean;
readonly subScript?: boolean;
readonly superScript?: boolean;
readonly underline?: {
readonly type?: UnderlineType;
readonly color?: string;
};
readonly color?: string;
readonly font?: string;
readonly characterSpacing?: number;
readonly highlight?: string;
readonly shadow?: {
readonly type: string;
readonly fill: string;
readonly color: string;
};
};
}
export interface ICharacterStyleOptions extends IBaseCharacterStyleOptions {
readonly id: string;
readonly name?: string;
}
export class CharacterStyle extends Style { export class CharacterStyle extends Style {
private readonly runProperties: RunProperties; private readonly runProperties: RunProperties;
constructor(styleId: string, name?: string) { constructor(options: ICharacterStyleOptions) {
super({ type: "character", styleId: styleId }, name); super({ type: "character", styleId: options.id }, options.name);
this.runProperties = new RunProperties(); this.runProperties = new RunProperties();
this.root.push(this.runProperties); this.root.push(this.runProperties);
this.root.push(new UiPriority("99")); this.root.push(new UiPriority(99));
this.root.push(new UnhideWhenUsed()); this.root.push(new UnhideWhenUsed());
}
public basedOn(parentId: string): CharacterStyle { if (options.basedOn) {
this.root.push(new BasedOn(parentId)); this.root.push(new BasedOn(options.basedOn));
return this; }
}
public addRunProperty(property: XmlComponent): CharacterStyle { if (options.link) {
this.runProperties.push(property); this.root.push(new Link(options.link));
return this; }
}
public color(color: string): CharacterStyle { if (options.semiHidden) {
return this.addRunProperty(new formatting.Color(color)); this.root.push(new SemiHidden());
} }
public bold(): CharacterStyle { if (options.run) {
return this.addRunProperty(new formatting.Bold()); if (options.run.size) {
} this.runProperties.push(new formatting.Size(options.run.size));
this.runProperties.push(new formatting.SizeComplexScript(options.run.size));
}
public italics(): CharacterStyle { if (options.run.bold) {
return this.addRunProperty(new formatting.Italics()); this.runProperties.push(new formatting.Bold());
} }
public underline(underlineType?: string, color?: string): CharacterStyle { if (options.run.italics) {
return this.addRunProperty(new formatting.Underline(underlineType, color)); this.runProperties.push(new formatting.Italics());
} }
public superScript(): CharacterStyle { if (options.run.smallCaps) {
return this.addRunProperty(new formatting.SuperScript()); this.runProperties.push(new formatting.SmallCaps());
} }
public size(twips: number): CharacterStyle { if (options.run.allCaps) {
return this.addRunProperty(new formatting.Size(twips)).addRunProperty(new formatting.SizeComplexScript(twips)); this.runProperties.push(new formatting.Caps());
} }
public link(link: string): CharacterStyle { if (options.run.strike) {
this.root.push(new Link(link)); this.runProperties.push(new formatting.Strike());
return this; }
}
public semiHidden(): CharacterStyle { if (options.run.doubleStrike) {
this.root.push(new SemiHidden()); this.runProperties.push(new formatting.DoubleStrike());
return this; }
}
public highlight(color: string): CharacterStyle { if (options.run.subScript) {
return this.addRunProperty(new formatting.Highlight(color)); this.runProperties.push(new formatting.SubScript());
} }
public shadow(value: string, fill: string, color: string): CharacterStyle { if (options.run.superScript) {
return this.addRunProperty(new formatting.Shading(value, fill, color)); this.runProperties.push(new formatting.SuperScript());
}
if (options.run.underline) {
this.runProperties.push(new formatting.Underline(options.run.underline.type, options.run.underline.color));
}
if (options.run.color) {
this.runProperties.push(new formatting.Color(options.run.color));
}
if (options.run.font) {
this.runProperties.push(new formatting.RunFonts(options.run.font));
}
if (options.run.characterSpacing) {
this.runProperties.push(new formatting.CharacterSpacing(options.run.characterSpacing));
}
if (options.run.highlight) {
this.runProperties.push(new formatting.Highlight(options.run.highlight));
}
if (options.run.shadow) {
this.runProperties.push(new formatting.Shading(options.run.shadow.type, options.run.shadow.fill, options.run.shadow.color));
}
}
} }
} }

View File

@ -30,9 +30,9 @@ describe("Style components", () => {
}); });
it("UiPriority#constructor", () => { it("UiPriority#constructor", () => {
const style = new components.UiPriority("123"); const style = new components.UiPriority(123);
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ "w:uiPriority": { _attr: { "w:val": "123" } } }); expect(tree).to.deep.equal({ "w:uiPriority": { _attr: { "w:val": 123 } } });
}); });
it("UnhideWhenUsed#constructor", () => { it("UnhideWhenUsed#constructor", () => {

View File

@ -1,7 +1,8 @@
// http://officeopenxml.com/WPstyleGenProps.php
import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
interface IComponentAttributes { interface IComponentAttributes {
readonly val: string; readonly val: string | number;
} }
class ComponentAttributes extends XmlAttributeComponent<IComponentAttributes> { class ComponentAttributes extends XmlAttributeComponent<IComponentAttributes> {
@ -37,7 +38,7 @@ export class Link extends XmlComponent {
} }
export class UiPriority extends XmlComponent { export class UiPriority extends XmlComponent {
constructor(value: string) { constructor(value: number) {
super("w:uiPriority"); super("w:uiPriority");
// TODO: this value should be a ST_DecimalNumber // TODO: this value should be a ST_DecimalNumber
this.root.push(new ComponentAttributes({ val: value })); this.root.push(new ComponentAttributes({ val: value }));

View File

@ -1,12 +1,15 @@
import { expect } from "chai"; import { expect } from "chai";
import { Formatter } from "export/formatter"; import { Formatter } from "export/formatter";
import * as defaultStyels from "./default-styles"; import * as defaultStyles from "./default-styles";
import { EMPTY_OBJECT } from "file/xml-components"; import { EMPTY_OBJECT } from "file/xml-components";
describe("Default Styles", () => { describe("Default Styles", () => {
it("HeadingStyle#constructor", () => { it("HeadingStyle#constructor", () => {
const style = new defaultStyels.HeadingStyle("Heading1", "Heading 1"); const style = new defaultStyles.HeadingStyle({
id: "Heading1",
name: "Heading 1",
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -20,7 +23,7 @@ describe("Default Styles", () => {
}); });
it("TitleStyle#constructor", () => { it("TitleStyle#constructor", () => {
const style = new defaultStyels.TitleStyle(); const style = new defaultStyles.TitleStyle({});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -34,7 +37,7 @@ describe("Default Styles", () => {
}); });
it("Heading1Style#constructor", () => { it("Heading1Style#constructor", () => {
const style = new defaultStyels.Heading1Style(); const style = new defaultStyles.Heading1Style({});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -48,7 +51,7 @@ describe("Default Styles", () => {
}); });
it("Heading2Style#constructor", () => { it("Heading2Style#constructor", () => {
const style = new defaultStyels.Heading2Style(); const style = new defaultStyles.Heading2Style({});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -62,7 +65,7 @@ describe("Default Styles", () => {
}); });
it("Heading3Style#constructor", () => { it("Heading3Style#constructor", () => {
const style = new defaultStyels.Heading3Style(); const style = new defaultStyles.Heading3Style({});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -76,7 +79,7 @@ describe("Default Styles", () => {
}); });
it("Heading4Style#constructor", () => { it("Heading4Style#constructor", () => {
const style = new defaultStyels.Heading4Style(); const style = new defaultStyles.Heading4Style({});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -90,7 +93,7 @@ describe("Default Styles", () => {
}); });
it("Heading5Style#constructor", () => { it("Heading5Style#constructor", () => {
const style = new defaultStyels.Heading5Style(); const style = new defaultStyles.Heading5Style({});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -104,7 +107,7 @@ describe("Default Styles", () => {
}); });
it("Heading6Style#constructor", () => { it("Heading6Style#constructor", () => {
const style = new defaultStyels.Heading6Style(); const style = new defaultStyles.Heading6Style({});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -118,7 +121,7 @@ describe("Default Styles", () => {
}); });
it("ListParagraph#constructor", () => { it("ListParagraph#constructor", () => {
const style = new defaultStyels.ListParagraph(); const style = new defaultStyles.ListParagraph({});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -131,7 +134,7 @@ describe("Default Styles", () => {
}); });
it("FootnoteText#constructor", () => { it("FootnoteText#constructor", () => {
const style = new defaultStyels.FootnoteText(); const style = new defaultStyles.FootnoteText({});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -171,14 +174,14 @@ describe("Default Styles", () => {
{ "w:basedOn": { _attr: { "w:val": "Normal" } } }, { "w:basedOn": { _attr: { "w:val": "Normal" } } },
{ "w:link": { _attr: { "w:val": "FootnoteTextChar" } } }, { "w:link": { _attr: { "w:val": "FootnoteTextChar" } } },
{ {
"w:uiPriority": { "w:semiHidden": EMPTY_OBJECT,
_attr: {
"w:val": "99",
},
},
}, },
{ {
"w:semiHidden": EMPTY_OBJECT, "w:uiPriority": {
_attr: {
"w:val": 99,
},
},
}, },
{ {
"w:unhideWhenUsed": EMPTY_OBJECT, "w:unhideWhenUsed": EMPTY_OBJECT,
@ -188,7 +191,7 @@ describe("Default Styles", () => {
}); });
it("FootnoteReferenceStyle#constructor", () => { it("FootnoteReferenceStyle#constructor", () => {
const style = new defaultStyels.FootnoteReferenceStyle(); const style = new defaultStyles.FootnoteReferenceStyle({});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -208,7 +211,7 @@ describe("Default Styles", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -225,7 +228,7 @@ describe("Default Styles", () => {
}); });
it("FootnoteTextChar#constructor", () => { it("FootnoteTextChar#constructor", () => {
const style = new defaultStyels.FootnoteTextChar(); const style = new defaultStyles.FootnoteTextChar({});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -252,7 +255,7 @@ describe("Default Styles", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -269,19 +272,28 @@ describe("Default Styles", () => {
}); });
it("HyperlinkStyle#constructor", () => { it("HyperlinkStyle#constructor", () => {
const style = new defaultStyels.HyperlinkStyle(); const style = new defaultStyles.HyperlinkStyle({});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
{ _attr: { "w:type": "character", "w:styleId": "Hyperlink" } }, { _attr: { "w:type": "character", "w:styleId": "Hyperlink" } },
{ "w:name": { _attr: { "w:val": "Hyperlink" } } }, { "w:name": { _attr: { "w:val": "Hyperlink" } } },
{ {
"w:rPr": [{ "w:color": { _attr: { "w:val": "0563C1" } } }, { "w:u": { _attr: { "w:val": "single" } } }], "w:rPr": [
{ "w:u": { _attr: { "w:val": "single" } } },
{
"w:color": {
_attr: {
"w:val": "0563C1",
},
},
},
],
}, },
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },

View File

@ -1,106 +1,170 @@
import { CharacterStyle } from "./character-style"; import { UnderlineType } from "file/paragraph/run/underline";
import { ParagraphStyle } from "./paragraph-style";
import { CharacterStyle, IBaseCharacterStyleOptions } from "./character-style";
import { IBaseParagraphStyleOptions, IParagraphStyleOptions, ParagraphStyle } from "./paragraph-style";
export class HeadingStyle extends ParagraphStyle { export class HeadingStyle extends ParagraphStyle {
constructor(styleId: string, name: string) { constructor(options: IParagraphStyleOptions) {
super(styleId, name); super({
this.basedOn("Normal"); ...options,
this.next("Normal"); basedOn: "Normal",
this.quickFormat(); next: "Normal",
quickFormat: true,
});
} }
} }
export class TitleStyle extends HeadingStyle { export class TitleStyle extends HeadingStyle {
constructor() { constructor(options: IBaseParagraphStyleOptions) {
super("Title", "Title"); super({
...options,
id: "Title",
name: "Title",
});
} }
} }
export class Heading1Style extends HeadingStyle { export class Heading1Style extends HeadingStyle {
constructor() { constructor(options: IBaseParagraphStyleOptions) {
super("Heading1", "Heading 1"); super({
...options,
id: "Heading1",
name: "Heading 1",
});
} }
} }
export class Heading2Style extends HeadingStyle { export class Heading2Style extends HeadingStyle {
constructor() { constructor(options: IBaseParagraphStyleOptions) {
super("Heading2", "Heading 2"); super({
...options,
id: "Heading2",
name: "Heading 2",
});
} }
} }
export class Heading3Style extends HeadingStyle { export class Heading3Style extends HeadingStyle {
constructor() { constructor(options: IBaseParagraphStyleOptions) {
super("Heading3", "Heading 3"); super({
...options,
id: "Heading3",
name: "Heading 3",
});
} }
} }
export class Heading4Style extends HeadingStyle { export class Heading4Style extends HeadingStyle {
constructor() { constructor(options: IBaseParagraphStyleOptions) {
super("Heading4", "Heading 4"); super({
...options,
id: "Heading4",
name: "Heading 4",
});
} }
} }
export class Heading5Style extends HeadingStyle { export class Heading5Style extends HeadingStyle {
constructor() { constructor(options: IBaseParagraphStyleOptions) {
super("Heading5", "Heading 5"); super({
...options,
id: "Heading5",
name: "Heading 5",
});
} }
} }
export class Heading6Style extends HeadingStyle { export class Heading6Style extends HeadingStyle {
constructor() { constructor(options: IBaseParagraphStyleOptions) {
super("Heading6", "Heading 6"); super({
...options,
id: "Heading6",
name: "Heading 6",
});
} }
} }
export class ListParagraph extends ParagraphStyle { export class ListParagraph extends ParagraphStyle {
constructor() { constructor(options: IBaseParagraphStyleOptions) {
super("ListParagraph", "List Paragraph"); super({
this.basedOn("Normal"); ...options,
this.quickFormat(); id: "ListParagraph",
name: "List Paragraph",
basedOn: "Normal",
quickFormat: true,
});
} }
} }
export class FootnoteText extends ParagraphStyle { export class FootnoteText extends ParagraphStyle {
constructor() { constructor(options: IBaseParagraphStyleOptions) {
super("FootnoteText", "footnote text"); super({
this.basedOn("Normal") ...options,
.link("FootnoteTextChar") id: "FootnoteText",
.uiPriority("99") name: "footnote text",
.semiHidden() link: "FootnoteTextChar",
.unhideWhenUsed() basedOn: "Normal",
.spacing({ uiPriority: 99,
after: 0, semiHidden: true,
line: 240, unhideWhenUsed: true,
lineRule: "auto", paragraph: {
}) spacing: {
.size(20); after: 0,
line: 240,
lineRule: "auto",
},
},
run: {
size: 20,
},
});
} }
} }
export class FootnoteReferenceStyle extends CharacterStyle { export class FootnoteReferenceStyle extends CharacterStyle {
constructor() { constructor(options: IBaseCharacterStyleOptions) {
super("FootnoteReference", "footnote reference"); super({
this.basedOn("DefaultParagraphFont") ...options,
.semiHidden() id: "FootnoteReference",
.superScript(); name: "footnote reference",
basedOn: "DefaultParagraphFont",
semiHidden: true,
run: {
superScript: true,
},
});
} }
} }
export class FootnoteTextChar extends CharacterStyle { export class FootnoteTextChar extends CharacterStyle {
constructor() { constructor(options: IBaseCharacterStyleOptions) {
super("FootnoteTextChar", "Footnote Text Char"); super({
this.basedOn("DefaultParagraphFont") ...options,
.link("FootnoteText") id: "FootnoteTextChar",
.semiHidden() name: "Footnote Text Char",
.size(20); basedOn: "DefaultParagraphFont",
link: "FootnoteText",
semiHidden: true,
run: {
size: 20,
},
});
} }
} }
export class HyperlinkStyle extends CharacterStyle { export class HyperlinkStyle extends CharacterStyle {
constructor() { constructor(options: IBaseCharacterStyleOptions) {
super("Hyperlink", "Hyperlink"); super({
this.basedOn("DefaultParagraphFont") ...options,
.color("0563C1") id: "Hyperlink",
.underline("single"); name: "Hyperlink",
basedOn: "DefaultParagraphFont",
run: {
color: "0563C1",
underline: {
type: UnderlineType.SINGLE,
},
},
});
} }
} }

View File

@ -1,7 +1,9 @@
import { expect } from "chai"; import { expect } from "chai";
import { Formatter } from "export/formatter"; import { Formatter } from "export/formatter";
import { TabStopPosition } from "file/paragraph"; import { AlignmentType, TabStopPosition } from "file/paragraph";
import { UnderlineType } from "file/paragraph/run/underline";
import { ShadingType } from "file/table";
import { EMPTY_OBJECT } from "file/xml-components"; import { EMPTY_OBJECT } from "file/xml-components";
import { ParagraphStyle } from "./paragraph-style"; import { ParagraphStyle } from "./paragraph-style";
@ -9,7 +11,7 @@ import { ParagraphStyle } from "./paragraph-style";
describe("ParagraphStyle", () => { describe("ParagraphStyle", () => {
describe("#constructor", () => { describe("#constructor", () => {
it("should set the style type to paragraph and use the given style id", () => { it("should set the style type to paragraph and use the given style id", () => {
const style = new ParagraphStyle("myStyleId"); const style = new ParagraphStyle({ id: "myStyleId" });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": { _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, "w:style": { _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
@ -17,7 +19,10 @@ describe("ParagraphStyle", () => {
}); });
it("should set the name of the style, if given", () => { it("should set the name of the style, if given", () => {
const style = new ParagraphStyle("myStyleId", "Style Name"); const style = new ParagraphStyle({
id: "myStyleId",
name: "Style Name",
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -30,7 +35,7 @@ describe("ParagraphStyle", () => {
describe("formatting methods: style attributes", () => { describe("formatting methods: style attributes", () => {
it("#basedOn", () => { it("#basedOn", () => {
const style = new ParagraphStyle("myStyleId").basedOn("otherId"); const style = new ParagraphStyle({ id: "myStyleId", basedOn: "otherId" });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -41,7 +46,7 @@ describe("ParagraphStyle", () => {
}); });
it("#quickFormat", () => { it("#quickFormat", () => {
const style = new ParagraphStyle("myStyleId").quickFormat(); const style = new ParagraphStyle({ id: "myStyleId", quickFormat: true });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:qFormat": EMPTY_OBJECT }], "w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:qFormat": EMPTY_OBJECT }],
@ -49,7 +54,7 @@ describe("ParagraphStyle", () => {
}); });
it("#next", () => { it("#next", () => {
const style = new ParagraphStyle("myStyleId").next("otherId"); const style = new ParagraphStyle({ id: "myStyleId", next: "otherId" });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -62,7 +67,12 @@ describe("ParagraphStyle", () => {
describe("formatting methods: paragraph properties", () => { describe("formatting methods: paragraph properties", () => {
it("#indent", () => { it("#indent", () => {
const style = new ParagraphStyle("myStyleId").indent({ left: 720 }); const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
indent: { left: 720 },
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -75,7 +85,7 @@ describe("ParagraphStyle", () => {
}); });
it("#spacing", () => { it("#spacing", () => {
const style = new ParagraphStyle("myStyleId").spacing({ before: 50, after: 150 }); const style = new ParagraphStyle({ id: "myStyleId", paragraph: { spacing: { before: 50, after: 150 } } });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -88,7 +98,12 @@ describe("ParagraphStyle", () => {
}); });
it("#center", () => { it("#center", () => {
const style = new ParagraphStyle("myStyleId").center(); const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
alignment: AlignmentType.CENTER,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -101,7 +116,12 @@ describe("ParagraphStyle", () => {
}); });
it("#character spacing", () => { it("#character spacing", () => {
const style = new ParagraphStyle("myStyleId").characterSpacing(24); const style = new ParagraphStyle({
id: "myStyleId",
run: {
characterSpacing: 24,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -114,7 +134,12 @@ describe("ParagraphStyle", () => {
}); });
it("#left", () => { it("#left", () => {
const style = new ParagraphStyle("myStyleId").left(); const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
alignment: AlignmentType.LEFT,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -127,7 +152,12 @@ describe("ParagraphStyle", () => {
}); });
it("#right", () => { it("#right", () => {
const style = new ParagraphStyle("myStyleId").right(); const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
alignment: AlignmentType.RIGHT,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -140,7 +170,12 @@ describe("ParagraphStyle", () => {
}); });
it("#justified", () => { it("#justified", () => {
const style = new ParagraphStyle("myStyleId").justified(); const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
alignment: AlignmentType.JUSTIFIED,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -153,7 +188,12 @@ describe("ParagraphStyle", () => {
}); });
it("#thematicBreak", () => { it("#thematicBreak", () => {
const style = new ParagraphStyle("myStyleId").thematicBreak(); const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
thematicBreak: true,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -181,7 +221,12 @@ describe("ParagraphStyle", () => {
}); });
it("#leftTabStop", () => { it("#leftTabStop", () => {
const style = new ParagraphStyle("myStyleId").leftTabStop(1200); const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
leftTabStop: 1200,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -198,7 +243,12 @@ describe("ParagraphStyle", () => {
}); });
it("#maxRightTabStop", () => { it("#maxRightTabStop", () => {
const style = new ParagraphStyle("myStyleId").rightTabStop(TabStopPosition.MAX); const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
rightTabStop: TabStopPosition.MAX,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -215,7 +265,12 @@ describe("ParagraphStyle", () => {
}); });
it("#keepLines", () => { it("#keepLines", () => {
const style = new ParagraphStyle("myStyleId").keepLines(); const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
keepLines: true,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:pPr": [{ "w:keepLines": EMPTY_OBJECT }] }], "w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:pPr": [{ "w:keepLines": EMPTY_OBJECT }] }],
@ -223,7 +278,12 @@ describe("ParagraphStyle", () => {
}); });
it("#keepNext", () => { it("#keepNext", () => {
const style = new ParagraphStyle("myStyleId").keepNext(); const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
keepNext: true,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:pPr": [{ "w:keepNext": EMPTY_OBJECT }] }], "w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:pPr": [{ "w:keepNext": EMPTY_OBJECT }] }],
@ -231,7 +291,12 @@ describe("ParagraphStyle", () => {
}); });
it("#outlineLevel", () => { it("#outlineLevel", () => {
const style = new ParagraphStyle("myStyleId").outlineLevel(1); const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
outlineLevel: 1,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -244,7 +309,12 @@ describe("ParagraphStyle", () => {
describe("formatting methods: run properties", () => { describe("formatting methods: run properties", () => {
it("#size", () => { it("#size", () => {
const style = new ParagraphStyle("myStyleId").size(24); const style = new ParagraphStyle({
id: "myStyleId",
run: {
size: 24,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -257,7 +327,12 @@ describe("ParagraphStyle", () => {
}); });
it("#smallCaps", () => { it("#smallCaps", () => {
const style = new ParagraphStyle("myStyleId").smallCaps(); const style = new ParagraphStyle({
id: "myStyleId",
run: {
smallCaps: true,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -270,7 +345,12 @@ describe("ParagraphStyle", () => {
}); });
it("#allCaps", () => { it("#allCaps", () => {
const style = new ParagraphStyle("myStyleId").allCaps(); const style = new ParagraphStyle({
id: "myStyleId",
run: {
allCaps: true,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -283,7 +363,12 @@ describe("ParagraphStyle", () => {
}); });
it("#strike", () => { it("#strike", () => {
const style = new ParagraphStyle("myStyleId").strike(); const style = new ParagraphStyle({
id: "myStyleId",
run: {
strike: true,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -296,7 +381,12 @@ describe("ParagraphStyle", () => {
}); });
it("#doubleStrike", () => { it("#doubleStrike", () => {
const style = new ParagraphStyle("myStyleId").doubleStrike(); const style = new ParagraphStyle({
id: "myStyleId",
run: {
doubleStrike: true,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -309,7 +399,12 @@ describe("ParagraphStyle", () => {
}); });
it("#subScript", () => { it("#subScript", () => {
const style = new ParagraphStyle("myStyleId").subScript(); const style = new ParagraphStyle({
id: "myStyleId",
run: {
subScript: true,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -322,7 +417,12 @@ describe("ParagraphStyle", () => {
}); });
it("#superScript", () => { it("#superScript", () => {
const style = new ParagraphStyle("myStyleId").superScript(); const style = new ParagraphStyle({
id: "myStyleId",
run: {
superScript: true,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -335,7 +435,12 @@ describe("ParagraphStyle", () => {
}); });
it("#font", () => { it("#font", () => {
const style = new ParagraphStyle("myStyleId").font("Times"); const style = new ParagraphStyle({
id: "myStyleId",
run: {
font: "Times",
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -350,7 +455,12 @@ describe("ParagraphStyle", () => {
}); });
it("#bold", () => { it("#bold", () => {
const style = new ParagraphStyle("myStyleId").bold(); const style = new ParagraphStyle({
id: "myStyleId",
run: {
bold: true,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -363,7 +473,12 @@ describe("ParagraphStyle", () => {
}); });
it("#italics", () => { it("#italics", () => {
const style = new ParagraphStyle("myStyleId").italics(); const style = new ParagraphStyle({
id: "myStyleId",
run: {
italics: true,
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -376,7 +491,12 @@ describe("ParagraphStyle", () => {
}); });
it("#highlight", () => { it("#highlight", () => {
const style = new ParagraphStyle("myStyleId").highlight("005599"); const style = new ParagraphStyle({
id: "myStyleId",
run: {
highlight: "005599",
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -389,7 +509,16 @@ describe("ParagraphStyle", () => {
}); });
it("#shadow", () => { it("#shadow", () => {
const style = new ParagraphStyle("myStyleId").shadow("pct10", "00FFFF", "FF0000"); const style = new ParagraphStyle({
id: "myStyleId",
run: {
shadow: {
type: ShadingType.PERCENT_10,
fill: "00FFFF",
color: "FF0000",
},
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -403,7 +532,12 @@ describe("ParagraphStyle", () => {
describe("#underline", () => { describe("#underline", () => {
it("should set underline to 'single' if no arguments are given", () => { it("should set underline to 'single' if no arguments are given", () => {
const style = new ParagraphStyle("myStyleId").underline(); const style = new ParagraphStyle({
id: "myStyleId",
run: {
underline: {},
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -416,7 +550,14 @@ describe("ParagraphStyle", () => {
}); });
it("should set the style if given", () => { it("should set the style if given", () => {
const style = new ParagraphStyle("myStyleId").underline("double"); const style = new ParagraphStyle({
id: "myStyleId",
run: {
underline: {
type: UnderlineType.DOUBLE,
},
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -429,7 +570,15 @@ describe("ParagraphStyle", () => {
}); });
it("should set the style and color if given", () => { it("should set the style and color if given", () => {
const style = new ParagraphStyle("myStyleId").underline("double", "005599"); const style = new ParagraphStyle({
id: "myStyleId",
run: {
underline: {
type: UnderlineType.DOUBLE,
color: "005599",
},
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -443,7 +592,12 @@ describe("ParagraphStyle", () => {
}); });
it("#color", () => { it("#color", () => {
const style = new ParagraphStyle("myStyleId").color("123456"); const style = new ParagraphStyle({
id: "myStyleId",
run: {
color: "123456",
},
});
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -456,7 +610,7 @@ describe("ParagraphStyle", () => {
}); });
it("#link", () => { it("#link", () => {
const style = new ParagraphStyle("myStyleId").link("MyLink"); const style = new ParagraphStyle({ id: "myStyleId", link: "MyLink" });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:link": { _attr: { "w:val": "MyLink" } } }], "w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:link": { _attr: { "w:val": "MyLink" } } }],
@ -464,7 +618,7 @@ describe("ParagraphStyle", () => {
}); });
it("#semiHidden", () => { it("#semiHidden", () => {
const style = new ParagraphStyle("myStyleId").semiHidden(); const style = new ParagraphStyle({ id: "myStyleId", semiHidden: true });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:semiHidden": EMPTY_OBJECT }], "w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:semiHidden": EMPTY_OBJECT }],
@ -472,7 +626,7 @@ describe("ParagraphStyle", () => {
}); });
it("#uiPriority", () => { it("#uiPriority", () => {
const style = new ParagraphStyle("myStyleId").uiPriority("99"); const style = new ParagraphStyle({ id: "myStyleId", uiPriority: 99 });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [ "w:style": [
@ -480,7 +634,7 @@ describe("ParagraphStyle", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -489,7 +643,7 @@ describe("ParagraphStyle", () => {
}); });
it("#unhideWhenUsed", () => { it("#unhideWhenUsed", () => {
const style = new ParagraphStyle("myStyleId").unhideWhenUsed(); const style = new ParagraphStyle({ id: "myStyleId", unhideWhenUsed: true });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:unhideWhenUsed": EMPTY_OBJECT }], "w:style": [{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:unhideWhenUsed": EMPTY_OBJECT }],

View File

@ -14,179 +14,199 @@ import {
import { RightTabStop } from "file/paragraph/formatting"; import { RightTabStop } from "file/paragraph/formatting";
import * as formatting from "file/paragraph/run/formatting"; import * as formatting from "file/paragraph/run/formatting";
import { RunProperties } from "file/paragraph/run/properties"; import { RunProperties } from "file/paragraph/run/properties";
import { XmlComponent } from "file/xml-components"; import { UnderlineType } from "file/paragraph/run/underline";
import { ShadingType } from "file/table";
import { BasedOn, Link, Next, QuickFormat, SemiHidden, UiPriority, UnhideWhenUsed } from "./components"; import { BasedOn, Link, Next, QuickFormat, SemiHidden, UiPriority, UnhideWhenUsed } from "./components";
import { Style } from "./style"; import { Style } from "./style";
export interface IBaseParagraphStyleOptions {
readonly basedOn?: string;
readonly next?: string;
readonly quickFormat?: boolean;
readonly link?: string;
readonly semiHidden?: boolean;
readonly uiPriority?: number;
readonly unhideWhenUsed?: boolean;
readonly run?: {
readonly size?: number;
readonly bold?: boolean;
readonly italics?: boolean;
readonly smallCaps?: boolean;
readonly allCaps?: boolean;
readonly strike?: boolean;
readonly doubleStrike?: boolean;
readonly subScript?: boolean;
readonly superScript?: boolean;
readonly underline?: {
readonly type?: UnderlineType;
readonly color?: string;
};
readonly color?: string;
readonly font?: string;
readonly characterSpacing?: number;
readonly highlight?: string;
readonly shadow?: {
readonly type: ShadingType;
readonly fill: string;
readonly color: string;
};
};
readonly paragraph?: {
readonly alignment?: AlignmentType;
readonly thematicBreak?: boolean;
readonly rightTabStop?: number;
readonly leftTabStop?: number;
readonly indent?: object;
readonly spacing?: ISpacingProperties;
readonly keepNext?: boolean;
readonly keepLines?: boolean;
readonly outlineLevel?: number;
};
}
export interface IParagraphStyleOptions extends IBaseParagraphStyleOptions {
readonly id: string;
readonly name?: string;
}
export class ParagraphStyle extends Style { export class ParagraphStyle extends Style {
private readonly paragraphProperties: ParagraphProperties; private readonly paragraphProperties: ParagraphProperties;
private readonly runProperties: RunProperties; private readonly runProperties: RunProperties;
constructor(styleId: string, name?: string) { constructor(options: IParagraphStyleOptions) {
super({ type: "paragraph", styleId: styleId }, name); super({ type: "paragraph", styleId: options.id }, options.name);
this.paragraphProperties = new ParagraphProperties({}); this.paragraphProperties = new ParagraphProperties({});
this.runProperties = new RunProperties(); this.runProperties = new RunProperties();
this.root.push(this.paragraphProperties); this.root.push(this.paragraphProperties);
this.root.push(this.runProperties); this.root.push(this.runProperties);
}
public addParagraphProperty(property: XmlComponent): ParagraphStyle { if (options.basedOn) {
this.paragraphProperties.push(property); this.root.push(new BasedOn(options.basedOn));
return this; }
}
public outlineLevel(level: number): ParagraphStyle { if (options.next) {
this.paragraphProperties.push(new OutlineLevel(level)); this.root.push(new Next(options.next));
return this; }
}
public addRunProperty(property: XmlComponent): ParagraphStyle { if (options.quickFormat) {
this.runProperties.push(property); this.root.push(new QuickFormat());
return this; }
}
public basedOn(parentId: string): ParagraphStyle { if (options.link) {
this.root.push(new BasedOn(parentId)); this.root.push(new Link(options.link));
return this; }
}
public quickFormat(): ParagraphStyle { if (options.semiHidden) {
this.root.push(new QuickFormat()); this.root.push(new SemiHidden());
return this; }
}
public next(nextId: string): ParagraphStyle { if (options.uiPriority) {
this.root.push(new Next(nextId)); this.root.push(new UiPriority(options.uiPriority));
return this; }
}
// ---------- Run formatting ---------------------- // if (options.unhideWhenUsed) {
this.root.push(new UnhideWhenUsed());
}
public size(twips: number): ParagraphStyle { if (options.run) {
return this.addRunProperty(new formatting.Size(twips)).addRunProperty(new formatting.SizeComplexScript(twips)); if (options.run.size) {
} this.runProperties.push(new formatting.Size(options.run.size));
this.runProperties.push(new formatting.SizeComplexScript(options.run.size));
}
public bold(): ParagraphStyle { if (options.run.bold) {
return this.addRunProperty(new formatting.Bold()); this.runProperties.push(new formatting.Bold());
} }
public italics(): ParagraphStyle { if (options.run.italics) {
return this.addRunProperty(new formatting.Italics()); this.runProperties.push(new formatting.Italics());
} }
public smallCaps(): ParagraphStyle { if (options.run.smallCaps) {
return this.addRunProperty(new formatting.SmallCaps()); this.runProperties.push(new formatting.SmallCaps());
} }
public allCaps(): ParagraphStyle { if (options.run.allCaps) {
return this.addRunProperty(new formatting.Caps()); this.runProperties.push(new formatting.Caps());
} }
public strike(): ParagraphStyle { if (options.run.strike) {
return this.addRunProperty(new formatting.Strike()); this.runProperties.push(new formatting.Strike());
} }
public doubleStrike(): ParagraphStyle { if (options.run.doubleStrike) {
return this.addRunProperty(new formatting.DoubleStrike()); this.runProperties.push(new formatting.DoubleStrike());
} }
public subScript(): ParagraphStyle { if (options.run.subScript) {
return this.addRunProperty(new formatting.SubScript()); this.runProperties.push(new formatting.SubScript());
} }
public superScript(): ParagraphStyle { if (options.run.superScript) {
return this.addRunProperty(new formatting.SuperScript()); this.runProperties.push(new formatting.SuperScript());
} }
public underline(underlineType?: string, color?: string): ParagraphStyle { if (options.run.underline) {
return this.addRunProperty(new formatting.Underline(underlineType, color)); this.runProperties.push(new formatting.Underline(options.run.underline.type, options.run.underline.color));
} }
public color(color: string): ParagraphStyle { if (options.run.color) {
return this.addRunProperty(new formatting.Color(color)); this.runProperties.push(new formatting.Color(options.run.color));
} }
public font(fontName: string): ParagraphStyle { if (options.run.font) {
return this.addRunProperty(new formatting.RunFonts(fontName)); this.runProperties.push(new formatting.RunFonts(options.run.font));
} }
public characterSpacing(value: number): ParagraphStyle { if (options.run.characterSpacing) {
return this.addRunProperty(new formatting.CharacterSpacing(value)); this.runProperties.push(new formatting.CharacterSpacing(options.run.characterSpacing));
} }
public highlight(color: string): ParagraphStyle { if (options.run.highlight) {
return this.addRunProperty(new formatting.Highlight(color)); this.runProperties.push(new formatting.Highlight(options.run.highlight));
} }
public shadow(value: string, fill: string, color: string): ParagraphStyle { if (options.run.shadow) {
return this.addRunProperty(new formatting.Shading(value, fill, color)); this.runProperties.push(new formatting.Shading(options.run.shadow.type, options.run.shadow.fill, options.run.shadow.color));
} }
}
// --------------------- Paragraph formatting ------------------------ // if (options.paragraph) {
if (options.paragraph.alignment) {
this.paragraphProperties.push(new Alignment(options.paragraph.alignment));
}
public center(): ParagraphStyle { if (options.paragraph.thematicBreak) {
return this.addParagraphProperty(new Alignment(AlignmentType.CENTER)); this.paragraphProperties.push(new ThematicBreak());
} }
public left(): ParagraphStyle { if (options.paragraph.rightTabStop) {
return this.addParagraphProperty(new Alignment(AlignmentType.LEFT)); this.paragraphProperties.push(new RightTabStop(options.paragraph.rightTabStop));
} }
public right(): ParagraphStyle { if (options.paragraph.leftTabStop) {
return this.addParagraphProperty(new Alignment(AlignmentType.RIGHT)); this.paragraphProperties.push(new LeftTabStop(options.paragraph.leftTabStop));
} }
public justified(): ParagraphStyle { if (options.paragraph.indent) {
return this.addParagraphProperty(new Alignment(AlignmentType.BOTH)); this.paragraphProperties.push(new Indent(options.paragraph.indent));
} }
public thematicBreak(): ParagraphStyle { if (options.paragraph.spacing) {
return this.addParagraphProperty(new ThematicBreak()); this.paragraphProperties.push(new Spacing(options.paragraph.spacing));
} }
public rightTabStop(position: number): ParagraphStyle { if (options.paragraph.keepNext) {
return this.addParagraphProperty(new RightTabStop(position)); this.paragraphProperties.push(new KeepNext());
} }
public leftTabStop(position: number): ParagraphStyle { if (options.paragraph.keepLines) {
return this.addParagraphProperty(new LeftTabStop(position)); this.paragraphProperties.push(new KeepLines());
} }
public indent(attrs: object): ParagraphStyle { if (options.paragraph.outlineLevel) {
return this.addParagraphProperty(new Indent(attrs)); this.paragraphProperties.push(new OutlineLevel(options.paragraph.outlineLevel));
} }
}
public spacing(params: ISpacingProperties): ParagraphStyle {
return this.addParagraphProperty(new Spacing(params));
}
public keepNext(): ParagraphStyle {
return this.addParagraphProperty(new KeepNext());
}
public keepLines(): ParagraphStyle {
return this.addParagraphProperty(new KeepLines());
}
/*-------------- Style Properties -----------------*/
public link(link: string): ParagraphStyle {
this.root.push(new Link(link));
return this;
}
public semiHidden(): ParagraphStyle {
this.root.push(new SemiHidden());
return this;
}
public uiPriority(priority: string): ParagraphStyle {
this.root.push(new UiPriority(priority));
return this;
}
public unhideWhenUsed(): ParagraphStyle {
this.root.push(new UnhideWhenUsed());
return this;
} }
} }

View File

@ -1,31 +1,20 @@
import { assert, expect } from "chai"; import { expect } from "chai";
import { Formatter } from "export/formatter"; import { Formatter } from "export/formatter";
import { EMPTY_OBJECT } from "file/xml-components";
import { CharacterStyle, ParagraphStyle } from "./style";
import { Styles } from "./styles"; import { Styles } from "./styles";
import { EMPTY_OBJECT } from "file/xml-components";
describe("Styles", () => { describe("Styles", () => {
let styles: Styles;
beforeEach(() => {
styles = new Styles();
});
describe("#constructor()", () => {
it("should create styles with correct rootKey", () => {
const newJson = JSON.parse(JSON.stringify(styles));
assert.equal(newJson.rootKey, "w:styles");
});
});
describe("#createParagraphStyle", () => { describe("#createParagraphStyle", () => {
it("should create a new paragraph style and push it onto this collection", () => { it("should create a new paragraph style and push it onto this collection", () => {
const pStyle = styles.createParagraphStyle("pStyleId"); const styles = new Styles({
expect(pStyle).to.instanceOf(ParagraphStyle); paragraphStyles: [
{
id: "pStyleId",
},
],
});
const tree = new Formatter().format(styles)["w:styles"].filter((x) => !x._attr); const tree = new Formatter().format(styles)["w:styles"].filter((x) => !x._attr);
expect(tree).to.deep.equal([ expect(tree).to.deep.equal([
{ {
@ -35,8 +24,14 @@ describe("Styles", () => {
}); });
it("should set the paragraph name if given", () => { it("should set the paragraph name if given", () => {
const pStyle = styles.createParagraphStyle("pStyleId", "Paragraph Style"); const styles = new Styles({
expect(pStyle).to.instanceOf(ParagraphStyle); paragraphStyles: [
{
id: "pStyleId",
name: "Paragraph Style",
},
],
});
const tree = new Formatter().format(styles)["w:styles"].filter((x) => !x._attr); const tree = new Formatter().format(styles)["w:styles"].filter((x) => !x._attr);
expect(tree).to.deep.equal([ expect(tree).to.deep.equal([
{ {
@ -51,8 +46,13 @@ describe("Styles", () => {
describe("#createCharacterStyle", () => { describe("#createCharacterStyle", () => {
it("should create a new character style and push it onto this collection", () => { it("should create a new character style and push it onto this collection", () => {
const cStyle = styles.createCharacterStyle("pStyleId"); const styles = new Styles({
expect(cStyle).to.instanceOf(CharacterStyle); characterStyles: [
{
id: "pStyleId",
},
],
});
const tree = new Formatter().format(styles)["w:styles"].filter((x) => !x._attr); const tree = new Formatter().format(styles)["w:styles"].filter((x) => !x._attr);
expect(tree).to.deep.equal([ expect(tree).to.deep.equal([
{ {
@ -61,7 +61,7 @@ describe("Styles", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },
@ -74,8 +74,14 @@ describe("Styles", () => {
}); });
it("should set the character name if given", () => { it("should set the character name if given", () => {
const cStyle = styles.createCharacterStyle("pStyleId", "Character Style"); const styles = new Styles({
expect(cStyle).to.instanceOf(CharacterStyle); characterStyles: [
{
id: "pStyleId",
name: "Character Style",
},
],
});
const tree = new Formatter().format(styles)["w:styles"].filter((x) => !x._attr); const tree = new Formatter().format(styles)["w:styles"].filter((x) => !x._attr);
expect(tree).to.deep.equal([ expect(tree).to.deep.equal([
{ {
@ -85,7 +91,7 @@ describe("Styles", () => {
{ {
"w:uiPriority": { "w:uiPriority": {
_attr: { _attr: {
"w:val": "99", "w:val": 99,
}, },
}, },
}, },

View File

@ -1,36 +1,48 @@
import { BaseXmlComponent, XmlComponent } from "file/xml-components"; import { BaseXmlComponent, ImportedXmlComponent, XmlComponent } from "file/xml-components";
import { DocumentDefaults } from "./defaults"; import { DocumentDefaults } from "./defaults";
import { CharacterStyle, ParagraphStyle } from "./style"; import { CharacterStyle, ParagraphStyle } from "./style";
import { ICharacterStyleOptions } from "./style/character-style";
import { IParagraphStyleOptions } from "./style/paragraph-style";
export * from "./border"; export * from "./border";
export class Styles extends XmlComponent { interface IStylesOptions {
constructor(initialStyles?: BaseXmlComponent) { readonly initialStyles?: BaseXmlComponent;
super("w:styles"); readonly paragraphStyles?: IParagraphStyleOptions[];
if (initialStyles) { readonly characterStyles?: ICharacterStyleOptions[];
this.root.push(initialStyles); readonly importedStyles?: Array<XmlComponent | ParagraphStyle | CharacterStyle | ImportedXmlComponent>;
} }
}
public push(style: XmlComponent): Styles { export class Styles extends XmlComponent {
this.root.push(style); constructor(options: IStylesOptions) {
return this; super("w:styles");
if (options.initialStyles) {
this.root.push(options.initialStyles);
}
if (options.paragraphStyles) {
for (const style of options.paragraphStyles) {
this.root.push(new ParagraphStyle(style));
}
}
if (options.characterStyles) {
for (const style of options.characterStyles) {
this.root.push(new CharacterStyle(style));
}
}
if (options.importedStyles) {
for (const style of options.importedStyles) {
this.root.push(style);
}
}
} }
public createDocumentDefaults(): DocumentDefaults { public createDocumentDefaults(): DocumentDefaults {
const defaults = new DocumentDefaults(); const defaults = new DocumentDefaults();
this.push(defaults); this.root.push(defaults);
return defaults; return defaults;
} }
public createParagraphStyle(styleId: string, name?: string): ParagraphStyle {
const paragraphStyle = new ParagraphStyle(styleId, name);
this.push(paragraphStyle);
return paragraphStyle;
}
public createCharacterStyle(styleId: string, name?: string): CharacterStyle {
const characterStyle = new CharacterStyle(styleId, name);
this.push(characterStyle);
return characterStyle;
}
} }