diff --git a/ts/docx/document/index.ts b/ts/docx/document/index.ts index 555ebf55bf..a6e8db42b4 100644 --- a/ts/docx/document/index.ts +++ b/ts/docx/document/index.ts @@ -1,7 +1,9 @@ import { Paragraph } from "../paragraph"; +import { Table } from "../table"; import { XmlComponent } from "../xml-components"; import { Body } from "./body"; import { DocumentAttributes } from "./document-attributes"; + export class Document extends XmlComponent { private body: Body; @@ -39,4 +41,8 @@ export class Document extends XmlComponent { this.addParagraph(para); return para; } + + public addTable(table: Table): void { + this.body.push(table); + } } diff --git a/ts/docx/index.ts b/ts/docx/index.ts index 3c7ccdd38a..ad1049df20 100644 --- a/ts/docx/index.ts +++ b/ts/docx/index.ts @@ -2,3 +2,4 @@ export { Document } from "./document"; export { Paragraph } from "./paragraph"; export { Run } from "./run"; export { TextRun } from "./run/text-run"; +export { Table } from './table'; diff --git a/ts/docx/table.ts b/ts/docx/table.ts deleted file mode 100644 index fa2be84ac5..0000000000 --- a/ts/docx/table.ts +++ /dev/null @@ -1,3 +0,0 @@ -export class Table { - -} diff --git a/ts/docx/table/grid.ts b/ts/docx/table/grid.ts new file mode 100644 index 0000000000..70404104c0 --- /dev/null +++ b/ts/docx/table/grid.ts @@ -0,0 +1,17 @@ +import {XmlComponent, Attributes} from "../xml-components"; + +export class TableGrid extends XmlComponent { + private cols: Array; + constructor(cols: Array) { + super('w:tblGrid'); + this.cols = cols; + cols.forEach(col => this.root.push(col)); + } +} + +export class GridCol extends XmlComponent { + constructor(width?: number) { + super('w:gridCol'); + this.root.push(new Attributes({w: width.toString()})) + } +} diff --git a/ts/docx/table/index.ts b/ts/docx/table/index.ts new file mode 100644 index 0000000000..757af155eb --- /dev/null +++ b/ts/docx/table/index.ts @@ -0,0 +1,83 @@ +import {XmlComponent, Attributes} from "../xml-components"; +import {Paragraph} from "../paragraph"; +import {TableProperties} from "./properties"; +import {TableGrid, GridCol} from './grid'; + +export class Table extends XmlComponent { + properties: TableProperties; + private rows: Array; + private grid: TableGrid; + + constructor(rows: number, cols: number) { + super('w:tbl'); + this.properties = new TableProperties(); + this.root.push(this.properties); + + const gridCols = []; + for (let i = 0; i++; i < cols) { + gridCols.push(new GridCol()); + } + this.grid = new TableGrid(gridCols); + this.root.push(this.grid); + + this.rows = []; + for (let i = 0; i < rows; i++) { + const cells = []; + for (let j = 0; j < cols; j++) { + cells.push(new TableCell()); + } + const row = new TableRow(cells); + this.rows.push(row); + this.root.push(row); + } + } + + getRow(ix: number): TableRow { + return this.rows[ix]; + } +} + +class TableRow extends XmlComponent { + private properties: TableRowProperties; + private cells: Array; + + constructor(cells: Array) { + super('w:tr'); + this.properties = new TableRowProperties(); + this.root.push(this.properties); + this.cells = cells; + cells.forEach(c => this.root.push(c)) + } + + getCell(ix: number): TableCell { + return this.cells[ix]; + } +} + +class TableRowProperties extends XmlComponent { + constructor() { + super('w:trPr'); + } +} + +class TableCell extends XmlComponent { + private properties: TableCellProperties; + content: any; + + constructor() { + super('w:tc'); + this.properties = new TableCellProperties(); + this.root.push(this.properties); + this.root.push() + // 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'); + } +} diff --git a/ts/docx/table/properties.ts b/ts/docx/table/properties.ts new file mode 100644 index 0000000000..d37fef0c1b --- /dev/null +++ b/ts/docx/table/properties.ts @@ -0,0 +1,24 @@ +import {XmlComponent, Attributes} from "../xml-components"; + +export class TableProperties extends XmlComponent { + private width: PreferredTableWidth; + + constructor() { + super('w:tblPr'); + } + + setWidth(type: string, w: string) { + this.width = new PreferredTableWidth(type, w); + this.root.push(this.width); + } +} + +class PreferredTableWidth extends XmlComponent { + constructor(type: string, w: string) { + super('w:tblW'); + this.root.push(new Attributes({ + type, + w, + })) + } +}