From 2276572902bb82362344ecbed6e7b2bea1e355d6 Mon Sep 17 00:00:00 2001 From: alexbogomolov Date: Tue, 29 Oct 2019 12:55:15 +0200 Subject: [PATCH 1/3] add ability to vertically align content of section --- demo/48-vertical-align.ts | 31 +++++++++++++++++++ .../document/body/section-properties/index.ts | 1 + .../section-properties/section-properties.ts | 9 +++++- .../vertical-align/index.ts | 2 ++ .../vertical-align-attributes.ts | 12 +++++++ .../vertical-align/vertical-align.ts | 18 +++++++++++ 6 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 demo/48-vertical-align.ts create mode 100644 src/file/document/body/section-properties/vertical-align/index.ts create mode 100644 src/file/document/body/section-properties/vertical-align/vertical-align-attributes.ts create mode 100644 src/file/document/body/section-properties/vertical-align/vertical-align.ts diff --git a/demo/48-vertical-align.ts b/demo/48-vertical-align.ts new file mode 100644 index 0000000000..c22e6da45a --- /dev/null +++ b/demo/48-vertical-align.ts @@ -0,0 +1,31 @@ +// Example of making content of section vertically aligned +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, SectionVerticalAlignValue, TextRun } from "../build"; + +const doc = new Document(); + +doc.addSection({ + properties: { + valign: SectionVerticalAlignValue.Center, + }, + children: [ + new Paragraph({ + children: [ + new TextRun("Hello World"), + new TextRun({ + text: "Foo Bar", + bold: true, + }), + new TextRun({ + text: "Github is the best", + bold: true, + }).tab(), + ], + }), + ], +}); + +Packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/src/file/document/body/section-properties/index.ts b/src/file/document/body/section-properties/index.ts index a8985b1249..47e56ec172 100644 --- a/src/file/document/body/section-properties/index.ts +++ b/src/file/document/body/section-properties/index.ts @@ -5,3 +5,4 @@ export * from "./page-size"; export * from "./page-number"; export * from "./page-border"; export * from "./line-number"; +export * from "./vertical-align"; diff --git a/src/file/document/body/section-properties/section-properties.ts b/src/file/document/body/section-properties/section-properties.ts index 072f68acca..b4732dea32 100644 --- a/src/file/document/body/section-properties/section-properties.ts +++ b/src/file/document/body/section-properties/section-properties.ts @@ -18,6 +18,7 @@ import { IPageNumberTypeAttributes, PageNumberType } from "./page-number"; import { PageSize } from "./page-size/page-size"; import { IPageSizeAttributes, PageOrientation } from "./page-size/page-size-attributes"; import { TitlePage } from "./title-page/title-page"; +import { ISectionVerticalAlignAttributes, SectionVerticalAlign } from "./vertical-align"; export interface IHeaderFooterGroup { readonly default?: T; @@ -45,7 +46,8 @@ export type SectionPropertiesOptions = IPageSizeAttributes & IPageNumberTypeAttributes & ILineNumberAttributes & IPageBordersOptions & - ITitlePageOptions & { + ITitlePageOptions & + ISectionVerticalAlignAttributes & { readonly column?: { readonly space?: number; readonly count?: number; @@ -87,6 +89,7 @@ export class SectionProperties extends XmlComponent { pageBorderBottom, pageBorderLeft, titlePage = false, + valign, } = options; this.options = options; @@ -121,6 +124,10 @@ export class SectionProperties extends XmlComponent { if (titlePage) { this.root.push(new TitlePage()); } + + if (valign) { + this.root.push(new SectionVerticalAlign(valign)); + } } private addHeaders(headers?: IHeaderFooterGroup): void { diff --git a/src/file/document/body/section-properties/vertical-align/index.ts b/src/file/document/body/section-properties/vertical-align/index.ts new file mode 100644 index 0000000000..1f3fb76bb2 --- /dev/null +++ b/src/file/document/body/section-properties/vertical-align/index.ts @@ -0,0 +1,2 @@ +export * from "./vertical-align"; +export * from "./vertical-align-attributes"; diff --git a/src/file/document/body/section-properties/vertical-align/vertical-align-attributes.ts b/src/file/document/body/section-properties/vertical-align/vertical-align-attributes.ts new file mode 100644 index 0000000000..3e7af94460 --- /dev/null +++ b/src/file/document/body/section-properties/vertical-align/vertical-align-attributes.ts @@ -0,0 +1,12 @@ +import { XmlAttributeComponent } from "file/xml-components"; +import { SectionVerticalAlignValue } from "./vertical-align"; + +export interface ISectionVerticalAlignAttributes { + readonly valign?: SectionVerticalAlignValue; +} + +export class SectionVerticalAlignAttributes extends XmlAttributeComponent { + protected readonly xmlKeys = { + valign: "w:val", + }; +} diff --git a/src/file/document/body/section-properties/vertical-align/vertical-align.ts b/src/file/document/body/section-properties/vertical-align/vertical-align.ts new file mode 100644 index 0000000000..bde6408ddd --- /dev/null +++ b/src/file/document/body/section-properties/vertical-align/vertical-align.ts @@ -0,0 +1,18 @@ +// http://officeopenxml.com/WPsection.php + +import { XmlComponent } from "file/xml-components"; +import { SectionVerticalAlignAttributes } from "./vertical-align-attributes"; + +export enum SectionVerticalAlignValue { + Both = "both", + Bottom = "bottom", + Center = "center", + Top = "top", +} + +export class SectionVerticalAlign extends XmlComponent { + constructor(value: SectionVerticalAlignValue) { + super("w:vAlign"); + this.root.push(new SectionVerticalAlignAttributes({ valign: value })); + } +} From afd468277edff74bd6080230ee6c29f01bb7affc Mon Sep 17 00:00:00 2001 From: alexbogomolov Date: Thu, 31 Oct 2019 13:54:53 +0200 Subject: [PATCH 2/3] refactor code --- demo/48-vertical-align.ts | 2 +- .../body/section-properties/section-properties.ts | 6 +++--- .../vertical-align/vertical-align-attributes.ts | 4 ++-- .../vertical-align/vertical-align.ts | 10 +++++----- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/demo/48-vertical-align.ts b/demo/48-vertical-align.ts index c22e6da45a..a9eb7986da 100644 --- a/demo/48-vertical-align.ts +++ b/demo/48-vertical-align.ts @@ -7,7 +7,7 @@ const doc = new Document(); doc.addSection({ properties: { - valign: SectionVerticalAlignValue.Center, + verticalAlign: SectionVerticalAlignValue.CENTER, }, children: [ new Paragraph({ diff --git a/src/file/document/body/section-properties/section-properties.ts b/src/file/document/body/section-properties/section-properties.ts index b4732dea32..37e09e4498 100644 --- a/src/file/document/body/section-properties/section-properties.ts +++ b/src/file/document/body/section-properties/section-properties.ts @@ -89,7 +89,7 @@ export class SectionProperties extends XmlComponent { pageBorderBottom, pageBorderLeft, titlePage = false, - valign, + verticalAlign, } = options; this.options = options; @@ -125,8 +125,8 @@ export class SectionProperties extends XmlComponent { this.root.push(new TitlePage()); } - if (valign) { - this.root.push(new SectionVerticalAlign(valign)); + if (verticalAlign) { + this.root.push(new SectionVerticalAlign(verticalAlign)); } } diff --git a/src/file/document/body/section-properties/vertical-align/vertical-align-attributes.ts b/src/file/document/body/section-properties/vertical-align/vertical-align-attributes.ts index 3e7af94460..fa09712a00 100644 --- a/src/file/document/body/section-properties/vertical-align/vertical-align-attributes.ts +++ b/src/file/document/body/section-properties/vertical-align/vertical-align-attributes.ts @@ -2,11 +2,11 @@ import { XmlAttributeComponent } from "file/xml-components"; import { SectionVerticalAlignValue } from "./vertical-align"; export interface ISectionVerticalAlignAttributes { - readonly valign?: SectionVerticalAlignValue; + readonly verticalAlign?: SectionVerticalAlignValue; } export class SectionVerticalAlignAttributes extends XmlAttributeComponent { protected readonly xmlKeys = { - valign: "w:val", + verticalAlign: "w:val", }; } diff --git a/src/file/document/body/section-properties/vertical-align/vertical-align.ts b/src/file/document/body/section-properties/vertical-align/vertical-align.ts index bde6408ddd..b025059d16 100644 --- a/src/file/document/body/section-properties/vertical-align/vertical-align.ts +++ b/src/file/document/body/section-properties/vertical-align/vertical-align.ts @@ -4,15 +4,15 @@ import { XmlComponent } from "file/xml-components"; import { SectionVerticalAlignAttributes } from "./vertical-align-attributes"; export enum SectionVerticalAlignValue { - Both = "both", - Bottom = "bottom", - Center = "center", - Top = "top", + BOTH = "both", + BOTTOM = "bottom", + CENTER = "center", + TOP = "top", } export class SectionVerticalAlign extends XmlComponent { constructor(value: SectionVerticalAlignValue) { super("w:vAlign"); - this.root.push(new SectionVerticalAlignAttributes({ valign: value })); + this.root.push(new SectionVerticalAlignAttributes({ verticalAlign: value })); } } From 8566c64eab6f601f5fc0e6fa75cf59bf9010208a Mon Sep 17 00:00:00 2001 From: alexbogomolov Date: Thu, 31 Oct 2019 17:01:17 +0200 Subject: [PATCH 3/3] improve coverage --- .../document/body/section-properties/section-properties.spec.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/file/document/body/section-properties/section-properties.spec.ts b/src/file/document/body/section-properties/section-properties.spec.ts index abdbf9e1ed..70fc567b83 100644 --- a/src/file/document/body/section-properties/section-properties.spec.ts +++ b/src/file/document/body/section-properties/section-properties.spec.ts @@ -8,6 +8,7 @@ import { Media } from "file/media"; import { PageBorderOffsetFrom } from "./page-border"; import { PageNumberFormat } from "./page-number"; import { SectionProperties } from "./section-properties"; +import { SectionVerticalAlignValue } from "./vertical-align"; describe("SectionProperties", () => { describe("#constructor()", () => { @@ -39,6 +40,7 @@ describe("SectionProperties", () => { pageNumberStart: 10, pageNumberFormatType: PageNumberFormat.CARDINAL_TEXT, titlePage: true, + verticalAlign: SectionVerticalAlignValue.TOP, }); const tree = new Formatter().format(properties); expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);