feat: Added TableCellSpacing (#3052)

* feat: Added TableCellSpacingElement

* feat: Added cellSpacing (Table-constructor, TableProperties-constructor and ITableOptions)

* feat: Added cellSpacing (ITablePropertiesOptions and constructor implementation)

* feat: Added cellSpacing (ITableRowPropertiesOptions and constructor implementation)

* feat: Added cellSpacing (Implementation Test)

---------

Co-authored-by: Philip <1760073-philip.asx@users.noreply.gitlab.com>
This commit is contained in:
Philip
2025-04-16 23:38:28 +02:00
committed by GitHub
parent 72abd4b7ac
commit 44d4b93d06
6 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,39 @@
// http://officeopenxml.com/WPtableCellSpacing.php
import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { Percentage, UniversalMeasure, measurementOrPercentValue } from "@util/values";
// <xsd:simpleType name="ST_TblCellSpacing">
// <xsd:restriction base="xsd:string">
// <xsd:enumeration value="nil"/>
// <xsd:enumeration value="dxa"/>
// </xsd:restriction>
// </xsd:simpleType>
export const CellSpacingType = {
/** Value is in twentieths of a point */
DXA: "dxa",
/** No (empty) value. */
NIL: "nil",
} as const;
// <xsd:complexType name="CT_TblCellSpacing">
// <xsd:attribute name="w" type="ST_MeasurementOrPercent"/>
// <xsd:attribute name="type" type="ST_TblCellSpacing"/>
// </xsd:complexType>
export type ITableCellSpacingProperties = {
readonly value: number | Percentage | UniversalMeasure;
readonly type?: (typeof CellSpacingType)[keyof typeof CellSpacingType];
};
export class TableCellSpacingElement extends XmlComponent {
public constructor({ type = CellSpacingType.DXA, value }: ITableCellSpacingProperties) {
super("w:tblCellSpacing");
this.root.push(
new NextAttributeComponent<ITableCellSpacingProperties>({
type: { key: "w:type", value: type },
value: { key: "w:w", value: measurementOrPercentValue(value) },
}),
);
}
}

View File

@ -7,6 +7,7 @@ import { ShadingType } from "@file/shading";
import { WidthType } from "../table-width"; import { WidthType } from "../table-width";
import { TableLayoutType } from "./table-layout"; import { TableLayoutType } from "./table-layout";
import { TableProperties } from "./table-properties"; import { TableProperties } from "./table-properties";
import { CellSpacingType } from "../table-cell-spacing";
describe("TableProperties", () => { describe("TableProperties", () => {
describe("#constructor", () => { describe("#constructor", () => {
@ -91,6 +92,19 @@ describe("TableProperties", () => {
"w:tblPr": [{ "w:tblLayout": { _attr: { "w:type": "fixed" } } }], "w:tblPr": [{ "w:tblLayout": { _attr: { "w:type": "fixed" } } }],
}); });
}); });
it("should add a table cell spacing property", () => {
const tp = new TableProperties({
cellSpacing: {
value: 1234,
type: CellSpacingType.DXA,
},
});
const tree = new Formatter().format(tp);
expect(tree).to.deep.equal({
"w:tblPr": [{ "w:tblCellSpacing": { _attr: { "w:type": "dxa", "w:w": 1234 } } }],
});
});
}); });
describe("#cellMargin", () => { describe("#cellMargin", () => {

View File

@ -30,6 +30,7 @@ import { ITableBordersOptions, TableBorders } from "./table-borders";
import { ITableCellMarginOptions, TableCellMargin, TableCellMarginElementType } from "./table-cell-margin"; import { ITableCellMarginOptions, TableCellMargin, TableCellMarginElementType } from "./table-cell-margin";
import { ITableFloatOptions, TableFloatProperties } from "./table-float-properties"; import { ITableFloatOptions, TableFloatProperties } from "./table-float-properties";
import { TableLayout, TableLayoutType } from "./table-layout"; import { TableLayout, TableLayoutType } from "./table-layout";
import { ITableCellSpacingProperties, TableCellSpacingElement } from "../table-cell-spacing";
export type ITablePropertiesOptions = { export type ITablePropertiesOptions = {
readonly width?: ITableWidthProperties; readonly width?: ITableWidthProperties;
@ -42,6 +43,7 @@ export type ITablePropertiesOptions = {
readonly alignment?: (typeof AlignmentType)[keyof typeof AlignmentType]; readonly alignment?: (typeof AlignmentType)[keyof typeof AlignmentType];
readonly cellMargin?: ITableCellMarginOptions; readonly cellMargin?: ITableCellMarginOptions;
readonly visuallyRightToLeft?: boolean; readonly visuallyRightToLeft?: boolean;
readonly cellSpacing?: ITableCellSpacingProperties;
}; };
export class TableProperties extends IgnoreIfEmptyXmlComponent { export class TableProperties extends IgnoreIfEmptyXmlComponent {
@ -87,5 +89,9 @@ export class TableProperties extends IgnoreIfEmptyXmlComponent {
if (options.cellMargin) { if (options.cellMargin) {
this.root.push(new TableCellMargin(TableCellMarginElementType.TABLE, options.cellMargin)); this.root.push(new TableCellMargin(TableCellMarginElementType.TABLE, options.cellMargin));
} }
if (options.cellSpacing) {
this.root.push(new TableCellSpacingElement(options.cellSpacing));
}
} }
} }

View File

@ -4,6 +4,7 @@ import { Formatter } from "@export/formatter";
import { HeightRule } from "@file/table/table-row/table-row-height"; import { HeightRule } from "@file/table/table-row/table-row-height";
import { TableRowProperties } from "./table-row-properties"; import { TableRowProperties } from "./table-row-properties";
import { CellSpacingType } from "../table-cell-spacing";
describe("TableRowProperties", () => { describe("TableRowProperties", () => {
describe("#constructor", () => { describe("#constructor", () => {
@ -60,5 +61,18 @@ describe("TableRowProperties", () => {
const tree = new Formatter().format(rowProperties); const tree = new Formatter().format(rowProperties);
expect(tree).to.deep.equal({ "w:trPr": [{ "w:trHeight": { _attr: { "w:val": 100, "w:hRule": "atLeast" } } }] }); expect(tree).to.deep.equal({ "w:trPr": [{ "w:trHeight": { _attr: { "w:val": 100, "w:hRule": "atLeast" } } }] });
}); });
it("should add a table cell spacing property", () => {
const rowProperties = new TableRowProperties({
cellSpacing: {
value: 1234,
type: CellSpacingType.DXA,
},
});
const tree = new Formatter().format(rowProperties);
expect(tree).to.deep.equal({
"w:trPr": [{ "w:tblCellSpacing": { _attr: { "w:type": "dxa", "w:w": 1234 } } }],
});
});
}); });
}); });

View File

@ -31,6 +31,7 @@ import { IgnoreIfEmptyXmlComponent, OnOffElement } from "@file/xml-components";
import { PositiveUniversalMeasure } from "@util/values"; import { PositiveUniversalMeasure } from "@util/values";
import { HeightRule, TableRowHeight } from "./table-row-height"; import { HeightRule, TableRowHeight } from "./table-row-height";
import { ITableCellSpacingProperties, TableCellSpacingElement } from "../table-cell-spacing";
export type ITableRowPropertiesOptions = { export type ITableRowPropertiesOptions = {
readonly cantSplit?: boolean; readonly cantSplit?: boolean;
@ -39,6 +40,7 @@ export type ITableRowPropertiesOptions = {
readonly value: number | PositiveUniversalMeasure; readonly value: number | PositiveUniversalMeasure;
readonly rule: (typeof HeightRule)[keyof typeof HeightRule]; readonly rule: (typeof HeightRule)[keyof typeof HeightRule];
}; };
readonly cellSpacing?: ITableCellSpacingProperties;
}; };
export class TableRowProperties extends IgnoreIfEmptyXmlComponent { export class TableRowProperties extends IgnoreIfEmptyXmlComponent {
@ -56,5 +58,9 @@ export class TableRowProperties extends IgnoreIfEmptyXmlComponent {
if (options.height) { if (options.height) {
this.root.push(new TableRowHeight(options.height.value, options.height.rule)); this.root.push(new TableRowHeight(options.height.value, options.height.rule));
} }
if (options.cellSpacing) {
this.root.push(new TableCellSpacingElement(options.cellSpacing));
}
} }
} }

