Turn Run into a declaritive API

This commit is contained in:
Dolan
2019-06-17 01:51:57 +01:00
parent a1cd5e9573
commit fb65bb4207
27 changed files with 411 additions and 183 deletions

View File

@ -6,8 +6,14 @@ import { Document, Packer, Paragraph, TextRun } from "../build";
const doc = new Document(); const doc = new Document();
const paragraph = new Paragraph("Hello World"); const paragraph = new Paragraph("Hello World");
const institutionText = new TextRun("Foo Bar").bold(); const institutionText = new TextRun({
const dateText = new TextRun("Github is the best").tab().bold(); text: "Foo Bar",
bold: true,
});
const dateText = new TextRun({
text: "Github is the best",
bold: true,
}).tab();
paragraph.addRun(institutionText); paragraph.addRun(institutionText);
paragraph.addRun(dateText); paragraph.addRun(dateText);

View File

@ -241,8 +241,14 @@ class DocumentCreator {
maxRight: {}, maxRight: {},
}, },
}); });
const institution = new TextRun(institutionName).bold(); const institution = new TextRun({
const date = new TextRun(dateText).tab().bold(); text: institutionName,
bold: true,
});
const date = new TextRun({
text: dateText,
bold: true,
}).tab();
paragraph.addRun(institution); paragraph.addRun(institution);
paragraph.addRun(date); paragraph.addRun(date);
@ -252,7 +258,10 @@ class DocumentCreator {
public createRoleText(roleText: string): Paragraph { public createRoleText(roleText: string): Paragraph {
const paragraph = new Paragraph({}); const paragraph = new Paragraph({});
const role = new TextRun(roleText).italics(); const role = new TextRun({
text: roleText,
italics: true,
});
paragraph.addRun(role); paragraph.addRun(role);

View File

@ -6,8 +6,14 @@ import { Document, Packer, Paragraph, TextRun } from "../build";
const doc = new Document(); const doc = new Document();
const paragraph = new Paragraph("Hello World"); const paragraph = new Paragraph("Hello World");
const institutionText = new TextRun("Foo").bold(); const institutionText = new TextRun({
const dateText = new TextRun("Bar").tab().bold(); text: "Foo",
bold: true,
});
const dateText = new TextRun({
text: "Bar",
bold: true,
}).tab();
paragraph.addRun(institutionText); paragraph.addRun(institutionText);
paragraph.addRun(dateText); paragraph.addRun(dateText);

View File

@ -92,7 +92,16 @@ doc.addParagraph(
}), }),
); );
doc.addParagraph(new Paragraph({}).addRun(new TextRun("Some monospaced content").font("Monospace"))); doc.addParagraph(
new Paragraph({}).addRun(
new TextRun({
text: "Some monospaced content",
font: {
name: "Monospace",
},
}),
),
);
doc.addParagraph( doc.addParagraph(
new Paragraph({ new Paragraph({
@ -108,10 +117,25 @@ doc.addParagraph(
); );
const para = new Paragraph({}); const para = new Paragraph({});
doc.addParagraph(para); doc.addParagraph(para);
para.addRun(new TextRun("This is a bold run,").bold()); // Showing the different ways to create a TextRun
para.addRun(
new TextRun({
text: "This is a bold run,",
bold: true,
}),
);
para.addRun(new TextRun(" switching to normal ")); para.addRun(new TextRun(" switching to normal "));
para.addRun(new TextRun("and then underlined ").underline()); para.addRun(
para.addRun(new TextRun("and back to normal.")); new TextRun({
text: "and then underlined ",
underline: {},
}),
);
para.addRun(
new TextRun({
text: "and back to normal.",
}),
);
const packer = new Packer(); const packer = new Packer();

View File

@ -5,18 +5,35 @@ import { Document, Packer, Paragraph, TextRun } from "../build";
const doc = new Document(); const doc = new Document();
const paragraph1 = new Paragraph().bidirectional(); const paragraph1 = new Paragraph({
const textRun1 = new TextRun("שלום עולם").rightToLeft(); bidirectional: true,
});
const textRun1 = new TextRun({
text: "שלום עולם",
rightToLeft: true,
});
paragraph1.addRun(textRun1); paragraph1.addRun(textRun1);
doc.addParagraph(paragraph1); doc.addParagraph(paragraph1);
const paragraph2 = new Paragraph().bidirectional(); const paragraph2 = new Paragraph({
const textRun2 = new TextRun("שלום עולם").bold().rightToLeft(); bidirectional: true,
});
const textRun2 = new TextRun({
text: "שלום עולם",
bold: true,
rightToLeft: true,
});
paragraph2.addRun(textRun2); paragraph2.addRun(textRun2);
doc.addParagraph(paragraph2); doc.addParagraph(paragraph2);
const paragraph3 = new Paragraph().bidirectional(); const paragraph3 = new Paragraph({
const textRun3 = new TextRun("שלום עולם").italics().rightToLeft(); bidirectional: true,
});
const textRun3 = new TextRun({
text: "שלום עולם",
italics: true,
rightToLeft: true,
});
paragraph3.addRun(textRun3); paragraph3.addRun(textRun3);
doc.addParagraph(paragraph3); doc.addParagraph(paragraph3);

View File

@ -9,9 +9,23 @@ const paragraph = new Paragraph("No border!");
doc.addParagraph(paragraph); doc.addParagraph(paragraph);
const borderParagraph = new Paragraph("I have borders on my top and bottom sides!").createBorder(); const borderParagraph = new Paragraph({
borderParagraph.Borders.addTopBorder(); text: "I have borders on my top and bottom sides!",
borderParagraph.Borders.addBottomBorder(); border: {
top: {
color: "auto",
space: 1,
value: "single",
size: 6,
},
bottom: {
color: "auto",
space: 1,
value: "single",
size: 6,
},
},
});
doc.addParagraph(borderParagraph); doc.addParagraph(borderParagraph);

View File

@ -1,7 +1,7 @@
// Table of contents // Table of contents
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { File, Packer, Paragraph, StyleLevel, TableOfContents } from "../build"; import { File, HeadingLevel, Packer, Paragraph, StyleLevel, TableOfContents } from "../build";
const doc = new File(); const doc = new File();
@ -26,15 +26,30 @@ const toc = new TableOfContents("Summary", {
doc.addTableOfContents(toc); doc.addTableOfContents(toc);
doc.addParagraph(new Paragraph("Header #1").heading1().pageBreakBefore()); doc.addParagraph(new Paragraph({
text: "Header #1",
heading: HeadingLevel.HEADING_1,
pageBreakBefore: true,
}));
doc.addParagraph(new Paragraph("I'm a little text very nicely written.'")); doc.addParagraph(new Paragraph("I'm a little text very nicely written.'"));
doc.addParagraph(new Paragraph("Header #2").heading1().pageBreakBefore()); doc.addParagraph(new Paragraph({
text: "Header #2",
heading: HeadingLevel.HEADING_1,
pageBreakBefore: true,
}));
doc.addParagraph(new Paragraph("I'm a other text very nicely written.'")); doc.addParagraph(new Paragraph("I'm a other text very nicely written.'"));
doc.addParagraph(new Paragraph("Header #2.1").heading2()); doc.addParagraph(new Paragraph({
text: "Header #2.1",
heading: HeadingLevel.HEADING_2,
}));
doc.addParagraph(new Paragraph("I'm a another text very nicely written.'")); doc.addParagraph(new Paragraph("I'm a another text very nicely written.'"));
doc.addParagraph(new Paragraph("My Spectacular Style #1").style("MySpectacularStyle").pageBreakBefore()); doc.addParagraph(new Paragraph({
text: "My Spectacular Style #1",
style: "MySpectacularStyle",
pageBreakBefore: true,
}));
const packer = new Packer(); const packer = new Packer();

View File

@ -12,27 +12,50 @@ abstractNum.createLevel(0, "upperRoman", "%1", "start").addParagraphProperty(new
const concrete = numbering.createConcreteNumbering(abstractNum); const concrete = numbering.createConcreteNumbering(abstractNum);
const item1 = new Paragraph("line with contextual spacing"); const item1 = new Paragraph({
const item2 = new Paragraph("line with contextual spacing"); text: "line with contextual spacing",
const item3 = new Paragraph("line without contextual spacing"); numbering: {
const item4 = new Paragraph("line without contextual spacing"); num: concrete,
level: 0,
item1 },
.setNumbering(concrete, 0) contextualSpacing: true,
.spacing({ before: 200 }) spacing: {
.contextualSpacing(true); before: 200,
item2 },
.setNumbering(concrete, 0) });
.spacing({ before: 200 }) const item2 = new Paragraph({
.contextualSpacing(true); text: "line with contextual spacing",
item3 numbering: {
.setNumbering(concrete, 0) num: concrete,
.spacing({ before: 200 }) level: 0,
.contextualSpacing(false); },
item4 contextualSpacing: true,
.setNumbering(concrete, 0) spacing: {
.spacing({ before: 200 }) before: 200,
.contextualSpacing(false); },
});
const item3 = new Paragraph({
text: "line without contextual spacing",
numbering: {
num: concrete,
level: 0,
},
contextualSpacing: false,
spacing: {
before: 200,
},
});
const item4 = new Paragraph({
text: "line without contextual spacing",
numbering: {
num: concrete,
level: 0,
},
contextualSpacing: false,
spacing: {
before: 200,
},
});
doc.addParagraph(item1); doc.addParagraph(item1);
doc.addParagraph(item2); doc.addParagraph(item2);

View File

@ -14,25 +14,64 @@ abstractNum.createLevel(2, "lowerLetter", "%3)", "start").addParagraphProperty(n
const concrete = numbering.createConcreteNumbering(abstractNum); const concrete = numbering.createConcreteNumbering(abstractNum);
const topLevelP = new Paragraph("Hey you"); const topLevelP = new Paragraph({
const subP = new Paragraph("What's up fam"); text: "Hey you",
const secondSubP = new Paragraph("Hello World 2"); numbering: {
const subSubP = new Paragraph("Yeah boi"); num: concrete,
level: 0,
topLevelP.setNumbering(concrete, 0); },
subP.setNumbering(concrete, 1); });
secondSubP.setNumbering(concrete, 1); const subP = new Paragraph({
subSubP.setNumbering(concrete, 2); text: "What's up fam",
numbering: {
num: concrete,
level: 1,
},
});
const secondSubP = new Paragraph({
text: "Hello World 2",
numbering: {
num: concrete,
level: 1,
},
});
const subSubP = new Paragraph({
text: "Yeah boi",
numbering: {
num: concrete,
level: 2,
},
});
doc.addParagraph(topLevelP); doc.addParagraph(topLevelP);
doc.addParagraph(subP); doc.addParagraph(subP);
doc.addParagraph(secondSubP); doc.addParagraph(secondSubP);
doc.addParagraph(subSubP); doc.addParagraph(subSubP);
const bullet1 = new Paragraph("Hey you").bullet(); const bullet1 = new Paragraph({
const bullet2 = new Paragraph("What's up fam").bullet(1); text: "Hey you",
const bullet3 = new Paragraph("Hello World 2").bullet(2); bullet: {
const bullet4 = new Paragraph("Yeah boi").bullet(3); level: 0,
},
});
const bullet2 = new Paragraph({
text: "What's up fam",
bullet: {
level: 1,
},
});
const bullet3 = new Paragraph({
text: "Hello World 2",
bullet: {
level: 2,
},
});
const bullet4 = new Paragraph({
text: "Yeah boi",
bullet: {
level: 3,
},
});
doc.addParagraph(bullet1); doc.addParagraph(bullet1);
doc.addParagraph(bullet2); doc.addParagraph(bullet2);

View File

@ -1,7 +1,7 @@
// Example of how you would create a table and add data to it // Example of how you would create a table and add data to it
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { Document, Packer, Paragraph, VerticalAlign } from "../build"; import { Document, HeadingLevel, Packer, Paragraph, VerticalAlign } from "../build";
const doc = new Document(); const doc = new Document();
@ -17,9 +17,11 @@ table
table table
.getCell(1, 0) .getCell(1, 0)
.addParagraph( .addParagraph(
new Paragraph( new Paragraph({
"Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah", text:
).heading1(), "Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah",
heading: HeadingLevel.HEADING_1,
}),
); );
const packer = new Packer(); const packer = new Packer();

View File

@ -4,10 +4,9 @@ import * as fs from "fs";
import { Document, Packer, Paragraph } from "../build"; import { Document, Packer, Paragraph } from "../build";
const doc = new Document(); const doc = new Document();
const paragraph = new Paragraph(); const paragraph = new Paragraph({});
const link = doc.createHyperlink("http://www.example.com", "Hyperlink"); const link = doc.createHyperlink("http://www.example.com", "Hyperlink");
link.bold();
paragraph.addHyperLink(link); paragraph.addHyperLink(link);
doc.addParagraph(paragraph); doc.addParagraph(paragraph);

View File

@ -11,8 +11,14 @@ const doc = new Document(undefined, {
}); });
const paragraph = new Paragraph("Hello World"); const paragraph = new Paragraph("Hello World");
const institutionText = new TextRun("Foo bar").bold(); const institutionText = new TextRun({
const dateText = new TextRun("Github is the best").tab().bold(); text: "Foo bar",
bold: true,
});
const dateText = new TextRun({
text: "Github is the best",
bold: true,
}).tab();
paragraph.addRun(institutionText); paragraph.addRun(institutionText);
paragraph.addRun(dateText); paragraph.addRun(dateText);

View File

@ -29,7 +29,12 @@ describe("Formatter", () => {
it("should format simple paragraph with bold text", () => { it("should format simple paragraph with bold text", () => {
const paragraph = new file.Paragraph(""); const paragraph = new file.Paragraph("");
paragraph.addRun(new file.TextRun("test").bold()); paragraph.addRun(
new file.TextRun({
text: "test",
bold: true,
}),
);
const newJson = formatter.format(paragraph); const newJson = formatter.format(paragraph);
assert.isDefined(newJson["w:p"][1]["w:r"][0]["w:rPr"][0]["w:b"]._attr["w:val"]); assert.isDefined(newJson["w:p"][1]["w:r"][0]["w:rPr"][0]["w:b"]._attr["w:val"]);
}); });

View File

@ -30,6 +30,8 @@ import { TableOfContents } from "./table-of-contents";
export class File { export class File {
// tslint:disable-next-line:readonly-keyword // tslint:disable-next-line:readonly-keyword
private currentRelationshipId: number = 1; private currentRelationshipId: number = 1;
// tslint:disable-next-line:readonly-keyword
private styles: Styles;
private readonly document: Document; private readonly document: Document;
private readonly headers: IDocumentHeader[] = []; private readonly headers: IDocumentHeader[] = [];
@ -43,8 +45,6 @@ export class File {
private readonly settings: Settings; private readonly settings: Settings;
private readonly contentTypes: ContentTypes; private readonly contentTypes: ContentTypes;
private readonly appProperties: AppProperties; private readonly appProperties: AppProperties;
// tslint:disable-next-line:readonly-keyword
private styles: Styles;
constructor( constructor(
options: IPropertiesOptions = { options: IPropertiesOptions = {

View File

@ -3,7 +3,7 @@ import { ContinuationSeperator } from "./continuation-seperator";
export class ContinuationSeperatorRun extends Run { export class ContinuationSeperatorRun extends Run {
constructor() { constructor() {
super(); super({});
this.root.push(new ContinuationSeperator()); this.root.push(new ContinuationSeperator());
} }

View File

@ -3,9 +3,10 @@ import { FootnoteRef } from "./footnote-ref";
export class FootnoteRefRun extends Run { export class FootnoteRefRun extends Run {
constructor() { constructor() {
super(); super({
style: "FootnoteReference",
});
this.style("FootnoteReference");
this.root.push(new FootnoteRef()); this.root.push(new FootnoteRef());
} }
} }

View File

@ -26,7 +26,7 @@ export class FootnoteReference extends XmlComponent {
export class FootnoteReferenceRun extends Run { export class FootnoteReferenceRun extends Run {
constructor(id: number) { constructor(id: number) {
super(); super({});
this.properties.push(new Style("FootnoteReference")); this.properties.push(new Style("FootnoteReference"));

View File

@ -3,7 +3,7 @@ import { Seperator } from "./seperator";
export class SeperatorRun extends Run { export class SeperatorRun extends Run {
constructor() { constructor() {
super(); super({});
this.root.push(new Seperator()); this.root.push(new Seperator());
} }

View File

@ -15,7 +15,7 @@ class Break extends XmlComponent {
export class PageBreak extends Run { export class PageBreak extends Run {
constructor() { constructor() {
super(); super({});
this.root.push(new Break()); this.root.push(new Break());
} }
} }

View File

@ -20,7 +20,10 @@ export class Hyperlink extends XmlComponent {
const attributes = new HyperlinkAttributes(props); const attributes = new HyperlinkAttributes(props);
this.root.push(attributes); this.root.push(attributes);
this.textRun = new TextRun(text).style("Hyperlink"); this.textRun = new TextRun({
text: text,
style: "Hyperlink",
});
this.root.push(this.textRun); this.root.push(this.textRun);
} }

View File

@ -7,7 +7,7 @@ export class PictureRun extends Run {
private readonly drawing: Drawing; private readonly drawing: Drawing;
constructor(imageData: IMediaData, drawingOptions?: IDrawingOptions) { constructor(imageData: IMediaData, drawingOptions?: IDrawingOptions) {
super(); super({});
if (imageData === undefined) { if (imageData === undefined) {
throw new Error("imageData cannot be undefined"); throw new Error("imageData cannot be undefined");

View File

@ -4,17 +4,14 @@ import { Formatter } from "export/formatter";
import { Utility } from "tests/utility"; import { Utility } from "tests/utility";
import { Run } from "./"; import { Run } from "./";
import { UnderlineType } from "./underline";
describe("Run", () => { describe("Run", () => {
let run: Run;
beforeEach(() => {
run = new Run();
});
describe("#bold()", () => { describe("#bold()", () => {
it("it should add bold to the properties", () => { it("it should add bold to the properties", () => {
run.bold(); const run = new Run({
bold: true,
});
const newJson = Utility.jsonify(run); const newJson = Utility.jsonify(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:b"); assert.equal(newJson.root[0].root[0].rootKey, "w:b");
assert.equal(newJson.root[0].root[1].rootKey, "w:bCs"); assert.equal(newJson.root[0].root[1].rootKey, "w:bCs");
@ -23,7 +20,9 @@ describe("Run", () => {
describe("#italics()", () => { describe("#italics()", () => {
it("it should add italics to the properties", () => { it("it should add italics to the properties", () => {
run.italics(); const run = new Run({
italics: true,
});
const newJson = Utility.jsonify(run); const newJson = Utility.jsonify(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:i"); assert.equal(newJson.root[0].root[0].rootKey, "w:i");
assert.equal(newJson.root[0].root[1].rootKey, "w:iCs"); assert.equal(newJson.root[0].root[1].rootKey, "w:iCs");
@ -31,14 +30,10 @@ describe("Run", () => {
}); });
describe("#underline()", () => { 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", () => { it("should default to 'single' and no color", () => {
run.underline(); const run = new Run({
underline: {},
});
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:rPr": [{ "w:u": { _attr: { "w:val": "single" } } }] }], "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", () => { 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); const tree = new Formatter().format(run);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:u": { _attr: { "w:val": "double", "w:color": "990011" } } }] }], "w:r": [{ "w:rPr": [{ "w:u": { _attr: { "w:val": "double", "w:color": "990011" } } }] }],
@ -56,7 +56,9 @@ describe("Run", () => {
describe("#smallCaps()", () => { describe("#smallCaps()", () => {
it("it should add smallCaps to the properties", () => { it("it should add smallCaps to the properties", () => {
run.smallCaps(); const run = new Run({
smallCaps: true,
});
const newJson = Utility.jsonify(run); const newJson = Utility.jsonify(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:smallCaps"); assert.equal(newJson.root[0].root[0].rootKey, "w:smallCaps");
}); });
@ -64,7 +66,9 @@ describe("Run", () => {
describe("#caps()", () => { describe("#caps()", () => {
it("it should add caps to the properties", () => { it("it should add caps to the properties", () => {
run.allCaps(); const run = new Run({
allCaps: true,
});
const newJson = Utility.jsonify(run); const newJson = Utility.jsonify(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:caps"); assert.equal(newJson.root[0].root[0].rootKey, "w:caps");
}); });
@ -72,7 +76,9 @@ describe("Run", () => {
describe("#strike()", () => { describe("#strike()", () => {
it("it should add strike to the properties", () => { it("it should add strike to the properties", () => {
run.strike(); const run = new Run({
strike: true,
});
const newJson = Utility.jsonify(run); const newJson = Utility.jsonify(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:strike"); assert.equal(newJson.root[0].root[0].rootKey, "w:strike");
}); });
@ -80,7 +86,9 @@ describe("Run", () => {
describe("#doubleStrike()", () => { describe("#doubleStrike()", () => {
it("it should add caps to the properties", () => { it("it should add caps to the properties", () => {
run.doubleStrike(); const run = new Run({
doubleStrike: true,
});
const newJson = Utility.jsonify(run); const newJson = Utility.jsonify(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:dstrike"); assert.equal(newJson.root[0].root[0].rootKey, "w:dstrike");
}); });
@ -88,6 +96,7 @@ describe("Run", () => {
describe("#break()", () => { describe("#break()", () => {
it("it should add break to the run", () => { it("it should add break to the run", () => {
const run = new Run({});
run.break(); run.break();
const newJson = Utility.jsonify(run); const newJson = Utility.jsonify(run);
assert.equal(newJson.root[1].rootKey, "w:br"); assert.equal(newJson.root[1].rootKey, "w:br");
@ -96,6 +105,7 @@ describe("Run", () => {
describe("#tab()", () => { describe("#tab()", () => {
it("it should add break to the run", () => { it("it should add break to the run", () => {
const run = new Run({});
run.tab(); run.tab();
const newJson = Utility.jsonify(run); const newJson = Utility.jsonify(run);
assert.equal(newJson.root[1].rootKey, "w:tab"); assert.equal(newJson.root[1].rootKey, "w:tab");
@ -103,12 +113,12 @@ describe("Run", () => {
}); });
describe("#font()", () => { describe("#font()", () => {
it("should allow chaining calls", () => {
expect(run.font("Times")).to.equal(run);
});
it("should set the font as named", () => { it("should set the font as named", () => {
run.font("Times"); const run = new Run({
font: {
name: "Times",
},
});
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": [
@ -124,7 +134,9 @@ describe("Run", () => {
describe("#color", () => { describe("#color", () => {
it("should set the run to the color given", () => { it("should set the run to the color given", () => {
run.color("001122"); const run = new Run({
color: "001122",
});
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:rPr": [{ "w:color": { _attr: { "w:val": "001122" } } }] }], "w:r": [{ "w:rPr": [{ "w:color": { _attr: { "w:val": "001122" } } }] }],
@ -134,7 +146,9 @@ describe("Run", () => {
describe("#size", () => { describe("#size", () => {
it("should set the run to the given 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); const tree = new Formatter().format(run);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:r": [ "w:r": [
@ -148,7 +162,9 @@ describe("Run", () => {
describe("#rtl", () => { describe("#rtl", () => {
it("should set the run to the RTL mode", () => { it("should set the run to the RTL mode", () => {
run.rightToLeft(); const run = new Run({
rightToLeft: true,
});
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:rPr": [{ "w:rtl": { _attr: { "w:val": true } } }] }], "w:r": [{ "w:rPr": [{ "w:rtl": { _attr: { "w:val": true } } }] }],
@ -158,6 +174,7 @@ describe("Run", () => {
describe("#numberOfTotalPages", () => { describe("#numberOfTotalPages", () => {
it("should set the run to the RTL mode", () => { it("should set the run to the RTL mode", () => {
const run = new Run({});
run.numberOfTotalPages(); run.numberOfTotalPages();
const tree = new Formatter().format(run); const tree = new Formatter().format(run);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
@ -173,6 +190,7 @@ describe("Run", () => {
describe("#pageNumber", () => { describe("#pageNumber", () => {
it("should set the run to the RTL mode", () => { it("should set the run to the RTL mode", () => {
const run = new Run({});
run.pageNumber(); run.pageNumber();
const tree = new Formatter().format(run); const tree = new Formatter().format(run);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
@ -188,7 +206,9 @@ describe("Run", () => {
describe("#style", () => { describe("#style", () => {
it("should set the style to the given styleId", () => { it("should set the style to the given styleId", () => {
run.style("myRunStyle"); const run = new Run({
style: "myRunStyle",
});
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:rPr": [{ "w:rStyle": { _attr: { "w:val": "myRunStyle" } } }] }], "w:r": [{ "w:rPr": [{ "w:rStyle": { _attr: { "w:val": "myRunStyle" } } }] }],

View File

@ -20,50 +20,99 @@ import { RunFonts } from "./run-fonts";
import { SubScript, SuperScript } from "./script"; import { SubScript, SuperScript } from "./script";
import { Style } from "./style"; import { Style } from "./style";
import { Tab } from "./tab"; import { Tab } from "./tab";
import { Underline } from "./underline"; import { Underline, UnderlineType } from "./underline";
import { XmlComponent } from "file/xml-components"; 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 { export class Run extends XmlComponent {
protected readonly properties: RunProperties; protected readonly properties: RunProperties;
constructor() { constructor(options: IRunOptions) {
super("w:r"); super("w:r");
this.properties = new RunProperties(); this.properties = new RunProperties();
this.root.push(this.properties); this.root.push(this.properties);
}
public bold(): Run { if (options.bold) {
this.properties.push(new Bold()); this.properties.push(new Bold());
this.properties.push(new BoldComplexScript()); this.properties.push(new BoldComplexScript());
return this; }
}
public italics(): Run { if (options.italics) {
this.properties.push(new Italics()); this.properties.push(new Italics());
this.properties.push(new ItalicsComplexScript()); this.properties.push(new ItalicsComplexScript());
return this; }
}
public underline(underlineType?: string, color?: string): Run { if (options.underline) {
this.properties.push(new Underline(underlineType, color)); this.properties.push(new Underline(options.underline.type, options.underline.color));
return this; }
}
public color(color: string): Run { if (options.color) {
this.properties.push(new Color(color)); this.properties.push(new Color(options.color));
return this; }
}
public size(size: number): Run { if (options.size) {
this.properties.push(new Size(size)); this.properties.push(new Size(options.size));
this.properties.push(new SizeComplexScript(size)); this.properties.push(new SizeComplexScript(options.size));
return this; }
}
public rightToLeft(): Run { if (options.rightToLeft) {
this.properties.push(new RightToLeft()); this.properties.push(new RightToLeft());
return this; }
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 { public break(): Run {
@ -91,44 +140,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;
}
} }

View File

@ -4,7 +4,7 @@ import { SequentialIdentifierInstruction } from "./sequential-identifier-instruc
export class SequentialIdentifier extends Run { export class SequentialIdentifier extends Run {
constructor(identifier: string) { constructor(identifier: string) {
super(); super({});
this.root.push(new Begin(true)); this.root.push(new Begin(true));
this.root.push(new SequentialIdentifierInstruction(identifier)); this.root.push(new SequentialIdentifierInstruction(identifier));
this.root.push(new Separate()); this.root.push(new Separate());

View File

@ -1,9 +1,19 @@
import { Run } from "../run"; import { IRunOptions, Run } from "./run";
import { Text } from "./run-components/text"; import { Text } from "./run-components/text";
export interface ITextRunOptions extends IRunOptions {
readonly text: string;
}
export class TextRun extends Run { export class TextRun extends Run {
constructor(text: string) { constructor(options: ITextRunOptions | string) {
super(); if (typeof options === "string") {
this.root.push(new Text(text)); super({});
this.root.push(new Text(options));
return;
}
super(options);
this.root.push(new Text(options.text));
} }
} }

View File

@ -1,5 +1,25 @@
import { Attributes, XmlComponent } from "file/xml-components"; 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 { export abstract class BaseUnderline extends XmlComponent {
constructor(underlineType: string, color?: string) { constructor(underlineType: string, color?: string) {
super("w:u"); super("w:u");

View File

@ -17,7 +17,7 @@ export class TableOfContents extends XmlComponent {
const content = new StructuredDocumentTagContent(); const content = new StructuredDocumentTagContent();
const beginParagraph = new Paragraph({}); const beginParagraph = new Paragraph({});
const beginRun = new Run(); const beginRun = new Run({});
beginRun.addChildElement(new Begin(true)); beginRun.addChildElement(new Begin(true));
beginRun.addChildElement(new FieldInstruction(properties)); beginRun.addChildElement(new FieldInstruction(properties));
beginRun.addChildElement(new Separate()); beginRun.addChildElement(new Separate());
@ -25,7 +25,7 @@ export class TableOfContents extends XmlComponent {
content.addChildElement(beginParagraph); content.addChildElement(beginParagraph);
const endParagraph = new Paragraph({}); const endParagraph = new Paragraph({});
const endRun = new Run(); const endRun = new Run({});
endRun.addChildElement(new End()); endRun.addChildElement(new End());
endParagraph.addRun(endRun); endParagraph.addRun(endRun);
content.addChildElement(endParagraph); content.addChildElement(endParagraph);