Improve API

This commit is contained in:
Dolan
2018-10-15 21:54:33 +01:00
parent a9167b4809
commit 9f0b2f7074
2 changed files with 23 additions and 30 deletions

View File

@ -5,33 +5,29 @@ import { Document, Packer, Paragraph } from "../build";
const doc = new Document(); const doc = new Document();
doc let table = doc.createTable(2, 2);
.createTable(2, 2)
.getCell(0, 0) table.getCell(0, 0).addContent(new Paragraph("Hello"));
.addContent(new Paragraph("Hello")) table.getRow(0).mergeCells(0, 1);
.setHorizontalSpan(2);
doc.createParagraph("Another table").heading2(); doc.createParagraph("Another table").heading2();
doc table = doc.createTable(2, 3);
.createTable(2, 3) table.getCell(0, 0).addContent(new Paragraph("World"));
.getCell(0, 0) table.getRow(0).mergeCells(0, 2);
.addContent(new Paragraph("World"))
.setHorizontalSpan(3);
doc.createParagraph("Another table").heading2(); doc.createParagraph("Another table").heading2();
const table = doc.createTable(2, 4); table = doc.createTable(2, 4);
table table.getCell(0, 0).addContent(new Paragraph("Foo"));
.getCell(0, 0)
.addContent(new Paragraph("Foo"))
.setHorizontalSpan(4);
table.getCell(1, 0).addContent(new Paragraph("Bar1")); table.getCell(1, 0).addContent(new Paragraph("Bar1"));
table.getCell(1, 1).addContent(new Paragraph("Bar2")); table.getCell(1, 1).addContent(new Paragraph("Bar2"));
table.getCell(1, 2).addContent(new Paragraph("Bar3")); table.getCell(1, 2).addContent(new Paragraph("Bar3"));
table.getCell(1, 3).addContent(new Paragraph("Bar4")); 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

@ -52,7 +52,7 @@ export class Table extends XmlComponent {
for (let i = 0; i < rows; i++) { for (let i = 0; i < rows; i++) {
const cells: TableCell[] = []; const cells: TableCell[] = [];
for (let j = 0; j < cols; j++) { for (let j = 0; j < cols; j++) {
cells.push(new TableCell(this, i, j)); cells.push(new TableCell());
} }
const row = new TableRow(cells); const row = new TableRow(cells);
this.rows.push(row); this.rows.push(row);
@ -109,14 +109,20 @@ export class TableRow extends XmlComponent {
return cell; return cell;
} }
public addGridSpan(ix: number, cellSpan: number): TableCell { public addGridSpan(index: number, cellSpan: number): TableCell {
const remainCell = this.cells[ix]; const remainCell = this.cells[index];
remainCell.CellProperties.addGridSpan(cellSpan); remainCell.CellProperties.addGridSpan(cellSpan);
this.cells.splice(ix + 1, cellSpan - 1); this.cells.splice(index + 1, cellSpan - 1);
this.root.splice(ix + 2, cellSpan - 1); this.root.splice(index + 2, cellSpan - 1);
return remainCell; return remainCell;
} }
public mergeCells(startIndex: number, endIndex: number): TableCell {
const cellSpan = endIndex - startIndex + 1;
return this.addGridSpan(startIndex, cellSpan);
}
} }
export class TableRowProperties extends XmlComponent { export class TableRowProperties extends XmlComponent {
@ -128,7 +134,7 @@ export class TableRowProperties extends XmlComponent {
export class TableCell extends XmlComponent { export class TableCell extends XmlComponent {
private readonly properties: TableCellProperties; private readonly properties: TableCellProperties;
constructor(private readonly tableReference: Table, private readonly x: number, private readonly y: number) { constructor() {
super("w:tc"); super("w:tc");
this.properties = new TableCellProperties(); this.properties = new TableCellProperties();
this.root.push(this.properties); this.root.push(this.properties);
@ -155,15 +161,6 @@ export class TableCell extends XmlComponent {
return para; return para;
} }
public setHorizontalSpan(span: number): TableCell {
for (let i = 1; i < span; i++) {
this.tableReference.getCell(this.x, this.y + i).delete();
}
this.properties.addGridSpan(span);
return this;
}
public get CellProperties(): TableCellProperties { public get CellProperties(): TableCellProperties {
return this.properties; return this.properties;
} }