Merge branch 'master' of github.com:dolanmiu/docx into feat/declaritive

# Conflicts:
#	src/file/paragraph/run/run.ts
This commit is contained in:
Dolan
2019-08-06 21:06:06 +01:00
14 changed files with 380 additions and 54 deletions

27
demo/demo45.ts Normal file
View File

@ -0,0 +1,27 @@
import * as fs from "fs";
import { Document, 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()
)
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});

27
demo/demo46.ts Normal file
View File

@ -0,0 +1,27 @@
import * as fs from "fs";
import { Document, 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')
.shadow('pct10','00FFFF','FF0000')
)
.right()
)
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});

View File

@ -10,26 +10,28 @@ export class Footer extends InitializableXmlComponent {
constructor(referenceNumber: number, initContent?: XmlComponent) { constructor(referenceNumber: number, initContent?: XmlComponent) {
super("w:ftr", initContent); super("w:ftr", initContent);
this.refId = referenceNumber; this.refId = referenceNumber;
this.root.push( if (!initContent) {
new FooterAttributes({ this.root.push(
wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas", new FooterAttributes({
mc: "http://schemas.openxmlformats.org/markup-compatibility/2006", wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
o: "urn:schemas-microsoft-com:office:office", mc: "http://schemas.openxmlformats.org/markup-compatibility/2006",
r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships", o: "urn:schemas-microsoft-com:office:office",
m: "http://schemas.openxmlformats.org/officeDocument/2006/math", r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
v: "urn:schemas-microsoft-com:vml", m: "http://schemas.openxmlformats.org/officeDocument/2006/math",
wp14: "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", v: "urn:schemas-microsoft-com:vml",
wp: "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", wp14: "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
w10: "urn:schemas-microsoft-com:office:word", wp: "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
w: "http://schemas.openxmlformats.org/wordprocessingml/2006/main", w10: "urn:schemas-microsoft-com:office:word",
w14: "http://schemas.microsoft.com/office/word/2010/wordml", w: "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
w15: "http://schemas.microsoft.com/office/word/2012/wordml", w14: "http://schemas.microsoft.com/office/word/2010/wordml",
wpg: "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup", w15: "http://schemas.microsoft.com/office/word/2012/wordml",
wpi: "http://schemas.microsoft.com/office/word/2010/wordprocessingInk", wpg: "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",
wne: "http://schemas.microsoft.com/office/word/2006/wordml", wpi: "http://schemas.microsoft.com/office/word/2010/wordprocessingInk",
wps: "http://schemas.microsoft.com/office/word/2010/wordprocessingShape", wne: "http://schemas.microsoft.com/office/word/2006/wordml",
}), wps: "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
); }),
);
}
} }
public get ReferenceId(): number { public get ReferenceId(): number {

View File

@ -11,38 +11,39 @@ export class Header extends InitializableXmlComponent {
super("w:hdr", initContent); super("w:hdr", initContent);
this.refId = referenceNumber; this.refId = referenceNumber;
if (!initContent) {
this.root.push( this.root.push(
new HeaderAttributes({ new HeaderAttributes({
wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas", wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
mc: "http://schemas.openxmlformats.org/markup-compatibility/2006", mc: "http://schemas.openxmlformats.org/markup-compatibility/2006",
o: "urn:schemas-microsoft-com:office:office", o: "urn:schemas-microsoft-com:office:office",
r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships", r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
m: "http://schemas.openxmlformats.org/officeDocument/2006/math", m: "http://schemas.openxmlformats.org/officeDocument/2006/math",
v: "urn:schemas-microsoft-com:vml", v: "urn:schemas-microsoft-com:vml",
wp14: "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", wp14: "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
wp: "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", wp: "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
w10: "urn:schemas-microsoft-com:office:word", w10: "urn:schemas-microsoft-com:office:word",
w: "http://schemas.openxmlformats.org/wordprocessingml/2006/main", w: "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
w14: "http://schemas.microsoft.com/office/word/2010/wordml", w14: "http://schemas.microsoft.com/office/word/2010/wordml",
w15: "http://schemas.microsoft.com/office/word/2012/wordml", w15: "http://schemas.microsoft.com/office/word/2012/wordml",
wpg: "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup", wpg: "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",
wpi: "http://schemas.microsoft.com/office/word/2010/wordprocessingInk", wpi: "http://schemas.microsoft.com/office/word/2010/wordprocessingInk",
wne: "http://schemas.microsoft.com/office/word/2006/wordml", wne: "http://schemas.microsoft.com/office/word/2006/wordml",
wps: "http://schemas.microsoft.com/office/word/2010/wordprocessingShape", wps: "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
cx: "http://schemas.microsoft.com/office/drawing/2014/chartex", cx: "http://schemas.microsoft.com/office/drawing/2014/chartex",
cx1: "http://schemas.microsoft.com/office/drawing/2015/9/8/chartex", cx1: "http://schemas.microsoft.com/office/drawing/2015/9/8/chartex",
cx2: "http://schemas.microsoft.com/office/drawing/2015/10/21/chartex", cx2: "http://schemas.microsoft.com/office/drawing/2015/10/21/chartex",
cx3: "http://schemas.microsoft.com/office/drawing/2016/5/9/chartex", cx3: "http://schemas.microsoft.com/office/drawing/2016/5/9/chartex",
cx4: "http://schemas.microsoft.com/office/drawing/2016/5/10/chartex", cx4: "http://schemas.microsoft.com/office/drawing/2016/5/10/chartex",
cx5: "http://schemas.microsoft.com/office/drawing/2016/5/11/chartex", cx5: "http://schemas.microsoft.com/office/drawing/2016/5/11/chartex",
cx6: "http://schemas.microsoft.com/office/drawing/2016/5/12/chartex", cx6: "http://schemas.microsoft.com/office/drawing/2016/5/12/chartex",
cx7: "http://schemas.microsoft.com/office/drawing/2016/5/13/chartex", cx7: "http://schemas.microsoft.com/office/drawing/2016/5/13/chartex",
cx8: "http://schemas.microsoft.com/office/drawing/2016/5/14/chartex", cx8: "http://schemas.microsoft.com/office/drawing/2016/5/14/chartex",
w16cid: "http://schemas.microsoft.com/office/word/2016/wordml/cid", w16cid: "http://schemas.microsoft.com/office/word/2016/wordml/cid",
w16se: "http://schemas.microsoft.com/office/word/2015/wordml/symex", w16se: "http://schemas.microsoft.com/office/word/2015/wordml/symex",
}), }),
); );
}
} }
public get ReferenceId(): number { public get ReferenceId(): number {

View File

@ -199,6 +199,15 @@ export class LevelBase extends XmlComponent {
return this; return this;
} }
public highlight(color: string): Level {
this.addRunProperty(new formatting.Highlight(color));
return this;
}
public shadow(value: string, fill: string, color: string): Level {
this.addRunProperty(new formatting.Shadow(value, fill, color));
return this;
}
// --------------------- Paragraph formatting ------------------------ // // --------------------- Paragraph formatting ------------------------ //
public center(): Level { public center(): Level {

View File

@ -325,6 +325,24 @@ describe("AbstractNumbering", () => {
}); });
}); });
it("#highlight", () => {
const abstractNumbering = new AbstractNumbering(1);
const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.").highlight("005599");
const tree = new Formatter().format(level);
expect(tree["w:lvl"]).to.include({
"w:rPr": [{ "w:highlight": { _attr: { "w:val": "005599" } } }],
});
});
it("#shadow", () => {
const abstractNumbering = new AbstractNumbering(1);
const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.").shadow("pct10", "00FFFF", "FF0000");
const tree = new Formatter().format(level);
expect(tree["w:lvl"]).to.include({
"w:rPr": [{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } }],
});
});
describe("#underline", () => { describe("#underline", () => {
it("should set underline to 'single' if no arguments are given", () => { it("should set underline to 'single' if no arguments are given", () => {
const abstractNumbering = new AbstractNumbering(1); const abstractNumbering = new AbstractNumbering(1);

View File

@ -113,7 +113,7 @@ export class Imprint extends XmlComponent {
} }
} }
export class Shadow extends XmlComponent { /* export class Shadow extends XmlComponent {
constructor() { constructor() {
super("w:shadow"); super("w:shadow");
this.root.push( this.root.push(
@ -122,7 +122,7 @@ export class Shadow extends XmlComponent {
}), }),
); );
} }
} } */
export class SmallCaps extends XmlComponent { export class SmallCaps extends XmlComponent {
constructor() { constructor() {
@ -178,3 +178,51 @@ export class RightToLeft extends XmlComponent {
); );
} }
} }
export class Highlight extends XmlComponent {
constructor(color: string) {
super("w:highlight");
this.root.push(
new Attributes({
val: color,
}),
);
}
}
export class HighlightComplexScript extends XmlComponent {
constructor(color: string) {
super("w:highlightCs");
this.root.push(
new Attributes({
val: color,
}),
);
}
}
export class Shadow extends XmlComponent {
constructor(value: string, fill: string, color: string) {
super("w:shd");
this.root.push(
new Attributes({
val: value,
fill: fill,
color: color,
}),
);
}
}
export class ShadowComplexScript extends XmlComponent {
constructor(value: string, fill: string, color: string) {
super("w:shdCs");
this.root.push(
new Attributes({
val: value,
fill: fill,
color: color,
}),
);
}
}

View File

@ -129,6 +129,54 @@ describe("Run", () => {
}); });
}); });
describe("#highlight()", () => {
it("it should add highlight to the properties", () => {
run.highlight("005599");
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{ "w:highlight": { _attr: { "w:val": "005599" } } },
{
"w:highlightCs": {
_attr: {
"w:val": "005599",
},
},
},
],
},
],
});
});
});
describe("#shadow()", () => {
it("it should add shadow to the properties", () => {
run.shadow("pct10", "00FFFF", "FF0000");
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } },
{
"w:shdCs": {
_attr: {
"w:val": "pct10",
"w:fill": "00FFFF",
"w:color": "FF0000",
},
},
},
],
},
],
});
});
});
describe("#break()", () => { describe("#break()", () => {
it("it should add break to the run", () => { it("it should add break to the run", () => {
const run = new Run({}); const run = new Run({});

View File

@ -7,9 +7,13 @@ import {
BoldComplexScript, BoldComplexScript,
Color, Color,
DoubleStrike, DoubleStrike,
Highlight,
HighlightComplexScript,
Italics, Italics,
ItalicsComplexScript, ItalicsComplexScript,
RightToLeft, RightToLeft,
Shadow,
ShadowComplexScript,
Size, Size,
SizeComplexScript, SizeComplexScript,
Strike, Strike,
@ -140,4 +144,56 @@ 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;
}
} }

View File

@ -307,5 +307,51 @@ describe("CharacterStyle", () => {
], ],
}); });
}); });
it("#highlight", () => {
const style = new CharacterStyle("myStyleId").highlight("005599");
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "character", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:highlight": { _attr: { "w:val": "005599" } } }],
},
{
"w:uiPriority": {
_attr: {
"w:val": "99",
},
},
},
{
"w:unhideWhenUsed": EMPTY_OBJECT,
},
],
});
});
it("#shadow", () => {
const style = new CharacterStyle("myStyleId").shadow("pct10", "00FFFF", "FF0000");
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "character", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } }],
},
{
"w:uiPriority": {
_attr: {
"w:val": "99",
},
},
},
{
"w:unhideWhenUsed": EMPTY_OBJECT,
},
],
});
});
}); });
}); });

