diff --git a/demo/demo32.ts b/demo/demo32.ts index 402297a12a..7196058ec0 100644 --- a/demo/demo32.ts +++ b/demo/demo32.ts @@ -25,7 +25,7 @@ table = doc.createTable({ table .getCell(0, 0) .addParagraph(new Paragraph("World")) - .setMargains({ + .setMargins({ top: 1000, bottom: 1000, left: 1000, @@ -40,7 +40,7 @@ table = doc.createTable({ columns: 4, width: 7000, widthUnitType: WidthType.DXA, - margains: { + margins: { top: 400, bottom: 400, right: 400, diff --git a/docs/examples.md b/docs/examples.md index 89e5d4afda..f638e1aa25 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -112,7 +112,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo18.ts_ ## Margins -Example showing how to set custom margains +Example showing how to set custom margins [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo6.ts ':include') diff --git a/src/file/table/table-cell/cell-margain/table-cell-margains.ts b/src/file/table/table-cell/cell-margain/table-cell-margains.ts deleted file mode 100644 index 634b38af5d..0000000000 --- a/src/file/table/table-cell/cell-margain/table-cell-margains.ts +++ /dev/null @@ -1,21 +0,0 @@ -// http://officeopenxml.com/WPtableCellProperties-Margins.php -import { XmlComponent } from "file/xml-components"; - -import { BottomCellMargain, LeftCellMargain, RightCellMargain, TopCellMargain } from "./cell-margain"; - -export interface ITableCellMargainOptions { - readonly top?: number; - readonly left?: number; - readonly bottom?: number; - readonly right?: number; -} - -export class TableCellMargain extends XmlComponent { - constructor({ top = 0, left = 0, right = 0, bottom = 0 }: ITableCellMargainOptions) { - super("w:tcMar"); - this.root.push(new TopCellMargain(top)); - this.root.push(new BottomCellMargain(bottom)); - this.root.push(new RightCellMargain(right)); - this.root.push(new LeftCellMargain(left)); - } -} diff --git a/src/file/table/table-cell/cell-margain/cell-margain.spec.ts b/src/file/table/table-cell/cell-margin/cell-margin.spec.ts similarity index 65% rename from src/file/table/table-cell/cell-margain/cell-margain.spec.ts rename to src/file/table/table-cell/cell-margin/cell-margin.spec.ts index 393a8d0087..399020a2d2 100644 --- a/src/file/table/table-cell/cell-margain/cell-margain.spec.ts +++ b/src/file/table/table-cell/cell-margin/cell-margin.spec.ts @@ -2,13 +2,13 @@ import { expect } from "chai"; import { Formatter } from "export/formatter"; -import { BottomCellMargain, LeftCellMargain, RightCellMargain, TopCellMargain } from "./cell-margain"; +import { BottomCellMargin, LeftCellMargin, RightCellMargin, TopCellMargin } from "./cell-margin"; -describe("TopCellMargain", () => { +describe("TopCellMargin", () => { describe("#constructor", () => { it("should create", () => { - const cellMargain = new TopCellMargain(1); - const tree = new Formatter().format(cellMargain); + const cellMargin = new TopCellMargin(1); + const tree = new Formatter().format(cellMargin); expect(tree).to.deep.equal({ "w:top": { _attr: { @@ -21,11 +21,11 @@ describe("TopCellMargain", () => { }); }); -describe("BottomCellMargain", () => { +describe("BottomCellMargin", () => { describe("#constructor", () => { it("should create", () => { - const cellMargain = new BottomCellMargain(1); - const tree = new Formatter().format(cellMargain); + const cellMargin = new BottomCellMargin(1); + const tree = new Formatter().format(cellMargin); expect(tree).to.deep.equal({ "w:bottom": { _attr: { @@ -38,11 +38,11 @@ describe("BottomCellMargain", () => { }); }); -describe("LeftCellMargain", () => { +describe("LeftCellMargin", () => { describe("#constructor", () => { it("should create", () => { - const cellMargain = new LeftCellMargain(1); - const tree = new Formatter().format(cellMargain); + const cellMargin = new LeftCellMargin(1); + const tree = new Formatter().format(cellMargin); expect(tree).to.deep.equal({ "w:start": { _attr: { @@ -55,11 +55,11 @@ describe("LeftCellMargain", () => { }); }); -describe("RightCellMargain", () => { +describe("RightCellMargin", () => { describe("#constructor", () => { it("should create", () => { - const cellMargain = new RightCellMargain(1); - const tree = new Formatter().format(cellMargain); + const cellMargin = new RightCellMargin(1); + const tree = new Formatter().format(cellMargin); expect(tree).to.deep.equal({ "w:end": { _attr: { diff --git a/src/file/table/table-cell/cell-margain/cell-margain.ts b/src/file/table/table-cell/cell-margin/cell-margin.ts similarity index 66% rename from src/file/table/table-cell/cell-margain/cell-margain.ts rename to src/file/table/table-cell/cell-margin/cell-margin.ts index ccfd072f2d..416d4e3ad2 100644 --- a/src/file/table/table-cell/cell-margain/cell-margain.ts +++ b/src/file/table/table-cell/cell-margin/cell-margin.ts @@ -1,21 +1,21 @@ // http://officeopenxml.com/WPtableCellProperties-Margins.php import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; -export interface ICellMargainProperties { +export interface ICellMarginProperties { readonly type: string; readonly width: number; } -class CellMargainAttributes extends XmlAttributeComponent { +class CellMarginAttributes extends XmlAttributeComponent { protected readonly xmlKeys = { width: "w:w", type: "w:type" }; } -export class TopCellMargain extends XmlComponent { +export class TopCellMargin extends XmlComponent { constructor(value: number) { super("w:top"); this.root.push( - new CellMargainAttributes({ + new CellMarginAttributes({ width: value, type: "dxa", }), @@ -23,12 +23,12 @@ export class TopCellMargain extends XmlComponent { } } -export class BottomCellMargain extends XmlComponent { +export class BottomCellMargin extends XmlComponent { constructor(value: number) { super("w:bottom"); this.root.push( - new CellMargainAttributes({ + new CellMarginAttributes({ width: value, type: "dxa", }), @@ -36,12 +36,12 @@ export class BottomCellMargain extends XmlComponent { } } -export class LeftCellMargain extends XmlComponent { +export class LeftCellMargin extends XmlComponent { constructor(value: number) { super("w:start"); this.root.push( - new CellMargainAttributes({ + new CellMarginAttributes({ width: value, type: "dxa", }), @@ -49,12 +49,12 @@ export class LeftCellMargain extends XmlComponent { } } -export class RightCellMargain extends XmlComponent { +export class RightCellMargin extends XmlComponent { constructor(value: number) { super("w:end"); this.root.push( - new CellMargainAttributes({ + new CellMarginAttributes({ width: value, type: "dxa", }), diff --git a/src/file/table/table-cell/cell-margain/table-cell-margains.spec.ts b/src/file/table/table-cell/cell-margin/table-cell-margins.spec.ts similarity index 89% rename from src/file/table/table-cell/cell-margain/table-cell-margains.spec.ts rename to src/file/table/table-cell/cell-margin/table-cell-margins.spec.ts index 9cdd752fad..dc4a0a9ab9 100644 --- a/src/file/table/table-cell/cell-margain/table-cell-margains.spec.ts +++ b/src/file/table/table-cell/cell-margin/table-cell-margins.spec.ts @@ -2,13 +2,13 @@ import { expect } from "chai"; import { Formatter } from "export/formatter"; -import { TableCellMargain } from "./table-cell-margains"; +import { TableCellMargin } from "./table-cell-margins"; -describe("TableCellMargain", () => { +describe("TableCellMargin", () => { describe("#constructor", () => { it("should create with default values", () => { - const cellMargain = new TableCellMargain({}); - const tree = new Formatter().format(cellMargain); + const cellMargin = new TableCellMargin({}); + const tree = new Formatter().format(cellMargin); expect(tree).to.deep.equal({ "w:tcMar": [ { @@ -48,13 +48,13 @@ describe("TableCellMargain", () => { }); it("should create with values", () => { - const cellMargain = new TableCellMargain({ + const cellMargin = new TableCellMargin({ top: 5, bottom: 5, left: 5, right: 5, }); - const tree = new Formatter().format(cellMargain); + const tree = new Formatter().format(cellMargin); expect(tree).to.deep.equal({ "w:tcMar": [ { diff --git a/src/file/table/table-cell/cell-margin/table-cell-margins.ts b/src/file/table/table-cell/cell-margin/table-cell-margins.ts new file mode 100644 index 0000000000..b97c349c9c --- /dev/null +++ b/src/file/table/table-cell/cell-margin/table-cell-margins.ts @@ -0,0 +1,21 @@ +// http://officeopenxml.com/WPtableCellProperties-Margins.php +import { XmlComponent } from "file/xml-components"; + +import { BottomCellMargin, LeftCellMargin, RightCellMargin, TopCellMargin } from "./cell-margin"; + +export interface ITableCellMarginOptions { + readonly top?: number; + readonly left?: number; + readonly bottom?: number; + readonly right?: number; +} + +export class TableCellMargin extends XmlComponent { + constructor({ top = 0, left = 0, right = 0, bottom = 0 }: ITableCellMarginOptions) { + super("w:tcMar"); + this.root.push(new TopCellMargin(top)); + this.root.push(new BottomCellMargin(bottom)); + this.root.push(new RightCellMargin(right)); + this.root.push(new LeftCellMargin(left)); + } +} diff --git a/src/file/table/table-cell/table-cell-properties.ts b/src/file/table/table-cell/table-cell-properties.ts index 1d9ecb41c2..5a2544b9cd 100644 --- a/src/file/table/table-cell/table-cell-properties.ts +++ b/src/file/table/table-cell/table-cell-properties.ts @@ -1,7 +1,7 @@ import { IgnoreIfEmptyXmlComponent } from "file/xml-components"; import { ITableShadingAttributesProperties, TableShading } from "../shading"; -import { ITableCellMargainOptions, TableCellMargain } from "./cell-margain/table-cell-margains"; +import { ITableCellMarginOptions, TableCellMargin } from "./cell-margin/table-cell-margins"; import { GridSpan, TableCellBorders, TableCellWidth, VAlign, VerticalAlign, VMerge, VMergeType, WidthType } from "./table-cell-components"; export class TableCellProperties extends IgnoreIfEmptyXmlComponent { @@ -47,8 +47,8 @@ export class TableCellProperties extends IgnoreIfEmptyXmlComponent { return this; } - public addMargains(options: ITableCellMargainOptions): TableCellProperties { - this.root.push(new TableCellMargain(options)); + public addMargins(options: ITableCellMarginOptions): TableCellProperties { + this.root.push(new TableCellMargin(options)); return this; } diff --git a/src/file/table/table-cell/table-cell.ts b/src/file/table/table-cell/table-cell.ts index 2f4d996e8d..a3c8b427e1 100644 --- a/src/file/table/table-cell/table-cell.ts +++ b/src/file/table/table-cell/table-cell.ts @@ -4,7 +4,7 @@ import { IXmlableObject, XmlComponent } from "file/xml-components"; import { ITableShadingAttributesProperties } from "../shading"; import { Table } from "../table"; -import { ITableCellMargainOptions } from "./cell-margain/table-cell-margains"; +import { ITableCellMarginOptions } from "./cell-margin/table-cell-margins"; import { TableCellBorders, VerticalAlign, VMergeType } from "./table-cell-components"; import { TableCellProperties } from "./table-cell-properties"; @@ -61,8 +61,8 @@ export class TableCell extends XmlComponent { return this; } - public setMargains(margains: ITableCellMargainOptions): TableCell { - this.properties.addMargains(margains); + public setMargins(margins: ITableCellMarginOptions): TableCell { + this.properties.addMargins(margins); return this; } diff --git a/src/file/table/table-properties/table-cell-margin.spec.ts b/src/file/table/table-properties/table-cell-margin.spec.ts index c0935281b5..7f0d13d218 100644 --- a/src/file/table/table-properties/table-cell-margin.spec.ts +++ b/src/file/table/table-properties/table-cell-margin.spec.ts @@ -8,43 +8,43 @@ import { TableCellMargin } from "./table-cell-margin"; describe("TableCellMargin", () => { describe("#constructor", () => { it("should throw an error if theres no child elements", () => { - const cellMargain = new TableCellMargin(); - expect(() => new Formatter().format(cellMargain)).to.throw(); + const cellMargin = new TableCellMargin(); + expect(() => new Formatter().format(cellMargin)).to.throw(); }); }); describe("#addTopMargin", () => { it("adds a table cell top margin", () => { - const cellMargain = new TableCellMargin(); - cellMargain.addTopMargin(1234, WidthType.DXA); - const tree = new Formatter().format(cellMargain); + const cellMargin = new TableCellMargin(); + cellMargin.addTopMargin(1234, WidthType.DXA); + const tree = new Formatter().format(cellMargin); expect(tree).to.deep.equal({ "w:tblCellMar": [{ "w:top": { _attr: { "w:type": "dxa", "w:w": 1234 } } }] }); }); }); describe("#addLeftMargin", () => { it("adds a table cell left margin", () => { - const cellMargain = new TableCellMargin(); - cellMargain.addLeftMargin(1234, WidthType.DXA); - const tree = new Formatter().format(cellMargain); + const cellMargin = new TableCellMargin(); + cellMargin.addLeftMargin(1234, WidthType.DXA); + const tree = new Formatter().format(cellMargin); expect(tree).to.deep.equal({ "w:tblCellMar": [{ "w:left": { _attr: { "w:type": "dxa", "w:w": 1234 } } }] }); }); }); describe("#addBottomMargin", () => { it("adds a table cell bottom margin", () => { - const cellMargain = new TableCellMargin(); - cellMargain.addBottomMargin(1234, WidthType.DXA); - const tree = new Formatter().format(cellMargain); + const cellMargin = new TableCellMargin(); + cellMargin.addBottomMargin(1234, WidthType.DXA); + const tree = new Formatter().format(cellMargin); expect(tree).to.deep.equal({ "w:tblCellMar": [{ "w:bottom": { _attr: { "w:type": "dxa", "w:w": 1234 } } }] }); }); }); describe("#addRightMargin", () => { it("adds a table cell right margin", () => { - const cellMargain = new TableCellMargin(); - cellMargain.addRightMargin(1234, WidthType.DXA); - const tree = new Formatter().format(cellMargain); + const cellMargin = new TableCellMargin(); + cellMargin.addRightMargin(1234, WidthType.DXA); + const tree = new Formatter().format(cellMargin); expect(tree).to.deep.equal({ "w:tblCellMar": [{ "w:right": { _attr: { "w:type": "dxa", "w:w": 1234 } } }] }); }); }); diff --git a/src/file/table/table.ts b/src/file/table/table.ts index 06cfd7c960..9a99ce0408 100644 --- a/src/file/table/table.ts +++ b/src/file/table/table.ts @@ -22,8 +22,8 @@ export interface ITableOptions { readonly width?: number; readonly widthUnitType?: WidthType; readonly columnWidths?: number[]; - readonly margains?: { - readonly margainUnitType?: WidthType; + readonly margins?: { + readonly marginUnitType?: WidthType; readonly top?: number; readonly bottom?: number; readonly right?: number; @@ -42,7 +42,7 @@ export class Table extends XmlComponent { width = 100, widthUnitType = WidthType.AUTO, columnWidths = Array(columns).fill(100), - margains: { margainUnitType, top, bottom, right, left } = { margainUnitType: WidthType.AUTO, top: 0, bottom: 0, right: 0, left: 0 }, + margins: { marginUnitType, top, bottom, right, left } = { marginUnitType: WidthType.AUTO, top: 0, bottom: 0, right: 0, left: 0 }, float, }: ITableOptions) { super("w:tbl"); @@ -50,10 +50,10 @@ export class Table extends XmlComponent { this.root.push(this.properties); this.properties.setBorder(); this.properties.setWidth(width, widthUnitType); - this.properties.CellMargin.addBottomMargin(bottom || 0, margainUnitType); - this.properties.CellMargin.addTopMargin(top || 0, margainUnitType); - this.properties.CellMargin.addLeftMargin(left || 0, margainUnitType); - this.properties.CellMargin.addRightMargin(right || 0, margainUnitType); + this.properties.CellMargin.addBottomMargin(bottom || 0, marginUnitType); + this.properties.CellMargin.addTopMargin(top || 0, marginUnitType); + this.properties.CellMargin.addLeftMargin(left || 0, marginUnitType); + this.properties.CellMargin.addRightMargin(right || 0, marginUnitType); const grid = new TableGrid(columnWidths); this.root.push(grid);