diff --git a/demo/demo45.ts b/demo/demo45.ts index 7cef926537..dc44693e4d 100644 --- a/demo/demo45.ts +++ b/demo/demo45.ts @@ -1,24 +1,32 @@ import * as fs from "fs"; -import { Document, Packer, Paragraph, TextRun } from "../build"; +import { AlignmentType, Document, Header, Packer, Paragraph, TextRun } from "../build"; const doc = new Document(); -const header = doc.Header.createTable(1, 3) - // @ts-ignore - header.properties.root[1] = [] - - header.getCell(0, 2).addParagraph( - new Paragraph() - .addRun( - new TextRun('W.P. 660') - .color('red') - .bold() - .size(12 * 2) - .font('Garamond') - .highlight('yellow') - ) - .right() - ) +doc.addSection({ + headers: { + default: new Header({ + children: [ + new Paragraph({ + alignment: AlignmentType.RIGHT, + children: [ + new TextRun({ + text: "Hello World", + color: "red", + bold: true, + size: 24, + font: { + name: "Garamond", + }, + highlight: "yellow", + }), + ], + }), + ], + }), + }, + children: [], +}); const packer = new Packer(); diff --git a/demo/demo46.ts b/demo/demo46.ts index bc38e8dc19..2bb31abdf9 100644 --- a/demo/demo46.ts +++ b/demo/demo46.ts @@ -1,24 +1,36 @@ import * as fs from "fs"; -import { Document, Packer, Paragraph, TextRun } from "../build"; +import { AlignmentType, Document, Header, Packer, Paragraph, ShadingType, TextRun } from "../build"; const doc = new Document(); -const header = doc.Header.createTable(1, 3) - // @ts-ignore - header.properties.root[1] = [] - - header.getCell(0, 2).addParagraph( - new Paragraph() - .addRun( - new TextRun('W.P. 660') - .color('red') - .bold() - .size(12 * 2) - .font('Garamond') - .shadow('pct10','00FFFF','FF0000') - ) - .right() - ) +doc.addSection({ + headers: { + default: new Header({ + children: [ + new Paragraph({ + alignment: AlignmentType.RIGHT, + children: [ + new TextRun({ + text: "Hello World", + color: "red", + bold: true, + size: 24, + font: { + name: "Garamond", + }, + shadow: { + type: ShadingType.REVERSE_DIAGONAL_STRIPE, + color: "00FFFF", + fill: "FF0000", + }, + }), + ], + }), + ], + }), + }, + children: [], +}); const packer = new Packer(); diff --git a/src/file/paragraph/run/run.spec.ts b/src/file/paragraph/run/run.spec.ts index a77e59844f..ed43584292 100644 --- a/src/file/paragraph/run/run.spec.ts +++ b/src/file/paragraph/run/run.spec.ts @@ -1,6 +1,7 @@ import { expect } from "chai"; import { Formatter } from "export/formatter"; +import { ShadingType } from "file/table"; import { Run } from "./"; import { UnderlineType } from "./underline"; @@ -131,7 +132,10 @@ describe("Run", () => { describe("#highlight()", () => { it("it should add highlight to the properties", () => { - run.highlight("005599"); + const run = new Run({ + doubleStrike: true, + highlight: "005599", + }); const tree = new Formatter().format(run); expect(tree).to.deep.equal({ "w:r": [ @@ -154,7 +158,14 @@ describe("Run", () => { describe("#shadow()", () => { it("it should add shadow to the properties", () => { - run.shadow("pct10", "00FFFF", "FF0000"); + const run = new Run({ + doubleStrike: true, + shadow: { + type: ShadingType.PERCENT_10, + fill: "00FFFF", + color: "FF0000", + }, + }); const tree = new Formatter().format(run); expect(tree).to.deep.equal({ "w:r": [ diff --git a/src/file/paragraph/run/run.ts b/src/file/paragraph/run/run.ts index 5b62d5b9c8..2e5cd63439 100644 --- a/src/file/paragraph/run/run.ts +++ b/src/file/paragraph/run/run.ts @@ -1,4 +1,7 @@ // http://officeopenxml.com/WPtext.php +import { ShadingType } from "file/table"; +import { XmlComponent } from "file/xml-components"; + import { Break } from "./break"; import { Caps, SmallCaps } from "./caps"; import { Begin, End, Separate } from "./field"; @@ -26,8 +29,6 @@ import { Style } from "./style"; import { Tab } from "./tab"; import { Underline, UnderlineType } from "./underline"; -import { XmlComponent } from "file/xml-components"; - export interface IRunOptions { readonly bold?: true; readonly italics?: true; @@ -49,6 +50,12 @@ export interface IRunOptions { readonly name: string; readonly hint?: string; }; + readonly highlight?: string; + readonly shadow?: { + readonly type: ShadingType; + readonly fill: string; + readonly color: string; + }; } export class Run extends XmlComponent { @@ -117,6 +124,16 @@ export class Run extends XmlComponent { if (options.font) { this.properties.push(new RunFonts(options.font.name, options.font.hint)); } + + if (options.highlight) { + this.properties.push(new Highlight(options.highlight)); + this.properties.push(new HighlightComplexScript(options.highlight)); + } + + if (options.shadow) { + this.properties.push(new Shadow(options.shadow.type, options.shadow.fill, options.shadow.color)); + this.properties.push(new ShadowComplexScript(options.shadow.type, options.shadow.fill, options.shadow.color)); + } } public break(): Run { @@ -144,56 +161,4 @@ export class Run extends XmlComponent { this.root.push(new End()); return this; } - - public smallCaps(): Run { - this.properties.push(new SmallCaps()); - return this; - } - - public allCaps(): Run { - this.properties.push(new Caps()); - return this; - } - - public strike(): Run { - this.properties.push(new Strike()); - return this; - } - - public doubleStrike(): Run { - this.properties.push(new DoubleStrike()); - return this; - } - - public subScript(): Run { - this.properties.push(new SubScript()); - return this; - } - - public superScript(): Run { - this.properties.push(new SuperScript()); - return this; - } - - public font(fontName: string, hint?: string | undefined): Run { - this.properties.push(new RunFonts(fontName, hint)); - return this; - } - - public style(styleId: string): Run { - this.properties.push(new Style(styleId)); - return this; - } - - public highlight(color: string): Run { - this.properties.push(new Highlight(color)); - this.properties.push(new HighlightComplexScript(color)); - return this; - } - - public shadow(value: string, fill: string, color: string): Run { - this.properties.push(new Shadow(value, fill, color)); - this.properties.push(new ShadowComplexScript(value, fill, color)); - return this; - } }