diff --git a/ts/docx/table/index.ts b/ts/docx/table/index.ts index 82fb8e5f29..995fd2224b 100644 --- a/ts/docx/table/index.ts +++ b/ts/docx/table/index.ts @@ -55,6 +55,11 @@ export class Table extends XmlComponent { this.properties.setWidth(type, width); return this; } + + public fixedWidthLayout(): Table { + this.properties.fixedWidthLayout(); + return this; + } } class TableRow extends XmlComponent { diff --git a/ts/docx/table/properties.ts b/ts/docx/table/properties.ts index 5a9c8d4e26..ab487e58fb 100644 --- a/ts/docx/table/properties.ts +++ b/ts/docx/table/properties.ts @@ -11,6 +11,11 @@ export class TableProperties extends XmlComponent { this.root.push(new PreferredTableWidth(type, w)); return this; } + + public fixedWidthLayout(): TableProperties { + this.root.push(new TableLayout("fixed")); + return this; + } } interface ITableWidth { @@ -28,3 +33,16 @@ class PreferredTableWidth extends XmlComponent { this.root.push(new TableWidthAttributes({type, w})); } } + +type tableLayout = "autofit" | "fixed"; + +class TableLayoutAttributes extends XmlAttributeComponent<{type: tableLayout}> { + protected xmlKeys = {type: "w:type"}; +} + +class TableLayout extends XmlComponent { + constructor(type: tableLayout) { + super("w:tblLayout"); + this.root.push(new TableLayoutAttributes({type})); + } +} diff --git a/ts/tests/docx/table/testProperties.ts b/ts/tests/docx/table/testProperties.ts index 50da89489e..427419cbd8 100644 --- a/ts/tests/docx/table/testProperties.ts +++ b/ts/tests/docx/table/testProperties.ts @@ -22,4 +22,16 @@ describe("TableProperties", () => { }); }); }); + + describe("#fixedWidthLayout", () => { + it("sets the table to fixed width layout", () => { + const tp = new TableProperties().fixedWidthLayout(); + const tree = new Formatter().format(tp); + expect(tree).to.deep.equal({ + "w:tblPr": [ + {"w:tblLayout": [{_attr: {"w:type": "fixed"}}]}, + ], + }); + }); + }); }); diff --git a/ts/tests/docx/table/testTable.ts b/ts/tests/docx/table/testTable.ts index fd4bff95f6..aa108cf396 100644 --- a/ts/tests/docx/table/testTable.ts +++ b/ts/tests/docx/table/testTable.ts @@ -93,4 +93,17 @@ describe("Table", () => { }); }); }); + + describe("#fixedWidthLayout", () => { + it("sets the table to fixed width layout", () => { + const table = new Table(2, 2).fixedWidthLayout(); + const tree = new Formatter().format(table); + expect(tree).to.have.property("w:tbl").which.is.an("array").with.has.length.at.least(1); + expect(tree["w:tbl"][0]).to.deep.equal({ + "w:tblPr": [ + {"w:tblLayout": [{_attr: {"w:type": "fixed"}}]}, + ], + }); + }); + }); });