diff --git a/demo/demo45.ts b/demo/demo45.ts new file mode 100644 index 0000000000..7cef926537 --- /dev/null +++ b/demo/demo45.ts @@ -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); +}); diff --git a/demo/demo46.ts b/demo/demo46.ts new file mode 100644 index 0000000000..bc38e8dc19 --- /dev/null +++ b/demo/demo46.ts @@ -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); +}); diff --git a/src/file/footer/footer.ts b/src/file/footer/footer.ts index cfac9b8d9a..d8b8cfe2ea 100644 --- a/src/file/footer/footer.ts +++ b/src/file/footer/footer.ts @@ -10,26 +10,28 @@ export class Footer extends InitializableXmlComponent { constructor(referenceNumber: number, initContent?: XmlComponent) { super("w:ftr", initContent); this.refId = referenceNumber; - this.root.push( - new FooterAttributes({ - wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas", - mc: "http://schemas.openxmlformats.org/markup-compatibility/2006", - o: "urn:schemas-microsoft-com:office:office", - r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships", - m: "http://schemas.openxmlformats.org/officeDocument/2006/math", - v: "urn:schemas-microsoft-com:vml", - wp14: "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", - wp: "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", - w10: "urn:schemas-microsoft-com:office:word", - w: "http://schemas.openxmlformats.org/wordprocessingml/2006/main", - w14: "http://schemas.microsoft.com/office/word/2010/wordml", - w15: "http://schemas.microsoft.com/office/word/2012/wordml", - wpg: "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup", - wpi: "http://schemas.microsoft.com/office/word/2010/wordprocessingInk", - wne: "http://schemas.microsoft.com/office/word/2006/wordml", - wps: "http://schemas.microsoft.com/office/word/2010/wordprocessingShape", - }), - ); + if (!initContent) { + this.root.push( + new FooterAttributes({ + wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas", + mc: "http://schemas.openxmlformats.org/markup-compatibility/2006", + o: "urn:schemas-microsoft-com:office:office", + r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships", + m: "http://schemas.openxmlformats.org/officeDocument/2006/math", + v: "urn:schemas-microsoft-com:vml", + wp14: "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", + wp: "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", + w10: "urn:schemas-microsoft-com:office:word", + w: "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + w14: "http://schemas.microsoft.com/office/word/2010/wordml", + w15: "http://schemas.microsoft.com/office/word/2012/wordml", + wpg: "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup", + wpi: "http://schemas.microsoft.com/office/word/2010/wordprocessingInk", + wne: "http://schemas.microsoft.com/office/word/2006/wordml", + wps: "http://schemas.microsoft.com/office/word/2010/wordprocessingShape", + }), + ); + } } public get ReferenceId(): number { diff --git a/src/file/header/header.ts b/src/file/header/header.ts index 8c63a6723c..ef7a3f4d5d 100644 --- a/src/file/header/header.ts +++ b/src/file/header/header.ts @@ -11,38 +11,39 @@ export class Header extends InitializableXmlComponent { super("w:hdr", initContent); this.refId = referenceNumber; - - this.root.push( - new HeaderAttributes({ - wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas", - mc: "http://schemas.openxmlformats.org/markup-compatibility/2006", - o: "urn:schemas-microsoft-com:office:office", - r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships", - m: "http://schemas.openxmlformats.org/officeDocument/2006/math", - v: "urn:schemas-microsoft-com:vml", - wp14: "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", - wp: "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", - w10: "urn:schemas-microsoft-com:office:word", - w: "http://schemas.openxmlformats.org/wordprocessingml/2006/main", - w14: "http://schemas.microsoft.com/office/word/2010/wordml", - w15: "http://schemas.microsoft.com/office/word/2012/wordml", - wpg: "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup", - wpi: "http://schemas.microsoft.com/office/word/2010/wordprocessingInk", - wne: "http://schemas.microsoft.com/office/word/2006/wordml", - wps: "http://schemas.microsoft.com/office/word/2010/wordprocessingShape", - cx: "http://schemas.microsoft.com/office/drawing/2014/chartex", - cx1: "http://schemas.microsoft.com/office/drawing/2015/9/8/chartex", - cx2: "http://schemas.microsoft.com/office/drawing/2015/10/21/chartex", - cx3: "http://schemas.microsoft.com/office/drawing/2016/5/9/chartex", - cx4: "http://schemas.microsoft.com/office/drawing/2016/5/10/chartex", - cx5: "http://schemas.microsoft.com/office/drawing/2016/5/11/chartex", - cx6: "http://schemas.microsoft.com/office/drawing/2016/5/12/chartex", - cx7: "http://schemas.microsoft.com/office/drawing/2016/5/13/chartex", - cx8: "http://schemas.microsoft.com/office/drawing/2016/5/14/chartex", - w16cid: "http://schemas.microsoft.com/office/word/2016/wordml/cid", - w16se: "http://schemas.microsoft.com/office/word/2015/wordml/symex", - }), - ); + if (!initContent) { + this.root.push( + new HeaderAttributes({ + wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas", + mc: "http://schemas.openxmlformats.org/markup-compatibility/2006", + o: "urn:schemas-microsoft-com:office:office", + r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships", + m: "http://schemas.openxmlformats.org/officeDocument/2006/math", + v: "urn:schemas-microsoft-com:vml", + wp14: "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", + wp: "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", + w10: "urn:schemas-microsoft-com:office:word", + w: "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + w14: "http://schemas.microsoft.com/office/word/2010/wordml", + w15: "http://schemas.microsoft.com/office/word/2012/wordml", + wpg: "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup", + wpi: "http://schemas.microsoft.com/office/word/2010/wordprocessingInk", + wne: "http://schemas.microsoft.com/office/word/2006/wordml", + wps: "http://schemas.microsoft.com/office/word/2010/wordprocessingShape", + cx: "http://schemas.microsoft.com/office/drawing/2014/chartex", + cx1: "http://schemas.microsoft.com/office/drawing/2015/9/8/chartex", + cx2: "http://schemas.microsoft.com/office/drawing/2015/10/21/chartex", + cx3: "http://schemas.microsoft.com/office/drawing/2016/5/9/chartex", + cx4: "http://schemas.microsoft.com/office/drawing/2016/5/10/chartex", + cx5: "http://schemas.microsoft.com/office/drawing/2016/5/11/chartex", + cx6: "http://schemas.microsoft.com/office/drawing/2016/5/12/chartex", + cx7: "http://schemas.microsoft.com/office/drawing/2016/5/13/chartex", + cx8: "http://schemas.microsoft.com/office/drawing/2016/5/14/chartex", + w16cid: "http://schemas.microsoft.com/office/word/2016/wordml/cid", + w16se: "http://schemas.microsoft.com/office/word/2015/wordml/symex", + }), + ); + } } public get ReferenceId(): number { diff --git a/src/file/numbering/level.ts b/src/file/numbering/level.ts index d8c3d07b0f..bbfb27493f 100644 --- a/src/file/numbering/level.ts +++ b/src/file/numbering/level.ts @@ -199,6 +199,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 { diff --git a/src/file/numbering/numbering.spec.ts b/src/file/numbering/numbering.spec.ts index ab74e1c39c..f38261b071 100644 --- a/src/file/numbering/numbering.spec.ts +++ b/src/file/numbering/numbering.spec.ts @@ -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); diff --git a/src/file/paragraph/run/formatting.ts b/src/file/paragraph/run/formatting.ts index 30d437e268..87273b9459 100644 --- a/src/file/paragraph/run/formatting.ts +++ b/src/file/paragraph/run/formatting.ts @@ -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, + }), + ); + } +} diff --git a/src/file/paragraph/run/run.spec.ts b/src/file/paragraph/run/run.spec.ts index 8d9d9ecc5a..a77e59844f 100644 --- a/src/file/paragraph/run/run.spec.ts +++ b/src/file/paragraph/run/run.spec.ts @@ -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()", () => { it("it should add break to the run", () => { const run = new Run({}); diff --git a/src/file/paragraph/run/run.ts b/src/file/paragraph/run/run.ts index 8909fe66bf..5b62d5b9c8 100644 --- a/src/file/paragraph/run/run.ts +++ b/src/file/paragraph/run/run.ts @@ -7,9 +7,13 @@ import { BoldComplexScript, Color, DoubleStrike, + Highlight, + HighlightComplexScript, Italics, ItalicsComplexScript, RightToLeft, + Shadow, + ShadowComplexScript, Size, SizeComplexScript, Strike, @@ -140,4 +144,56 @@ 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; + } + + 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; + } } diff --git a/src/file/styles/style/character-style.spec.ts b/src/file/styles/style/character-style.spec.ts index 793ead4ff2..c7594f3829 100644 --- a/src/file/styles/style/character-style.spec.ts +++ b/src/file/styles/style/character-style.spec.ts @@ -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, + }, + ], + }); + }); }); }); diff --git a/src/file/styles/style/character-style.ts b/src/file/styles/style/character-style.ts index c7f677b331..4ac49ba950 100644 --- a/src/file/styles/style/character-style.ts +++ b/src/file/styles/style/character-style.ts @@ -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)); + } } diff --git a/src/file/styles/style/paragraph-style.spec.ts b/src/file/styles/style/paragraph-style.spec.ts index 47e447abfd..91f109e11e 100644 --- a/src/file/styles/style/paragraph-style.spec.ts +++ b/src/file/styles/style/paragraph-style.spec.ts @@ -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(); diff --git a/src/file/styles/style/paragraph-style.ts b/src/file/styles/style/paragraph-style.ts index 46e02155be..a1d3d86775 100644 --- a/src/file/styles/style/paragraph-style.ts +++ b/src/file/styles/style/paragraph-style.ts @@ -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 { diff --git a/src/file/xml-components/attributes.ts b/src/file/xml-components/attributes.ts index 02ed46b434..703ee1a679 100644 --- a/src/file/xml-components/attributes.ts +++ b/src/file/xml-components/attributes.ts @@ -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 { protected readonly xmlKeys = { val: "w:val", color: "w:color", + fill: "w:fill", space: "w:space", sz: "w:sz", type: "w:type",