From c97d15cb9f813956882332e110d7ca443267610b Mon Sep 17 00:00:00 2001 From: Dolan Date: Tue, 25 Jun 2019 01:21:28 +0100 Subject: [PATCH] Remove create table helper function --- demo/demo20.ts | 6 +- demo/demo24.ts | 7 +- demo/demo31.ts | 23 +-- demo/demo32.ts | 20 +- demo/demo34.ts | 15 +- demo/demo4.ts | 7 +- demo/demo41.ts | 7 +- demo/demo43.ts | 7 +- docs/usage/tables.md | 49 ++--- src/file/document/document.spec.ts | 37 ---- src/file/document/document.ts | 8 +- src/file/file.spec.ts | 13 -- src/file/file.ts | 6 +- src/file/footer-wrapper.spec.ts | 15 +- src/file/footer-wrapper.ts | 25 ++- src/file/footer/footer.ts | 9 - src/file/header-wrapper.spec.ts | 15 +- src/file/header-wrapper.ts | 25 ++- src/file/header/header.ts | 9 - src/file/media/media.ts | 4 +- src/file/paragraph/paragraph.ts.orig | 243 ------------------------ src/file/table/table-cell/table-cell.ts | 4 + 22 files changed, 132 insertions(+), 422 deletions(-) delete mode 100644 src/file/paragraph/paragraph.ts.orig diff --git a/demo/demo20.ts b/demo/demo20.ts index d80d23913b..8038944ed0 100644 --- a/demo/demo20.ts +++ b/demo/demo20.ts @@ -1,14 +1,16 @@ // Add custom borders to table cell // Import from 'docx' rather than '../build' if you install from npm 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 table = doc.createTable({ +const table = new Table({ rows: 4, columns: 4, }); + +doc.addTable(table); table .getCell(2, 2) .addParagraph(new Paragraph("Hello")) diff --git a/demo/demo24.ts b/demo/demo24.ts index ed84acf727..7a50a911b4 100644 --- a/demo/demo24.ts +++ b/demo/demo24.ts @@ -1,14 +1,17 @@ // Add image to table cell // Import from 'docx' rather than '../build' if you install from npm 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 table = doc.createTable({ +const table = new Table({ rows: 4, columns: 4, }); + +doc.addTable(table); + table.getCell(2, 2).addParagraph(new Paragraph("Hello")); const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg")); diff --git a/demo/demo31.ts b/demo/demo31.ts index a17f18b94c..15f6de5a64 100644 --- a/demo/demo31.ts +++ b/demo/demo31.ts @@ -1,28 +1,29 @@ // 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 * 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 table = doc.createTable({ +const table = new Table({ rows: 2, columns: 2, }); + +doc.addTable(table); + table .getCell(1, 1) .addParagraph(new Paragraph("This text should be in the middle of the cell")) .setVerticalAlign(VerticalAlign.CENTER); -table - .getCell(1, 0) - .addParagraph( - new Paragraph({ - 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", - heading: HeadingLevel.HEADING_1, - }), - ); +table.getCell(1, 0).addParagraph( + new Paragraph({ + 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", + heading: HeadingLevel.HEADING_1, + }), +); const packer = new Packer(); diff --git a/demo/demo32.ts b/demo/demo32.ts index 421838afa9..2591a72174 100644 --- a/demo/demo32.ts +++ b/demo/demo32.ts @@ -1,15 +1,17 @@ // Example of how you would merge cells together // Import from 'docx' rather than '../build' if you install from npm 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(); -let table = doc.createTable({ +let table = new Table({ rows: 2, columns: 2, }); +doc.addTable(table); + table.getCell(0, 0).addParagraph(new Paragraph("Hello")); table.getRow(0).mergeCells(0, 1); @@ -20,13 +22,16 @@ doc.addParagraph( }), ); -table = doc.createTable({ +table = new Table({ rows: 2, columns: 3, width: 100, widthUnitType: WidthType.AUTO, columnWidths: [1000, 1000, 1000], }); + +doc.addTable(table); + table .getCell(0, 0) .addParagraph(new Paragraph("World")) @@ -45,7 +50,7 @@ doc.addParagraph( }), ); -table = doc.createTable({ +table = new Table({ rows: 2, columns: 4, width: 7000, @@ -57,6 +62,9 @@ table = doc.createTable({ left: 400, }, }); + +doc.addTable(table); + table.getCell(0, 0).addParagraph(new Paragraph("Foo")); table.getCell(0, 1).addParagraph(new Paragraph("v")); @@ -97,13 +105,15 @@ table.getRow(0).mergeCells(0, 3); doc.addParagraph(new Paragraph("hi")); -doc.createTable({ +table = new Table({ rows: 2, columns: 2, width: 100, widthUnitType: WidthType.PERCENTAGE, }); +doc.addTable(table); + const packer = new Packer(); packer.toBuffer(doc).then((buffer) => { diff --git a/demo/demo34.ts b/demo/demo34.ts index 0899f26bfe..678094988f 100644 --- a/demo/demo34.ts +++ b/demo/demo34.ts @@ -1,11 +1,20 @@ // 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, RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType, WidthType } from "../build"; +import { + Document, + Packer, + Paragraph, + RelativeHorizontalPosition, + RelativeVerticalPosition, + Table, + TableAnchorType, + WidthType, +} from "../build"; const doc = new Document(); -const table = doc.createTable({ +const table = new Table({ rows: 2, columns: 2, float: { @@ -17,6 +26,8 @@ const table = doc.createTable({ width: 4535, widthUnitType: WidthType.DXA, }); + +doc.addTable(table); table.setFixedWidthLayout(); table.getCell(0, 0).addParagraph(new Paragraph("Hello")); diff --git a/demo/demo4.ts b/demo/demo4.ts index 805e7ed33f..19242b1a71 100644 --- a/demo/demo4.ts +++ b/demo/demo4.ts @@ -1,14 +1,17 @@ // 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 * as fs from "fs"; -import { Document, Packer, Paragraph } from "../build"; +import { Document, Packer, Paragraph, Table } from "../build"; const doc = new Document(); -const table = doc.createTable({ +const table = new Table({ rows: 4, columns: 4, }); + +doc.addTable(table); + table.getCell(2, 2).addParagraph(new Paragraph("Hello")); const packer = new Packer(); diff --git a/demo/demo41.ts b/demo/demo41.ts index 3fc387a1c7..2d3192bdf2 100644 --- a/demo/demo41.ts +++ b/demo/demo41.ts @@ -1,14 +1,17 @@ // Multiple cells merging in the same table // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, Packer, Paragraph } from "../build"; +import { Document, Packer, Paragraph, Table } from "../build"; const doc = new Document(); -const table = doc.createTable({ +const table = new Table({ rows: 13, columns: 6, }); + +doc.addTable(table); + let row = 0; table.getCell(row, 0).addParagraph(new Paragraph("0,0")); table.getCell(row, 1).addParagraph(new Paragraph("0,1")); diff --git a/demo/demo43.ts b/demo/demo43.ts index 65d34355aa..65ce2d443a 100644 --- a/demo/demo43.ts +++ b/demo/demo43.ts @@ -1,14 +1,17 @@ // Add image to table cell // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, Packer, Paragraph } from "../build"; +import { Document, Packer, Paragraph, Table } from "../build"; const doc = new Document(); -const table = doc.createTable({ +const table = new Table({ rows: 4, columns: 4, }); + +doc.addTable(table); + table.getCell(2, 2).addParagraph(new Paragraph("Hello")); table.getColumn(3).mergeCells(1, 2); // table.getCell(3, 2).addParagraph(new Paragraph("Hello")); diff --git a/docs/usage/tables.md b/docs/usage/tables.md index ed669cfd18..437d579b8f 100644 --- a/docs/usage/tables.md +++ b/docs/usage/tables.md @@ -4,10 +4,13 @@ You can create tables with `docx`. More information can be found [here](http://o ## 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 -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` @@ -20,11 +23,10 @@ doc.addTable(table); The snippet below creates a table of 2 rows and 4 columns. ```ts -const table = doc.createTable(2, 4); - -// Or - -const table = new Table(2, 4); +const table = new Table({ + rows: 2, + columns: 4, +}); doc.addTable(table); ``` @@ -70,7 +72,7 @@ column.getCell(index); ## 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 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)` format can be: -* WidthType.AUTO -* WidthType.DXA: value is in twentieths of a point -* WidthType.NIL: is considered as zero -* WidthType.PCT: percent of table width + +- WidthType.AUTO +- WidthType.DXA: value is in twentieths of a point +- WidthType.NIL: is considered as zero +- WidthType.PCT: percent of table width ### Example @@ -115,7 +118,7 @@ cell.Properties.setWidth(100, WidthType.DXA); ``` ```ts -cell.Properties.setWidth('50%', WidthType.PCT); +cell.Properties.setWidth("50%", WidthType.PCT); ``` ## Borders @@ -241,11 +244,10 @@ If a table is paginated on multiple pages, it is possible to repeat a row at the ```ts table.getRow(0).setTableHeader(); ``` - ## 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_ @@ -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](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_ @@ -261,11 +263,11 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo20.ts_ 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_ -[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_ @@ -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](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_ @@ -281,11 +283,11 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo31.ts_ 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_ -[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_ @@ -293,13 +295,12 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo41.ts_ 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_ ### 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_ - diff --git a/src/file/document/document.spec.ts b/src/file/document/document.spec.ts index 6b20515138..780dca0f27 100644 --- a/src/file/document/document.spec.ts +++ b/src/file/document/document.spec.ts @@ -2,7 +2,6 @@ import { assert, expect } from "chai"; import { Formatter } from "export/formatter"; -import { Table } from "../table"; import { Document } from "./document"; describe("Document", () => { @@ -29,40 +28,4 @@ describe("Document", () => { 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); - }); - }); }); diff --git a/src/file/document/document.ts b/src/file/document/document.ts index 8c36c70a84..c517124ef3 100644 --- a/src/file/document/document.ts +++ b/src/file/document/document.ts @@ -1,7 +1,7 @@ // http://officeopenxml.com/WPdocument.php import { XmlComponent } from "file/xml-components"; import { Paragraph } from "../paragraph"; -import { ITableOptions, Table } from "../table"; +import { Table } from "../table"; import { TableOfContents } from "../table-of-contents"; import { Body } from "./body"; import { SectionPropertiesOptions } from "./body/section-properties"; @@ -52,12 +52,6 @@ export class Document extends XmlComponent { return this; } - public createTable(options: ITableOptions): Table { - const table = new Table(options); - this.addTable(table); - return table; - } - public get Body(): Body { return this.body; } diff --git a/src/file/file.spec.ts b/src/file/file.spec.ts index 33a2c09432..d406070443 100644 --- a/src/file/file.spec.ts +++ b/src/file/file.spec.ts @@ -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", () => { it("should call the underlying document's addTableOfContents", () => { const wrapper = new File(); diff --git a/src/file/file.ts b/src/file/file.ts index bb27abb411..4b9fa58514 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -24,7 +24,7 @@ import { Settings } from "./settings"; import { Styles } from "./styles"; import { ExternalStylesFactory } from "./styles/external-styles-factory"; import { DefaultStylesFactory } from "./styles/factory"; -import { ITableOptions, Table } from "./table"; +import { Table } from "./table"; import { TableOfContents } from "./table-of-contents"; export class File { @@ -127,10 +127,6 @@ export class File { return this; } - public createTable(options: ITableOptions): Table { - return this.document.createTable(options); - } - public addImage(image: Image): File { this.document.addParagraph(image.Paragraph); return this; diff --git a/src/file/footer-wrapper.spec.ts b/src/file/footer-wrapper.spec.ts index 90f9e71865..37121f0813 100644 --- a/src/file/footer-wrapper.spec.ts +++ b/src/file/footer-wrapper.spec.ts @@ -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", () => { it("should call the underlying footer's addImage", () => { const file = new FooterWrapper(new Media(), 1); @@ -56,12 +46,11 @@ describe("FooterWrapper", () => { describe("#createImage", () => { it("should call the underlying footer's createImage", () => { const file = new FooterWrapper(new Media(), 1); - const spy = sinon.spy(file.Media, "addMedia"); - const fileSpy = sinon.spy(file, "addImage"); + const spy = sinon.spy(Media, "addImage"); file.createImage(""); expect(spy.called).to.equal(true); - expect(fileSpy.called).to.equal(true); + spy.restore(); }); }); diff --git a/src/file/footer-wrapper.ts b/src/file/footer-wrapper.ts index b8302dc5ca..b2d5e503f4 100644 --- a/src/file/footer-wrapper.ts +++ b/src/file/footer-wrapper.ts @@ -1,9 +1,10 @@ import { XmlComponent } from "file/xml-components"; import { FooterReferenceType } from "./document"; +import { IDrawingOptions } from "./drawing"; import { Footer } from "./footer/footer"; import { Image, Media } from "./media"; -import { ImageParagraph, Paragraph } from "./paragraph"; +import { Paragraph } from "./paragraph"; import { Relationships } from "./relationships"; import { Table } from "./table"; @@ -29,22 +30,26 @@ export class FooterWrapper { this.footer.addTable(table); } - public createTable(rows: number, cols: number): Table { - return this.footer.createTable(rows, cols); + public addImage(image: Image): FooterWrapper { + this.footer.addParagraph(image.Paragraph); + return this; } public addChildElement(childElement: XmlComponent): void { this.footer.addChildElement(childElement); } - public createImage(image: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): void { - const mediaData = this.media.addMedia(image, width, height); - this.addImage(new Image(new ImageParagraph(mediaData))); - } + public createImage( + buffer: Buffer | string | Uint8Array | ArrayBuffer, + 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 { - this.footer.addParagraph(image.Paragraph); - return this; + return paragraph; } public get Footer(): Footer { diff --git a/src/file/footer/footer.ts b/src/file/footer/footer.ts index cbd69a8a3d..bd2a76bd96 100644 --- a/src/file/footer/footer.ts +++ b/src/file/footer/footer.ts @@ -45,13 +45,4 @@ export class Footer extends InitializableXmlComponent { public addTable(table: Table): void { this.root.push(table); } - - public createTable(rows: number, cols: number): Table { - const table = new Table({ - rows: rows, - columns: cols, - }); - this.addTable(table); - return table; - } } diff --git a/src/file/header-wrapper.spec.ts b/src/file/header-wrapper.spec.ts index 01e54847ca..202f034f13 100644 --- a/src/file/header-wrapper.spec.ts +++ b/src/file/header-wrapper.spec.ts @@ -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", () => { it("should call the underlying header's addImage", () => { const file = new HeaderWrapper(new Media(), 1); @@ -56,12 +46,11 @@ describe("HeaderWrapper", () => { describe("#createImage", () => { it("should call the underlying header's createImage", () => { const file = new HeaderWrapper(new Media(), 1); - const spy = sinon.spy(file.Media, "addMedia"); - const fileSpy = sinon.spy(file, "addImage"); + const spy = sinon.spy(Media, "addImage"); file.createImage(""); expect(spy.called).to.equal(true); - expect(fileSpy.called).to.equal(true); + spy.restore(); }); }); diff --git a/src/file/header-wrapper.ts b/src/file/header-wrapper.ts index b06dbb4933..4bc4134f08 100644 --- a/src/file/header-wrapper.ts +++ b/src/file/header-wrapper.ts @@ -1,9 +1,10 @@ import { XmlComponent } from "file/xml-components"; import { HeaderReferenceType } from "./document"; +import { IDrawingOptions } from "./drawing"; import { Header } from "./header/header"; import { Image, Media } from "./media"; -import { ImageParagraph, Paragraph } from "./paragraph"; +import { Paragraph } from "./paragraph"; import { Relationships } from "./relationships"; import { Table } from "./table"; @@ -29,22 +30,26 @@ export class HeaderWrapper { this.header.addTable(table); } - public createTable(rows: number, cols: number): Table { - return this.header.createTable(rows, cols); + public addImage(image: Image): HeaderWrapper { + this.header.addParagraph(image.Paragraph); + return this; } public addChildElement(childElement: XmlComponent | string): void { this.header.addChildElement(childElement); } - public createImage(image: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): void { - const mediaData = this.media.addMedia(image, width, height); - this.addImage(new Image(new ImageParagraph(mediaData))); - } + public createImage( + buffer: Buffer | string | Uint8Array | ArrayBuffer, + 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 { - this.header.addParagraph(image.Paragraph); - return this; + return paragraph; } public get Header(): Header { diff --git a/src/file/header/header.ts b/src/file/header/header.ts index 65a89e4943..67c889446d 100644 --- a/src/file/header/header.ts +++ b/src/file/header/header.ts @@ -56,13 +56,4 @@ export class Header extends InitializableXmlComponent { public addTable(table: Table): void { this.root.push(table); } - - public createTable(rows: number, cols: number): Table { - const table = new Table({ - rows: rows, - columns: cols, - }); - this.addTable(table); - return table; - } } diff --git a/src/file/media/media.ts b/src/file/media/media.ts index 75087a66ab..0d062c6d64 100644 --- a/src/file/media/media.ts +++ b/src/file/media/media.ts @@ -1,12 +1,14 @@ import { IDrawingOptions } from "../drawing"; import { File } from "../file"; +import { FooterWrapper } from "../footer-wrapper"; +import { HeaderWrapper } from "../header-wrapper"; import { PictureRun } from "../paragraph"; import { IMediaData } from "./data"; // import { Image } from "./image"; export class Media { public static addImage( - file: File, + file: File | HeaderWrapper | FooterWrapper, buffer: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number, diff --git a/src/file/paragraph/paragraph.ts.orig b/src/file/paragraph/paragraph.ts.orig deleted file mode 100644 index 7ffbeca43d..0000000000 --- a/src/file/paragraph/paragraph.ts.orig +++ /dev/null @@ -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; - } -} diff --git a/src/file/table/table-cell/table-cell.ts b/src/file/table/table-cell/table-cell.ts index 99acef0b31..830797dbed 100644 --- a/src/file/table/table-cell/table-cell.ts +++ b/src/file/table/table-cell/table-cell.ts @@ -8,6 +8,10 @@ import { ITableCellMarginOptions } from "./cell-margin/table-cell-margins"; import { TableCellBorders, VerticalAlign, VMergeType } from "./table-cell-components"; import { TableCellProperties } from "./table-cell-properties"; +export interface ITableCellOptions { + readonly shading?: ITableShadingAttributesProperties; +} + export class TableCell extends XmlComponent { private readonly properties: TableCellProperties;