Refactor row merging to table level

This commit is contained in:
Dolan Miu
2019-09-25 00:57:24 +01:00
parent cc36ea7542
commit 7aa4134e2b
5 changed files with 86 additions and 42 deletions

View File

@ -5,7 +5,6 @@ import { IXmlableObject, XmlComponent } from "file/xml-components";
import { ITableShadingAttributesProperties } from "../shading";
import { Table } from "../table";
import { TableRow } from "../table-row";
import { ITableCellMarginOptions } from "./cell-margin/table-cell-margins";
import { VerticalAlign, VMergeType } from "./table-cell-components";
import { TableCellProperties } from "./table-cell-properties";
@ -42,15 +41,8 @@ export interface ITableCellOptions {
readonly children: Array<Paragraph | Table>;
}
interface ITableCellMetaData {
readonly column: TableCell[];
readonly row: TableRow;
}
export class TableCell extends XmlComponent {
private readonly properties: TableCellProperties;
// tslint:disable-next-line: readonly-keyword
private metaData: ITableCellMetaData;
constructor(readonly options: ITableCellOptions) {
super("w:tc");
@ -82,6 +74,10 @@ export class TableCell extends XmlComponent {
this.properties.addGridSpan(options.columnSpan);
}
if (options.rowSpan && options.rowSpan > 1) {
this.properties.addVerticalMerge(VMergeType.RESTART);
}
if (options.borders) {
if (options.borders.top) {
this.properties.Borders.addTopBorder(options.borders.top.style, options.borders.top.size, options.borders.top.color);
@ -107,32 +103,10 @@ export class TableCell extends XmlComponent {
}
public prepForXml(): IXmlableObject | undefined {
// Row Span has to be added in this method and not the constructor because it needs to know information about the column which happens after Table Cell construction
// Row Span of 1 will crash word as it will add RESTART and not a corresponding CONTINUE
if (this.options.rowSpan && this.options.rowSpan > 1) {
this.properties.addVerticalMerge(VMergeType.RESTART);
const currentIndex = this.metaData.column.indexOf(this);
for (let i = currentIndex + 1; i <= currentIndex + this.options.rowSpan - 1; i++) {
this.metaData.column[i].metaData.row.Children.splice(
i,
0,
new TableCell({
children: [],
}),
);
this.metaData.column[i].properties.addVerticalMerge(VMergeType.CONTINUE);
}
}
// Cells must end with a paragraph
if (!(this.root[this.root.length - 1] instanceof Paragraph)) {
this.root.push(new Paragraph({}));
}
return super.prepForXml();
}
public set MetaData(metaData: ITableCellMetaData) {
this.metaData = metaData;
}
}