Merge pull request #206 from dolanmiu/feat/refactor-setwidth
Breaking Change. Swap arguments around and make default WidthType
This commit is contained in:
@ -20,7 +20,7 @@ const table = doc.createTable(2, 2).float({
|
||||
relativeVerticalPosition: RelativeVerticalPosition.BOTTOM,
|
||||
});
|
||||
table.setFixedWidthLayout();
|
||||
table.setWidth(WidthType.DXA, 4535);
|
||||
table.setWidth(4535, WidthType.DXA);
|
||||
|
||||
table.getCell(0, 0).addContent(new Paragraph("Hello"));
|
||||
table.getRow(0).mergeCells(0, 1);
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
import { BorderStyle } from "file/styles";
|
||||
|
||||
import { VerticalAlign, VMergeType, WidthType } from "./table-cell-components";
|
||||
import { TableCellProperties } from "./table-cell-properties";
|
||||
@ -42,12 +43,19 @@ describe("TableCellProperties", () => {
|
||||
});
|
||||
|
||||
describe("#setWidth", () => {
|
||||
it("sets width", () => {
|
||||
it("should set width", () => {
|
||||
const cellMargain = new TableCellProperties();
|
||||
cellMargain.setWidth(1, WidthType.DXA);
|
||||
const tree = new Formatter().format(cellMargain);
|
||||
expect(tree).to.deep.equal({ "w:tcPr": [{ "w:tcW": [{ _attr: { "w:type": "dxa", "w:w": 1 } }] }] });
|
||||
});
|
||||
|
||||
it("should set width using default of AUTO", () => {
|
||||
const cellMargain = new TableCellProperties();
|
||||
cellMargain.setWidth(1);
|
||||
const tree = new Formatter().format(cellMargain);
|
||||
expect(tree).to.deep.equal({ "w:tcPr": [{ "w:tcW": [{ _attr: { "w:type": "auto", "w:w": 1 } }] }] });
|
||||
});
|
||||
});
|
||||
|
||||
describe("#setShading", () => {
|
||||
@ -61,4 +69,18 @@ describe("TableCellProperties", () => {
|
||||
expect(tree).to.deep.equal({ "w:tcPr": [{ "w:shd": [{ _attr: { "w:fill": "test", "w:color": "000" } }] }] });
|
||||
});
|
||||
});
|
||||
|
||||
describe("#Borders", () => {
|
||||
it("should return the TableCellBorders if Border has borders", () => {
|
||||
const cellMargain = new TableCellProperties();
|
||||
cellMargain.Borders.addTopBorder(BorderStyle.DASH_DOT_STROKED, 3, "red");
|
||||
const borders = cellMargain.Borders;
|
||||
|
||||
const tree = new Formatter().format(borders);
|
||||
|
||||
expect(tree).to.deep.equal({
|
||||
"w:tcBorders": [{ "w:top": [{ _attr: { "w:val": "dashDotStroked", "w:sz": 3, "w:color": "red" } }] }],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -44,7 +44,7 @@ export class TableCellProperties extends XmlComponent {
|
||||
return this;
|
||||
}
|
||||
|
||||
public setWidth(width: string | number, type: WidthType): TableCellProperties {
|
||||
public setWidth(width: string | number, type: WidthType = WidthType.AUTO): TableCellProperties {
|
||||
this.root.push(new TableCellWidth(width, type));
|
||||
|
||||
return this;
|
||||
|
@ -15,13 +15,21 @@ describe("TableProperties", () => {
|
||||
});
|
||||
|
||||
describe("#setWidth", () => {
|
||||
it("adds a table width property", () => {
|
||||
const tp = new TableProperties().setWidth(WidthType.DXA, 1234);
|
||||
it("should add a table width property", () => {
|
||||
const tp = new TableProperties().setWidth(1234, WidthType.DXA);
|
||||
const tree = new Formatter().format(tp);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:tblPr": [{ "w:tblW": [{ _attr: { "w:type": "dxa", "w:w": 1234 } }] }],
|
||||
});
|
||||
});
|
||||
|
||||
it("should add a table width property with default of AUTO", () => {
|
||||
const tp = new TableProperties().setWidth(1234);
|
||||
const tree = new Formatter().format(tp);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:tblPr": [{ "w:tblW": [{ _attr: { "w:type": "auto", "w:w": 1234 } }] }],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("#setFixedWidthLayout", () => {
|
||||
|
@ -17,8 +17,8 @@ export class TableProperties extends XmlComponent {
|
||||
this.root.push(this.cellMargin);
|
||||
}
|
||||
|
||||
public setWidth(type: WidthType, w: number | string): TableProperties {
|
||||
this.root.push(new PreferredTableWidth(type, w));
|
||||
public setWidth(width: number | string, type: WidthType = WidthType.AUTO): TableProperties {
|
||||
this.root.push(new PreferredTableWidth(type, width));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -176,8 +176,8 @@ describe("Table", () => {
|
||||
});
|
||||
|
||||
describe("#setWidth", () => {
|
||||
it("sets the preferred width on the table", () => {
|
||||
const table = new Table(2, 2).setWidth(WidthType.PERCENTAGE, 1000);
|
||||
it("should set the preferred width on the table", () => {
|
||||
const table = new Table(2, 2).setWidth(1000, WidthType.PERCENTAGE);
|
||||
const tree = new Formatter().format(table);
|
||||
expect(tree)
|
||||
.to.have.property("w:tbl")
|
||||
@ -187,6 +187,15 @@ describe("Table", () => {
|
||||
"w:tblPr": [DEFAULT_TABLE_PROPERTIES, { "w:tblW": [{ _attr: { "w:type": "pct", "w:w": 1000 } }] }],
|
||||
});
|
||||
});
|
||||
|
||||
it("sets the preferred width on the table with a default of AUTO", () => {
|
||||
const table = new Table(2, 2).setWidth(1000);
|
||||
const tree = new Formatter().format(table);
|
||||
|
||||
expect(tree["w:tbl"][0]).to.deep.equal({
|
||||
"w:tblPr": [DEFAULT_TABLE_PROPERTIES, { "w:tblW": [{ _attr: { "w:type": "auto", "w:w": 1000 } }] }],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("#setFixedWidthLayout", () => {
|
||||
|
@ -65,8 +65,8 @@ export class Table extends XmlComponent {
|
||||
return this.getRow(row).getCell(col);
|
||||
}
|
||||
|
||||
public setWidth(type: WidthType, width: number | string): Table {
|
||||
this.properties.setWidth(type, width);
|
||||
public setWidth(width: number | string, type: WidthType = WidthType.AUTO): Table {
|
||||
this.properties.setWidth(width, type);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user