From ec5a678f05c437c80c115302479086c0349a0fea Mon Sep 17 00:00:00 2001 From: moritz-tr Date: Mon, 22 Jul 2019 17:36:13 +0200 Subject: [PATCH 1/4] fix: multiple def of header/footer attributes --- src/file/footer/footer.ts | 42 +++++++++++++------------ src/file/header/header.ts | 65 ++++++++++++++++++++------------------- 2 files changed, 55 insertions(+), 52 deletions(-) diff --git a/src/file/footer/footer.ts b/src/file/footer/footer.ts index 6934447d83..9466729b86 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 a812b1b453..0f753134ba 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 { From 6f6f1e249ce3708a8fff9ae97515a04ba9f40ed2 Mon Sep 17 00:00:00 2001 From: Steve Wainstead Date: Thu, 1 Aug 2019 11:12:07 -0400 Subject: [PATCH 2/4] Removed artifact left over from (likely) the patch program --- src/file/paragraph/paragraph.ts.orig | 243 --------------------------- 1 file changed, 243 deletions(-) delete mode 100644 src/file/paragraph/paragraph.ts.orig diff --git a/src/file/paragraph/paragraph.ts.orig b/src/file/paragraph/paragraph.ts.orig deleted file mode 100644 index 7ffbeca43d..0000000000 --- a/src/file/paragraph/paragraph.ts.orig +++ /dev/null @@ -1,243 +0,0 @@ -// http://officeopenxml.com/WPparagraph.php -import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run"; -import { Image } from "file/media"; -import { Num } from "file/numbering/num"; -import { XmlComponent } from "file/xml-components"; - -import { Alignment } from "./formatting/alignment"; -import { Bidirectional } from "./formatting/bidirectional"; -<<<<<<< HEAD -import { Border, ThematicBreak } from "./formatting/border"; -import { Indent } from "./formatting/indent"; -======= -import { ThematicBreak } from "./formatting/border"; -import { IIndentAttributesProperties, Indent } from "./formatting/indent"; ->>>>>>> a53818754a1c76b9930ee2ecc642570170fa3c06 -import { KeepLines, KeepNext } from "./formatting/keep"; -import { PageBreak, PageBreakBefore } from "./formatting/page-break"; -import { ISpacingProperties, Spacing } from "./formatting/spacing"; -import { Style } from "./formatting/style"; -import { CenterTabStop, LeftTabStop, MaxRightTabStop, RightTabStop } from "./formatting/tab-stop"; -import { NumberProperties } from "./formatting/unordered-list"; -import { Bookmark, Hyperlink } from "./links"; -import { ParagraphProperties } from "./properties"; -import { PictureRun, Run, TextRun } from "./run"; - -export class Paragraph extends XmlComponent { - private readonly properties: ParagraphProperties; - - constructor(text?: string) { - super("w:p"); - this.properties = new ParagraphProperties(); - this.root.push(this.properties); - if (text !== undefined) { - this.root.push(new TextRun(text)); - } - } - - public get Borders(): Border { - return this.properties.paragraphBorder; - } - - public createBorder(): Paragraph { - this.properties.createBorder(); - return this; - } - - public addRun(run: Run): Paragraph { - this.root.push(run); - return this; - } - - public addHyperLink(hyperlink: Hyperlink): Paragraph { - this.root.push(hyperlink); - return this; - } - - public addBookmark(bookmark: Bookmark): Paragraph { - // Bookmarks by spec have three components, a start, text, and end - this.root.push(bookmark.start); - this.root.push(bookmark.text); - this.root.push(bookmark.end); - return this; - } - - public createTextRun(text: string): TextRun { - const run = new TextRun(text); - this.addRun(run); - return run; - } - - public addImage(image: Image): PictureRun { - const run = image.Run; - this.addRun(run); - - return run; - } - - public heading1(): Paragraph { - this.properties.push(new Style("Heading1")); - return this; - } - - public heading2(): Paragraph { - this.properties.push(new Style("Heading2")); - return this; - } - - public heading3(): Paragraph { - this.properties.push(new Style("Heading3")); - return this; - } - - public heading4(): Paragraph { - this.properties.push(new Style("Heading4")); - return this; - } - - public heading5(): Paragraph { - this.properties.push(new Style("Heading5")); - return this; - } - - public heading6(): Paragraph { - this.properties.push(new Style("Heading6")); - return this; - } - - public title(): Paragraph { - this.properties.push(new Style("Title")); - return this; - } - - public center(): Paragraph { - this.properties.push(new Alignment("center")); - return this; - } - - public left(): Paragraph { - this.properties.push(new Alignment("left")); - return this; - } - - public right(): Paragraph { - this.properties.push(new Alignment("right")); - return this; - } - - public start(): Paragraph { - this.properties.push(new Alignment("start")); - return this; - } - - public end(): Paragraph { - this.properties.push(new Alignment("end")); - return this; - } - - public distribute(): Paragraph { - this.properties.push(new Alignment("distribute")); - return this; - } - - public justified(): Paragraph { - this.properties.push(new Alignment("both")); - return this; - } - - public thematicBreak(): Paragraph { - this.properties.push(new ThematicBreak()); - return this; - } - - public pageBreak(): Paragraph { - this.root.push(new PageBreak()); - return this; - } - - public pageBreakBefore(): Paragraph { - this.properties.push(new PageBreakBefore()); - return this; - } - - public maxRightTabStop(): Paragraph { - this.properties.push(new MaxRightTabStop()); - return this; - } - - public leftTabStop(position: number): Paragraph { - this.properties.push(new LeftTabStop(position)); - return this; - } - - public rightTabStop(position: number): Paragraph { - this.properties.push(new RightTabStop(position)); - return this; - } - - public centerTabStop(position: number): Paragraph { - this.properties.push(new CenterTabStop(position)); - return this; - } - - public bullet(indentLevel: number = 0): Paragraph { - this.properties.push(new Style("ListParagraph")); - this.properties.push(new NumberProperties(1, indentLevel)); - return this; - } - - public setNumbering(numbering: Num, indentLevel: number): Paragraph { - this.properties.push(new Style("ListParagraph")); - this.properties.push(new NumberProperties(numbering.id, indentLevel)); - return this; - } - - public setCustomNumbering(numberId: number, indentLevel: number): Paragraph { - this.properties.push(new NumberProperties(numberId, indentLevel)); - return this; - } - - public style(styleId: string): Paragraph { - this.properties.push(new Style(styleId)); - return this; - } - - public indent(attrs: IIndentAttributesProperties): Paragraph { - this.properties.push(new Indent(attrs)); - return this; - } - - public spacing(params: ISpacingProperties): Paragraph { - this.properties.push(new Spacing(params)); - return this; - } - - public keepNext(): Paragraph { - this.properties.push(new KeepNext()); - return this; - } - - public keepLines(): Paragraph { - this.properties.push(new KeepLines()); - return this; - } - - public referenceFootnote(id: number): Paragraph { - this.root.push(new FootnoteReferenceRun(id)); - return this; - } - - public addRunToFront(run: Run): Paragraph { - this.root.splice(1, 0, run); - return this; - } - - public bidirectional(): Paragraph { - this.properties.push(new Bidirectional()); - return this; - } - - public get Properties(): ParagraphProperties { - return this.properties; - } -} From fa710d1ba6d7bc7c4a0a5de2147ad06db63cf890 Mon Sep 17 00:00:00 2001 From: Forman Date: Mon, 5 Aug 2019 13:42:45 +0300 Subject: [PATCH 3/4] Highlighting --- demo/demo45.ts | 27 ++++++++++ demo/demo46.ts | 27 ++++++++++ src/file/numbering/level.ts | 9 ++++ src/file/numbering/numbering.spec.ts | 18 +++++++ src/file/paragraph/run/formatting.ts | 52 ++++++++++++++++++- src/file/paragraph/run/run.spec.ts | 48 +++++++++++++++++ src/file/paragraph/run/run.ts | 16 ++++++ src/file/styles/style/character-style.spec.ts | 46 ++++++++++++++++ src/file/styles/style/character-style.ts | 8 +++ src/file/styles/style/paragraph-style.spec.ts | 26 ++++++++++ src/file/styles/style/paragraph-style.ts | 8 +++ src/file/xml-components/attributes.ts | 2 + 12 files changed, 285 insertions(+), 2 deletions(-) create mode 100644 demo/demo45.ts create mode 100644 demo/demo46.ts 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/numbering/level.ts b/src/file/numbering/level.ts index 716f313104..d53bd84522 100644 --- a/src/file/numbering/level.ts +++ b/src/file/numbering/level.ts @@ -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 { diff --git a/src/file/numbering/numbering.spec.ts b/src/file/numbering/numbering.spec.ts index 3ba39b15f6..2240fb4108 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 ae6526a229..eb4b523976 100644 --- a/src/file/paragraph/run/run.spec.ts +++ b/src/file/paragraph/run/run.spec.ts @@ -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(); diff --git a/src/file/paragraph/run/run.ts b/src/file/paragraph/run/run.ts index 32777cd31d..1ef16d0f45 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, @@ -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; + } } 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 41ef0fddab..ac39460861 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 d231c58f33..0b1438f3d0 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", From ebc7dca9496109c8ef8bb9e4d7b8bcaf60011363 Mon Sep 17 00:00:00 2001 From: Forman Date: Tue, 6 Aug 2019 13:39:05 +0300 Subject: [PATCH 4/4] Fix tests --- src/file/numbering/numbering.spec.ts | 4 ++-- src/file/paragraph/run/run.spec.ts | 4 ++-- src/file/styles/style/paragraph-style.spec.ts | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/file/numbering/numbering.spec.ts b/src/file/numbering/numbering.spec.ts index 2240fb4108..f483e723f3 100644 --- a/src/file/numbering/numbering.spec.ts +++ b/src/file/numbering/numbering.spec.ts @@ -330,7 +330,7 @@ describe("AbstractNumbering", () => { 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" } } }], + "w:rPr": [{ "w:highlight": { _attr: { "w:val": "005599" } } }], }); }); @@ -339,7 +339,7 @@ describe("AbstractNumbering", () => { 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" } } }], + "w:rPr": [{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } }], }); }); diff --git a/src/file/paragraph/run/run.spec.ts b/src/file/paragraph/run/run.spec.ts index eb4b523976..0a36d1dbe5 100644 --- a/src/file/paragraph/run/run.spec.ts +++ b/src/file/paragraph/run/run.spec.ts @@ -123,7 +123,7 @@ describe("Run", () => { "w:r": [ { "w:rPr": [ - { "w:highlight ": { _attr: { "w:val": "005599" } } }, + { "w:highlight": { _attr: { "w:val": "005599" } } }, { "w:highlightCs": { _attr: { @@ -146,7 +146,7 @@ describe("Run", () => { "w:r": [ { "w:rPr": [ - { "w:shd ": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } }, + { "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } }, { "w:shdCs": { _attr: { diff --git a/src/file/styles/style/paragraph-style.spec.ts b/src/file/styles/style/paragraph-style.spec.ts index ac39460861..4c6d1a402c 100644 --- a/src/file/styles/style/paragraph-style.spec.ts +++ b/src/file/styles/style/paragraph-style.spec.ts @@ -382,7 +382,7 @@ describe("ParagraphStyle", () => { "w:style": [ { _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { - "w:rPr": [{ "w:highlight ": { _attr: { "w:val": "005599" } } }], + "w:rPr": [{ "w:highlight": { _attr: { "w:val": "005599" } } }], }, ], }); @@ -395,7 +395,7 @@ describe("ParagraphStyle", () => { "w:style": [ { _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { - "w:rPr": [{ "w:shd ": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } }], + "w:rPr": [{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } }], }, ], });