Declarative tables

This commit is contained in:
Dolan
2019-09-13 00:51:20 +01:00
parent 59fc1ed632
commit 418adca9f3
21 changed files with 978 additions and 536 deletions

View File

@ -10,7 +10,9 @@ import { TableRow } from "./table-row";
describe("TableRow", () => {
describe("#constructor", () => {
it("should create with no cells", () => {
const tableRow = new TableRow([]);
const tableRow = new TableRow({
children: [],
});
const tree = new Formatter().format(tableRow);
expect(tree).to.deep.equal({
"w:tr": EMPTY_OBJECT,
@ -18,7 +20,13 @@ describe("TableRow", () => {
});
it("should create with one cell", () => {
const tableRow = new TableRow([new TableCell()]);
const tableRow = new TableRow({
children: [
new TableCell({
children: [],
}),
],
});
const tree = new Formatter().format(tableRow);
expect(tree).to.deep.equal({
"w:tr": [
@ -32,46 +40,15 @@ describe("TableRow", () => {
],
});
});
});
describe("#getCell", () => {
it("should get the cell", () => {
const cell = new TableCell();
const tableRow = new TableRow([cell]);
expect(tableRow.getCell(0)).to.equal(cell);
});
it("should throw an error if index is out of bounds", () => {
const cell = new TableCell();
const tableRow = new TableRow([cell]);
expect(() => tableRow.getCell(1)).to.throw();
});
});
describe("#addGridSpan", () => {
it("should merge the cell", () => {
const tableRow = new TableRow([new TableCell(), new TableCell()]);
tableRow.addGridSpan(0, 2);
expect(() => tableRow.getCell(1)).to.throw();
});
});
describe("#mergeCells", () => {
it("should merge the cell", () => {
const tableRow = new TableRow([new TableCell(), new TableCell()]);
tableRow.mergeCells(0, 1);
expect(() => tableRow.getCell(1)).to.throw();
});
});
describe("#setHeight", () => {
it("should set row height", () => {
const tableRow = new TableRow([]);
tableRow.setHeight(100, HeightRule.EXACT);
const tableRow = new TableRow({
children: [],
height: {
height: 100,
rule: HeightRule.EXACT,
},
});
const tree = new Formatter().format(tableRow);
expect(tree).to.deep.equal({
"w:tr": [
@ -91,4 +68,22 @@ describe("TableRow", () => {
});
});
});
// describe("#mergeCells", () => {
// it("should merge the cell", () => {
// const tableRow = new TableRow({
// children: [
// new TableCell({
// children: [],
// }),
// new TableCell({
// children: [],
// }),
// ],
// });
// tableRow.mergeCells(0, 1);
// expect(() => tableRow.getCell(1)).to.throw();
// });
// });
});

View File

@ -3,56 +3,57 @@ import { XmlComponent } from "file/xml-components";
import { TableCell } from "../table-cell";
import { TableRowProperties } from "./table-row-properties";
export interface ITableRowOptions {
readonly cantSplit?: boolean;
readonly tableHeader?: boolean;
readonly height?: {
readonly height: number;
readonly rule: HeightRule;
};
readonly children: TableCell[];
}
export class TableRow extends XmlComponent {
private readonly properties: TableRowProperties;
constructor(private readonly cells: TableCell[]) {
constructor(private readonly options: ITableRowOptions) {
super("w:tr");
this.properties = new TableRowProperties();
this.root.push(this.properties);
cells.forEach((c) => this.root.push(c));
}
public getCell(index: number): TableCell {
const cell = this.cells[index];
if (!cell) {
throw Error("Index out of bounds when trying to get cell on row");
for (const child of options.children) {
this.root.push(child);
}
return cell;
if (options.cantSplit) {
this.properties.setCantSplit();
}
if (options.tableHeader) {
this.properties.setTableHeader();
}
if (options.height) {
this.properties.setHeight(options.height.height, options.height.rule);
}
}
public addGridSpan(index: number, cellSpan: number): TableCell {
const remainCell = this.cells[index];
remainCell.addGridSpan(cellSpan);
this.cells.splice(index + 1, cellSpan - 1);
this.root.splice(index + 2, cellSpan - 1);
return remainCell;
public get CellCount(): number {
return this.options.children.length;
}
public mergeCells(startIndex: number, endIndex: number): TableCell {
const cellSpan = endIndex - startIndex + 1;
// public mergeCells(startIndex: number, endIndex: number): TableCell {
// const cellSpan = endIndex - startIndex + 1;
return this.addGridSpan(startIndex, cellSpan);
}
// return this.addGridSpan(startIndex, cellSpan);
// }
public setCantSplit(): TableRow {
this.properties.setCantSplit();
// private addGridSpan(index: number, cellSpan: number): TableCell {
// const remainCell = this.options.children[index];
// remainCell.addGridSpan(cellSpan);
// this.options.children.splice(index + 1, cellSpan - 1);
// this.root.splice(index + 2, cellSpan - 1);
return this;
}
public setTableHeader(): TableRow {
this.properties.setTableHeader();
return this;
}
public setHeight(height: number, rule: HeightRule): TableRow {
this.properties.setHeight(height, rule);
return this;
}
// return remainCell;
// }
}