Make fixed layout declaritive

This commit is contained in:
Dolan
2019-06-25 20:57:46 +01:00
parent b566b0f765
commit 3ef8f5311d
7 changed files with 34 additions and 31 deletions

View File

@ -9,6 +9,7 @@ import {
RelativeVerticalPosition, RelativeVerticalPosition,
Table, Table,
TableAnchorType, TableAnchorType,
TableLayoutType,
WidthType, WidthType,
} from "../build"; } from "../build";
@ -25,10 +26,10 @@ const table = new Table({
}, },
width: 4535, width: 4535,
widthUnitType: WidthType.DXA, widthUnitType: WidthType.DXA,
layout: TableLayoutType.FIXED,
}); });
doc.addTable(table); doc.add(table);
table.setFixedWidthLayout();
table.getCell(0, 0).addParagraph(new Paragraph("Hello")); table.getCell(0, 0).addParagraph(new Paragraph("Hello"));
table.getRow(0).mergeCells(0, 1); table.getRow(0).mergeCells(0, 1);

View File

@ -27,8 +27,6 @@ class BorderProperty extends XmlComponent {
sz: options.size, sz: options.size,
}); });
this.root.push(attrs); this.root.push(attrs);
return this;
} }
} }

View File

@ -1,2 +1,3 @@
export * from "./table-properties"; export * from "./table-properties";
export * from "./table-float-properties"; export * from "./table-float-properties";
export * from "./table-layout";

View File

@ -3,6 +3,7 @@ import { expect } from "chai";
import { Formatter } from "export/formatter"; import { Formatter } from "export/formatter";
import { WidthType } from "../table-cell"; import { WidthType } from "../table-cell";
import { TableLayoutType } from "./table-layout";
import { TableProperties } from "./table-properties"; import { TableProperties } from "./table-properties";
describe("TableProperties", () => { describe("TableProperties", () => {
@ -35,9 +36,10 @@ describe("TableProperties", () => {
}); });
}); });
describe("#setFixedWidthLayout", () => { describe("#setLayout", () => {
it("sets the table to fixed width layout", () => { it("sets the table to fixed width layout", () => {
const tp = new TableProperties().setFixedWidthLayout(); const tp = new TableProperties();
tp.setLayout(TableLayoutType.FIXED);
const tree = new Formatter().format(tp); const tree = new Formatter().format(tp);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:tblPr": [{ "w:tblLayout": { _attr: { "w:type": "fixed" } } }], "w:tblPr": [{ "w:tblLayout": { _attr: { "w:type": "fixed" } } }],

View File

@ -23,9 +23,8 @@ export class TableProperties extends IgnoreIfEmptyXmlComponent {
return this; return this;
} }
public setFixedWidthLayout(): TableProperties { public setLayout(type: TableLayoutType): void {
this.root.push(new TableLayout(TableLayoutType.FIXED)); this.root.push(new TableLayout(type));
return this;
} }
public setBorder(): TableProperties { public setBorder(): TableProperties {

View File

@ -9,6 +9,7 @@ import { Table } from "./table";
import { RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType } from "./table-properties"; import { RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType } from "./table-properties";
import { EMPTY_OBJECT } from "file/xml-components"; import { EMPTY_OBJECT } from "file/xml-components";
import { TableLayoutType } from "./table-properties/table-layout";
const DEFAULT_TABLE_PROPERTIES = { const DEFAULT_TABLE_PROPERTIES = {
"w:tblCellMar": [ "w:tblCellMar": [
@ -134,6 +135,22 @@ describe("Table", () => {
], ],
}); });
}); });
it("sets the table to fixed width layout", () => {
const table = new Table({
rows: 1,
columns: 1,
layout: TableLayoutType.FIXED,
});
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": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS, { "w:tblLayout": { _attr: { "w:type": "fixed" } } }],
});
});
}); });
describe("#getRow and Row#getCell", () => { describe("#getRow and Row#getCell", () => {
@ -252,23 +269,6 @@ describe("Table", () => {
// }); // });
// }); // });
describe("#setFixedWidthLayout", () => {
it("sets the table to fixed width layout", () => {
const table = new Table({
rows: 1,
columns: 1,
}).setFixedWidthLayout();
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": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS, { "w:tblLayout": { _attr: { "w:type": "fixed" } } }],
});
});
});
describe("Cell", () => { describe("Cell", () => {
describe("#prepForXml", () => { describe("#prepForXml", () => {
it("inserts a paragraph at the end of the cell if it is empty", () => { it("inserts a paragraph at the end of the cell if it is empty", () => {

View File

@ -5,6 +5,7 @@ import { TableGrid } from "./grid";
import { TableCell, WidthType } from "./table-cell"; import { TableCell, WidthType } from "./table-cell";
import { TableColumn } from "./table-column"; import { TableColumn } from "./table-column";
import { ITableFloatOptions, TableProperties } from "./table-properties"; import { ITableFloatOptions, TableProperties } from "./table-properties";
import { TableLayoutType } from "./table-properties/table-layout";
import { TableRow } from "./table-row"; import { TableRow } from "./table-row";
/* /*
0-width columns don't get rendered correctly, so we need 0-width columns don't get rendered correctly, so we need
@ -30,6 +31,7 @@ export interface ITableOptions {
readonly left?: number; readonly left?: number;
}; };
readonly float?: ITableFloatOptions; readonly float?: ITableFloatOptions;
readonly layout?: TableLayoutType;
} }
export class Table extends XmlComponent { export class Table extends XmlComponent {
@ -44,6 +46,7 @@ export class Table extends XmlComponent {
columnWidths = Array<number>(columns).fill(100), columnWidths = Array<number>(columns).fill(100),
margins: { marginUnitType, top, bottom, right, left } = { marginUnitType: 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, float,
layout,
}: ITableOptions) { }: ITableOptions) {
super("w:tbl"); super("w:tbl");
this.properties = new TableProperties(); this.properties = new TableProperties();
@ -73,6 +76,10 @@ export class Table extends XmlComponent {
if (float) { if (float) {
this.properties.setTableFloatProperties(float); this.properties.setTableFloatProperties(float);
} }
if (layout) {
this.properties.setLayout(layout);
}
} }
public getRow(index: number): TableRow { public getRow(index: number): TableRow {
@ -94,9 +101,4 @@ export class Table extends XmlComponent {
public getCell(row: number, col: number): TableCell { public getCell(row: number, col: number): TableCell {
return this.getRow(row).getCell(col); return this.getRow(row).getCell(col);
} }
public setFixedWidthLayout(): Table {
this.properties.setFixedWidthLayout();
return this;
}
} }