From 4cfb0ade8853a994dbe13888e2d58f2cbb11d7a7 Mon Sep 17 00:00:00 2001 From: Dolan Miu Date: Fri, 28 Oct 2022 22:20:16 +0100 Subject: [PATCH] #1744 - Add `vanish` and `specVanish` --- .nycrc | 2 +- demo/2-declaritive-styles.ts | 8 ++++++ docs/usage/text.md | 22 ++++++++++++++++ src/file/paragraph/run/properties.ts | 13 ++++++++++ src/file/paragraph/run/run.spec.ts | 38 ++++++++++++++++++++++++++++ src/file/paragraph/run/tab.ts | 1 + 6 files changed, 83 insertions(+), 1 deletion(-) diff --git a/.nycrc b/.nycrc index 7e8914f7fe..872d9c30a8 100644 --- a/.nycrc +++ b/.nycrc @@ -1,7 +1,7 @@ { "check-coverage": true, "statements": 99.72, - "branches": 97.97, + "branches": 98.08, "functions": 99.83, "lines": 99.72, "include": [ diff --git a/demo/2-declaritive-styles.ts b/demo/2-declaritive-styles.ts index 67c338364a..ce0c03b0ad 100644 --- a/demo/2-declaritive-styles.ts +++ b/demo/2-declaritive-styles.ts @@ -195,6 +195,14 @@ const doc = new Document({ new TextRun({ text: "and back to normal.", }), + new TextRun({ + text: "This text will be invisible!", + vanish: true, + }), + new TextRun({ + text: "This text will be VERY invisible! Word processors cannot override this!", + specVanish: true, + }), ], }), new Paragraph({ diff --git a/docs/usage/text.md b/docs/usage/text.md index 0b7b88686b..173aeabb74 100644 --- a/docs/usage/text.md +++ b/docs/usage/text.md @@ -151,6 +151,28 @@ const text = new TextRun({ }); ``` +### Vanish and SpecVanish + +You may want to hide your text in your document. + +`Vanish` should affect the normal display of text, but an application may have settings to force hidden text to be displayed. + +```ts +const text = new TextRun({ + text: "This text will be hidden", + vanish: true, +}); +``` + +`SpecVanish` was typically used to ensure that a paragraph style can be applied to a part of a paragraph, and still appear as in the Table of Contents (which in previous word processors would ignore the use of the style if it were being used as a character style). + +```ts +const text = new TextRun({ + text: "This text will be hidden forever.", + specVanish: true, +}); +``` + ## Break Sometimes you would want to put text underneath another line of text but inside the same paragraph. diff --git a/src/file/paragraph/run/properties.ts b/src/file/paragraph/run/properties.ts index 9943551364..b83e0b2c8d 100644 --- a/src/file/paragraph/run/properties.ts +++ b/src/file/paragraph/run/properties.ts @@ -45,6 +45,8 @@ export interface IRunStylePropertiesOptions { readonly imprint?: boolean; readonly revision?: IRunPropertiesChangeOptions; readonly border?: IBorderOptions; + readonly vanish?: boolean; + readonly specVanish?: boolean; } export interface IRunPropertiesOptions extends IRunStylePropertiesOptions { @@ -215,6 +217,17 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent { if (options.border) { this.push(new BorderElement("w:bdr", options.border)); } + + if (options.vanish) { + // https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_vanish_topic_ID0E6W3O.html + // http://www.datypic.com/sc/ooxml/e-w_vanish-1.html + this.push(new OnOffElement("w:vanish", options.vanish)); + } + + if (options.specVanish) { + // https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_specVanish_topic_ID0EIE1O.html + this.push(new OnOffElement("w:specVanish", options.vanish)); + } } public push(item: XmlComponent): void { diff --git a/src/file/paragraph/run/run.spec.ts b/src/file/paragraph/run/run.spec.ts index 2205461e64..3fd6de2d0d 100644 --- a/src/file/paragraph/run/run.spec.ts +++ b/src/file/paragraph/run/run.spec.ts @@ -519,4 +519,42 @@ describe("Run", () => { }); }); }); + + describe("#vanish and #specVanish", () => { + it("should correctly set vanish", () => { + const run = new Run({ + vanish: true, + }); + const tree = new Formatter().format(run); + expect(tree).to.deep.equal({ + "w:r": [ + { + "w:rPr": [ + { + "w:vanish": {}, + }, + ], + }, + ], + }); + }); + + it("should correctly set specVanish", () => { + const run = new Run({ + specVanish: true, + }); + const tree = new Formatter().format(run); + expect(tree).to.deep.equal({ + "w:r": [ + { + "w:rPr": [ + { + "w:specVanish": {}, + }, + ], + }, + ], + }); + }); + }); }); diff --git a/src/file/paragraph/run/tab.ts b/src/file/paragraph/run/tab.ts index eed0ad21f1..061e3deb3a 100644 --- a/src/file/paragraph/run/tab.ts +++ b/src/file/paragraph/run/tab.ts @@ -1,3 +1,4 @@ +// https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_tab_topic_ID0EM6AO.html import { XmlComponent } from "@file/xml-components"; //