Work on new table API

This commit is contained in:
Dolan
2019-03-13 02:29:11 +00:00
parent f3ba11b21c
commit 2cb7d44a77
10 changed files with 91 additions and 30 deletions

View File

@ -13,8 +13,8 @@ export class TableRow extends XmlComponent {
cells.forEach((c) => this.root.push(c));
}
public getCell(ix: number): TableCell {
const cell = this.cells[ix];
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");

View File

@ -21,31 +21,62 @@ export interface IWidthOptions {
readonly type?: WidthType;
}
export interface ITableOptions {
readonly rows: number;
readonly columns: number;
readonly width?: number;
readonly widthUnitType?: WidthType;
readonly columnWidths?: number[];
readonly margains?: {
readonly margainUnitType?: WidthType;
readonly top?: number;
readonly bottom?: number;
readonly right?: number;
readonly left?: number;
};
readonly float?: ITableFloatOptions;
}
export class Table extends XmlComponent {
private readonly properties: TableProperties;
private readonly rows: TableRow[];
constructor(
rowCount: number,
columnCount: number,
widthOptions: IWidthOptions = { width: 100, type: WidthType.AUTO },
colSizes: number[] = Array<number>(columnCount).fill(100),
) {
constructor({
rows,
columns,
width = 100,
widthUnitType = WidthType.AUTO,
columnWidths = Array<number>(columns).fill(100),
margains: { margainUnitType, top, bottom, right, left } = { margainUnitType: WidthType.AUTO, top: 0, bottom: 0, right: 0, left: 0 },
float,
}: ITableOptions) {
super("w:tbl");
this.properties = new TableProperties();
this.root.push(this.properties);
this.properties.setBorder();
this.properties.setWidth(widthOptions.width, widthOptions.type);
const grid = new TableGrid(colSizes);
this.properties.setWidth(width, widthUnitType);
this.properties.CellMargin.addBottomMargin(bottom || 0, margainUnitType);
this.properties.CellMargin.addTopMargin(top || 0, margainUnitType);
this.properties.CellMargin.addLeftMargin(left || 0, margainUnitType);
this.properties.CellMargin.addRightMargin(right || 0, margainUnitType);
const grid = new TableGrid(columnWidths);
this.root.push(grid);
this.rows = [];
for (let i = 0; i < rowCount; i++) {
const cells = Array<TableCell>(columnCount).fill(new TableCell());
const row = new TableRow(cells);
this.rows.push(row);
this.root.push(row);
this.rows = Array(rows)
.fill(0)
.map(() => {
const cells = Array(columns)
.fill(0)
.map(() => new TableCell());
const row = new TableRow(cells);
return row;
});
this.rows.forEach((x) => this.root.push(x));
if (float) {
this.properties.setTableFloatProperties(float);
}
}