Remove create table helper function

This commit is contained in:
Dolan
2019-06-25 01:21:28 +01:00
parent dfe986331d
commit c97d15cb9f
22 changed files with 132 additions and 422 deletions

View File

@ -1,14 +1,16 @@
// Add custom borders to table cell // Add custom borders to table cell
// 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 { BorderStyle, Document, Packer, Paragraph } from "../build"; import { BorderStyle, Document, Packer, Paragraph, Table } from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable({ const table = new Table({
rows: 4, rows: 4,
columns: 4, columns: 4,
}); });
doc.addTable(table);
table table
.getCell(2, 2) .getCell(2, 2)
.addParagraph(new Paragraph("Hello")) .addParagraph(new Paragraph("Hello"))

View File

@ -1,14 +1,17 @@
// Add image to table cell // Add image to table cell
// 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, Media, Packer, Paragraph } from "../build"; import { Document, Media, Packer, Paragraph, Table } from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable({ const table = new Table({
rows: 4, rows: 4,
columns: 4, columns: 4,
}); });
doc.addTable(table);
table.getCell(2, 2).addParagraph(new Paragraph("Hello")); table.getCell(2, 2).addParagraph(new Paragraph("Hello"));
const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg")); const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));

View File

@ -1,28 +1,29 @@
// Example of how you would create a table and add data to it // Example of how you would create a table and add data to it
// 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, HeadingLevel, Packer, Paragraph, VerticalAlign } from "../build"; import { Document, HeadingLevel, Packer, Paragraph, VerticalAlign, Table } from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable({ const table = new Table({
rows: 2, rows: 2,
columns: 2, columns: 2,
}); });
doc.addTable(table);
table table
.getCell(1, 1) .getCell(1, 1)
.addParagraph(new Paragraph("This text should be in the middle of the cell")) .addParagraph(new Paragraph("This text should be in the middle of the cell"))
.setVerticalAlign(VerticalAlign.CENTER); .setVerticalAlign(VerticalAlign.CENTER);
table table.getCell(1, 0).addParagraph(
.getCell(1, 0)
.addParagraph(
new Paragraph({ new Paragraph({
text: text:
"Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah", "Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah",
heading: HeadingLevel.HEADING_1, heading: HeadingLevel.HEADING_1,
}), }),
); );
const packer = new Packer(); const packer = new Packer();

View File

