diff --git a/src/file/table/properties.spec.ts b/src/file/table/properties.spec.ts index c10c18f4c2..a26e3fd352 100644 --- a/src/file/table/properties.spec.ts +++ b/src/file/table/properties.spec.ts @@ -2,6 +2,7 @@ import { expect } from "chai"; import { Formatter } from "../../export/formatter"; import { TableProperties } from "./properties"; +import { WidthType } from "./table-cell"; describe("TableProperties", () => { describe("#constructor", () => { @@ -14,7 +15,7 @@ describe("TableProperties", () => { describe("#setWidth", () => { it("adds a table width property", () => { - const tp = new TableProperties().setWidth("dxa", 1234); + const tp = new TableProperties().setWidth(WidthType.DXA, 1234); const tree = new Formatter().format(tp); expect(tree).to.deep.equal({ "w:tblPr": [{ "w:tblW": [{ _attr: { "w:type": "dxa", "w:w": 1234 } }] }], @@ -31,4 +32,15 @@ describe("TableProperties", () => { }); }); }); + + describe("#cellMargin", () => { + it("adds a table cell top margin", () => { + const tp = new TableProperties(); + tp.CellMargin.addTopMargin(1234, WidthType.DXA); + const tree = new Formatter().format(tp); + expect(tree).to.deep.equal({ + "w:tblPr": [{ "w:tblCellMar": [{ "w:top": [{ _attr: { "w:sz": "dxa", "w:w": 1234 } }] }] }], + }); + }); + }); }); diff --git a/src/file/table/properties.ts b/src/file/table/properties.ts index 64b0ed53ad..7c5b0bad76 100644 --- a/src/file/table/properties.ts +++ b/src/file/table/properties.ts @@ -1,13 +1,18 @@ import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; - -export type WidthTypes = "dxa" | "pct" | "nil" | "auto"; +import { WidthType } from "./table-cell"; +import { TableCellMargin } from "./table-cell-margin"; export class TableProperties extends XmlComponent { + private readonly cellMargain: TableCellMargin; + constructor() { super("w:tblPr"); + + this.cellMargain = new TableCellMargin(); + this.root.push(this.cellMargain); } - public setWidth(type: WidthTypes, w: number | string): TableProperties { + public setWidth(type: WidthType, w: number | string): TableProperties { this.root.push(new PreferredTableWidth(type, w)); return this; } @@ -21,10 +26,14 @@ export class TableProperties extends XmlComponent { this.root.push(new TableBorders()); return this; } + + public get CellMargin(): TableCellMargin { + return this.cellMargain; + } } interface ITableWidth { - type: WidthTypes; + type: WidthType; w: number | string; } @@ -33,7 +42,7 @@ class TableWidthAttributes extends XmlAttributeComponent { } class PreferredTableWidth extends XmlComponent { - constructor(type: WidthTypes, w: number | string) { + constructor(type: WidthType, w: number | string) { super("w:tblW"); this.root.push(new TableWidthAttributes({ type, w })); } diff --git a/src/file/table/table-cell-margin.ts b/src/file/table/table-cell-margin.ts new file mode 100644 index 0000000000..198dc89cbd --- /dev/null +++ b/src/file/table/table-cell-margin.ts @@ -0,0 +1,55 @@ +import { IXmlableObject, XmlAttributeComponent, XmlComponent } from "file/xml-components"; +import { WidthType } from "./table-cell"; + +class TableCellMarginAttributes extends XmlAttributeComponent<{ type: WidthType; value: number }> { + protected xmlKeys = { value: "w:w", type: "w:sz" }; +} + +class BaseTableCellMargin extends XmlComponent { + public setProperties(value: number, type: WidthType = WidthType.DXA): void { + this.root.push( + new TableCellMarginAttributes({ + type: type, + value: value, + }), + ); + } +} + +export class TableCellMargin extends XmlComponent { + constructor() { + super("w:tblCellMar"); + } + + public prepForXml(): IXmlableObject { + return this.root.length > 0 ? super.prepForXml() : ""; + } + + public addTopMargin(value: number, type: WidthType = WidthType.DXA): void { + const top = new BaseTableCellMargin("w:top"); + + top.setProperties(value, type); + this.root.push(top); + } + + public addLeftMargin(value: number, type: WidthType = WidthType.DXA): void { + const left = new BaseTableCellMargin("w:left"); + + left.setProperties(value, type); + this.root.push(left); + } + + public addBottomMargin(value: number, type: WidthType = WidthType.DXA): void { + const bottom = new BaseTableCellMargin("w:bottom"); + + bottom.setProperties(value, type); + this.root.push(bottom); + } + + public addRightMargin(value: number, type: WidthType = WidthType.DXA): void { + const right = new BaseTableCellMargin("w:right"); + + right.setProperties(value, type); + this.root.push(right); + } +}