From 5f22950721c4614d56bde6bdc29ed5ec0c153c44 Mon Sep 17 00:00:00 2001 From: Dolan Date: Fri, 3 Aug 2018 00:01:42 +0100 Subject: [PATCH] Change image API so that it now sends Images --- demo/demo24.js | 4 ++-- src/file/document/document.ts | 13 +++---------- src/file/file.ts | 17 ++++++++++------- src/file/footer-wrapper.ts | 15 ++++++++------- src/file/footer/footer.ts | 15 ++++----------- src/file/header-wrapper.ts | 15 ++++++++------- src/file/header/header.ts | 11 +---------- src/file/media/media.ts | 10 ++++++---- 8 files changed, 42 insertions(+), 58 deletions(-) diff --git a/demo/demo24.js b/demo/demo24.js index 8313f117cb..a95189a6ed 100644 --- a/demo/demo24.js +++ b/demo/demo24.js @@ -6,8 +6,8 @@ var doc = new docx.Document(); const table = doc.createTable(4, 4); table.getCell(2, 2).addContent(new docx.Paragraph('Hello')); -const imageData = docx.Media.addImage(doc, "./demo/images/image1.jpeg"); -table.getCell(1, 1).addContent(new docx.Image(imageData)); +const image = docx.Media.addImage(doc, "./demo/images/image1.jpeg"); +table.getCell(1, 1).addContent(image); var exporter = new docx.LocalPacker(doc); exporter.pack('My Document'); diff --git a/src/file/document/document.ts b/src/file/document/document.ts index 1e795c06d6..69fb55a25a 100644 --- a/src/file/document/document.ts +++ b/src/file/document/document.ts @@ -1,7 +1,6 @@ // http://officeopenxml.com/WPdocument.php -import { IMediaData } from "file/media"; import { XmlComponent } from "file/xml-components"; -import { Image, Paragraph } from "../paragraph"; +import { Paragraph } from "../paragraph"; import { Table } from "../table"; import { Body } from "./body"; import { SectionPropertiesOptions } from "./body/section-properties/section-properties"; @@ -37,8 +36,9 @@ export class Document extends XmlComponent { this.root.push(this.body); } - public addParagraph(paragraph: Paragraph): void { + public addParagraph(paragraph: Paragraph): Document { this.body.push(paragraph); + return this; } public createParagraph(text?: string): Paragraph { @@ -57,13 +57,6 @@ export class Document extends XmlComponent { return table; } - public createDrawing(imageData: IMediaData): Image { - const image = new Image(imageData); - this.addParagraph(image); - - return image; - } - get Body(): Body { return this.body; } diff --git a/src/file/file.ts b/src/file/file.ts index 71a2a2fbad..f8d13ed2bf 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -1,4 +1,3 @@ -import { IMediaData } from "file/media"; import { AppProperties } from "./app-properties/app-properties"; import { ContentTypes } from "./content-types/content-types"; import { CoreProperties, IPropertiesOptions } from "./core-properties"; @@ -126,18 +125,22 @@ export class File { } public createImage(filePath: string): Image { - const mediaData = Media.addImage(this, filePath); - return this.document.createDrawing(mediaData); + const image = Media.addImage(this, filePath); + this.document.addParagraph(image); + + return image; } - public insertImage(mediaData: IMediaData): File { - this.document.createDrawing(mediaData); + public insertImage(image: Image): File { + this.document.addParagraph(image); return this; } public createImageFromBuffer(buffer: Buffer, width?: number, height?: number): Image { - const mediaData = Media.addImageFromBuffer(this, buffer, width, height); - return this.document.createDrawing(mediaData); + const image = Media.addImageFromBuffer(this, buffer, width, height); + this.document.addParagraph(image); + + return image; } public createHyperlink(link: string, text?: string): Hyperlink { diff --git a/src/file/footer-wrapper.ts b/src/file/footer-wrapper.ts index 63c8a58978..8d90a5a941 100644 --- a/src/file/footer-wrapper.ts +++ b/src/file/footer-wrapper.ts @@ -1,7 +1,7 @@ import { XmlComponent } from "file/xml-components"; import { Footer } from "./footer/footer"; -import { IMediaData, Media } from "./media"; -import { Paragraph } from "./paragraph"; +import { Media } from "./media"; +import { Image, Paragraph } from "./paragraph"; import { Relationships } from "./relationships"; import { Table } from "./table"; @@ -32,10 +32,6 @@ export class FooterWrapper { return this.footer.createTable(rows, cols); } - public addDrawing(imageData: IMediaData): void { - this.footer.addDrawing(imageData); - } - public addChildElement(childElement: XmlComponent | string): void { this.footer.addChildElement(childElement); } @@ -47,7 +43,12 @@ export class FooterWrapper { "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", `media/${mediaData.fileName}`, ); - this.addDrawing(mediaData); + this.insertImage(new Image(mediaData)); + } + + public insertImage(image: Image): FooterWrapper { + this.footer.addParagraph(image); + return this; } public get Footer(): Footer { diff --git a/src/file/footer/footer.ts b/src/file/footer/footer.ts index cbdd4a544d..4237e350a4 100644 --- a/src/file/footer/footer.ts +++ b/src/file/footer/footer.ts @@ -1,7 +1,6 @@ // http://officeopenxml.com/WPfooters.php -import { IMediaData } from "file/media"; import { XmlComponent } from "file/xml-components"; -import { Paragraph, PictureRun } from "../paragraph"; +import { Paragraph } from "../paragraph"; import { Table } from "../table"; import { FooterAttributes } from "./footer-attributes"; @@ -36,8 +35,10 @@ export class Footer extends XmlComponent { return this.refId; } - public addParagraph(paragraph: Paragraph): void { + public addParagraph(paragraph: Paragraph): Footer { this.root.push(paragraph); + + return this; } public createParagraph(text?: string): Paragraph { @@ -55,12 +56,4 @@ export class Footer extends XmlComponent { this.addTable(table); return table; } - - public addDrawing(imageData: IMediaData): void { - const paragraph = new Paragraph(); - const run = new PictureRun(imageData); - paragraph.addRun(run); - - this.root.push(paragraph); - } } diff --git a/src/file/header-wrapper.ts b/src/file/header-wrapper.ts index c23f4db448..b8183b92ba 100644 --- a/src/file/header-wrapper.ts +++ b/src/file/header-wrapper.ts @@ -1,7 +1,7 @@ import { XmlComponent } from "file/xml-components"; import { Header } from "./header/header"; -import { IMediaData, Media } from "./media"; -import { Paragraph } from "./paragraph"; +import { Media } from "./media"; +import { Image, Paragraph } from "./paragraph"; import { Relationships } from "./relationships"; import { Table } from "./table"; @@ -32,10 +32,6 @@ export class HeaderWrapper { return this.header.createTable(rows, cols); } - public addDrawing(imageData: IMediaData): void { - this.header.addDrawing(imageData); - } - public addChildElement(childElement: XmlComponent | string): void { this.header.addChildElement(childElement); } @@ -47,7 +43,12 @@ export class HeaderWrapper { "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", `media/${mediaData.fileName}`, ); - this.addDrawing(mediaData); + this.insertImage(new Image(mediaData)); + } + + public insertImage(image: Image): HeaderWrapper { + this.header.addParagraph(image); + return this; } public get Header(): Header { diff --git a/src/file/header/header.ts b/src/file/header/header.ts index b0ffaa8a63..403e922ccf 100644 --- a/src/file/header/header.ts +++ b/src/file/header/header.ts @@ -1,7 +1,6 @@ // http://officeopenxml.com/WPheaders.php -import { IMediaData } from "file/media"; import { XmlComponent } from "file/xml-components"; -import { Paragraph, PictureRun } from "../paragraph"; +import { Paragraph } from "../paragraph"; import { Table } from "../table"; import { HeaderAttributes } from "./header-attributes"; @@ -55,12 +54,4 @@ export class Header extends XmlComponent { this.addTable(table); return table; } - - public addDrawing(imageData: IMediaData): void { - const paragraph = new Paragraph(); - const run = new PictureRun(imageData); - paragraph.addRun(run); - - this.root.push(paragraph); - } } diff --git a/src/file/media/media.ts b/src/file/media/media.ts index 757e9f3115..ed081cc5e1 100644 --- a/src/file/media/media.ts +++ b/src/file/media/media.ts @@ -3,6 +3,7 @@ import * as sizeOf from "image-size"; import * as path from "path"; import { File } from "../file"; +import { Image } from "../paragraph"; import { IMediaData } from "./data"; interface IHackedFile { @@ -10,7 +11,7 @@ interface IHackedFile { } export class Media { - public static addImage(file: File, filePath: string): IMediaData { + public static addImage(file: File, filePath: string): Image { // Workaround to expose id without exposing to API const exposedFile = (file as {}) as IHackedFile; const mediaData = file.Media.addMedia(filePath, exposedFile.currentRelationshipId++); @@ -19,10 +20,10 @@ export class Media { "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", `media/${mediaData.fileName}`, ); - return mediaData; + return new Image(mediaData); } - public static addImageFromBuffer(file: File, buffer: Buffer, width?: number, height?: number): IMediaData { + public static addImageFromBuffer(file: File, buffer: Buffer, width?: number, height?: number): Image { // Workaround to expose id without exposing to API const exposedFile = (file as {}) as IHackedFile; const mediaData = file.Media.addMediaFromBuffer( @@ -37,7 +38,8 @@ export class Media { "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", `media/${mediaData.fileName}`, ); - return mediaData; + + return new Image(mediaData); } private static generateId(): string {