@ -1,15 +1,17 @@
// Example of how you would merge cells together // Example of how you would merge cells together
// 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, HeadingLevel, Packer, Paragraph, ShadingType, WidthType } from "../build"; import { Document, HeadingLevel, Packer, Paragraph, ShadingType, Table, WidthType } from "../build";
const doc = new Document(); const doc = new Document();
let table = doc.createTable({ let table = new Table({
rows: 2, rows: 2,
columns: 2, columns: 2,
}); });
doc.addTable(table);
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);
@ -20,13 +22,16 @@ doc.addParagraph(
}), }),
); );
table = doc.createTable({ table = new Table({
rows: 2, rows: 2,
columns: 3, columns: 3,
width: 100, width: 100,
widthUnitType: WidthType.AUTO, widthUnitType: WidthType.AUTO,
columnWidths: [1000, 1000, 1000], columnWidths: [1000, 1000, 1000],
}); });
doc.addTable(table);
table table
.getCell(0, 0) .getCell(0, 0)
.addParagraph(new Paragraph("World")) .addParagraph(new Paragraph("World"))
@ -45,7 +50,7 @@ doc.addParagraph(
}), }),
); );
table = doc.createTable({ table = new Table({
rows: 2, rows: 2,
columns: 4, columns: 4,
width: 7000, width: 7000,
@ -57,6 +62,9 @@ table = doc.createTable({
left: 400, left: 400,
}, },
}); });
doc.addTable(table);
table.getCell(0, 0).addParagraph(new Paragraph("Foo")); table.getCell(0, 0).addParagraph(new Paragraph("Foo"));
table.getCell(0, 1).addParagraph(new Paragraph("v")); table.getCell(0, 1).addParagraph(new Paragraph("v"));
@ -97,13 +105,15 @@ table.getRow(0).mergeCells(0, 3);
doc.addParagraph(new Paragraph("hi")); doc.addParagraph(new Paragraph("hi"));
doc.createTable({ table = new Table({
rows: 2, rows: 2,
columns: 2, columns: 2,
width: 100, width: 100,
widthUnitType: WidthType.PERCENTAGE, widthUnitType: WidthType.PERCENTAGE,
}); });
doc.addTable(table);
const packer = new Packer(); const packer = new Packer();
packer.toBuffer(doc).then((buffer) => { packer.toBuffer(doc).then((buffer) => {

View File

@ -1,11 +1,20 @@
// 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, RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType, WidthType } from "../build"; import {
Document,
Packer,
Paragraph,
RelativeHorizontalPosition,
RelativeVerticalPosition,
Table,
TableAnchorType,
WidthType,
} from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable({ const table = new Table({
rows: 2, rows: 2,
columns: 2, columns: 2,
float: { float: {
@ -17,6 +26,8 @@ const table = doc.createTable({
width: 4535, width: 4535,
widthUnitType: WidthType.DXA, widthUnitType: WidthType.DXA,
}); });
doc.addTable(table);
table.setFixedWidthLayout(); table.setFixedWidthLayout();
table.getCell(0, 0).addParagraph(new Paragraph("Hello")); table.getCell(0, 0).addParagraph(new Paragraph("Hello"));

View File

@ -1,14 +1,17 @@
// Example of how you would create a table and add data to it // Example of how you would create a table and add data to it
// 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 } from "../build"; import { Document, Packer, Paragraph, Table } from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable({ const table = new Table({
rows: 4, rows: 4,
columns: 4, columns: 4,
}); });
doc.addTable(table);
table.getCell(2, 2).addParagraph(new Paragraph("Hello")); table.getCell(2, 2).addParagraph(new Paragraph("Hello"));
const packer = new Packer(); const packer = new Packer();

View File

@ -1,14 +1,17 @@
// Multiple cells merging in the same table // Multiple cells merging in the same table
// 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 } from "../build"; import { Document, Packer, Paragraph, Table } from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable({ const table = new Table({
rows: 13, rows: 13,
columns: 6, columns: 6,
}); });
doc.addTable(table);
let row = 0; let row = 0;
table.getCell(row, 0).addParagraph(new Paragraph("0,0")); table.getCell(row, 0).addParagraph(new Paragraph("0,0"));
table.getCell(row, 1).addParagraph(new Paragraph("0,1")); table.getCell(row, 1).addParagraph(new Paragraph("0,1"));

View File

@ -1,14 +1,17 @@
// Add image to table cell // Add image to table cell
// 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 } from "../build"; import { Document, Packer, Paragraph, Table } from "../build";
const doc = new Document(); const doc = new Document();
const table = doc.createTable({ const table = new Table({
rows: 4, rows: 4,
columns: 4, columns: 4,
}); });
doc.addTable(table);
table.getCell(2, 2).addParagraph(new Paragraph("Hello")); table.getCell(2, 2).addParagraph(new Paragraph("Hello"));
table.getColumn(3).mergeCells(1, 2); table.getColumn(3).mergeCells(1, 2);
// table.getCell(3, 2).addParagraph(new Paragraph("Hello")); // table.getCell(3, 2).addParagraph(new Paragraph("Hello"));

View File