View File

@ -58,4 +58,12 @@ export class CharacterStyle extends Style {
this.root.push(new SemiHidden()); this.root.push(new SemiHidden());
return this; return this;
} }
public highlight(color: string): CharacterStyle {
return this.addRunProperty(new formatting.Highlight(color));
}
public shadow(value: string, fill: string, color: string): CharacterStyle {
return this.addRunProperty(new formatting.Shadow(value, fill, color));
}
} }

View File

@ -375,6 +375,32 @@ describe("ParagraphStyle", () => {
}); });
}); });
it("#highlight", () => {
const style = new ParagraphStyle("myStyleId").highlight("005599");
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:highlight": { _attr: { "w:val": "005599" } } }],
},
],
});
});
it("#shadow", () => {
const style = new ParagraphStyle("myStyleId").shadow("pct10", "00FFFF", "FF0000");
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } }],
},
],
});
});
describe("#underline", () => { describe("#underline", () => {
it("should set underline to 'single' if no arguments are given", () => { it("should set underline to 'single' if no arguments are given", () => {
const style = new ParagraphStyle("myStyleId").underline(); const style = new ParagraphStyle("myStyleId").underline();

View File

@ -114,6 +114,14 @@ export class ParagraphStyle extends Style {
return this.addRunProperty(new formatting.CharacterSpacing(value)); return this.addRunProperty(new formatting.CharacterSpacing(value));
} }
public highlight(color: string): ParagraphStyle {
return this.addRunProperty(new formatting.Highlight(color));
}
public shadow(value: string, fill: string, color: string): ParagraphStyle {
return this.addRunProperty(new formatting.Shadow(value, fill, color));
}
// --------------------- Paragraph formatting ------------------------ // // --------------------- Paragraph formatting ------------------------ //
public center(): ParagraphStyle { public center(): ParagraphStyle {

View File

@ -3,6 +3,7 @@ import { XmlAttributeComponent } from "./default-attributes";
export interface IAttributesProperties { export interface IAttributesProperties {
readonly val?: string | number | boolean; readonly val?: string | number | boolean;
readonly color?: string; readonly color?: string;
readonly fill?: string;
readonly space?: string; readonly space?: string;
readonly sz?: string; readonly sz?: string;
readonly type?: string; readonly type?: string;
@ -26,6 +27,7 @@ export class Attributes extends XmlAttributeComponent<IAttributesProperties> {
protected readonly xmlKeys = { protected readonly xmlKeys = {
val: "w:val", val: "w:val",
color: "w:color", color: "w:color",
fill: "w:fill",
space: "w:space", space: "w:space",
sz: "w:sz", sz: "w:sz",
type: "w:type", type: "w:type",