Skip to content

Commit

Permalink
ISSUE-5984 Avoid enter editing in non selectable object (fabricjs#5989)
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur authored and Hristo Chakarov committed Jul 27, 2021
1 parent 2110043 commit 6764a0f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/mixins/itext_click_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},

/**
Expand Down Expand Up @@ -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;
},

/**
Expand Down
59 changes: 59 additions & 0 deletions test/unit/itext_click_behaviour.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6764a0f

Please sign in to comment.