1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 03:57:21 +00:00

Fix edit-bitmap crash with missing tiddlers

Fixes #4141
This commit is contained in:
Jeremy Ruston 2019-08-02 09:16:36 +01:00
parent 9a2de11db1
commit 394725f00c

View File

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