View File

@ -4,6 +4,7 @@ import { FileChild } from "@file/file-child";
import { AlignmentType } from "../paragraph"; import { AlignmentType } from "../paragraph";
import { TableGrid } from "./grid"; import { TableGrid } from "./grid";
import { TableCell, VerticalMergeType } from "./table-cell"; import { TableCell, VerticalMergeType } from "./table-cell";
import { ITableCellSpacingProperties } from "./table-cell-spacing";
import { ITableBordersOptions, ITableFloatOptions, TableProperties } from "./table-properties"; import { ITableBordersOptions, ITableFloatOptions, TableProperties } from "./table-properties";
import { ITableCellMarginOptions } from "./table-properties/table-cell-margin"; import { ITableCellMarginOptions } from "./table-properties/table-cell-margin";
import { TableLayoutType } from "./table-properties/table-layout"; import { TableLayoutType } from "./table-properties/table-layout";
@ -32,6 +33,7 @@ export type ITableOptions = {
readonly borders?: ITableBordersOptions; readonly borders?: ITableBordersOptions;
readonly alignment?: (typeof AlignmentType)[keyof typeof AlignmentType]; readonly alignment?: (typeof AlignmentType)[keyof typeof AlignmentType];
readonly visuallyRightToLeft?: boolean; readonly visuallyRightToLeft?: boolean;
readonly cellSpacing?: ITableCellSpacingProperties;
}; };
export class Table extends FileChild { export class Table extends FileChild {
@ -48,6 +50,7 @@ export class Table extends FileChild {
borders, borders,
alignment, alignment,
visuallyRightToLeft, visuallyRightToLeft,
cellSpacing,
}: ITableOptions) { }: ITableOptions) {
super("w:tbl"); super("w:tbl");
@ -62,6 +65,7 @@ export class Table extends FileChild {
alignment, alignment,
cellMargin: margins, cellMargin: margins,
visuallyRightToLeft, visuallyRightToLeft,
cellSpacing,
}), }),
); );