From 6764a0fbb680ab924b2d02dc4c0ac32c6dc625c6 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Fri, 22 Nov 2019 22:43:09 +0900 Subject: [PATCH] ISSUE-5984 Avoid enter editing in non selectable object (#5989) --- src/mixins/itext_click_behavior.mixin.js | 28 +++++++---- test/unit/itext_click_behaviour.js | 59 ++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 9 deletions(-) diff --git a/src/mixins/itext_click_behavior.mixin.js b/src/mixins/itext_click_behavior.mixin.js index c2a4f25846f..ca93f1df3b1 100644 --- a/src/mixins/itext_click_behavior.mixin.js +++ b/src/mixins/itext_click_behavior.mixin.js @@ -60,16 +60,26 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot this.initClicks(); }, + /** + * Default handler for double click, select a word + */ + doubleClickHandler: function(options) { + this.selectWord(this.getSelectionStartFromPointer(options.e)); + }, + + /** + * Default handler for triple click, select a line + */ + tripleClickHandler: function(options) { + this.selectLine(this.getSelectionStartFromPointer(options.e)); + }, + /** * Initializes double and triple click event handlers */ initClicks: function() { - this.on('mousedblclick', function(options) { - this.selectWord(this.getSelectionStartFromPointer(options.e)); - }); - this.on('tripleclick', function(options) { - this.selectLine(this.getSelectionStartFromPointer(options.e)); - }); + this.on('mousedblclick', this.doubleClickHandler); + this.on('tripleclick', this.tripleClickHandler); }, /** @@ -107,9 +117,9 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot if (!this.canvas || !this.editable || (options.e.button && options.e.button !== 1)) { return; } - if (this === this.canvas._activeObject) { - this.selected = true; - } + // we want to avoid that an object that was selected and then becomes unselectable, + // may trigger editing mode in some way. + this.selected = this === this.canvas._activeObject; }, /** diff --git a/test/unit/itext_click_behaviour.js b/test/unit/itext_click_behaviour.js index e111f1ebd1d..b78eff0bb0c 100644 --- a/test/unit/itext_click_behaviour.js +++ b/test/unit/itext_click_behaviour.js @@ -15,6 +15,65 @@ var selection = iText._getNewSelectionStartFromOffset({ y: 1, x: 1000 }, 500, 520, index, jlen); assert.equal(selection, index, 'index value did not change'); }); + QUnit.test('doubleClickHandler', function(assert) { + var iText = new fabric.IText('test need some word\nsecond line'); + iText.canvas = canvas; + var eventData = { + which: 1, + target: canvas.upperCanvasEl, + clientX: 40, + clientY: 10 + }; + iText.doubleClickHandler({ + e: eventData + }); + assert.equal(iText.selectionStart, 0, 'dblClcik selection start is'); + assert.equal(iText.selectionEnd, 4, 'dblClcik selection end is'); + var eventData = { + which: 1, + target: canvas.upperCanvasEl, + clientX: 40, + clientY: 60 + }; + iText.doubleClickHandler({ + e: eventData + }); + assert.equal(iText.selectionStart, 19, 'second dblClcik selection start is'); + assert.equal(iText.selectionEnd, 26, 'second dblClcik selection end is'); + }); + QUnit.test('tripleClickHandler', function(assert) { + var iText = new fabric.IText('test need some word\nsecond line'); + iText.canvas = canvas; + var eventData = { + which: 1, + target: canvas.upperCanvasEl, + clientX: 40, + clientY: 10 + }; + iText.tripleClickHandler({ + e: eventData + }); + assert.equal(iText.selectionStart, 0, 'tripleClick selection start is'); + assert.equal(iText.selectionEnd, 19, 'tripleClick selection end is'); + var eventData = { + which: 1, + target: canvas.upperCanvasEl, + clientX: 40, + clientY: 60 + }; + iText.tripleClickHandler({ + e: eventData + }); + assert.equal(iText.selectionStart, 20, 'second tripleClick selection start is'); + assert.equal(iText.selectionEnd, 31, 'second tripleClick selection end is'); + }); + QUnit.test('_getNewSelectionStartFromOffset end of line', function(assert) { + var iText = new fabric.IText('test need some word\nsecond line'); + var index = 10; + var jlen = 20; + var selection = iText._getNewSelectionStartFromOffset({ y: 1, x: 1000 }, 500, 520, index, jlen); + assert.equal(selection, index, 'index value did not change'); + }); QUnit.test('_getNewSelectionStartFromOffset middle of line', function(assert) { var iText = new fabric.IText('test need some word\nsecond line'); var index = 10;