From 6d0ad48224390eabc59deafeb0953c2b12eb9f5e Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Sun, 28 Jan 2018 12:51:51 +0100 Subject: [PATCH] safeguard canvas (#4650) --- src/mixins/itext_click_behavior.mixin.js | 48 ++++++++++++++---------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/mixins/itext_click_behavior.mixin.js b/src/mixins/itext_click_behavior.mixin.js index 739e091253e..cad56c43338 100644 --- a/src/mixins/itext_click_behavior.mixin.js +++ b/src/mixins/itext_click_behavior.mixin.js @@ -68,31 +68,39 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot }, /** - * Initializes "mousedown" event handler + * Default event handler for the basic functionalities needed on _mouseDown + * can be overridden to do something different. + * Scope of this implementation is: find the click position, set selectionStart + * find selectionEnd, initialize the drawing of either cursor or selection area */ - initMousedownHandler: function() { - this.on('mousedown', function(options) { - if (!this.editable || (options.e.button && options.e.button !== 1)) { - return; - } - var pointer = this.canvas.getPointer(options.e); + _mouseDownHandler: function(options) { + if (!this.canvas || !this.editable || (options.e.button && options.e.button !== 1)) { + return; + } + var pointer = this.canvas.getPointer(options.e); - this.__mousedownX = pointer.x; - this.__mousedownY = pointer.y; - this.__isMousedown = true; + this.__mousedownX = pointer.x; + this.__mousedownY = pointer.y; + this.__isMousedown = true; - if (this.selected) { - this.setCursorByClick(options.e); - } + if (this.selected) { + this.setCursorByClick(options.e); + } - if (this.isEditing) { - this.__selectionStartOnMouseDown = this.selectionStart; - if (this.selectionStart === this.selectionEnd) { - this.abortCursorAnimation(); - } - this.renderCursorOrSelection(); + if (this.isEditing) { + this.__selectionStartOnMouseDown = this.selectionStart; + if (this.selectionStart === this.selectionEnd) { + this.abortCursorAnimation(); } - }); + this.renderCursorOrSelection(); + } + }, + + /** + * Initializes "mousedown" event handler + */ + initMousedownHandler: function() { + this.on('mousedown', this._mouseDownHandler); }, /**