Files
docx-js/src/file/table/table-cell/table-cell-properties.ts
Firat Ciftci 7d1129900f Fix: separate vertical alignment enums for ITableCellOptions and ISectionPropertiesOptions (#3079)
* fix: separate vertical alignment enums for table and section properties

- Introduced `VerticalAlignTable` for table-cell vertical alignment with valid values: `top`, `center`, and `bottom`.
- Added `VerticalAlignSection` for section properties, extending `VerticalAlignTable` with an additional value `both`.
- Marked the original `VerticalAlign` as deprecated for backward compatibility, directing users to the new enums.
- Updated type definitions for better clarity on valid vertical alignments.

* refactor: update vertical alignment imports and types for section and table properties

- Renamed `VerticalAlign` to `VerticalAlignSection` in section properties and `VerticalAlignTable` in table-cell properties for clarity.
- Updated type definitions to reflect the new enum names, ensuring better organization and understanding of vertical alignment options.
- Adjusted related test cases to utilize the new imports and types.

* refactor: update demos to use new enums for table and section properties for vertical alignment
2025-05-02 19:58:10 +01:00

69 lines
2.4 KiB
TypeScript

import { TableVerticalAlign, VerticalAlignElement } from "@file/vertical-align";
import { IgnoreIfEmptyXmlComponent } from "@file/xml-components";
import { IShadingAttributesProperties, Shading } from "../../shading";
import { ITableCellMarginOptions, TableCellMargin, TableCellMarginElementType } from "../table-properties/table-cell-margin";
import { ITableWidthProperties, TableWidthElement } from "../table-width";
import {
GridSpan,
ITableCellBorders,
TDirection,
TableCellBorders,
TextDirection,
VerticalMerge,
VerticalMergeType,
} from "./table-cell-components";
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;
};
export class TableCellProperties extends IgnoreIfEmptyXmlComponent {
public constructor(options: ITableCellPropertiesOptions) {
super("w:tcPr");
if (options.width) {
this.root.push(new TableWidthElement("w:tcW", options.width));
}
if (options.columnSpan) {
this.root.push(new GridSpan(options.columnSpan));
}
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));
}
if (options.borders) {
this.root.push(new TableCellBorders(options.borders));
}
if (options.shading) {
this.root.push(new Shading(options.shading));
}
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));
}
}
}