From b05748da253ba5128f8b955cfdb9c38ebfc92b66 Mon Sep 17 00:00:00 2001 From: Tom Hunkapiller Date: Tue, 25 May 2021 04:27:10 +0300 Subject: [PATCH] update file/styles --- src/file/styles/style/character-style.spec.ts | 182 +++++++++--------- src/file/styles/style/character-style.ts | 32 +-- src/file/styles/style/components.spec.ts | 38 ---- src/file/styles/style/components.ts | 43 +---- src/file/styles/style/default-styles.spec.ts | 111 ++++++----- src/file/styles/style/paragraph-style.ts | 43 +---- src/file/styles/style/style.spec.ts | 15 +- src/file/styles/style/style.ts | 80 +++++++- src/file/styles/styles.ts | 7 + 9 files changed, 252 insertions(+), 299 deletions(-) diff --git a/src/file/styles/style/character-style.spec.ts b/src/file/styles/style/character-style.spec.ts index f6def753b3..08e6779f7d 100644 --- a/src/file/styles/style/character-style.spec.ts +++ b/src/file/styles/style/character-style.spec.ts @@ -65,9 +65,6 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, - { - "w:rPr": [{ "w:smallCaps": {} }], - }, { "w:uiPriority": { _attr: { @@ -78,6 +75,9 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, + { + "w:rPr": [{ "w:smallCaps": {} }], + }, ], }); }); @@ -93,9 +93,6 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, - { - "w:rPr": [{ "w:caps": {} }], - }, { "w:uiPriority": { _attr: { @@ -106,6 +103,9 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, + { + "w:rPr": [{ "w:caps": {} }], + }, ], }); }); @@ -121,9 +121,6 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, - { - "w:rPr": [{ "w:strike": {} }], - }, { "w:uiPriority": { _attr: { @@ -134,6 +131,9 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, + { + "w:rPr": [{ "w:strike": {} }], + }, ], }); }); @@ -149,9 +149,6 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, - { - "w:rPr": [{ "w:dstrike": {} }], - }, { "w:uiPriority": { _attr: { @@ -162,6 +159,9 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, + { + "w:rPr": [{ "w:dstrike": {} }], + }, ], }); }); @@ -177,6 +177,16 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, + { + "w:uiPriority": { + _attr: { + "w:val": 99, + }, + }, + }, + { + "w:unhideWhenUsed": EMPTY_OBJECT, + }, { "w:rPr": [ { @@ -188,16 +198,6 @@ describe("CharacterStyle", () => { }, ], }, - { - "w:uiPriority": { - _attr: { - "w:val": 99, - }, - }, - }, - { - "w:unhideWhenUsed": EMPTY_OBJECT, - }, ], }); }); @@ -213,6 +213,16 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, + { + "w:uiPriority": { + _attr: { + "w:val": 99, + }, + }, + }, + { + "w:unhideWhenUsed": EMPTY_OBJECT, + }, { "w:rPr": [ { @@ -227,16 +237,6 @@ describe("CharacterStyle", () => { }, ], }, - { - "w:uiPriority": { - _attr: { - "w:val": 99, - }, - }, - }, - { - "w:unhideWhenUsed": EMPTY_OBJECT, - }, ], }); }); @@ -255,6 +255,16 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, + { + "w:uiPriority": { + _attr: { + "w:val": 99, + }, + }, + }, + { + "w:unhideWhenUsed": EMPTY_OBJECT, + }, { "w:rPr": [ { @@ -267,16 +277,6 @@ describe("CharacterStyle", () => { }, ], }, - { - "w:uiPriority": { - _attr: { - "w:val": 99, - }, - }, - }, - { - "w:unhideWhenUsed": EMPTY_OBJECT, - }, ], }); }); @@ -292,9 +292,6 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, - { - "w:rPr": [{ "w:spacing": { _attr: { "w:val": 100 } } }], - }, { "w:uiPriority": { _attr: { @@ -305,6 +302,9 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, + { + "w:rPr": [{ "w:spacing": { _attr: { "w:val": 100 } } }], + }, ], }); }); @@ -317,6 +317,7 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, + { "w:basedOn": { _attr: { "w:val": "otherId" } } }, { "w:uiPriority": { _attr: { @@ -327,7 +328,6 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, - { "w:basedOn": { _attr: { "w:val": "otherId" } } }, ], }); }); @@ -365,9 +365,6 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, - { - "w:rPr": expected, - }, { "w:uiPriority": { _attr: { @@ -378,6 +375,9 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, + { + "w:rPr": expected, + }, ], }); }); @@ -395,9 +395,6 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, - { - "w:rPr": [{ "w:u": { _attr: { "w:val": "single" } } }], - }, { "w:uiPriority": { _attr: { @@ -408,6 +405,9 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, + { + "w:rPr": [{ "w:u": { _attr: { "w:val": "single" } } }], + }, ], }); }); @@ -425,9 +425,6 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, - { - "w:rPr": [{ "w:u": { _attr: { "w:val": "double" } } }], - }, { "w:uiPriority": { _attr: { @@ -438,6 +435,9 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, + { + "w:rPr": [{ "w:u": { _attr: { "w:val": "double" } } }], + }, ], }); }); @@ -456,9 +456,6 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, - { - "w:rPr": [{ "w:u": { _attr: { "w:val": "double", "w:color": "005599" } } }], - }, { "w:uiPriority": { _attr: { @@ -469,6 +466,9 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, + { + "w:rPr": [{ "w:u": { _attr: { "w:val": "double", "w:color": "005599" } } }], + }, ], }); }); @@ -486,9 +486,6 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, - { - "w:rPr": [{ "w:em": { _attr: { "w:val": "dot" } } }], - }, { "w:uiPriority": { _attr: { @@ -499,6 +496,9 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, + { + "w:rPr": [{ "w:em": { _attr: { "w:val": "dot" } } }], + }, ], }); }); @@ -516,9 +516,6 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, - { - "w:rPr": [{ "w:em": { _attr: { "w:val": "dot" } } }], - }, { "w:uiPriority": { _attr: { @@ -529,6 +526,9 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, + { + "w:rPr": [{ "w:em": { _attr: { "w:val": "dot" } } }], + }, ], }); }); @@ -545,6 +545,16 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, + { + "w:uiPriority": { + _attr: { + "w:val": 99, + }, + }, + }, + { + "w:unhideWhenUsed": EMPTY_OBJECT, + }, { "w:rPr": [ { @@ -556,16 +566,6 @@ describe("CharacterStyle", () => { }, ], }, - { - "w:uiPriority": { - _attr: { - "w:val": 99, - }, - }, - }, - { - "w:unhideWhenUsed": EMPTY_OBJECT, - }, ], }); }); @@ -581,9 +581,6 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, - { - "w:rPr": [{ "w:color": { _attr: { "w:val": "123456" } } }], - }, { "w:uiPriority": { _attr: { @@ -594,6 +591,9 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, + { + "w:rPr": [{ "w:color": { _attr: { "w:val": "123456" } } }], + }, ], }); }); @@ -624,9 +624,6 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, - { - "w:rPr": expected, - }, { "w:uiPriority": { _attr: { @@ -637,6 +634,9 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, + { + "w:rPr": expected, + }, ], }); }); @@ -668,9 +668,6 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, - { - "w:rPr": expected, - }, { "w:uiPriority": { _attr: { @@ -681,6 +678,9 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, + { + "w:rPr": expected, + }, ], }); }); @@ -692,6 +692,7 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, + { "w:link": { _attr: { "w:val": "MyLink" } } }, { "w:uiPriority": { _attr: { @@ -702,7 +703,6 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, - { "w:link": { _attr: { "w:val": "MyLink" } } }, ], }); }); @@ -720,8 +720,8 @@ describe("CharacterStyle", () => { }, }, }, - { "w:unhideWhenUsed": EMPTY_OBJECT }, { "w:semiHidden": EMPTY_OBJECT }, + { "w:unhideWhenUsed": EMPTY_OBJECT }, ], }); }); @@ -757,9 +757,6 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, - { - "w:rPr": expected, - }, { "w:uiPriority": { _attr: { @@ -770,6 +767,9 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, + { + "w:rPr": expected, + }, ], }); }); @@ -803,9 +803,6 @@ describe("CharacterStyle", () => { expect(tree).to.deep.equal({ "w:style": [ { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, - { - "w:rPr": expected, - }, { "w:uiPriority": { _attr: { @@ -816,6 +813,9 @@ describe("CharacterStyle", () => { { "w:unhideWhenUsed": EMPTY_OBJECT, }, + { + "w:rPr": expected, + }, ], }); }); diff --git a/src/file/styles/style/character-style.ts b/src/file/styles/style/character-style.ts index 63252bddd5..229c8e54c6 100644 --- a/src/file/styles/style/character-style.ts +++ b/src/file/styles/style/character-style.ts @@ -1,12 +1,8 @@ import { IRunStylePropertiesOptions, RunProperties } from "file/paragraph/run/properties"; -import { BasedOn, Link, SemiHidden, UiPriority, UnhideWhenUsed } from "./components"; -import { Style } from "./style"; +import { IStyleOptions, Style } from "./style"; -export interface IBaseCharacterStyleOptions { - readonly basedOn?: string; - readonly link?: string; - readonly semiHidden?: boolean; +export interface IBaseCharacterStyleOptions extends IStyleOptions { readonly run?: IRunStylePropertiesOptions; } @@ -19,24 +15,16 @@ export class StyleForCharacter extends Style { private readonly runProperties: RunProperties; constructor(options: ICharacterStyleOptions) { - super({ type: "character", styleId: options.id }, options.name); + super( + { type: "character", styleId: options.id }, + { + uiPriority: 99, + unhideWhenUsed: true, + ...options, + }, + ); this.runProperties = new RunProperties(options.run); - this.root.push(this.runProperties); - this.root.push(new UiPriority(99)); - this.root.push(new UnhideWhenUsed()); - - if (options.basedOn) { - this.root.push(new BasedOn(options.basedOn)); - } - - if (options.link) { - this.root.push(new Link(options.link)); - } - - if (options.semiHidden) { - this.root.push(new SemiHidden()); - } } } diff --git a/src/file/styles/style/components.spec.ts b/src/file/styles/style/components.spec.ts index b1720ef4b4..7ce7df62ab 100644 --- a/src/file/styles/style/components.spec.ts +++ b/src/file/styles/style/components.spec.ts @@ -2,8 +2,6 @@ import { expect } from "chai"; import { Formatter } from "export/formatter"; import * as components from "./components"; -import { EMPTY_OBJECT } from "file/xml-components"; - describe("Style components", () => { it("Name#constructor", () => { const style = new components.Name("Style Name"); @@ -11,45 +9,9 @@ describe("Style components", () => { expect(tree).to.deep.equal({ "w:name": { _attr: { "w:val": "Style Name" } } }); }); - it("BasedOn#constructor", () => { - const style = new components.BasedOn("otherId"); - const tree = new Formatter().format(style); - expect(tree).to.deep.equal({ "w:basedOn": { _attr: { "w:val": "otherId" } } }); - }); - - it("Next#constructor", () => { - const style = new components.Next("otherId"); - const tree = new Formatter().format(style); - expect(tree).to.deep.equal({ "w:next": { _attr: { "w:val": "otherId" } } }); - }); - - it("Link#constructor", () => { - const style = new components.Link("otherId"); - const tree = new Formatter().format(style); - expect(tree).to.deep.equal({ "w:link": { _attr: { "w:val": "otherId" } } }); - }); - it("UiPriority#constructor", () => { const style = new components.UiPriority(123); const tree = new Formatter().format(style); expect(tree).to.deep.equal({ "w:uiPriority": { _attr: { "w:val": 123 } } }); }); - - it("UnhideWhenUsed#constructor", () => { - const style = new components.UnhideWhenUsed(); - const tree = new Formatter().format(style); - expect(tree).to.deep.equal({ "w:unhideWhenUsed": EMPTY_OBJECT }); - }); - - it("QuickFormat#constructor", () => { - const style = new components.QuickFormat(); - const tree = new Formatter().format(style); - expect(tree).to.deep.equal({ "w:qFormat": EMPTY_OBJECT }); - }); - - it("SemiHidden#constructor", () => { - const style = new components.SemiHidden(); - const tree = new Formatter().format(style); - expect(tree).to.deep.equal({ "w:semiHidden": EMPTY_OBJECT }); - }); }); diff --git a/src/file/styles/style/components.ts b/src/file/styles/style/components.ts index e8a217c638..4b35ba38b3 100644 --- a/src/file/styles/style/components.ts +++ b/src/file/styles/style/components.ts @@ -1,4 +1,5 @@ // http://officeopenxml.com/WPstyleGenProps.php +import { decimalNumber } from "file/values"; import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; class ComponentAttributes extends XmlAttributeComponent<{ @@ -14,53 +15,13 @@ export class Name extends XmlComponent { } } -export class BasedOn extends XmlComponent { - constructor(value: string) { - super("w:basedOn"); - this.root.push(new ComponentAttributes({ val: value })); - } -} - -export class Next extends XmlComponent { - constructor(value: string) { - super("w:next"); - this.root.push(new ComponentAttributes({ val: value })); - } -} - -export class Link extends XmlComponent { - constructor(value: string) { - super("w:link"); - this.root.push(new ComponentAttributes({ val: value })); - } -} - export class UiPriority extends XmlComponent { constructor(value: number) { super("w:uiPriority"); - // TODO: this value should be a ST_DecimalNumber - this.root.push(new ComponentAttributes({ val: value })); - } -} - -export class UnhideWhenUsed extends XmlComponent { - constructor() { - super("w:unhideWhenUsed"); - } -} - -export class QuickFormat extends XmlComponent { - constructor() { - super("w:qFormat"); + this.root.push(new ComponentAttributes({ val: decimalNumber(value) })); } } export class TableProperties extends XmlComponent {} export class RsId extends XmlComponent {} - -export class SemiHidden extends XmlComponent { - constructor() { - super("w:semiHidden"); - } -} diff --git a/src/file/styles/style/default-styles.spec.ts b/src/file/styles/style/default-styles.spec.ts index 962f47d9c0..dda921a110 100644 --- a/src/file/styles/style/default-styles.spec.ts +++ b/src/file/styles/style/default-styles.spec.ts @@ -154,6 +154,21 @@ describe("Default Styles", () => { "w:style": [ { _attr: { "w:type": "paragraph", "w:styleId": "FootnoteText" } }, { "w:name": { _attr: { "w:val": "footnote text" } } }, + { "w:basedOn": { _attr: { "w:val": "Normal" } } }, + { "w:link": { _attr: { "w:val": "FootnoteTextChar" } } }, + { + "w:uiPriority": { + _attr: { + "w:val": 99, + }, + }, + }, + { + "w:semiHidden": EMPTY_OBJECT, + }, + { + "w:unhideWhenUsed": EMPTY_OBJECT, + }, { "w:pPr": [ { @@ -185,21 +200,6 @@ describe("Default Styles", () => { }, ], }, - { "w:basedOn": { _attr: { "w:val": "Normal" } } }, - { "w:link": { _attr: { "w:val": "FootnoteTextChar" } } }, - { - "w:semiHidden": EMPTY_OBJECT, - }, - { - "w:uiPriority": { - _attr: { - "w:val": 99, - }, - }, - }, - { - "w:unhideWhenUsed": EMPTY_OBJECT, - }, ], }); }); @@ -211,6 +211,20 @@ describe("Default Styles", () => { "w:style": [ { _attr: { "w:type": "character", "w:styleId": "FootnoteReference" } }, { "w:name": { _attr: { "w:val": "footnote reference" } } }, + { "w:basedOn": { _attr: { "w:val": "DefaultParagraphFont" } } }, + { + "w:uiPriority": { + _attr: { + "w:val": 99, + }, + }, + }, + { + "w:semiHidden": EMPTY_OBJECT, + }, + { + "w:unhideWhenUsed": EMPTY_OBJECT, + }, { "w:rPr": [ { @@ -222,21 +236,6 @@ describe("Default Styles", () => { }, ], }, - { - "w:uiPriority": { - _attr: { - "w:val": 99, - }, - }, - }, - { - "w:unhideWhenUsed": EMPTY_OBJECT, - }, - { "w:basedOn": { _attr: { "w:val": "DefaultParagraphFont" } } }, - - { - "w:semiHidden": EMPTY_OBJECT, - }, ], }); }); @@ -248,6 +247,21 @@ describe("Default Styles", () => { "w:style": [ { _attr: { "w:type": "character", "w:styleId": "FootnoteTextChar" } }, { "w:name": { _attr: { "w:val": "Footnote Text Char" } } }, + { "w:basedOn": { _attr: { "w:val": "DefaultParagraphFont" } } }, + { "w:link": { _attr: { "w:val": "FootnoteText" } } }, + { + "w:uiPriority": { + _attr: { + "w:val": 99, + }, + }, + }, + { + "w:semiHidden": EMPTY_OBJECT, + }, + { + "w:unhideWhenUsed": EMPTY_OBJECT, + }, { "w:rPr": [ { @@ -266,21 +280,6 @@ describe("Default Styles", () => { }, ], }, - { - "w:uiPriority": { - _attr: { - "w:val": 99, - }, - }, - }, - { - "w:unhideWhenUsed": EMPTY_OBJECT, - }, - { "w:basedOn": { _attr: { "w:val": "DefaultParagraphFont" } } }, - { "w:link": { _attr: { "w:val": "FootnoteText" } } }, - { - "w:semiHidden": EMPTY_OBJECT, - }, ], }); }); @@ -292,6 +291,17 @@ describe("Default Styles", () => { "w:style": [ { _attr: { "w:type": "character", "w:styleId": "Hyperlink" } }, { "w:name": { _attr: { "w:val": "Hyperlink" } } }, + { "w:basedOn": { _attr: { "w:val": "DefaultParagraphFont" } } }, + { + "w:uiPriority": { + _attr: { + "w:val": 99, + }, + }, + }, + { + "w:unhideWhenUsed": EMPTY_OBJECT, + }, { "w:rPr": [ { "w:u": { _attr: { "w:val": "single" } } }, @@ -304,17 +314,6 @@ describe("Default Styles", () => { }, ], }, - { - "w:uiPriority": { - _attr: { - "w:val": 99, - }, - }, - }, - { - "w:unhideWhenUsed": EMPTY_OBJECT, - }, - { "w:basedOn": { _attr: { "w:val": "DefaultParagraphFont" } } }, ], }); }); diff --git a/src/file/styles/style/paragraph-style.ts b/src/file/styles/style/paragraph-style.ts index 25c035e514..5f90a87fe2 100644 --- a/src/file/styles/style/paragraph-style.ts +++ b/src/file/styles/style/paragraph-style.ts @@ -1,17 +1,8 @@ import { IParagraphStylePropertiesOptions, IRunStylePropertiesOptions, ParagraphProperties } from "file/paragraph"; import { RunProperties } from "file/paragraph/run/properties"; +import { IStyleOptions, Style } from "./style"; -import { BasedOn, Link, Next, QuickFormat, SemiHidden, UiPriority, UnhideWhenUsed } from "./components"; -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; +export interface IBaseParagraphStyleOptions extends IStyleOptions { readonly paragraph?: IParagraphStylePropertiesOptions; readonly run?: IRunStylePropertiesOptions; } @@ -26,40 +17,12 @@ export class StyleForParagraph extends Style { private readonly runProperties: RunProperties; constructor(options: IParagraphStyleOptions) { - super({ type: "paragraph", styleId: options.id }, options.name); + super({ type: "paragraph", styleId: options.id }, options); this.paragraphProperties = new ParagraphProperties(options.paragraph); this.runProperties = new RunProperties(options.run); this.root.push(this.paragraphProperties); this.root.push(this.runProperties); - - if (options.basedOn) { - this.root.push(new BasedOn(options.basedOn)); - } - - if (options.next) { - this.root.push(new Next(options.next)); - } - - if (options.quickFormat) { - this.root.push(new QuickFormat()); - } - - if (options.link) { - this.root.push(new Link(options.link)); - } - - if (options.semiHidden) { - this.root.push(new SemiHidden()); - } - - if (options.uiPriority) { - this.root.push(new UiPriority(options.uiPriority)); - } - - if (options.unhideWhenUsed) { - this.root.push(new UnhideWhenUsed()); - } } } diff --git a/src/file/styles/style/style.spec.ts b/src/file/styles/style/style.spec.ts index 55f55bd398..4135272e5d 100644 --- a/src/file/styles/style/style.spec.ts +++ b/src/file/styles/style/style.spec.ts @@ -5,11 +5,14 @@ import { Style } from "./style"; describe("Style", () => { describe("#constructor()", () => { it("should set the given properties", () => { - const style = new Style({ - type: "paragraph", - styleId: "myStyleId", - default: true, - }); + const style = new Style( + { + type: "paragraph", + styleId: "myStyleId", + default: true, + }, + {}, + ); const tree = new Formatter().format(style); expect(tree).to.deep.equal({ "w:style": { _attr: { "w:type": "paragraph", "w:styleId": "myStyleId", "w:default": true } }, @@ -22,7 +25,7 @@ describe("Style", () => { type: "paragraph", styleId: "myStyleId", }, - "Style Name", + { name: "Style Name" }, ); const tree = new Formatter().format(style); expect(tree).to.deep.equal({ diff --git a/src/file/styles/style/style.ts b/src/file/styles/style/style.ts index d0d69f17ed..767539503b 100644 --- a/src/file/styles/style/style.ts +++ b/src/file/styles/style/style.ts @@ -1,5 +1,36 @@ -import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; -import { Name } from "./components"; +import { OnOffElement, StringValueElement, XmlAttributeComponent, XmlComponent } from "file/xml-components"; +import { Name, UiPriority } from "./components"; + +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// export interface IStyleAttributes { readonly type?: string; @@ -8,6 +39,17 @@ export interface IStyleAttributes { readonly customStyle?: string; } +export interface IStyleOptions { + readonly name?: string; + readonly basedOn?: string; + readonly next?: string; + readonly link?: string; + readonly uiPriority?: number; + readonly semiHidden?: boolean; + readonly unhideWhenUsed?: boolean; + readonly quickFormat?: boolean; +} + class StyleAttributes extends XmlAttributeComponent { protected readonly xmlKeys = { type: "w:type", @@ -18,11 +60,39 @@ class StyleAttributes extends XmlAttributeComponent { } export class Style extends XmlComponent { - constructor(attributes: IStyleAttributes, name?: string) { + constructor(attributes: IStyleAttributes, options: IStyleOptions) { super("w:style"); this.root.push(new StyleAttributes(attributes)); - if (name) { - this.root.push(new Name(name)); + if (options.name) { + this.root.push(new Name(options.name)); + } + + if (options.basedOn) { + this.root.push(new StringValueElement("w:basedOn", options.basedOn)); + } + + if (options.next) { + this.root.push(new StringValueElement("w:next", options.next)); + } + + if (options.link) { + this.root.push(new StringValueElement("w:link", options.link)); + } + + if (options.uiPriority !== undefined) { + this.root.push(new UiPriority(options.uiPriority)); + } + + if (options.semiHidden !== undefined) { + this.root.push(new OnOffElement("w:semiHidden", options.semiHidden)); + } + + if (options.unhideWhenUsed !== undefined) { + this.root.push(new OnOffElement("w:unhideWhenUsed", options.unhideWhenUsed)); + } + + if (options.quickFormat !== undefined) { + this.root.push(new OnOffElement("w:qFormat", options.quickFormat)); } } } diff --git a/src/file/styles/styles.ts b/src/file/styles/styles.ts index 071a386c00..e37fe8c263 100644 --- a/src/file/styles/styles.ts +++ b/src/file/styles/styles.ts @@ -12,6 +12,13 @@ export interface IStylesOptions { readonly importedStyles?: (XmlComponent | StyleForParagraph | StyleForCharacter | ImportedXmlComponent)[]; } +// +// +// +// +// +// +// export class Styles extends XmlComponent { constructor(options: IStylesOptions) { super("w:styles");