diff --git a/core/modules/widgets/edit-bitmap.js b/core/modules/widgets/edit-bitmap.js index 0a7ecd0ef..0e72b5df1 100644 --- a/core/modules/widgets/edit-bitmap.js +++ b/core/modules/widgets/edit-bitmap.js @@ -14,7 +14,8 @@ Edit-bitmap widget // Default image sizes var DEFAULT_IMAGE_WIDTH = 600, - DEFAULT_IMAGE_HEIGHT = 370; + DEFAULT_IMAGE_HEIGHT = 370, + DEFAULT_IMAGE_TYPE = "image/png"; // Configuration tiddlers var LINE_WIDTH_TITLE = "$:/config/BitmapEditor/LineWidth", @@ -154,7 +155,13 @@ EditBitmapWidget.prototype.loadCanvas = function() { self.refreshToolbar(); }; // Get the current bitmap into an image object - currImage.src = "data:" + tiddler.fields.type + ";base64," + tiddler.fields.text; + if(tiddler && tiddler.fields.type && tiddler.fields.text) { + currImage.src = "data:" + tiddler.fields.type + ";base64," + tiddler.fields.text; + } else { + currImage.width = DEFAULT_IMAGE_WIDTH; + currImage.height = DEFAULT_IMAGE_HEIGHT; + currImage.onerror(); + } }; EditBitmapWidget.prototype.initCanvas = function(canvas,width,height,image) { @@ -319,18 +326,16 @@ EditBitmapWidget.prototype.strokeEnd = function() { }; EditBitmapWidget.prototype.saveChanges = function() { - var tiddler = this.wiki.getTiddler(this.editTitle); - if(tiddler) { - // data URIs look like "data:;base64," - var dataURL = this.canvasDomNode.toDataURL(tiddler.fields.type), - posColon = dataURL.indexOf(":"), - posSemiColon = dataURL.indexOf(";"), - posComma = dataURL.indexOf(","), - type = dataURL.substring(posColon+1,posSemiColon), - text = dataURL.substring(posComma+1); - var update = {type: type, text: text}; - this.wiki.addTiddler(new $tw.Tiddler(this.wiki.getModificationFields(),tiddler,update,this.wiki.getCreationFields())); - } + var tiddler = this.wiki.getTiddler(this.editTitle) || new $tw.Tiddler({title: this.editTitle,type: DEFAULT_IMAGE_TYPE}); + // data URIs look like "data:;base64," + var dataURL = this.canvasDomNode.toDataURL(tiddler.fields.type), + posColon = dataURL.indexOf(":"), + posSemiColon = dataURL.indexOf(";"), + posComma = dataURL.indexOf(","), + type = dataURL.substring(posColon+1,posSemiColon), + text = dataURL.substring(posComma+1); + var update = {type: type, text: text}; + this.wiki.addTiddler(new $tw.Tiddler(this.wiki.getModificationFields(),tiddler,update,this.wiki.getCreationFields())); }; exports["edit-bitmap"] = EditBitmapWidget;