Merge pull request #340 from filippomuscolino/feat/row-height

Add table row height
This commit is contained in:
Dolan
2019-06-25 20:18:17 +01:00
committed by GitHub
7 changed files with 96 additions and 3 deletions

View File

@ -2,3 +2,4 @@ export * from "./table";
export * from "./table-cell"; export * from "./table-cell";
export * from "./table-properties"; export * from "./table-properties";
export * from "./shading"; export * from "./shading";
export * from "./table-row";

View File

@ -1,2 +1,3 @@
export * from "./table-row"; export * from "./table-row";
export * from "./table-row-properties"; export * from "./table-row-properties";
export * from "./table-row-height";

View File

@ -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<ITableRowHeight> {
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,
}),
);
}
}

View File

@ -1,5 +1,6 @@
import { expect } from "chai"; import { expect } from "chai";
import { Formatter } from "export/formatter"; import { Formatter } from "export/formatter";
import { HeightRule } from "file/table/table-row/table-row-height";
import { TableRowProperties } from "./table-row-properties"; import { TableRowProperties } from "./table-row-properties";
describe("TableRowProperties", () => { describe("TableRowProperties", () => {
@ -31,4 +32,25 @@ describe("TableRowProperties", () => {
expect(tree).to.deep.equal({ "w:trPr": [{ "w:tblHeader": { _attr: { "w:val": true } } }] }); expect(tree).to.deep.equal({ "w:trPr": [{ "w:tblHeader": { _attr: { "w:val": true } } }] });
}); });
}); });
describe("#setHeight", () => {
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" } } }] });
});
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" } } }] });
});
it("sets row height at least", () => {
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" } } }] });
});
});
}); });

View File

@ -1,3 +1,4 @@
import { HeightRule, TableRowHeight } from "file/table/table-row/table-row-height";
import { IgnoreIfEmptyXmlComponent, XmlAttributeComponent, XmlComponent } from "file/xml-components"; import { IgnoreIfEmptyXmlComponent, XmlAttributeComponent, XmlComponent } from "file/xml-components";
export class TableRowProperties extends IgnoreIfEmptyXmlComponent { export class TableRowProperties extends IgnoreIfEmptyXmlComponent {
@ -16,6 +17,12 @@ export class TableRowProperties extends IgnoreIfEmptyXmlComponent {
return this; 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 }> { class CantSplitAttributes extends XmlAttributeComponent<{ readonly val: boolean }> {

View File

@ -2,11 +2,11 @@ import { expect } from "chai";
import { Formatter } from "export/formatter"; 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 { TableCell } from "../table-cell";
import { TableRow } from "./table-row"; import { TableRow } from "./table-row";
import { EMPTY_OBJECT } from "file/xml-components";
describe("TableRow", () => { describe("TableRow", () => {
describe("#constructor", () => { describe("#constructor", () => {
it("should create with no cells", () => { it("should create with no cells", () => {
@ -67,4 +67,28 @@ describe("TableRow", () => {
expect(() => tableRow.getCell(1)).to.throw(); 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,
},
},
},
],
},
],
});
});
});
}); });

View File

@ -1,5 +1,5 @@
import { HeightRule } from "file/table/table-row/table-row-height";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { TableCell } from "../table-cell"; import { TableCell } from "../table-cell";
import { TableRowProperties } from "./table-row-properties"; import { TableRowProperties } from "./table-row-properties";
@ -49,4 +49,10 @@ export class TableRow extends XmlComponent {
return this; return this;
} }
public setHeight(height: number, rule: HeightRule): TableRow {
this.properties.setHeight(height, rule);
return this;
}
} }