Work on new table API

This commit is contained in:
Dolan
2019-03-13 02:29:11 +00:00
parent f3ba11b21c
commit 2cb7d44a77
10 changed files with 91 additions and 30 deletions

View File

@ -59,7 +59,10 @@ describe("Document", () => {
describe("#createTable", () => { describe("#createTable", () => {
it("should create a new table and append it to body", () => { it("should create a new table and append it to body", () => {
const table = document.createTable(2, 3); const table = document.createTable({
rows: 2,
columns: 3,
});
expect(table).to.be.an.instanceof(Table); expect(table).to.be.an.instanceof(Table);
const body = new Formatter().format(document)["w:document"][1]["w:body"]; const body = new Formatter().format(document)["w:document"][1]["w:body"];
expect(body) expect(body)
@ -69,7 +72,10 @@ describe("Document", () => {
}); });
it("should create a table with the correct dimensions", () => { it("should create a table with the correct dimensions", () => {
document.createTable(2, 3); document.createTable({
rows: 2,
columns: 3,
});
const body = new Formatter().format(document)["w:document"][1]["w:body"]; const body = new Formatter().format(document)["w:document"][1]["w:body"];
expect(body) expect(body)
.to.be.an("array") .to.be.an("array")

View File

@ -1,7 +1,7 @@
// http://officeopenxml.com/WPdocument.php // http://officeopenxml.com/WPdocument.php
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { Paragraph } from "../paragraph"; import { Paragraph } from "../paragraph";
import { Table } from "../table"; import { ITableOptions, Table } from "../table";
import { TableOfContents } from "../table-of-contents"; import { TableOfContents } from "../table-of-contents";
import { Body } from "./body"; import { Body } from "./body";
import { SectionPropertiesOptions } from "./body/section-properties"; import { SectionPropertiesOptions } from "./body/section-properties";
@ -58,8 +58,8 @@ export class Document extends XmlComponent {
return this; return this;
} }
public createTable(rows: number, cols: number): Table { public createTable(options: ITableOptions): Table {
const table = new Table(rows, cols); const table = new Table(options);
this.addTable(table); this.addTable(table);
return table; return table;
} }

View File

@ -93,7 +93,12 @@ describe("File", () => {
it("should call the underlying document's addTable", () => { it("should call the underlying document's addTable", () => {
const wrapper = new File(); const wrapper = new File();
const spy = sinon.spy(wrapper.Document, "addTable"); const spy = sinon.spy(wrapper.Document, "addTable");
wrapper.addTable(new Table(1, 1)); wrapper.addTable(
new Table({
rows: 1,
columns: 1,
}),
);
expect(spy.called).to.equal(true); expect(spy.called).to.equal(true);
}); });
@ -103,7 +108,10 @@ describe("File", () => {
it("should call the underlying document's createTable", () => { it("should call the underlying document's createTable", () => {
const wrapper = new File(); const wrapper = new File();
const spy = sinon.spy(wrapper.Document, "createTable"); const spy = sinon.spy(wrapper.Document, "createTable");
wrapper.createTable(1, 1); wrapper.createTable({
rows: 1,
columns: 1,
});
expect(spy.called).to.equal(true); expect(spy.called).to.equal(true);
}); });

View File

@ -24,7 +24,7 @@ import { Settings } from "./settings";
import { Styles } from "./styles"; import { Styles } from "./styles";
import { ExternalStylesFactory } from "./styles/external-styles-factory"; import { ExternalStylesFactory } from "./styles/external-styles-factory";
import { DefaultStylesFactory } from "./styles/factory"; import { DefaultStylesFactory } from "./styles/factory";
import { Table } from "./table"; import { ITableOptions, Table } from "./table";
import { TableOfContents } from "./table-of-contents"; import { TableOfContents } from "./table-of-contents";
export class File { export class File {
@ -131,8 +131,8 @@ export class File {
return this; return this;
} }
public createTable(rows: number, cols: number): Table { public createTable(options: ITableOptions): Table {
return this.document.createTable(rows, cols); return this.document.createTable(options);
} }
public addImage(image: Image): File { public addImage(image: Image): File {

View File

@ -21,7 +21,12 @@ describe("FooterWrapper", () => {
it("should call the underlying footer's addParagraph", () => { it("should call the underlying footer's addParagraph", () => {
const file = new FooterWrapper(new Media(), 1); const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.Footer, "addTable"); const spy = sinon.spy(file.Footer, "addTable");
file.addTable(new Table(1, 1)); file.addTable(
new Table({
rows: 1,
columns: 1,
}),
);
expect(spy.called).to.equal(true); expect(spy.called).to.equal(true);
}); });

View File

@ -53,7 +53,10 @@ export class Footer extends InitializableXmlComponent {
} }
public createTable(rows: number, cols: number): Table { public createTable(rows: number, cols: number): Table {
const table = new Table(rows, cols); const table = new Table({
rows: rows,
columns: cols,
});
this.addTable(table); this.addTable(table);
return table; return table;
} }

View File

@ -21,7 +21,12 @@ describe("HeaderWrapper", () => {
it("should call the underlying header's addTable", () => { it("should call the underlying header's addTable", () => {
const wrapper = new HeaderWrapper(new Media(), 1); const wrapper = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(wrapper.Header, "addTable"); const spy = sinon.spy(wrapper.Header, "addTable");
wrapper.addTable(new Table(1, 1)); wrapper.addTable(
new Table({
rows: 1,
columns: 1,
}),
);
expect(spy.called).to.equal(true); expect(spy.called).to.equal(true);
}); });

View File

@ -64,7 +64,10 @@ export class Header extends InitializableXmlComponent {
} }
public createTable(rows: number, cols: number): Table { public createTable(rows: number, cols: number): Table {
const table = new Table(rows, cols); const table = new Table({
rows: rows,
columns: cols,
});
this.addTable(table); this.addTable(table);
return table; return table;
} }

View File

@ -13,8 +13,8 @@ export class TableRow extends XmlComponent {
cells.forEach((c) => this.root.push(c)); cells.forEach((c) => this.root.push(c));
} }
public getCell(ix: number): TableCell { public getCell(index: number): TableCell {
const cell = this.cells[ix]; const cell = this.cells[index];
if (!cell) { if (!cell) {
throw Error("Index out of bounds when trying to get cell on row"); throw Error("Index out of bounds when trying to get cell on row");

View File

@ -21,31 +21,62 @@ export interface IWidthOptions {
readonly type?: WidthType; readonly type?: WidthType;
} }
export interface ITableOptions {
readonly rows: number;
readonly columns: number;
readonly width?: number;
readonly widthUnitType?: WidthType;
readonly columnWidths?: number[];
readonly margains?: {
readonly margainUnitType?: WidthType;
readonly top?: number;
readonly bottom?: number;
readonly right?: number;
readonly left?: number;
};
readonly float?: ITableFloatOptions;
}
export class Table extends XmlComponent { export class Table extends XmlComponent {
private readonly properties: TableProperties; private readonly properties: TableProperties;
private readonly rows: TableRow[]; private readonly rows: TableRow[];
constructor( constructor({
rowCount: number, rows,
columnCount: number, columns,
widthOptions: IWidthOptions = { width: 100, type: WidthType.AUTO }, width = 100,
colSizes: number[] = Array<number>(columnCount).fill(100), widthUnitType = WidthType.AUTO,
) { columnWidths = Array<number>(columns).fill(100),
margains: { margainUnitType, top, bottom, right, left } = { margainUnitType: WidthType.AUTO, top: 0, bottom: 0, right: 0, left: 0 },
float,
}: ITableOptions) {
super("w:tbl"); super("w:tbl");
this.properties = new TableProperties(); this.properties = new TableProperties();
this.root.push(this.properties); this.root.push(this.properties);
this.properties.setBorder(); this.properties.setBorder();
this.properties.setWidth(widthOptions.width, widthOptions.type); this.properties.setWidth(width, widthUnitType);
const grid = new TableGrid(colSizes); 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);
const grid = new TableGrid(columnWidths);
this.root.push(grid); this.root.push(grid);
this.rows = []; this.rows = Array(rows)
for (let i = 0; i < rowCount; i++) { .fill(0)
const cells = Array<TableCell>(columnCount).fill(new TableCell()); .map(() => {
const row = new TableRow(cells); const cells = Array(columns)
this.rows.push(row); .fill(0)
this.root.push(row); .map(() => new TableCell());
const row = new TableRow(cells);
return row;
});
this.rows.forEach((x) => this.root.push(x));
if (float) {
this.properties.setTableFloatProperties(float);
} }
} }