Consolidate highlight and shadow API

This commit is contained in:
Dolan
2019-08-06 21:37:33 +01:00
parent 099eb1d202
commit e45e7ffe06
4 changed files with 86 additions and 90 deletions

View File

@ -1,24 +1,32 @@
import * as fs from "fs"; 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 doc = new Document();
const header = doc.Header.createTable(1, 3) doc.addSection({
// @ts-ignore headers: {
header.properties.root[1] = [] default: new Header({
children: [
header.getCell(0, 2).addParagraph( new Paragraph({
new Paragraph() alignment: AlignmentType.RIGHT,
.addRun( children: [
new TextRun('W.P. 660') new TextRun({
.color('red') text: "Hello World",
.bold() color: "red",
.size(12 * 2) bold: true,
.font('Garamond') size: 24,
.highlight('yellow') font: {
) name: "Garamond",
.right() },
) highlight: "yellow",
}),
],
}),
],
}),
},
children: [],
});
const packer = new Packer(); const packer = new Packer();

View File

@ -1,24 +1,36 @@
import * as fs from "fs"; 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 doc = new Document();
const header = doc.Header.createTable(1, 3) doc.addSection({
// @ts-ignore headers: {
header.properties.root[1] = [] default: new Header({
children: [
header.getCell(0, 2).addParagraph( new Paragraph({
new Paragraph() alignment: AlignmentType.RIGHT,
.addRun( children: [
new TextRun('W.P. 660') new TextRun({
.color('red') text: "Hello World",
.bold() color: "red",
.size(12 * 2) bold: true,
.font('Garamond') size: 24,
.shadow('pct10','00FFFF','FF0000') font: {
) name: "Garamond",
.right() },
) shadow: {
type: ShadingType.REVERSE_DIAGONAL_STRIPE,
color: "00FFFF",
fill: "FF0000",
},
}),
],
}),
],
}),
},
children: [],
});
const packer = new Packer(); const packer = new Packer();

View File

@ -1,6 +1,7 @@
import { expect } from "chai"; import { expect } from "chai";
import { Formatter } from "export/formatter"; import { Formatter } from "export/formatter";
import { ShadingType } from "file/table";
import { Run } from "./"; import { Run } from "./";
import { UnderlineType } from "./underline"; import { UnderlineType } from "./underline";
@ -131,7 +132,10 @@ describe("Run", () => {
describe("#highlight()", () => { describe("#highlight()", () => {
it("it should add highlight to the properties", () => { 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); const tree = new Formatter().format(run);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:r": [ "w:r": [
@ -154,7 +158,14 @@ describe("Run", () => {
describe("#shadow()", () => { describe("#shadow()", () => {
it("it should add shadow to the properties", () => { 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); const tree = new Formatter().format(run);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:r": [ "w:r": [

View File

@ -1,4 +1,7 @@
// http://officeopenxml.com/WPtext.php // http://officeopenxml.com/WPtext.php
import { ShadingType } from "file/table";
import { XmlComponent } from "file/xml-components";
import { Break } from "./break"; import { Break } from "./break";
import { Caps, SmallCaps } from "./caps"; import { Caps, SmallCaps } from "./caps";
import { Begin, End, Separate } from "./field"; import { Begin, End, Separate } from "./field";
@ -26,8 +29,6 @@ import { Style } from "./style";
import { Tab } from "./tab"; import { Tab } from "./tab";
import { Underline, UnderlineType } from "./underline"; import { Underline, UnderlineType } from "./underline";
import { XmlComponent } from "file/xml-components";
export interface IRunOptions { export interface IRunOptions {
readonly bold?: true; readonly bold?: true;
readonly italics?: true; readonly italics?: true;
@ -49,6 +50,12 @@ export interface IRunOptions {
readonly name: string; readonly name: string;
readonly hint?: string; readonly hint?: string;
}; };
readonly highlight?: string;
readonly shadow?: {
readonly type: ShadingType;
readonly fill: string;
readonly color: string;
};
} }
export class Run extends XmlComponent { export class Run extends XmlComponent {
@ -117,6 +124,16 @@ export class Run extends XmlComponent {
if (options.font) { if (options.font) {
this.properties.push(new RunFonts(options.font.name, options.font.hint)); 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 { public break(): Run {
@ -144,56 +161,4 @@ export class Run extends XmlComponent {
this.root.push(new End()); this.root.push(new End());
return this; 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;
}
} }