Skip to content

Commit 6db1b1d

Browse files
authored
Use the new mousedown:before to understand if an object is already selected on mousedown (#5010)
* fix something * other changes
1 parent b77957f commit 6db1b1d

File tree

5 files changed

+31
-28
lines changed

5 files changed

+31
-28
lines changed

src/mixins/canvas_grouping.mixin.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@
161161
this.setCursor(this.defaultCursor);
162162
// clear selection and current transformation
163163
this._groupSelector = null;
164-
this._currentTransform = null;
165164
}
166165
});
167166

src/mixins/itext_behavior.mixin.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
this.mouseMoveHandler = this.mouseMoveHandler.bind(this);
1616
},
1717

18-
onDeselect: function(options) {
18+
onDeselect: function() {
1919
this.isEditing && this.exitEditing();
2020
this.selected = false;
21-
fabric.Object.prototype.onDeselect.call(this, options);
2221
},
2322

2423
/**
@@ -59,13 +58,13 @@
5958
* @private
6059
*/
6160
_initCanvasHandlers: function(canvas) {
62-
canvas._mouseUpITextHandler = (function() {
61+
canvas._mouseUpITextHandler = function() {
6362
if (canvas._iTextInstances) {
6463
canvas._iTextInstances.forEach(function(obj) {
6564
obj.__isMousedown = false;
6665
});
6766
}
68-
}).bind(this);
67+
};
6968
canvas.on('mouse:up', canvas._mouseUpITextHandler);
7069
},
7170

src/mixins/itext_click_behavior.mixin.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
1212

1313
this.__lastPointer = { };
1414

15-
this.on('mousedown', this.onMouseDown.bind(this));
15+
this.on('mousedown', this.onMouseDown);
1616
},
1717

1818
/**
@@ -24,7 +24,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
2424
return;
2525
}
2626
this.__newClickTime = +new Date();
27-
var newPointer = this.canvas.getPointer(options.e);
27+
var newPointer = options.pointer;
2828
if (this.isTripleClick(newPointer)) {
2929
this.fire('tripleclick', options);
3030
this._stopEvent(options.e);
@@ -82,10 +82,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
8282
if (!this.canvas || !this.editable || (options.e.button && options.e.button !== 1)) {
8383
return;
8484
}
85-
var pointer = this.canvas.getPointer(options.e);
8685

87-
this.__mousedownX = pointer.x;
88-
this.__mousedownY = pointer.y;
8986
this.__isMousedown = true;
9087

9188
if (this.selected) {
@@ -102,21 +99,25 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
10299
},
103100

104101
/**
105-
* Initializes "mousedown" event handler
102+
* Default event handler for the basic functionalities needed on mousedown:before
103+
* can be overridden to do something different.
104+
* Scope of this implementation is: verify the object is already selected when mousing down
106105
*/
107-
initMousedownHandler: function() {
108-
this.on('mousedown', this._mouseDownHandler);
106+
_mouseDownHandlerBefore: function(options) {
107+
if (!this.canvas || !this.editable || (options.e.button && options.e.button !== 1)) {
108+
return;
109+
}
110+
if (this === this.canvas._activeObject) {
111+
this.selected = true;
112+
}
109113
},
110114

111115
/**
112-
* detect if object moved
113-
* @private
116+
* Initializes "mousedown" event handler
114117
*/
115-
_isObjectMoved: function(e) {
116-
var pointer = this.canvas.getPointer(e);
117-
118-
return this.__mousedownX !== pointer.x ||
119-
this.__mousedownY !== pointer.y;
118+
initMousedownHandler: function() {
119+
this.on('mousedown', this._mouseDownHandler);
120+
this.on('mousedown:before', this._mouseDownHandlerBefore);
120121
},
121122

122123
/**
@@ -132,7 +133,9 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
132133
*/
133134
mouseUpHandler: function(options) {
134135
this.__isMousedown = false;
135-
if (!this.editable || this._isObjectMoved(options.e) || (options.e.button && options.e.button !== 1)) {
136+
if (!this.editable ||
137+
(options.transform && options.transform.actionPerformed) ||
138+
(options.e.button && options.e.button !== 1)) {
136139
return;
137140
}
138141

src/mixins/object_origin.mixin.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,6 @@
250250
_getLeftTopCoords: function() {
251251
return this.translateToOriginPoint(this.getCenterPoint(), 'left', 'top');
252252
},
253-
254-
/**
255-
* Callback; invoked right before object is about to go from active to inactive
256-
*/
257-
onDeselect: function() {
258-
/* NOOP */
259-
}
260253
});
261254

262255
})();

test/unit/itext_click_behaviour.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,13 @@
2727
var selection = iText._getNewSelectionStartFromOffset({ y: 1, x: 1000 }, 500, 520, index, jlen);
2828
assert.equal(selection, index, 'index value was NOT moved to next char, since is already at end of text');
2929
});
30+
QUnit.test('_mouseDownHandlerBefore set up selected property', function(assert) {
31+
var iText = new fabric.IText('test need some word\nsecond line');
32+
assert.equal(iText.selected, undefined, 'iText has no selected property');
33+
iText.canvas = {
34+
_activeObject: iText,
35+
};
36+
iText._mouseDownHandlerBefore({ e: {} });
37+
assert.equal(iText.selected, true, 'iText has selected property');
38+
});
3039
})();

0 commit comments

Comments
 (0)