Declarative tables
This commit is contained in:
@ -1,77 +1,107 @@
|
||||
// http://officeopenxml.com/WPtableGrid.php
|
||||
import { Paragraph } from "file/paragraph";
|
||||
import { BorderStyle } from "file/styles";
|
||||
import { IXmlableObject, XmlComponent } from "file/xml-components";
|
||||
|
||||
import { ITableShadingAttributesProperties } from "../shading";
|
||||
import { Table } from "../table";
|
||||
import { ITableCellMarginOptions } from "./cell-margin/table-cell-margins";
|
||||
import { TableCellBorders, VerticalAlign, VMergeType } from "./table-cell-components";
|
||||
import { VerticalAlign, VMergeType } from "./table-cell-components";
|
||||
import { TableCellProperties } from "./table-cell-properties";
|
||||
|
||||
export interface ITableCellOptions {
|
||||
readonly shading?: ITableShadingAttributesProperties;
|
||||
readonly margins?: ITableCellMarginOptions;
|
||||
readonly verticalAlign?: VerticalAlign;
|
||||
readonly verticalMerge?: VMergeType;
|
||||
readonly columnSpan?: number;
|
||||
readonly borders?: {
|
||||
readonly top?: {
|
||||
readonly style: BorderStyle;
|
||||
readonly size: number;
|
||||
readonly color: string;
|
||||
};
|
||||
readonly bottom?: {
|
||||
readonly style: BorderStyle;
|
||||
readonly size: number;
|
||||
readonly color: string;
|
||||
};
|
||||
readonly left?: {
|
||||
readonly style: BorderStyle;
|
||||
readonly size: number;
|
||||
readonly color: string;
|
||||
};
|
||||
readonly right?: {
|
||||
readonly style: BorderStyle;
|
||||
readonly size: number;
|
||||
readonly color: string;
|
||||
};
|
||||
};
|
||||
readonly children: Array<Paragraph | Table>;
|
||||
}
|
||||
|
||||
export class TableCell extends XmlComponent {
|
||||
private readonly properties: TableCellProperties;
|
||||
|
||||
constructor() {
|
||||
constructor(readonly options: ITableCellOptions) {
|
||||
super("w:tc");
|
||||
|
||||
this.properties = new TableCellProperties();
|
||||
this.root.push(this.properties);
|
||||
}
|
||||
|
||||
public add(item: Paragraph | Table): TableCell {
|
||||
this.root.push(item);
|
||||
for (const child of options.children) {
|
||||
this.root.push(child);
|
||||
}
|
||||
|
||||
return this;
|
||||
if (options.verticalAlign) {
|
||||
this.properties.setVerticalAlign(options.verticalAlign);
|
||||
}
|
||||
|
||||
if (options.verticalMerge) {
|
||||
this.properties.addVerticalMerge(options.verticalMerge);
|
||||
}
|
||||
|
||||
if (options.margins) {
|
||||
this.properties.addMargins(options.margins);
|
||||
}
|
||||
|
||||
if (options.shading) {
|
||||
this.properties.setShading(options.shading);
|
||||
}
|
||||
|
||||
if (options.columnSpan) {
|
||||
this.properties.addGridSpan(options.columnSpan);
|
||||
}
|
||||
|
||||
if (options.borders) {
|
||||
if (options.borders.top) {
|
||||
this.properties.Borders.addTopBorder(options.borders.top.style, options.borders.top.size, options.borders.top.color);
|
||||
}
|
||||
if (options.borders.bottom) {
|
||||
this.properties.Borders.addBottomBorder(
|
||||
options.borders.bottom.style,
|
||||
options.borders.bottom.size,
|
||||
options.borders.bottom.color,
|
||||
);
|
||||
}
|
||||
if (options.borders.left) {
|
||||
this.properties.Borders.addLeftBorder(options.borders.left.style, options.borders.left.size, options.borders.left.color);
|
||||
}
|
||||
if (options.borders.right) {
|
||||
this.properties.Borders.addRightBorder(
|
||||
options.borders.right.style,
|
||||
options.borders.right.size,
|
||||
options.borders.right.color,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public prepForXml(): IXmlableObject | undefined {
|
||||
// Cells must end with a paragraph
|
||||
if (!(this.root[this.root.length - 1] instanceof Paragraph)) {
|
||||
const para = new Paragraph({});
|
||||
this.add(para);
|
||||
this.root.push(new Paragraph({}));
|
||||
}
|
||||
return super.prepForXml();
|
||||
}
|
||||
|
||||
public setVerticalAlign(type: VerticalAlign): TableCell {
|
||||
this.properties.setVerticalAlign(type);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public addGridSpan(cellSpan: number): TableCell {
|
||||
this.properties.addGridSpan(cellSpan);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public addVerticalMerge(type: VMergeType): TableCell {
|
||||
this.properties.addVerticalMerge(type);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public setMargins(margins: ITableCellMarginOptions): TableCell {
|
||||
this.properties.addMargins(margins);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public setShading(attrs: ITableShadingAttributesProperties): TableCell {
|
||||
this.properties.setShading(attrs);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public get Borders(): TableCellBorders {
|
||||
return this.properties.Borders;
|
||||
}
|
||||
|
||||
public get Properties(): TableCellProperties {
|
||||
return this.properties;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user