@ -4,10 +4,13 @@ You can create tables with `docx`. More information can be found [here](http://o
## Create Table ## Create Table
To create a table, simply use the `createTable()` method on a `document`. To create a table, simply create one with `new Table()`, then add it to the document: `doc.addTable()`.
```ts ```ts
const table = doc.createTable([NUMBER OF ROWS], [NUMBER OF COLUMNS]); const table = doc.addTable(new Table({
rows: [NUMBER OF ROWS],
columns: [NUMBER OF COLUMNS]
});
``` ```
Alternatively, you can create a table object directly, and then add it in the `document` Alternatively, you can create a table object directly, and then add it in the `document`
@ -20,11 +23,10 @@ doc.addTable(table);
The snippet below creates a table of 2 rows and 4 columns. The snippet below creates a table of 2 rows and 4 columns.
```ts ```ts
const table = doc.createTable(2, 4); const table = new Table({
rows: 2,
// Or columns: 4,
});
const table = new Table(2, 4);
doc.addTable(table); doc.addTable(table);
``` ```
@ -70,7 +72,7 @@ column.getCell(index);
## Cells ## Cells
The `createTable()` method created a table with cells. To access the cell, use the `getCell()` method. To access the cell, use the `getCell()` method.
```ts ```ts
const cell = table.getCell([ROW INDEX], [COLUMN INDEX]); const cell = table.getCell([ROW INDEX], [COLUMN INDEX]);
@ -103,10 +105,11 @@ You can specify the width of a cell using:
`cell.Properties.setWidth(width, format)` `cell.Properties.setWidth(width, format)`
format can be: format can be:
* WidthType.AUTO
* WidthType.DXA: value is in twentieths of a point - WidthType.AUTO
* WidthType.NIL: is considered as zero - WidthType.DXA: value is in twentieths of a point
* WidthType.PCT: percent of table width - WidthType.NIL: is considered as zero
- WidthType.PCT: percent of table width
### Example ### Example
@ -115,7 +118,7 @@ cell.Properties.setWidth(100, WidthType.DXA);
``` ```
```ts ```ts
cell.Properties.setWidth('50%', WidthType.PCT); cell.Properties.setWidth("50%", WidthType.PCT);
``` ```
## Borders ## Borders
@ -242,10 +245,9 @@ If a table is paginated on multiple pages, it is possible to repeat a row at the
table.getRow(0).setTableHeader(); table.getRow(0).setTableHeader();
``` ```
## Examples ## Examples
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo4.ts ':include') [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo4.ts ":include")
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo4.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo4.ts_
@ -253,7 +255,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo4.ts_
Example showing how to add colourful borders to tables Example showing how to add colourful borders to tables
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo20.ts ':include') [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo20.ts ":include")
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo20.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo20.ts_
@ -261,11 +263,11 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo20.ts_
Example showing how to add images to tables Example showing how to add images to tables
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo24.ts ':include') [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo24.ts ":include")
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo24.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo24.ts_
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo36.ts ':include') [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo36.ts ":include")
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo36.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo36.ts_
@ -273,7 +275,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo36.ts_
Example showing how align text in a table cell Example showing how align text in a table cell
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo31.ts ':include') [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo31.ts ":include")
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo31.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo31.ts_
@ -281,11 +283,11 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo31.ts_
Example showing merging of `rows` Example showing merging of `rows`
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo32.ts ':include') [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo32.ts ":include")
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo32.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo32.ts_
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo41.ts ':include') [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo41.ts ":include")
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo41.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo41.ts_
@ -293,13 +295,12 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo41.ts_
Example showing merging of `columns` Example showing merging of `columns`
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo43.ts ':include') [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo43.ts ":include")
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo43.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo43.ts_
### Floating tables ### Floating tables
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo34.ts ':include') [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo34.ts ":include")
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo34.ts_ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo34.ts_

View File

