Highlighting

This commit is contained in:
Forman
2019-08-05 13:42:45 +03:00
parent 9e8d67c4a9
commit fa710d1ba6
12 changed files with 285 additions and 2 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

@ -198,6 +198,15 @@ export class LevelBase extends XmlComponent {
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 ------------------------ //
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", () => {
it("should set underline to 'single' if no arguments are given", () => {
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() {
super("w:shadow");
this.root.push(
@ -122,7 +122,7 @@ export class Shadow extends XmlComponent {
}),
);
}
}
} */
export class SmallCaps extends XmlComponent {
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

@ -115,6 +115,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()", () => {
it("it should add break to the run", () => {
run.break();

View File

@ -7,9 +7,13 @@ import {
BoldComplexScript,
Color,
DoubleStrike,
Highlight,
HighlightComplexScript,
Italics,
ItalicsComplexScript,
RightToLeft,
Shadow,
ShadowComplexScript,
Size,
SizeComplexScript,
Strike,
@ -131,4 +135,16 @@ export class Run extends XmlComponent {
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());
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", () => {
it("should set underline to 'single' if no arguments are given", () => {
const style = new ParagraphStyle("myStyleId").underline();

View File

@ -114,6 +114,14 @@ export class ParagraphStyle extends Style {
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 ------------------------ //
public center(): ParagraphStyle {

View File

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