Improve API

This commit is contained in:
Dolan
2018-10-23 20:03:29 +01:00
parent 54697ab6b1
commit 4633592711
3 changed files with 32 additions and 45 deletions

View File

@ -1,41 +1,30 @@
// Example of how you would create a table with float positions // Example of how you would create a table with float positions
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { Document, Packer, Paragraph, TableFloatProperties, TableAnchorType, RelativeHorizontalPosition, RelativeVerticalPosition, WidthType } from "../build"; import {
Document,
Packer,
Paragraph,
RelativeHorizontalPosition,
RelativeVerticalPosition,
TableAnchorType,
WidthType,
} from "../build";
const doc = new Document(); const doc = new Document();
let table = doc.createTable(2, 2) const table = doc.createTable(2, 2).float({
.setTableFloatProperties(new TableFloatProperties({
horizontalAnchor: TableAnchorType.MARGIN, horizontalAnchor: TableAnchorType.MARGIN,
verticalAnchor: TableAnchorType.MARGIN, verticalAnchor: TableAnchorType.MARGIN,
relativeHorizontalPosition: RelativeHorizontalPosition.RIGHT, relativeHorizontalPosition: RelativeHorizontalPosition.RIGHT,
relativeVerticalPosition: RelativeVerticalPosition.BOTTOM relativeVerticalPosition: RelativeVerticalPosition.BOTTOM,
})); });
table.setFixedWidthLayout(); table.setFixedWidthLayout();
table.setWidth(WidthType.DXA, 4535); table.setWidth(WidthType.DXA, 4535);
table.getCell(0, 0).addContent(new Paragraph("Hello")); table.getCell(0, 0).addContent(new Paragraph("Hello"));
table.getRow(0).mergeCells(0, 1); table.getRow(0).mergeCells(0, 1);
doc.createParagraph("Another table").heading2();
table = doc.createTable(2, 3);
table.getCell(0, 0).addContent(new Paragraph("World"));
table.getRow(0).mergeCells(0, 2);
doc.createParagraph("Another table").heading2();
table = doc.createTable(2, 4);
table.getCell(0, 0).addContent(new Paragraph("Foo"));
table.getCell(1, 0).addContent(new Paragraph("Bar1"));
table.getCell(1, 1).addContent(new Paragraph("Bar2"));
table.getCell(1, 2).addContent(new Paragraph("Bar3"));
table.getCell(1, 3).addContent(new Paragraph("Bar4"));
table.getRow(0).mergeCells(0, 3);
const packer = new Packer(); const packer = new Packer();
packer.toBuffer(doc).then((buffer) => { packer.toBuffer(doc).then((buffer) => {

View File

@ -5,7 +5,7 @@ import { Formatter } from "../../export/formatter";
import { Paragraph } from "../paragraph"; import { Paragraph } from "../paragraph";
import { Table } from "./"; import { Table } from "./";
import { WidthType } from "./table-cell"; import { WidthType } from "./table-cell";
import { RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType, TableFloatProperties } from "./table-float-properties"; import { RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType } from "./table-float-properties";
const DEFAULT_TABLE_PROPERTIES = { const DEFAULT_TABLE_PROPERTIES = {
"w:tblBorders": [ "w:tblBorders": [
@ -296,22 +296,20 @@ describe("Table", () => {
}); });
}); });
describe("#setTableFloatProperties", () => { describe("#float", () => {
it("sets the table float properties", () => { it("sets the table float properties", () => {
const table = new Table(1, 1).setTableFloatProperties( const table = new Table(1, 1).float({
new TableFloatProperties({ horizontalAnchor: TableAnchorType.MARGIN,
horizontalAnchor: TableAnchorType.MARGIN, verticalAnchor: TableAnchorType.PAGE,
verticalAnchor: TableAnchorType.PAGE, absoluteHorizontalPosition: 10,
absoluteHorizontalPosition: 10, relativeHorizontalPosition: RelativeHorizontalPosition.CENTER,
relativeHorizontalPosition: RelativeHorizontalPosition.CENTER, absoluteVerticalPosition: 20,
absoluteVerticalPosition: 20, relativeVerticalPosition: RelativeVerticalPosition.BOTTOM,
relativeVerticalPosition: RelativeVerticalPosition.BOTTOM, bottomFromText: 30,
bottomFromText: 30, topFromText: 40,
topFromText: 40, leftFromText: 50,
leftFromText: 50, rightFromText: 60,
rightFromText: 60, });
}),
);
const tree = new Formatter().format(table); const tree = new Formatter().format(table);
expect(tree) expect(tree)
.to.have.property("w:tbl") .to.have.property("w:tbl")

View File

@ -14,7 +14,7 @@ import { IXmlableObject, XmlComponent } from "file/xml-components";
import { Paragraph } from "../paragraph"; import { Paragraph } from "../paragraph";
import { TableGrid } from "./grid"; import { TableGrid } from "./grid";
import { TableProperties } from "./properties"; import { TableProperties } from "./properties";
import { TableFloatProperties } from "./table-float-properties"; import { ITableFloatOptions, TableFloatProperties } from "./table-float-properties";
export class Table extends XmlComponent { export class Table extends XmlComponent {
private readonly properties: TableProperties; private readonly properties: TableProperties;
@ -85,13 +85,13 @@ export class Table extends XmlComponent {
return this; return this;
} }
public get Properties(): TableProperties { public float(tableFloatProperties: ITableFloatOptions): Table {
return this.properties; this.properties.setTableFloatProperties(new TableFloatProperties(tableFloatProperties));
return this;
} }
public setTableFloatProperties(tableFloatProperties: TableFloatProperties): Table { public get Properties(): TableProperties {
this.properties.setTableFloatProperties(tableFloatProperties); return this.properties;
return this;
} }
} }