Files
docx-js/src/file/table/table-cell/table-cell-properties.ts

69 lines
2.4 KiB
TypeScript
Raw Normal View History

import { TableVerticalAlign, VerticalAlignElement } from "@file/vertical-align";
import { IgnoreIfEmptyXmlComponent } from "@file/xml-components";
2018-10-23 23:44:50 +01:00
import { IShadingAttributesProperties, Shading } from "../../shading";
import { ITableCellMarginOptions, TableCellMargin, TableCellMarginElementType } from "../table-properties/table-cell-margin";
import { ITableWidthProperties, TableWidthElement } from "../table-width";
2019-09-26 02:03:17 +01:00
import {
GridSpan,
ITableCellBorders,
2020-05-10 10:36:25 -07:00
TDirection,
TableCellBorders,
2020-05-10 10:36:25 -07:00
TextDirection,
2019-09-26 02:03:17 +01:00
VerticalMerge,
VerticalMergeType,
} from "./table-cell-components";
2018-10-23 23:44:50 +01:00
export type ITableCellPropertiesOptions = {
readonly shading?: IShadingAttributesProperties;
readonly margins?: ITableCellMarginOptions;
readonly verticalAlign?: TableVerticalAlign;
readonly textDirection?: (typeof TextDirection)[keyof typeof TextDirection];
readonly verticalMerge?: (typeof VerticalMergeType)[keyof typeof VerticalMergeType];
readonly width?: ITableWidthProperties;
readonly columnSpan?: number;
readonly rowSpan?: number;
readonly borders?: ITableCellBorders;
};
2018-10-23 23:44:50 +01:00
export class TableCellProperties extends IgnoreIfEmptyXmlComponent {
2022-08-31 07:52:27 +01:00
public constructor(options: ITableCellPropertiesOptions) {
2018-10-23 23:44:50 +01:00
super("w:tcPr");
if (options.width) {
this.root.push(new TableWidthElement("w:tcW", options.width));
}
2018-10-23 23:44:50 +01:00
if (options.columnSpan) {
this.root.push(new GridSpan(options.columnSpan));
}
2018-10-23 23:44:50 +01:00
if (options.verticalMerge) {
this.root.push(new VerticalMerge(options.verticalMerge));
} else if (options.rowSpan && options.rowSpan > 1) {
// if cell already have a `verticalMerge`, don't handle `rowSpan`
this.root.push(new VerticalMerge(VerticalMergeType.RESTART));
}
2018-10-23 23:44:50 +01:00
if (options.borders) {
this.root.push(new TableCellBorders(options.borders));
}
2018-10-23 23:44:50 +01:00
if (options.shading) {
this.root.push(new Shading(options.shading));
}
2018-10-23 23:44:50 +01:00
if (options.margins) {
this.root.push(new TableCellMargin(TableCellMarginElementType.TABLE_CELL, options.margins));
}
if (options.textDirection) {
this.root.push(new TDirection(options.textDirection));
}
if (options.verticalAlign) {
this.root.push(new VerticalAlignElement(options.verticalAlign));
}
}
2018-10-23 23:44:50 +01:00
}