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
// Import from 'docx' rather than '../build' if you install from npm
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();
let table = doc.createTable(2, 2)
.setTableFloatProperties(new TableFloatProperties({
const table = doc.createTable(2, 2).float({
horizontalAnchor: TableAnchorType.MARGIN,
verticalAnchor: TableAnchorType.MARGIN,
relativeHorizontalPosition: RelativeHorizontalPosition.RIGHT,
relativeVerticalPosition: RelativeVerticalPosition.BOTTOM
}));
relativeVerticalPosition: RelativeVerticalPosition.BOTTOM,
});
table.setFixedWidthLayout();
table.setWidth(WidthType.DXA, 4535);
table.getCell(0, 0).addContent(new Paragraph("Hello"));
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();
packer.toBuffer(doc).then((buffer) => {

View File

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

View File

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