diff --git a/demo/demo24.ts b/demo/demo24.ts index 831a4c29a8..ed84acf727 100644 --- a/demo/demo24.ts +++ b/demo/demo24.ts @@ -12,7 +12,7 @@ const table = doc.createTable({ table.getCell(2, 2).addParagraph(new Paragraph("Hello")); const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg")); -table.getCell(1, 1).addParagraph(image.Paragraph); +table.getCell(1, 1).addParagraph(new Paragraph(image)); const packer = new Packer(); diff --git a/demo/demo36.ts b/demo/demo36.ts index f40d0e4e3e..655410b56e 100644 --- a/demo/demo36.ts +++ b/demo/demo36.ts @@ -1,7 +1,7 @@ // 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, Table } from "../build"; +import { Document, Media, Packer, Paragraph, Table } from "../build"; const doc = new Document(); const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg")); @@ -10,7 +10,7 @@ const table = new Table({ rows: 2, columns: 2, }); -table.getCell(1, 1).addParagraph(image.Paragraph); +table.getCell(1, 1).addParagraph(new Paragraph(image)); doc.addTable(table); diff --git a/src/file/file.ts b/src/file/file.ts index 50f4ff9613..bb27abb411 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -141,11 +141,12 @@ export class File { width?: number, height?: number, drawingOptions?: IDrawingOptions, - ): Image { + ): Paragraph { const image = Media.addImage(this, buffer, width, height, drawingOptions); - this.document.addParagraph(image.Paragraph); + const paragraph = new Paragraph(image); + this.document.addParagraph(paragraph); - return image; + return paragraph; } public createHyperlink(link: string, text?: string): Hyperlink { diff --git a/src/file/media/media.spec.ts b/src/file/media/media.spec.ts index 9c53f1910d..b7ce0c106c 100644 --- a/src/file/media/media.spec.ts +++ b/src/file/media/media.spec.ts @@ -5,6 +5,7 @@ import { stub } from "sinon"; import { Formatter } from "export/formatter"; import { File } from "../file"; +import { Paragraph } from "../paragraph"; import { Media } from "./media"; describe("Media", () => { @@ -13,10 +14,10 @@ describe("Media", () => { const file = new File(); const image = Media.addImage(file, ""); - let tree = new Formatter().format(image.Paragraph); + let tree = new Formatter().format(new Paragraph(image)); expect(tree["w:p"]).to.be.an.instanceof(Array); - tree = new Formatter().format(image.Run); + tree = new Formatter().format(image); expect(tree["w:r"]).to.be.an.instanceof(Array); }); @@ -26,7 +27,7 @@ describe("Media", () => { const file = new File(); const image1 = Media.addImage(file, "test"); - const tree = new Formatter().format(image1.Paragraph); + const tree = new Formatter().format(new Paragraph(image1)); const inlineElements = tree["w:p"][0]["w:r"][0]["w:drawing"][0]["wp:inline"]; const graphicData = inlineElements.find((x) => x["a:graphic"]); @@ -38,7 +39,7 @@ describe("Media", () => { }); const image2 = Media.addImage(file, "test"); - const tree2 = new Formatter().format(image2.Paragraph); + const tree2 = new Formatter().format(new Paragraph(image2)); const inlineElements2 = tree2["w:p"][0]["w:r"][0]["w:drawing"][0]["wp:inline"]; const graphicData2 = inlineElements2.find((x) => x["a:graphic"]); diff --git a/src/file/media/media.ts b/src/file/media/media.ts index 8a160291ca..75087a66ab 100644 --- a/src/file/media/media.ts +++ b/src/file/media/media.ts @@ -1,8 +1,8 @@ import { IDrawingOptions } from "../drawing"; import { File } from "../file"; -import { ImageParagraph } from "../paragraph"; +import { PictureRun } from "../paragraph"; import { IMediaData } from "./data"; -import { Image } from "./image"; +// import { Image } from "./image"; export class Media { public static addImage( @@ -11,10 +11,10 @@ export class Media { width?: number, height?: number, drawingOptions?: IDrawingOptions, - ): Image { + ): PictureRun { // Workaround to expose id without exposing to API const mediaData = file.Media.addMedia(buffer, width, height); - return new Image(new ImageParagraph(mediaData, drawingOptions)); + return new PictureRun(mediaData, drawingOptions); } private static generateId(): string { diff --git a/src/file/paragraph/paragraph.ts b/src/file/paragraph/paragraph.ts index 51940a6373..74aff05bf5 100644 --- a/src/file/paragraph/paragraph.ts +++ b/src/file/paragraph/paragraph.ts @@ -55,12 +55,13 @@ export interface IParagraphOptions { readonly level: number; readonly custom?: boolean; }; + readonly children?: Array; } export class Paragraph extends XmlComponent { private readonly properties: ParagraphProperties; - constructor(options: string | IParagraphOptions) { + constructor(options: string | PictureRun | IParagraphOptions) { super("w:p"); if (typeof options === "string") { @@ -70,6 +71,13 @@ export class Paragraph extends XmlComponent { return; } + if (options instanceof PictureRun) { + this.properties = new ParagraphProperties({}); + this.root.push(this.properties); + this.root.push(options); + return; + } + this.properties = new ParagraphProperties({ border: options.border, }); @@ -163,6 +171,12 @@ export class Paragraph extends XmlComponent { this.root.push(run); } } + + if (options.children) { + for (const child of options.children) { + this.root.push(child); + } + } } public addRun(run: Run): Paragraph {