Files
docx-js/ts/docx/table/index.ts

108 lines
3.1 KiB
TypeScript
Raw Normal View History

import { Paragraph } from "../paragraph";
import { XmlComponent } from "../xml-components";
2017-03-10 17:38:04 +01:00
import { TableGrid } from "./grid";
2017-03-10 18:54:35 +01:00
import { TableProperties, widthTypes } from "./properties";
2017-03-07 19:22:10 +01:00
export class Table extends XmlComponent {
private properties: TableProperties;
private rows: TableRow[];
2017-03-07 19:22:10 +01:00
private grid: TableGrid;
constructor(rows: number, cols: number) {
super("w:tbl");
2017-03-07 19:22:10 +01:00
this.properties = new TableProperties();
this.root.push(this.properties);
const gridCols: number[] = [];
2017-03-10 17:38:04 +01:00
for (let i = 0; i < cols; i++) {
2017-03-10 18:54:35 +01:00
/*
0-width columns don't get rendered correctly, so we need
to give them some value. A reasonable default would be
~6in / numCols, but if we do that it becomes very hard
to resize the table using setWidth, unless the layout
algorithm is set to 'fixed'. Instead, the approach here
means even in 'auto' layout, setting a width on the
table will make it look reasonable, as the layout
algorithm will expand columns to fit its content
*/
gridCols.push(1);
2017-03-07 19:22:10 +01:00
}
this.grid = new TableGrid(gridCols);
this.root.push(this.grid);
this.rows = [];
for (let i = 0; i < rows; i++) {
const cells: TableCell[] = [];
2017-03-07 19:22:10 +01:00
for (let j = 0; j < cols; j++) {
cells.push(new TableCell());
}
const row = new TableRow(cells);
this.rows.push(row);
this.root.push(row);
}
}
public getRow(ix: number): TableRow {
2017-03-07 19:22:10 +01:00
return this.rows[ix];
}
public getCell(row: number, col: number): TableCell {
return this.getRow(row).getCell(col);
}
2017-03-10 18:54:35 +01:00
public setWidth(type: widthTypes, width: number | string): Table {
this.properties.setWidth(type, width);
return this;
}
public fixedWidthLayout(): Table {
this.properties.fixedWidthLayout();
return this;
}
2017-03-07 19:22:10 +01:00
}
class TableRow extends XmlComponent {
private properties: TableRowProperties;
private cells: TableCell[];
2017-03-07 19:22:10 +01:00
constructor(cells: TableCell[]) {
super("w:tr");
2017-03-07 19:22:10 +01:00
this.properties = new TableRowProperties();
this.root.push(this.properties);
this.cells = cells;
cells.forEach((c) => this.root.push(c));
2017-03-07 19:22:10 +01:00
}
public getCell(ix: number): TableCell {
2017-03-07 19:22:10 +01:00
return this.cells[ix];
}
}
class TableRowProperties extends XmlComponent {
constructor() {
super("w:trPr");
2017-03-07 19:22:10 +01:00
}
}
class TableCell extends XmlComponent {
2017-03-10 17:38:04 +01:00
public content: Paragraph;
2017-03-07 19:22:10 +01:00
private properties: TableCellProperties;
constructor() {
super("w:tc");
2017-03-07 19:22:10 +01:00
this.properties = new TableCellProperties();
this.root.push(this.properties);
// Table cells can have any block-level content, but for now
// we only allow a single paragraph:
this.content = new Paragraph();
this.root.push(this.content);
}
}
class TableCellProperties extends XmlComponent {
constructor() {
super("w:tcPr");
2017-03-07 19:22:10 +01:00
}
}