From 439ab8441e25e848ec67a628e98bda19397d6f26 Mon Sep 17 00:00:00 2001 From: askoufis Date: Thu, 8 Jul 2021 17:39:21 +1000 Subject: [PATCH] Export new `ColumnBreak` class via more generic `Break` class --- demo/67-column-break.ts | 28 +++++++++++++++++++ .../{page-break.spec.ts => break.spec.ts} | 27 +++++++++++++++++- .../formatting/{page-break.ts => break.ts} | 19 +++++++++++-- src/file/paragraph/formatting/index.ts | 2 +- src/file/paragraph/paragraph.ts | 3 +- src/file/paragraph/properties.ts | 2 +- 6 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 demo/67-column-break.ts rename src/file/paragraph/formatting/{page-break.spec.ts => break.spec.ts} (58%) rename src/file/paragraph/formatting/{page-break.ts => break.ts} (61%) diff --git a/demo/67-column-break.ts b/demo/67-column-break.ts new file mode 100644 index 0000000000..42a1a32f1b --- /dev/null +++ b/demo/67-column-break.ts @@ -0,0 +1,28 @@ +// Section with 2 columns including a column break +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, ColumnBreak, TextRun } from "../build"; + +const doc = new Document({ + sections: [ + { + properties: { + column: { + space: 708, + count: 2, + }, + }, + children: [ + new Paragraph({ children: [ + new TextRun('This text will be in the first column.'), + new ColumnBreak(), + new TextRun('This text will be in the second column.'), + ] }), + ], + }, + ], +}); + +Packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/src/file/paragraph/formatting/page-break.spec.ts b/src/file/paragraph/formatting/break.spec.ts similarity index 58% rename from src/file/paragraph/formatting/page-break.spec.ts rename to src/file/paragraph/formatting/break.spec.ts index 412a8d3c54..5f838438e4 100644 --- a/src/file/paragraph/formatting/page-break.spec.ts +++ b/src/file/paragraph/formatting/break.spec.ts @@ -2,7 +2,7 @@ import { expect } from "chai"; import { Formatter } from "export/formatter"; -import { PageBreak, PageBreakBefore } from "./page-break"; +import { ColumnBreak, PageBreak, PageBreakBefore } from "./break"; describe("PageBreak", () => { let pageBreak: PageBreak; @@ -29,6 +29,31 @@ describe("PageBreak", () => { }); }); +describe("ColumnBreak", () => { + let columnBreak: ColumnBreak; + + beforeEach(() => { + columnBreak = new ColumnBreak(); + }); + + describe("#constructor()", () => { + it("should create a Column Break with correct attributes", () => { + const tree = new Formatter().format(columnBreak); + expect(tree).to.deep.equal({ + "w:r": [ + { + "w:br": { + _attr: { + "w:type": "column", + }, + }, + }, + ], + }); + }); + }); +}); + describe("PageBreakBefore", () => { it("should create page break before", () => { const pageBreakBefore = new PageBreakBefore(); diff --git a/src/file/paragraph/formatting/page-break.ts b/src/file/paragraph/formatting/break.ts similarity index 61% rename from src/file/paragraph/formatting/page-break.ts rename to src/file/paragraph/formatting/break.ts index af08f766d3..9e9d400d21 100644 --- a/src/file/paragraph/formatting/page-break.ts +++ b/src/file/paragraph/formatting/break.ts @@ -2,12 +2,18 @@ import { Attributes, XmlComponent } from "file/xml-components"; import { Run } from "../run"; +enum BreakType { + COLUMN = "column", + PAGE = "page", + // textWrapping breaks are the default and already exposed via the "Run" class +} + class Break extends XmlComponent { - constructor() { + constructor(type: BreakType) { super("w:br"); this.root.push( new Attributes({ - type: "page", + type, }), ); } @@ -16,7 +22,14 @@ class Break extends XmlComponent { export class PageBreak extends Run { constructor() { super({}); - this.root.push(new Break()); + this.root.push(new Break(BreakType.PAGE)); + } +} + +export class ColumnBreak extends Run { + constructor() { + super({}); + this.root.push(new Break(BreakType.COLUMN)); } } diff --git a/src/file/paragraph/formatting/index.ts b/src/file/paragraph/formatting/index.ts index 0d83804dc6..b3f209e072 100644 --- a/src/file/paragraph/formatting/index.ts +++ b/src/file/paragraph/formatting/index.ts @@ -1,7 +1,7 @@ export * from "./alignment"; export * from "./border"; export * from "./indent"; -export * from "./page-break"; +export * from "./break"; export * from "./spacing"; export * from "./style"; export * from "./tab-stop"; diff --git a/src/file/paragraph/paragraph.ts b/src/file/paragraph/paragraph.ts index aef1a0a4fc..091a3a6bb3 100644 --- a/src/file/paragraph/paragraph.ts +++ b/src/file/paragraph/paragraph.ts @@ -6,7 +6,7 @@ import { IContext, IXmlableObject, XmlComponent } from "file/xml-components"; import { TargetModeType } from "../relationships/relationship/relationship"; import { DeletedTextRun, InsertedTextRun } from "../track-revision"; -import { PageBreak } from "./formatting/page-break"; +import { PageBreak, ColumnBreak } from "./formatting/break"; import { Bookmark, ConcreteHyperlink, ExternalHyperlink, InternalHyperlink } from "./links"; import { Math } from "./math"; import { IParagraphPropertiesOptions, ParagraphProperties } from "./properties"; @@ -18,6 +18,7 @@ export type ParagraphChild = | SymbolRun | Bookmark | PageBreak + | ColumnBreak | SequentialIdentifier | FootnoteReferenceRun | InternalHyperlink diff --git a/src/file/paragraph/properties.ts b/src/file/paragraph/properties.ts index d3cb2081e9..3f9560da2b 100644 --- a/src/file/paragraph/properties.ts +++ b/src/file/paragraph/properties.ts @@ -5,7 +5,7 @@ import { IShadingAttributesProperties, Shading } from "../shading"; import { Alignment, AlignmentType } from "./formatting/alignment"; import { Border, IBordersOptions, ThematicBreak } from "./formatting/border"; import { IIndentAttributesProperties, Indent } from "./formatting/indent"; -import { PageBreakBefore } from "./formatting/page-break"; +import { PageBreakBefore } from "./formatting/break"; import { ISpacingProperties, Spacing } from "./formatting/spacing"; import { HeadingLevel, Style } from "./formatting/style"; import { LeaderType, TabStop, TabStopPosition, TabStopType } from "./formatting/tab-stop";