From 1b336785b4c6e89c3dd43d435b70439832ac90eb Mon Sep 17 00:00:00 2001 From: fmuscolino Date: Tue, 26 Feb 2019 17:28:13 +0100 Subject: [PATCH 1/2] Add bold and italics to character style --- src/file/styles/style/character-style.spec.ts | 50 +++++++++++++++++++ src/file/styles/style/character-style.ts | 8 +++ 2 files changed, 58 insertions(+) diff --git a/src/file/styles/style/character-style.spec.ts b/src/file/styles/style/character-style.spec.ts index 1556aae109..727b7f3a3f 100644 --- a/src/file/styles/style/character-style.spec.ts +++ b/src/file/styles/style/character-style.spec.ts @@ -243,6 +243,56 @@ describe("CharacterStyle", () => { }); }); + it("#bold", () => { + const style = new CharacterStyle("myStyleId").bold(); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, + { + "w:rPr": [{ "w:b": [{ _attr: { "w:val": true } }] }], + }, + { + "w:uiPriority": [ + { + _attr: { + "w:val": "99", + }, + }, + ], + }, + { + "w:unhideWhenUsed": [], + }, + ], + }); + }); + + it("#italics", () => { + const style = new CharacterStyle("myStyleId").italics(); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + { _attr: { "w:type": "character", "w:styleId": "myStyleId" } }, + { + "w:rPr": [{ "w:i": [{ _attr: { "w:val": true } }] }], + }, + { + "w:uiPriority": [ + { + _attr: { + "w:val": "99", + }, + }, + ], + }, + { + "w:unhideWhenUsed": [], + }, + ], + }); + }); + it("#link", () => { const style = new CharacterStyle("myStyleId").link("MyLink"); const tree = new Formatter().format(style); diff --git a/src/file/styles/style/character-style.ts b/src/file/styles/style/character-style.ts index f7354ae788..c7f677b331 100644 --- a/src/file/styles/style/character-style.ts +++ b/src/file/styles/style/character-style.ts @@ -29,6 +29,14 @@ export class CharacterStyle extends Style { return this.addRunProperty(new formatting.Color(color)); } + public bold(): CharacterStyle { + return this.addRunProperty(new formatting.Bold()); + } + + public italics(): CharacterStyle { + return this.addRunProperty(new formatting.Italics()); + } + public underline(underlineType?: string, color?: string): CharacterStyle { return this.addRunProperty(new formatting.Underline(underlineType, color)); } From 3e2130bc801672f4de4fe7cb08ec288280b8e444 Mon Sep 17 00:00:00 2001 From: fmuscolino Date: Tue, 26 Feb 2019 13:26:01 +0100 Subject: [PATCH 2/2] Customize text run of an hyperlink --- src/file/paragraph/links/hyperlink.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/file/paragraph/links/hyperlink.ts b/src/file/paragraph/links/hyperlink.ts index ecdfafe803..3ed33f44ad 100644 --- a/src/file/paragraph/links/hyperlink.ts +++ b/src/file/paragraph/links/hyperlink.ts @@ -5,6 +5,7 @@ import { HyperlinkAttributes, IHyperlinkAttributesProperties } from "./hyperlink export class Hyperlink extends XmlComponent { public readonly linkId: number; + private readonly textRun: TextRun; constructor(text: string, relationshipsCount: number, anchor?: string) { super("w:hyperlink"); @@ -19,6 +20,11 @@ export class Hyperlink extends XmlComponent { const attributes = new HyperlinkAttributes(props); this.root.push(attributes); - this.root.push(new TextRun(text).style("Hyperlink")); + this.textRun = new TextRun(text).style("Hyperlink"); + this.root.push(this.textRun); + } + + public get TextRun(): TextRun { + return this.textRun; } }