Turn Run into a declaritive API
This commit is contained in:
@ -15,7 +15,7 @@ class Break extends XmlComponent {
|
||||
|
||||
export class PageBreak extends Run {
|
||||
constructor() {
|
||||
super();
|
||||
super({});
|
||||
this.root.push(new Break());
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,10 @@ export class Hyperlink extends XmlComponent {
|
||||
|
||||
const attributes = new HyperlinkAttributes(props);
|
||||
this.root.push(attributes);
|
||||
this.textRun = new TextRun(text).style("Hyperlink");
|
||||
this.textRun = new TextRun({
|
||||
text: text,
|
||||
style: "Hyperlink",
|
||||
});
|
||||
this.root.push(this.textRun);
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,7 @@ export class PictureRun extends Run {
|
||||
private readonly drawing: Drawing;
|
||||
|
||||
constructor(imageData: IMediaData, drawingOptions?: IDrawingOptions) {
|
||||
super();
|
||||
super({});
|
||||
|
||||
if (imageData === undefined) {
|
||||
throw new Error("imageData cannot be undefined");
|
||||
|
@ -4,17 +4,14 @@ import { Formatter } from "export/formatter";
|
||||
import { Utility } from "tests/utility";
|
||||
|
||||
import { Run } from "./";
|
||||
import { UnderlineType } from "./underline";
|
||||
|
||||
describe("Run", () => {
|
||||
let run: Run;
|
||||
|
||||
beforeEach(() => {
|
||||
run = new Run();
|
||||
});
|
||||
|
||||
describe("#bold()", () => {
|
||||
it("it should add bold to the properties", () => {
|
||||
run.bold();
|
||||
const run = new Run({
|
||||
bold: true,
|
||||
});
|
||||
const newJson = Utility.jsonify(run);
|
||||
assert.equal(newJson.root[0].root[0].rootKey, "w:b");
|
||||
assert.equal(newJson.root[0].root[1].rootKey, "w:bCs");
|
||||
@ -23,7 +20,9 @@ describe("Run", () => {
|
||||
|
||||
describe("#italics()", () => {
|
||||
it("it should add italics to the properties", () => {
|
||||
run.italics();
|
||||
const run = new Run({
|
||||
italics: true,
|
||||
});
|
||||
const newJson = Utility.jsonify(run);
|
||||
assert.equal(newJson.root[0].root[0].rootKey, "w:i");
|
||||
assert.equal(newJson.root[0].root[1].rootKey, "w:iCs");
|
||||
@ -31,14 +30,10 @@ describe("Run", () => {
|
||||
});
|
||||
|
||||
describe("#underline()", () => {
|
||||
it("it should add underline to the properties", () => {
|
||||
run.underline();
|
||||
const newJson = Utility.jsonify(run);
|
||||
assert.equal(newJson.root[0].root[0].rootKey, "w:u");
|
||||
});
|
||||
|
||||
it("should default to 'single' and no color", () => {
|
||||
run.underline();
|
||||
const run = new Run({
|
||||
underline: {},
|
||||
});
|
||||
const tree = new Formatter().format(run);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:r": [{ "w:rPr": [{ "w:u": { _attr: { "w:val": "single" } } }] }],
|
||||
@ -46,7 +41,12 @@ describe("Run", () => {
|
||||
});
|
||||
|
||||
it("should set the style type and color if given", () => {
|
||||
run.underline("double", "990011");
|
||||
const run = new Run({
|
||||
underline: {
|
||||
type: UnderlineType.DOUBLE,
|
||||
color: "990011",
|
||||
},
|
||||
});
|
||||
const tree = new Formatter().format(run);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:r": [{ "w:rPr": [{ "w:u": { _attr: { "w:val": "double", "w:color": "990011" } } }] }],
|
||||
@ -56,7 +56,9 @@ describe("Run", () => {
|
||||
|
||||
describe("#smallCaps()", () => {
|
||||
it("it should add smallCaps to the properties", () => {
|
||||
run.smallCaps();
|
||||
const run = new Run({
|
||||
smallCaps: true,
|
||||
});
|
||||
const newJson = Utility.jsonify(run);
|
||||
assert.equal(newJson.root[0].root[0].rootKey, "w:smallCaps");
|
||||
});
|
||||
@ -64,7 +66,9 @@ describe("Run", () => {
|
||||
|
||||
describe("#caps()", () => {
|
||||
it("it should add caps to the properties", () => {
|
||||
run.allCaps();
|
||||
const run = new Run({
|
||||
allCaps: true,
|
||||
});
|
||||
const newJson = Utility.jsonify(run);
|
||||
assert.equal(newJson.root[0].root[0].rootKey, "w:caps");
|
||||
});
|
||||
@ -72,7 +76,9 @@ describe("Run", () => {
|
||||
|
||||
describe("#strike()", () => {
|
||||
it("it should add strike to the properties", () => {
|
||||
run.strike();
|
||||
const run = new Run({
|
||||
strike: true,
|
||||
});
|
||||
const newJson = Utility.jsonify(run);
|
||||
assert.equal(newJson.root[0].root[0].rootKey, "w:strike");
|
||||
});
|
||||
@ -80,7 +86,9 @@ describe("Run", () => {
|
||||
|
||||
describe("#doubleStrike()", () => {
|
||||
it("it should add caps to the properties", () => {
|
||||
run.doubleStrike();
|
||||
const run = new Run({
|
||||
doubleStrike: true,
|
||||
});
|
||||
const newJson = Utility.jsonify(run);
|
||||
assert.equal(newJson.root[0].root[0].rootKey, "w:dstrike");
|
||||
});
|
||||
@ -88,6 +96,7 @@ describe("Run", () => {
|
||||
|
||||
describe("#break()", () => {
|
||||
it("it should add break to the run", () => {
|
||||
const run = new Run({});
|
||||
run.break();
|
||||
const newJson = Utility.jsonify(run);
|
||||
assert.equal(newJson.root[1].rootKey, "w:br");
|
||||
@ -96,6 +105,7 @@ describe("Run", () => {
|
||||
|
||||
describe("#tab()", () => {
|
||||
it("it should add break to the run", () => {
|
||||
const run = new Run({});
|
||||
run.tab();
|
||||
const newJson = Utility.jsonify(run);
|
||||
assert.equal(newJson.root[1].rootKey, "w:tab");
|
||||
@ -103,12 +113,12 @@ describe("Run", () => {
|
||||
});
|
||||
|
||||
describe("#font()", () => {
|
||||
it("should allow chaining calls", () => {
|
||||
expect(run.font("Times")).to.equal(run);
|
||||
});
|
||||
|
||||
it("should set the font as named", () => {
|
||||
run.font("Times");
|
||||
const run = new Run({
|
||||
font: {
|
||||
name: "Times",
|
||||
},
|
||||
});
|
||||
const tree = new Formatter().format(run);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:r": [
|
||||
@ -124,7 +134,9 @@ describe("Run", () => {
|
||||
|
||||
describe("#color", () => {
|
||||
it("should set the run to the color given", () => {
|
||||
run.color("001122");
|
||||
const run = new Run({
|
||||
color: "001122",
|
||||
});
|
||||
const tree = new Formatter().format(run);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:r": [{ "w:rPr": [{ "w:color": { _attr: { "w:val": "001122" } } }] }],
|
||||
@ -134,7 +146,9 @@ describe("Run", () => {
|
||||
|
||||
describe("#size", () => {
|
||||
it("should set the run to the given size", () => {
|
||||
run.size(24);
|
||||
const run = new Run({
|
||||
size: 24,
|
||||
});
|
||||
const tree = new Formatter().format(run);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:r": [
|
||||
@ -148,7 +162,9 @@ describe("Run", () => {
|
||||
|
||||
describe("#rtl", () => {
|
||||
it("should set the run to the RTL mode", () => {
|
||||
run.rightToLeft();
|
||||
const run = new Run({
|
||||
rightToLeft: true,
|
||||
});
|
||||
const tree = new Formatter().format(run);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:r": [{ "w:rPr": [{ "w:rtl": { _attr: { "w:val": true } } }] }],
|
||||
@ -158,6 +174,7 @@ describe("Run", () => {
|
||||
|
||||
describe("#numberOfTotalPages", () => {
|
||||
it("should set the run to the RTL mode", () => {
|
||||
const run = new Run({});
|
||||
run.numberOfTotalPages();
|
||||
const tree = new Formatter().format(run);
|
||||
expect(tree).to.deep.equal({
|
||||
@ -173,6 +190,7 @@ describe("Run", () => {
|
||||
|
||||
describe("#pageNumber", () => {
|
||||
it("should set the run to the RTL mode", () => {
|
||||
const run = new Run({});
|
||||
run.pageNumber();
|
||||
const tree = new Formatter().format(run);
|
||||
expect(tree).to.deep.equal({
|
||||
@ -188,7 +206,9 @@ describe("Run", () => {
|
||||
|
||||
describe("#style", () => {
|
||||
it("should set the style to the given styleId", () => {
|
||||
run.style("myRunStyle");
|
||||
const run = new Run({
|
||||
style: "myRunStyle",
|
||||
});
|
||||
const tree = new Formatter().format(run);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:r": [{ "w:rPr": [{ "w:rStyle": { _attr: { "w:val": "myRunStyle" } } }] }],
|
||||
|
@ -20,50 +20,99 @@ import { RunFonts } from "./run-fonts";
|
||||
import { SubScript, SuperScript } from "./script";
|
||||
import { Style } from "./style";
|
||||
import { Tab } from "./tab";
|
||||
import { Underline } from "./underline";
|
||||
import { Underline, UnderlineType } from "./underline";
|
||||
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
export interface IRunOptions {
|
||||
readonly bold?: true;
|
||||
readonly italics?: true;
|
||||
readonly underline?: {
|
||||
readonly color?: string;
|
||||
readonly type?: UnderlineType;
|
||||
};
|
||||
readonly color?: string;
|
||||
readonly size?: number;
|
||||
readonly rightToLeft?: boolean;
|
||||
readonly smallCaps?: boolean;
|
||||
readonly allCaps?: boolean;
|
||||
readonly strike?: boolean;
|
||||
readonly doubleStrike?: boolean;
|
||||
readonly subScript?: boolean;
|
||||
readonly superScript?: boolean;
|
||||
readonly style?: string;
|
||||
readonly font?: {
|
||||
readonly name: string;
|
||||
readonly hint?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export class Run extends XmlComponent {
|
||||
protected readonly properties: RunProperties;
|
||||
|
||||
constructor() {
|
||||
constructor(options: IRunOptions) {
|
||||
super("w:r");
|
||||
this.properties = new RunProperties();
|
||||
this.root.push(this.properties);
|
||||
}
|
||||
|
||||
public bold(): Run {
|
||||
this.properties.push(new Bold());
|
||||
this.properties.push(new BoldComplexScript());
|
||||
return this;
|
||||
}
|
||||
if (options.bold) {
|
||||
this.properties.push(new Bold());
|
||||
this.properties.push(new BoldComplexScript());
|
||||
}
|
||||
|
||||
public italics(): Run {
|
||||
this.properties.push(new Italics());
|
||||
this.properties.push(new ItalicsComplexScript());
|
||||
return this;
|
||||
}
|
||||
if (options.italics) {
|
||||
this.properties.push(new Italics());
|
||||
this.properties.push(new ItalicsComplexScript());
|
||||
}
|
||||
|
||||
public underline(underlineType?: string, color?: string): Run {
|
||||
this.properties.push(new Underline(underlineType, color));
|
||||
return this;
|
||||
}
|
||||
if (options.underline) {
|
||||
this.properties.push(new Underline(options.underline.type, options.underline.color));
|
||||
}
|
||||
|
||||
public color(color: string): Run {
|
||||
this.properties.push(new Color(color));
|
||||
return this;
|
||||
}
|
||||
if (options.color) {
|
||||
this.properties.push(new Color(options.color));
|
||||
}
|
||||
|
||||
public size(size: number): Run {
|
||||
this.properties.push(new Size(size));
|
||||
this.properties.push(new SizeComplexScript(size));
|
||||
return this;
|
||||
}
|
||||
if (options.size) {
|
||||
this.properties.push(new Size(options.size));
|
||||
this.properties.push(new SizeComplexScript(options.size));
|
||||
}
|
||||
|
||||
public rightToLeft(): Run {
|
||||
this.properties.push(new RightToLeft());
|
||||
return this;
|
||||
if (options.rightToLeft) {
|
||||
this.properties.push(new RightToLeft());
|
||||
}
|
||||
|
||||
if (options.smallCaps) {
|
||||
this.properties.push(new SmallCaps());
|
||||
}
|
||||
|
||||
if (options.allCaps) {
|
||||
this.properties.push(new Caps());
|
||||
}
|
||||
|
||||
if (options.strike) {
|
||||
this.properties.push(new Strike());
|
||||
}
|
||||
|
||||
if (options.doubleStrike) {
|
||||
this.properties.push(new DoubleStrike());
|
||||
}
|
||||
|
||||
if (options.subScript) {
|
||||
this.properties.push(new SubScript());
|
||||
}
|
||||
|
||||
if (options.superScript) {
|
||||
this.properties.push(new SuperScript());
|
||||
}
|
||||
|
||||
if (options.style) {
|
||||
this.properties.push(new Style(options.style));
|
||||
}
|
||||
|
||||
if (options.font) {
|
||||
this.properties.push(new RunFonts(options.font.name, options.font.hint));
|
||||
}
|
||||
}
|
||||
|
||||
public break(): Run {
|
||||
@ -91,44 +140,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;
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import { SequentialIdentifierInstruction } from "./sequential-identifier-instruc
|
||||
|
||||
export class SequentialIdentifier extends Run {
|
||||
constructor(identifier: string) {
|
||||
super();
|
||||
super({});
|
||||
this.root.push(new Begin(true));
|
||||
this.root.push(new SequentialIdentifierInstruction(identifier));
|
||||
this.root.push(new Separate());
|
||||
|
@ -1,9 +1,19 @@
|
||||
import { Run } from "../run";
|
||||
import { IRunOptions, Run } from "./run";
|
||||
import { Text } from "./run-components/text";
|
||||
|
||||
export interface ITextRunOptions extends IRunOptions {
|
||||
readonly text: string;
|
||||
}
|
||||
|
||||
export class TextRun extends Run {
|
||||
constructor(text: string) {
|
||||
super();
|
||||
this.root.push(new Text(text));
|
||||
constructor(options: ITextRunOptions | string) {
|
||||
if (typeof options === "string") {
|
||||
super({});
|
||||
this.root.push(new Text(options));
|
||||
return;
|
||||
}
|
||||
|
||||
super(options);
|
||||
this.root.push(new Text(options.text));
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,25 @@
|
||||
import { Attributes, XmlComponent } from "file/xml-components";
|
||||
|
||||
export enum UnderlineType {
|
||||
SINGLE = "single",
|
||||
WORDS = "words",
|
||||
DOUBLE = "double",
|
||||
THICK = "thick",
|
||||
DOTTED = "dotted",
|
||||
DOTTEDHEAVY = "dottedHeavy",
|
||||
DASH = "dash",
|
||||
DASHEDHEAVY = "dashedHeavy",
|
||||
DASHLONG = "dashLong",
|
||||
DASHLONGHEAVY = "dashLongHeavy",
|
||||
DOTDASH = "dotDash",
|
||||
DASHDOTHEAVY = "dashDotHeavy",
|
||||
DOTDOTDASH = "dotDotDash",
|
||||
DASHDOTDOTHEAVY = "dashDotDotHeavy",
|
||||
WAVE = "wave",
|
||||
WAVYHEAVY = "wavyHeavy",
|
||||
WAVYDOUBLE = "wavyDouble",
|
||||
}
|
||||
|
||||
export abstract class BaseUnderline extends XmlComponent {
|
||||
constructor(underlineType: string, color?: string) {
|
||||
super("w:u");
|
||||
|
Reference in New Issue
Block a user