From af48af285495e0b21ebd3b9870980194f68f912d Mon Sep 17 00:00:00 2001 From: Dolan Date: Wed, 29 Mar 2017 22:57:52 +0100 Subject: [PATCH] added error checking and updated demo --- demo/demo.js | 2 +- ts/docx/run/run-components/drawing/index.ts | 4 ++++ ts/media/index.ts | 10 ++++++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/demo/demo.js b/demo/demo.js index 4545fce7fe..fd1958a724 100644 --- a/demo/demo.js +++ b/demo/demo.js @@ -12,7 +12,7 @@ doc.addParagraph(paragraph); var media = new docx.Media(); media.addMedia("happy-penguins", "./demo/penguins.jpg"); -// var pictureRun = new docx.PictureRun(""); +var pictureRun = new docx.PictureRun(media.getMedia("happy-penguins")); var exporter = new docx.LocalPacker(doc, undefined, undefined, undefined, media); exporter.pack('My Document'); diff --git a/ts/docx/run/run-components/drawing/index.ts b/ts/docx/run/run-components/drawing/index.ts index 3b1d187f0d..fab434e058 100644 --- a/ts/docx/run/run-components/drawing/index.ts +++ b/ts/docx/run/run-components/drawing/index.ts @@ -7,6 +7,10 @@ export class Drawing extends XmlComponent { constructor(imageData: IData) { super("w:drawing"); + if (imageData === undefined) { + throw new Error("imageData cannot be undefined"); + } + this.root.push(new Inline(imageData.referenceId)); } } diff --git a/ts/media/index.ts b/ts/media/index.ts index 7f6ef61b7e..66cddca4ad 100644 --- a/ts/media/index.ts +++ b/ts/media/index.ts @@ -9,8 +9,14 @@ export class Media { this.map = new Map(); } - public getMedia(key: string): IData | undefined { - return this.map.get(key); + public getMedia(key: string): IData { + const data = this.map.get(key); + + if (data === undefined) { + throw new Error(`Cannot find image with the key ${key}`); + } + + return data; } public addMedia(key: string, filePath: string): void {