clean up initial table attempt (linter, interfaces, etc.)

This commit is contained in:
felipe
2017-03-10 16:51:19 +01:00
parent f1fb0c4f22
commit e7e5c61a90
4 changed files with 54 additions and 42 deletions

View File

@ -2,4 +2,4 @@ export { Document } from "./document";
export { Paragraph } from "./paragraph"; export { Paragraph } from "./paragraph";
export { Run } from "./run"; export { Run } from "./run";
export { TextRun } from "./run/text-run"; export { TextRun } from "./run/text-run";
export { Table } from './table'; export { Table } from "./table";

View File

@ -1,17 +1,19 @@
import {XmlComponent, Attributes} from "../xml-components"; import { XmlAttributeComponent, XmlComponent } from "../xml-components";
export class TableGrid extends XmlComponent { export class TableGrid extends XmlComponent {
private cols: Array<GridCol>; constructor(cols: number[]) {
constructor(cols: Array<GridCol>) { super("w:tblGrid");
super('w:tblGrid'); cols.forEach((col) => this.root.push(new GridCol(col)));
this.cols = cols;
cols.forEach(col => this.root.push(col));
} }
} }
class GridColAttributes extends XmlAttributeComponent<{w: number}> {
protected xmlKeys = {w: "w:w"};
}
export class GridCol extends XmlComponent { export class GridCol extends XmlComponent {
constructor(width?: number) { constructor(width?: number) {
super('w:gridCol'); super("w:gridCol");
this.root.push(new Attributes({w: width.toString()})) this.root.push(new GridColAttributes({w: width}));
} }
} }

View File

@ -1,21 +1,22 @@
import {XmlComponent, Attributes} from "../xml-components"; import { Paragraph } from "../paragraph";
import {Paragraph} from "../paragraph"; import { XmlComponent } from "../xml-components";
import {TableProperties} from "./properties";
import {TableGrid, GridCol} from './grid'; import { GridCol, TableGrid } from "./grid";
import { TableProperties } from "./properties";
export class Table extends XmlComponent { export class Table extends XmlComponent {
properties: TableProperties; private properties: TableProperties;
private rows: Array<TableRow>; private rows: TableRow[];
private grid: TableGrid; private grid: TableGrid;
constructor(rows: number, cols: number) { constructor(rows: number, cols: number) {
super('w:tbl'); super("w:tbl");
this.properties = new TableProperties(); this.properties = new TableProperties();
this.root.push(this.properties); this.root.push(this.properties);
const gridCols = []; const gridCols: number[] = [];
for (let i = 0; i++; i < cols) { for (let i = 0; i++; i < cols) {
gridCols.push(new GridCol()); gridCols.push(0);
} }
this.grid = new TableGrid(gridCols); this.grid = new TableGrid(gridCols);
this.root.push(this.grid); this.root.push(this.grid);
@ -32,43 +33,46 @@ export class Table extends XmlComponent {
} }
} }
getRow(ix: number): TableRow { public getRow(ix: number): TableRow {
return this.rows[ix]; return this.rows[ix];
} }
public getCell(row: number, col: number): TableCell {
return this.getRow(row).getCell(col);
}
} }
class TableRow extends XmlComponent { class TableRow extends XmlComponent {
private properties: TableRowProperties; private properties: TableRowProperties;
private cells: Array<TableCell>; private cells: TableCell[];
constructor(cells: Array<TableCell>) { constructor(cells: TableCell[]) {
super('w:tr'); super("w:tr");
this.properties = new TableRowProperties(); this.properties = new TableRowProperties();
this.root.push(this.properties); this.root.push(this.properties);
this.cells = cells; this.cells = cells;
cells.forEach(c => this.root.push(c)) cells.forEach((c) => this.root.push(c));
} }
getCell(ix: number): TableCell { public getCell(ix: number): TableCell {
return this.cells[ix]; return this.cells[ix];
} }
} }
class TableRowProperties extends XmlComponent { class TableRowProperties extends XmlComponent {
constructor() { constructor() {
super('w:trPr'); super("w:trPr");
} }
} }
class TableCell extends XmlComponent { class TableCell extends XmlComponent {
public content: XmlComponent;
private properties: TableCellProperties; private properties: TableCellProperties;
content: any;
constructor() { constructor() {
super('w:tc'); super("w:tc");
this.properties = new TableCellProperties(); this.properties = new TableCellProperties();
this.root.push(this.properties); this.root.push(this.properties);
this.root.push()
// Table cells can have any block-level content, but for now // Table cells can have any block-level content, but for now
// we only allow a single paragraph: // we only allow a single paragraph:
this.content = new Paragraph(); this.content = new Paragraph();
@ -78,6 +82,6 @@ class TableCell extends XmlComponent {
class TableCellProperties extends XmlComponent { class TableCellProperties extends XmlComponent {
constructor() { constructor() {
super('w:tcPr'); super("w:tcPr");
} }
} }

View File

@ -1,24 +1,30 @@
import {XmlComponent, Attributes} from "../xml-components"; import { XmlAttributeComponent, XmlComponent } from "../xml-components";
type widthTypes = "dxa" | "pct" | "nil" | "auto";
export class TableProperties extends XmlComponent { export class TableProperties extends XmlComponent {
private width: PreferredTableWidth;
constructor() { constructor() {
super('w:tblPr'); super("w:tblPr");
} }
setWidth(type: string, w: string) { public setWidth(type: widthTypes, w: number | string): TableProperties {
this.width = new PreferredTableWidth(type, w); this.root.push(new PreferredTableWidth(type, w));
this.root.push(this.width); return this;
} }
} }
interface ITableWidth {
type: widthTypes;
w: number | string;
}
class TableWidthAttributes extends XmlAttributeComponent<ITableWidth> {
protected xmlKeys = {type: "w:type", w: "w:w"};
}
class PreferredTableWidth extends XmlComponent { class PreferredTableWidth extends XmlComponent {
constructor(type: string, w: string) { constructor(type: widthTypes, w: number | string) {
super('w:tblW'); super("w:tblW");
this.root.push(new Attributes({ this.root.push(new TableWidthAttributes({type, w}));
type,
w,
}))
} }
} }