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 paragraph = new Paragraph("Hello World");
const institutionText = new TextRun("Foo Bar").bold();
const dateText = new TextRun("Github is the best").tab().bold();
const institutionText = new TextRun({
text: "Foo Bar",
bold: true,
});
const dateText = new TextRun({
text: "Github is the best",
bold: true,
}).tab();
paragraph.addRun(institutionText);
paragraph.addRun(dateText);

View File

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

View File

@ -6,8 +6,14 @@ import { Document, Packer, Paragraph, TextRun } from "../build";
const doc = new Document();
const paragraph = new Paragraph("Hello World");
const institutionText = new TextRun("Foo").bold();
const dateText = new TextRun("Bar").tab().bold();
const institutionText = new TextRun({
text: "Foo",
bold: true,
});
const dateText = new TextRun({
text: "Bar",
bold: true,
}).tab();
paragraph.addRun(institutionText);
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(
new Paragraph({
@ -108,10 +117,25 @@ doc.addParagraph(
);
const para = new Paragraph({});
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("and then underlined ").underline());
para.addRun(new TextRun("and back to normal."));
para.addRun(
new TextRun({
text: "and then underlined ",
underline: {},
}),
);
para.addRun(
new TextRun({
text: "and back to normal.",
}),
);
const packer = new Packer();

View File

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

View File

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

View File

@ -1,7 +1,7 @@
// Table of contents
// Import from 'docx' rather than '../build' if you install from npm
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();
@ -26,15 +26,30 @@ const toc = new TableOfContents("Summary", {
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("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("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("My Spectacular Style #1").style("MySpectacularStyle").pageBreakBefore());
doc.addParagraph(new Paragraph({
text: "My Spectacular Style #1",
style: "MySpectacularStyle",
pageBreakBefore: true,
}));
const packer = new Packer();

View File

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

View File

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

View File

@ -1,7 +1,7 @@
// 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 * as fs from "fs";
import { Document, Packer, Paragraph, VerticalAlign } from "../build";
import { Document, HeadingLevel, Packer, Paragraph, VerticalAlign } from "../build";
const doc = new Document();
@ -17,9 +17,11 @@ table
table
.getCell(1, 0)
.addParagraph(
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",
).heading1(),
new Paragraph({
text:
"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();

View File

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

View File

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