@ -2,7 +2,6 @@ import { assert, expect } from "chai";
import { Formatter } from "export/formatter"; import { Formatter } from "export/formatter";
import { Table } from "../table";
import { Document } from "./document"; import { Document } from "./document";
describe("Document", () => { describe("Document", () => {
@ -29,40 +28,4 @@ describe("Document", () => {
expect(body[0]).to.have.property("w:sectPr"); expect(body[0]).to.have.property("w:sectPr");
}); });
}); });
describe("#createTable", () => {
it("should create a new table and append it to body", () => {
const table = document.createTable({
rows: 2,
columns: 3,
});
expect(table).to.be.an.instanceof(Table);
const body = new Formatter().format(document)["w:document"][1]["w:body"];
expect(body)
.to.be.an("array")
.which.has.length.at.least(1);
expect(body[0]).to.have.property("w:tbl");
});
it("should create a table with the correct dimensions", () => {
document.createTable({
rows: 2,
columns: 3,
});
const body = new Formatter().format(document)["w:document"][1]["w:body"];
expect(body)
.to.be.an("array")
.which.has.length.at.least(1);
expect(body[0])
.to.have.property("w:tbl")
.which.includes({
"w:tblGrid": [
{ "w:gridCol": { _attr: { "w:w": 100 } } },
{ "w:gridCol": { _attr: { "w:w": 100 } } },
{ "w:gridCol": { _attr: { "w:w": 100 } } },
],
});
expect(body[0]["w:tbl"].filter((x) => x["w:tr"])).to.have.length(2);
});
});
}); });

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 { ITableOptions, Table } from "../table"; import { 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";
@ -52,12 +52,6 @@ export class Document extends XmlComponent {
return this; return this;
} }
public createTable(options: ITableOptions): Table {
const table = new Table(options);
this.addTable(table);
return table;
}
public get Body(): Body { public get Body(): Body {
return this.body; return this.body;
} }

View File

