Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-5984 Avoid enter editing in non selectable object #5989

Merged
merged 5 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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