From 1649d2a0fac657084403e8cdb9f422e878f0dba2 Mon Sep 17 00:00:00 2001 From: Max Lay Date: Wed, 11 Dec 2019 15:05:09 +1300 Subject: [PATCH 1/4] Allow contextual spacing to be specified as an argument to paragraph style --- src/file/styles/style-options.ts | 1 + src/file/styles/style/paragraph-style.ts | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/file/styles/style-options.ts b/src/file/styles/style-options.ts index dcedadbec5..744f4244fb 100644 --- a/src/file/styles/style-options.ts +++ b/src/file/styles/style-options.ts @@ -29,6 +29,7 @@ export interface IRunStyleOptions { export interface IParagraphStyleOptions2 { readonly alignment?: AlignmentType; readonly thematicBreak?: boolean; + readonly contextualSpacing?: boolean; readonly rightTabStop?: number; readonly leftTabStop?: number; readonly indent?: IIndentAttributesProperties; diff --git a/src/file/styles/style/paragraph-style.ts b/src/file/styles/style/paragraph-style.ts index a47a33f492..4f7111625c 100644 --- a/src/file/styles/style/paragraph-style.ts +++ b/src/file/styles/style/paragraph-style.ts @@ -1,4 +1,4 @@ -import { Alignment, Indent, KeepLines, KeepNext, OutlineLevel, ParagraphProperties, Spacing, ThematicBreak } from "file/paragraph"; +import { Alignment, ContextualSpacing, Indent, KeepLines, KeepNext, OutlineLevel, ParagraphProperties, Spacing, ThematicBreak } from "file/paragraph"; import { TabStop, TabStopType } from "file/paragraph/formatting"; import * as formatting from "file/paragraph/run/formatting"; import { RunProperties } from "file/paragraph/run/properties"; @@ -134,6 +134,10 @@ export class ParagraphStyle extends Style { this.paragraphProperties.push(new ThematicBreak()); } + if (options.paragraph.contextualSpacing) { + this.paragraphProperties.push(new ContextualSpacing(options.paragraph.contextualSpacing)); + } + if (options.paragraph.rightTabStop) { this.paragraphProperties.push(new TabStop(TabStopType.RIGHT, options.paragraph.rightTabStop)); } From e0698554ad0276d1bba37e453c8a6fb1b99d9164 Mon Sep 17 00:00:00 2001 From: Max Lay Date: Wed, 11 Dec 2019 15:21:50 +1300 Subject: [PATCH 2/4] Fix style linting error --- src/file/styles/style/paragraph-style.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/file/styles/style/paragraph-style.ts b/src/file/styles/style/paragraph-style.ts index 4f7111625c..3adb1edbbe 100644 --- a/src/file/styles/style/paragraph-style.ts +++ b/src/file/styles/style/paragraph-style.ts @@ -1,4 +1,14 @@ -import { Alignment, ContextualSpacing, Indent, KeepLines, KeepNext, OutlineLevel, ParagraphProperties, Spacing, ThematicBreak } from "file/paragraph"; +import { + Alignment, + ContextualSpacing, + Indent, + KeepLines, + KeepNext, + OutlineLevel, + ParagraphProperties, + Spacing, + ThematicBreak, +} from "file/paragraph"; import { TabStop, TabStopType } from "file/paragraph/formatting"; import * as formatting from "file/paragraph/run/formatting"; import { RunProperties } from "file/paragraph/run/properties"; From b4cce534a5f57494e3953ed4e0756e8f894dd08f Mon Sep 17 00:00:00 2001 From: Max Lay Date: Thu, 12 Dec 2019 11:44:00 +1300 Subject: [PATCH 3/4] Test contextual spacing --- src/file/styles/style/paragraph-style.spec.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/file/styles/style/paragraph-style.spec.ts b/src/file/styles/style/paragraph-style.spec.ts index 93b0c6777c..8d9b4da15a 100644 --- a/src/file/styles/style/paragraph-style.spec.ts +++ b/src/file/styles/style/paragraph-style.spec.ts @@ -220,6 +220,32 @@ describe("ParagraphStyle", () => { }); }); + it("#contextualSpacing", () => { + const style = new ParagraphStyle({ + id: "myStyleId", + paragraph: { + contextualSpacing: true, + }, + }); + const tree = new Formatter().format(style); + expect(tree).to.deep.equal({ + "w:style": [ + { _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, + { + "w:pPr": [ + { + "w:contextualSpacing": { + _attr: { + "w:val": 1, + }, + }, + }, + ], + }, + ], + }); + }); + it("#leftTabStop", () => { const style = new ParagraphStyle({ id: "myStyleId", From 96471ecb456509c8b6492a47a945afc8a1bf36f1 Mon Sep 17 00:00:00 2001 From: mustapelto <46607498+mustapelto@users.noreply.github.com> Date: Sun, 15 Dec 2019 22:56:24 +0200 Subject: [PATCH 4/4] Fix for empty first paragraph If there is only one section, remove the extraneous empty paragraph during XML creation. --- src/file/document/body/body.spec.ts | 3 --- src/file/document/body/body.ts | 1 + src/file/file.spec.ts | 24 ++++++++++++------------ 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/file/document/body/body.spec.ts b/src/file/document/body/body.spec.ts index a5bc222fb4..b63a95dcd1 100644 --- a/src/file/document/body/body.spec.ts +++ b/src/file/document/body/body.spec.ts @@ -22,9 +22,6 @@ describe("Body", () => { expect(tree).to.deep.equal({ "w:body": [ - { - "w:p": {}, - }, { "w:sectPr": [ { "w:pgSz": { _attr: { "w:w": 10000, "w:h": 10000, "w:orient": "portrait" } } }, diff --git a/src/file/document/body/body.ts b/src/file/document/body/body.ts index e91b72d59a..ddf577b429 100644 --- a/src/file/document/body/body.ts +++ b/src/file/document/body/body.ts @@ -26,6 +26,7 @@ export class Body extends XmlComponent { public prepForXml(): IXmlableObject | undefined { if (this.sections.length === 1) { + this.root.splice(0, 1); this.root.push(this.sections.pop() as SectionProperties); } diff --git a/src/file/file.spec.ts b/src/file/file.spec.ts index e34feea1b6..13bdec87af 100644 --- a/src/file/file.spec.ts +++ b/src/file/file.spec.ts @@ -20,8 +20,8 @@ describe("File", () => { const tree = new Formatter().format(doc.Document.Body); - expect(tree["w:body"][1]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("default"); - expect(tree["w:body"][1]["w:sectPr"][5]["w:footerReference"]._attr["w:type"]).to.equal("default"); + expect(tree["w:body"][0]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("default"); + expect(tree["w:body"][0]["w:sectPr"][5]["w:footerReference"]._attr["w:type"]).to.equal("default"); }); it("should create with correct headers and footers", () => { @@ -39,8 +39,8 @@ describe("File", () => { const tree = new Formatter().format(doc.Document.Body); - expect(tree["w:body"][1]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("default"); - expect(tree["w:body"][1]["w:sectPr"][5]["w:footerReference"]._attr["w:type"]).to.equal("default"); + expect(tree["w:body"][0]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("default"); + expect(tree["w:body"][0]["w:sectPr"][5]["w:footerReference"]._attr["w:type"]).to.equal("default"); }); it("should create with first headers and footers", () => { @@ -58,8 +58,8 @@ describe("File", () => { const tree = new Formatter().format(doc.Document.Body); - expect(tree["w:body"][1]["w:sectPr"][5]["w:headerReference"]._attr["w:type"]).to.equal("first"); - expect(tree["w:body"][1]["w:sectPr"][7]["w:footerReference"]._attr["w:type"]).to.equal("first"); + expect(tree["w:body"][0]["w:sectPr"][5]["w:headerReference"]._attr["w:type"]).to.equal("first"); + expect(tree["w:body"][0]["w:sectPr"][7]["w:footerReference"]._attr["w:type"]).to.equal("first"); }); it("should create with correct headers", () => { @@ -81,13 +81,13 @@ describe("File", () => { const tree = new Formatter().format(doc.Document.Body); - expect(tree["w:body"][1]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("default"); - expect(tree["w:body"][1]["w:sectPr"][5]["w:headerReference"]._attr["w:type"]).to.equal("first"); - expect(tree["w:body"][1]["w:sectPr"][6]["w:headerReference"]._attr["w:type"]).to.equal("even"); + expect(tree["w:body"][0]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("default"); + expect(tree["w:body"][0]["w:sectPr"][5]["w:headerReference"]._attr["w:type"]).to.equal("first"); + expect(tree["w:body"][0]["w:sectPr"][6]["w:headerReference"]._attr["w:type"]).to.equal("even"); - expect(tree["w:body"][1]["w:sectPr"][7]["w:footerReference"]._attr["w:type"]).to.equal("default"); - expect(tree["w:body"][1]["w:sectPr"][8]["w:footerReference"]._attr["w:type"]).to.equal("first"); - expect(tree["w:body"][1]["w:sectPr"][9]["w:footerReference"]._attr["w:type"]).to.equal("even"); + expect(tree["w:body"][0]["w:sectPr"][7]["w:footerReference"]._attr["w:type"]).to.equal("default"); + expect(tree["w:body"][0]["w:sectPr"][8]["w:footerReference"]._attr["w:type"]).to.equal("first"); + expect(tree["w:body"][0]["w:sectPr"][9]["w:footerReference"]._attr["w:type"]).to.equal("even"); }); });