added error checking and updated demo

This commit is contained in:
Dolan
2017-03-29 22:57:52 +01:00
parent 15009884e3
commit af48af2854
3 changed files with 13 additions and 3 deletions

View File

@ -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');

View File

@ -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));
}
}

View File

@ -9,8 +9,14 @@ export class Media {
this.map = new Map<string, IData>();
}
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 {