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 | 52 ++++++++++++++---------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/src/mixins/itext_click_behavior.mixin.js b/src/mixins/itext_click_behavior.mixin.js index 739e0912..cad56c43 100644 --- a/src/mixins/itext_click_behavior.mixin.js +++ b/src/mixins/itext_click_behavior.mixin.js @@ -67,32 +67,40 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot }); }, + /** + * 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 + */ + _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; + + if (this.selected) { + this.setCursorByClick(options.e); + } + + 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', function(options) { - if (!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; - - if (this.selected) { - this.setCursorByClick(options.e); - } - - if (this.isEditing) { - this.__selectionStartOnMouseDown = this.selectionStart; - if (this.selectionStart === this.selectionEnd) { - this.abortCursorAnimation(); - } - this.renderCursorOrSelection(); - } - }); + this.on('mousedown', this._mouseDownHandler); }, /**