From 2d4412ce51ccd8fda15381949426c66b92cbbbab Mon Sep 17 00:00:00 2001 From: fmuscolino Date: Tue, 25 Jun 2019 12:02:53 +0200 Subject: [PATCH 1/2] Add table row height --- src/file/table/index.ts | 1 + src/file/table/table-row/index.ts | 1 + src/file/table/table-row/table-row-height.ts | 32 +++++++++++++++++++ .../table-row/table-row-properties.spec.ts | 28 ++++++++++++++++ .../table/table-row/table-row-properties.ts | 7 ++++ src/file/table/table-row/table-row.ts | 8 ++++- 6 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/file/table/table-row/table-row-height.ts diff --git a/src/file/table/index.ts b/src/file/table/index.ts index 47e0549503..cec3788f8e 100644 --- a/src/file/table/index.ts +++ b/src/file/table/index.ts @@ -2,3 +2,4 @@ export * from "./table"; export * from "./table-cell"; export * from "./table-properties"; export * from "./shading"; +export * from "./table-row"; diff --git a/src/file/table/table-row/index.ts b/src/file/table/table-row/index.ts index c8a880cf50..455c143d9a 100644 --- a/src/file/table/table-row/index.ts +++ b/src/file/table/table-row/index.ts @@ -1,2 +1,3 @@ export * from "./table-row"; export * from "./table-row-properties"; +export * from "./table-row-height"; diff --git a/src/file/table/table-row/table-row-height.ts b/src/file/table/table-row/table-row-height.ts new file mode 100644 index 0000000000..71e2c36aa1 --- /dev/null +++ b/src/file/table/table-row/table-row-height.ts @@ -0,0 +1,32 @@ +import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; + +export enum HeightRule { + /** Height is determined based on the content, so value is ignored. */ + AUTO = "auto", + /** At least the value specified */ + ATLEAST = "atLeast", + /** Exactly the value specified */ + EXACT = "exact", +} + +interface ITableRowHeight { + readonly height: number; + readonly rule: HeightRule; +} + +export class TableRowHeightAttributes extends XmlAttributeComponent { + protected readonly xmlKeys = { height: "w:val", rule: "w:hRule" }; +} + +export class TableRowHeight extends XmlComponent { + constructor(value: number, rule: HeightRule) { + super("w:trHeight"); + + this.root.push( + new TableRowHeightAttributes({ + height: value, + rule: rule, + }), + ); + } +} diff --git a/src/file/table/table-row/table-row-properties.spec.ts b/src/file/table/table-row/table-row-properties.spec.ts index 155238cb88..8c4cd2888e 100644 --- a/src/file/table/table-row/table-row-properties.spec.ts +++ b/src/file/table/table-row/table-row-properties.spec.ts @@ -1,5 +1,6 @@ import { expect } from "chai"; import { Formatter } from "export/formatter"; +import { HeightRule } from "file/table/table-row/table-row-height"; import { TableRowProperties } from "./table-row-properties"; describe("TableRowProperties", () => { @@ -31,4 +32,31 @@ describe("TableRowProperties", () => { expect(tree).to.deep.equal({ "w:trPr": [{ "w:tblHeader": { _attr: { "w:val": true } } }] }); }); }); + + describe("#setHeight", () => { + it("sets exact row height", () => { + const rowProperties = new TableRowProperties(); + rowProperties.setHeight(100, HeightRule.EXACT); + const tree = new Formatter().format(rowProperties); + expect(tree).to.deep.equal({ "w:trPr": [{ "w:trHeight": { _attr: { "w:val": 100, "w:hRule": "exact" } } }] }); + }); + }); + + describe("#setHeight", () => { + it("sets auto row height", () => { + const rowProperties = new TableRowProperties(); + rowProperties.setHeight(100, HeightRule.AUTO); + const tree = new Formatter().format(rowProperties); + expect(tree).to.deep.equal({ "w:trPr": [{ "w:trHeight": { _attr: { "w:val": 100, "w:hRule": "auto" } } }] }); + }); + }); + + describe("#setHeight", () => { + it("sets at least row height", () => { + const rowProperties = new TableRowProperties(); + rowProperties.setHeight(100, HeightRule.ATLEAST); + const tree = new Formatter().format(rowProperties); + expect(tree).to.deep.equal({ "w:trPr": [{ "w:trHeight": { _attr: { "w:val": 100, "w:hRule": "atLeast" } } }] }); + }); + }); }); diff --git a/src/file/table/table-row/table-row-properties.ts b/src/file/table/table-row/table-row-properties.ts index ed8332cc84..c9b60c13f4 100644 --- a/src/file/table/table-row/table-row-properties.ts +++ b/src/file/table/table-row/table-row-properties.ts @@ -1,3 +1,4 @@ +import { HeightRule, TableRowHeight } from "file/table/table-row/table-row-height"; import { IgnoreIfEmptyXmlComponent, XmlAttributeComponent, XmlComponent } from "file/xml-components"; export class TableRowProperties extends IgnoreIfEmptyXmlComponent { @@ -16,6 +17,12 @@ export class TableRowProperties extends IgnoreIfEmptyXmlComponent { return this; } + + public setHeight(height: number, rule: HeightRule): TableRowProperties { + this.root.push(new TableRowHeight(height, rule)); + + return this; + } } class CantSplitAttributes extends XmlAttributeComponent<{ readonly val: boolean }> { diff --git a/src/file/table/table-row/table-row.ts b/src/file/table/table-row/table-row.ts index 6e091abffd..f811392348 100644 --- a/src/file/table/table-row/table-row.ts +++ b/src/file/table/table-row/table-row.ts @@ -1,5 +1,5 @@ +import { HeightRule } from "file/table/table-row/table-row-height"; import { XmlComponent } from "file/xml-components"; - import { TableCell } from "../table-cell"; import { TableRowProperties } from "./table-row-properties"; @@ -49,4 +49,10 @@ export class TableRow extends XmlComponent { return this; } + + public setHeight(height: number, rule: HeightRule): TableRow { + this.properties.setHeight(height, rule); + + return this; + } } From b08354494c833447d7e220a0e9deaf51cbc7ab34 Mon Sep 17 00:00:00 2001 From: fmuscolino Date: Tue, 25 Jun 2019 14:48:45 +0200 Subject: [PATCH 2/2] Improve tests --- .../table-row/table-row-properties.spec.ts | 12 ++------ src/file/table/table-row/table-row.spec.ts | 28 +++++++++++++++++-- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/file/table/table-row/table-row-properties.spec.ts b/src/file/table/table-row/table-row-properties.spec.ts index 8c4cd2888e..05092d4178 100644 --- a/src/file/table/table-row/table-row-properties.spec.ts +++ b/src/file/table/table-row/table-row-properties.spec.ts @@ -34,25 +34,19 @@ describe("TableRowProperties", () => { }); describe("#setHeight", () => { - it("sets exact row height", () => { + it("sets row height exact", () => { const rowProperties = new TableRowProperties(); rowProperties.setHeight(100, HeightRule.EXACT); const tree = new Formatter().format(rowProperties); expect(tree).to.deep.equal({ "w:trPr": [{ "w:trHeight": { _attr: { "w:val": 100, "w:hRule": "exact" } } }] }); }); - }); - - describe("#setHeight", () => { - it("sets auto row height", () => { + it("sets row height auto", () => { const rowProperties = new TableRowProperties(); rowProperties.setHeight(100, HeightRule.AUTO); const tree = new Formatter().format(rowProperties); expect(tree).to.deep.equal({ "w:trPr": [{ "w:trHeight": { _attr: { "w:val": 100, "w:hRule": "auto" } } }] }); }); - }); - - describe("#setHeight", () => { - it("sets at least row height", () => { + it("sets row height at least", () => { const rowProperties = new TableRowProperties(); rowProperties.setHeight(100, HeightRule.ATLEAST); const tree = new Formatter().format(rowProperties); diff --git a/src/file/table/table-row/table-row.spec.ts b/src/file/table/table-row/table-row.spec.ts index 73b996cac2..3e9a479949 100644 --- a/src/file/table/table-row/table-row.spec.ts +++ b/src/file/table/table-row/table-row.spec.ts @@ -2,11 +2,11 @@ import { expect } from "chai"; import { Formatter } from "export/formatter"; +import { HeightRule } from "file/table/table-row/table-row-height"; +import { EMPTY_OBJECT } from "file/xml-components"; import { TableCell } from "../table-cell"; import { TableRow } from "./table-row"; -import { EMPTY_OBJECT } from "file/xml-components"; - describe("TableRow", () => { describe("#constructor", () => { it("should create with no cells", () => { @@ -67,4 +67,28 @@ describe("TableRow", () => { expect(() => tableRow.getCell(1)).to.throw(); }); }); + + describe("#setHeight", () => { + it("should set row height", () => { + const tableRow = new TableRow([]); + tableRow.setHeight(100, HeightRule.EXACT); + const tree = new Formatter().format(tableRow); + expect(tree).to.deep.equal({ + "w:tr": [ + { + "w:trPr": [ + { + "w:trHeight": { + _attr: { + "w:hRule": "exact", + "w:val": 100, + }, + }, + }, + ], + }, + ], + }); + }); + }); });