@ -104,19 +104,6 @@ describe("File", () => {
}); });
}); });
describe("#createTable", () => {
it("should call the underlying document's createTable", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.Document, "createTable");
wrapper.createTable({
rows: 1,
columns: 1,
});
expect(spy.called).to.equal(true);
});
});
describe("#addTableOfContents", () => { describe("#addTableOfContents", () => {
it("should call the underlying document's addTableOfContents", () => { it("should call the underlying document's addTableOfContents", () => {
const wrapper = new File(); const wrapper = new File();

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 { ITableOptions, Table } from "./table"; import { Table } from "./table";
import { TableOfContents } from "./table-of-contents"; import { TableOfContents } from "./table-of-contents";
export class File { export class File {
@ -127,10 +127,6 @@ export class File {
return this; return this;
} }
public createTable(options: ITableOptions): Table {
return this.document.createTable(options);
}
public addImage(image: Image): File { public addImage(image: Image): File {
this.document.addParagraph(image.Paragraph); this.document.addParagraph(image.Paragraph);
return this; return this;

View File

@ -32,16 +32,6 @@ describe("FooterWrapper", () => {
}); });
}); });
describe("#createTable", () => {
it("should call the underlying footer's createTable", () => {
const wrapper = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(wrapper.Footer, "createTable");
wrapper.createTable(1, 1);
expect(spy.called).to.equal(true);
});
});
describe("#addImage", () => { describe("#addImage", () => {
it("should call the underlying footer's addImage", () => { it("should call the underlying footer's addImage", () => {
const file = new FooterWrapper(new Media(), 1); const file = new FooterWrapper(new Media(), 1);
@ -56,12 +46,11 @@ describe("FooterWrapper", () => {
describe("#createImage", () => { describe("#createImage", () => {
it("should call the underlying footer's createImage", () => { it("should call the underlying footer's createImage", () => {
const file = new FooterWrapper(new Media(), 1); const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.Media, "addMedia"); const spy = sinon.spy(Media, "addImage");
const fileSpy = sinon.spy(file, "addImage");
file.createImage(""); file.createImage("");
expect(spy.called).to.equal(true); expect(spy.called).to.equal(true);
expect(fileSpy.called).to.equal(true); spy.restore();
}); });
}); });

View File

@ -1,9 +1,10 @@
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { FooterReferenceType } from "./document"; import { FooterReferenceType } from "./document";
import { IDrawingOptions } from "./drawing";
import { Footer } from "./footer/footer"; import { Footer } from "./footer/footer";
import { Image, Media } from "./media"; import { Image, Media } from "./media";
import { ImageParagraph, Paragraph } from "./paragraph"; import { Paragraph } from "./paragraph";
import { Relationships } from "./relationships"; import { Relationships } from "./relationships";
import { Table } from "./table"; import { Table } from "./table";
@ -29,22 +30,26 @@ export class FooterWrapper {
this.footer.addTable(table); this.footer.addTable(table);
} }
public createTable(rows: number, cols: number): Table { public addImage(image: Image): FooterWrapper {
return this.footer.createTable(rows, cols); this.footer.addParagraph(image.Paragraph);
return this;
} }
public addChildElement(childElement: XmlComponent): void { public addChildElement(childElement: XmlComponent): void {
this.footer.addChildElement(childElement); this.footer.addChildElement(childElement);
} }
public createImage(image: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): void { public createImage(
const mediaData = this.media.addMedia(image, width, height); buffer: Buffer | string | Uint8Array | ArrayBuffer,
this.addImage(new Image(new ImageParagraph(mediaData))); width?: number,
} height?: number,
drawingOptions?: IDrawingOptions,
): Paragraph {
const image = Media.addImage(this, buffer, width, height, drawingOptions);
const paragraph = new Paragraph(image);
this.addParagraph(paragraph);
public addImage(image: Image): FooterWrapper { return paragraph;
this.footer.addParagraph(image.Paragraph);
return this;
} }
public get Footer(): Footer { public get Footer(): Footer {

View File

@ -45,13 +45,4 @@ export class Footer extends InitializableXmlComponent {
public addTable(table: Table): void { public addTable(table: Table): void {
this.root.push(table); this.root.push(table);
} }
public createTable(rows: number, cols: number): Table {
const table = new Table({
rows: rows,
columns: cols,
});
this.addTable(table);
return table;
}
} }

View File

@ -32,16 +32,6 @@ describe("HeaderWrapper", () => {
}); });
}); });
describe("#createTable", () => {
it("should call the underlying header's createTable", () => {
const wrapper = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(wrapper.Header, "createTable");
wrapper.createTable(1, 1);
expect(spy.called).to.equal(true);
});
});
describe("#addImage", () => { describe("#addImage", () => {
it("should call the underlying header's addImage", () => { it("should call the underlying header's addImage", () => {
const file = new HeaderWrapper(new Media(), 1); const file = new HeaderWrapper(new Media(), 1);
@ -56,12 +46,11 @@ describe("HeaderWrapper", () => {
describe("#createImage", () => { describe("#createImage", () => {
it("should call the underlying header's createImage", () => { it("should call the underlying header's createImage", () => {
const file = new HeaderWrapper(new Media(), 1); const file = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(file.Media, "addMedia"); const spy = sinon.spy(Media, "addImage");
const fileSpy = sinon.spy(file, "addImage");
file.createImage(""); file.createImage("");
expect(spy.called).to.equal(true); expect(spy.called).to.equal(true);
expect(fileSpy.called).to.equal(true); spy.restore();
}); });
}); });

View File

@ -1,9 +1,10 @@
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { HeaderReferenceType } from "./document"; import { HeaderReferenceType } from "./document";
import { IDrawingOptions } from "./drawing";
import { Header } from "./header/header"; import { Header } from "./header/header";
import { Image, Media } from "./media"; import { Image, Media } from "./media";
import { ImageParagraph, Paragraph } from "./paragraph"; import { Paragraph } from "./paragraph";
import { Relationships } from "./relationships"; import { Relationships } from "./relationships";
import { Table } from "./table"; import { Table } from "./table";
@ -29,22 +30,26 @@ export class HeaderWrapper {
this.header.addTable(table); this.header.addTable(table);
} }
public createTable(rows: number, cols: number): Table { public addImage(image: Image): HeaderWrapper {
return this.header.createTable(rows, cols); this.header.addParagraph(image.Paragraph);
return this;
} }
public addChildElement(childElement: XmlComponent | string): void { public addChildElement(childElement: XmlComponent | string): void {
this.header.addChildElement(childElement); this.header.addChildElement(childElement);
} }
public createImage(image: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): void { public createImage(
const mediaData = this.media.addMedia(image, width, height); buffer: Buffer | string | Uint8Array | ArrayBuffer,
this.addImage(new Image(new ImageParagraph(mediaData))); width?: number,
} height?: number,
drawingOptions?: IDrawingOptions,
): Paragraph {
const image = Media.addImage(this, buffer, width, height, drawingOptions);
const paragraph = new Paragraph(image);
this.addParagraph(paragraph);
public addImage(image: Image): HeaderWrapper { return paragraph;
this.header.addParagraph(image.Paragraph);
return this;
} }
public get Header(): Header { public get Header(): Header {

View File

@ -56,13 +56,4 @@ export class Header extends InitializableXmlComponent {
public addTable(table: Table): void { public addTable(table: Table): void {
this.root.push(table); this.root.push(table);
} }
public createTable(rows: number, cols: number): Table {
const table = new Table({
rows: rows,
columns: cols,
});
this.addTable(table);
return table;
}
} }

View File

@ -1,12 +1,14 @@
import { IDrawingOptions } from "../drawing"; import { IDrawingOptions } from "../drawing";
import { File } from "../file"; import { File } from "../file";
import { FooterWrapper } from "../footer-wrapper";
import { HeaderWrapper } from "../header-wrapper";
import { PictureRun } from "../paragraph"; import { PictureRun } from "../paragraph";
import { IMediaData } from "./data"; import { IMediaData } from "./data";
// import { Image } from "./image"; // import { Image } from "./image";
export class Media { export class Media {
public static addImage( public static addImage(
file: File, file: File | HeaderWrapper | FooterWrapper,
buffer: Buffer | string | Uint8Array | ArrayBuffer, buffer: Buffer | string | Uint8Array | ArrayBuffer,
width?: number, width?: number,
height?: number, height?: number,

View File

@ -1,243 +0,0 @@
// http://officeopenxml.com/WPparagraph.php
import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run";
import { Image } from "file/media";
import { Num } from "file/numbering/num";
import { XmlComponent } from "file/xml-components";
import { Alignment } from "./formatting/alignment";
import { Bidirectional } from "./formatting/bidirectional";
<<<<<<< HEAD
import { Border, ThematicBreak } from "./formatting/border";
import { Indent } from "./formatting/indent";
=======
import { ThematicBreak } from "./formatting/border";
import { IIndentAttributesProperties, Indent } from "./formatting/indent";
>>>>>>> a53818754a1c76b9930ee2ecc642570170fa3c06
import { KeepLines, KeepNext } from "./formatting/keep";
import { PageBreak, PageBreakBefore } from "./formatting/page-break";
import { ISpacingProperties, Spacing } from "./formatting/spacing";
import { Style } from "./formatting/style";
import { CenterTabStop, LeftTabStop, MaxRightTabStop, RightTabStop } from "./formatting/tab-stop";
import { NumberProperties } from "./formatting/unordered-list";
import { Bookmark, Hyperlink } from "./links";
import { ParagraphProperties } from "./properties";
import { PictureRun, Run, TextRun } from "./run";
export class Paragraph extends XmlComponent {
private readonly properties: ParagraphProperties;
constructor(text?: string) {
super("w:p");
this.properties = new ParagraphProperties();
this.root.push(this.properties);
if (text !== undefined) {
this.root.push(new TextRun(text));
}
}
public get Borders(): Border {
return this.properties.paragraphBorder;
}
public createBorder(): Paragraph {
this.properties.createBorder();
return this;
}
public addRun(run: Run): Paragraph {
this.root.push(run);
return this;
}
public addHyperLink(hyperlink: Hyperlink): Paragraph {
this.root.push(hyperlink);
return this;
}
public addBookmark(bookmark: Bookmark): Paragraph {
// Bookmarks by spec have three components, a start, text, and end
this.root.push(bookmark.start);
this.root.push(bookmark.text);
this.root.push(bookmark.end);
return this;
}
public createTextRun(text: string): TextRun {
const run = new TextRun(text);
this.addRun(run);
return run;
}
public addImage(image: Image): PictureRun {
const run = image.Run;
this.addRun(run);
return run;
}
public heading1(): Paragraph {
this.properties.push(new Style("Heading1"));
return this;
}
public heading2(): Paragraph {
this.properties.push(new Style("Heading2"));
return this;
}
public heading3(): Paragraph {
this.properties.push(new Style("Heading3"));
return this;
}
public heading4(): Paragraph {
this.properties.push(new Style("Heading4"));
return this;
}
public heading5(): Paragraph {
this.properties.push(new Style("Heading5"));
return this;
}
public heading6(): Paragraph {
this.properties.push(new Style("Heading6"));
return this;
}
public title(): Paragraph {
this.properties.push(new Style("Title"));
return this;
}
public center(): Paragraph {
this.properties.push(new Alignment("center"));
return this;
}
public left(): Paragraph {
this.properties.push(new Alignment("left"));
return this;
}
public right(): Paragraph {
this.properties.push(new Alignment("right"));
return this;
}
public start(): Paragraph {
this.properties.push(new Alignment("start"));
return this;
}
public end(): Paragraph {
this.properties.push(new Alignment("end"));
return this;
}
public distribute(): Paragraph {
this.properties.push(new Alignment("distribute"));
return this;
}
public justified(): Paragraph {
this.properties.push(new Alignment("both"));
return this;
}
public thematicBreak(): Paragraph {
this.properties.push(new ThematicBreak());
return this;
}
public pageBreak(): Paragraph {
this.root.push(new PageBreak());
return this;
}
public pageBreakBefore(): Paragraph {
this.properties.push(new PageBreakBefore());
return this;
}
public maxRightTabStop(): Paragraph {
this.properties.push(new MaxRightTabStop());
return this;
}
public leftTabStop(position: number): Paragraph {
this.properties.push(new LeftTabStop(position));
return this;
}
public rightTabStop(position: number): Paragraph {
this.properties.push(new RightTabStop(position));
return this;
}
public centerTabStop(position: number): Paragraph {
this.properties.push(new CenterTabStop(position));
return this;
}
public bullet(indentLevel: number = 0): Paragraph {
this.properties.push(new Style("ListParagraph"));
this.properties.push(new NumberProperties(1, indentLevel));
return this;
}
public setNumbering(numbering: Num, indentLevel: number): Paragraph {
this.properties.push(new Style("ListParagraph"));
this.properties.push(new NumberProperties(numbering.id, indentLevel));
return this;
}
public setCustomNumbering(numberId: number, indentLevel: number): Paragraph {
this.properties.push(new NumberProperties(numberId, indentLevel));
return this;
}
public style(styleId: string): Paragraph {
this.properties.push(new Style(styleId));
return this;
}
public indent(attrs: IIndentAttributesProperties): Paragraph {
this.properties.push(new Indent(attrs));
return this;
}
public spacing(params: ISpacingProperties): Paragraph {
this.properties.push(new Spacing(params));
return this;
}
public keepNext(): Paragraph {
this.properties.push(new KeepNext());
return this;
}
public keepLines(): Paragraph {
this.properties.push(new KeepLines());
return this;
}
public referenceFootnote(id: number): Paragraph {
this.root.push(new FootnoteReferenceRun(id));
return this;
}
public addRunToFront(run: Run): Paragraph {
this.root.splice(1, 0, run);
return this;
}
public bidirectional(): Paragraph {
this.properties.push(new Bidirectional());
return this;
}
public get Properties(): ParagraphProperties {
return this.properties;
}
}

View File

@ -8,6 +8,10 @@ import { ITableCellMarginOptions } from "./cell-margin/table-cell-margins";
import { TableCellBorders, VerticalAlign, VMergeType } from "./table-cell-components"; import { TableCellBorders, VerticalAlign, VMergeType } from "./table-cell-components";
import { TableCellProperties } from "./table-cell-properties"; import { TableCellProperties } from "./table-cell-properties";
export interface ITableCellOptions {
readonly shading?: ITableShadingAttributesProperties;
}
export class TableCell extends XmlComponent { export class TableCell extends XmlComponent {
private readonly properties: TableCellProperties; private readonly properties: TableCellProperties;