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

Use the new mousedown:before to understand if an object is already selected on mousedown #5010

Merged
merged 2 commits into from
May 30, 2018
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
1 change: 0 additions & 1 deletion src/mixins/canvas_grouping.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@
this.setCursor(this.defaultCursor);
// clear selection and current transformation
this._groupSelector = null;
this._currentTransform = null;
}
});

Expand Down
7 changes: 3 additions & 4 deletions src/mixins/itext_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
this.mouseMoveHandler = this.mouseMoveHandler.bind(this);
},

onDeselect: function(options) {
onDeselect: function() {
this.isEditing && this.exitEditing();
this.selected = false;
fabric.Object.prototype.onDeselect.call(this, options);
},

/**
Expand Down Expand Up @@ -59,13 +58,13 @@
* @private
*/
_initCanvasHandlers: function(canvas) {
canvas._mouseUpITextHandler = (function() {
canvas._mouseUpITextHandler = function() {
if (canvas._iTextInstances) {
canvas._iTextInstances.forEach(function(obj) {
obj.__isMousedown = false;
});
}
}).bind(this);
};
canvas.on('mouse:up', canvas._mouseUpITextHandler);
},

Expand Down
35 changes: 19 additions & 16 deletions src/mixins/itext_click_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot

this.__lastPointer = { };

this.on('mousedown', this.onMouseDown.bind(this));
this.on('mousedown', this.onMouseDown);
},

/**
Expand All @@ -24,7 +24,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
return;
}
this.__newClickTime = +new Date();
var newPointer = this.canvas.getPointer(options.e);
var newPointer = options.pointer;
if (this.isTripleClick(newPointer)) {
this.fire('tripleclick', options);
this._stopEvent(options.e);
Expand Down Expand Up @@ -82,10 +82,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
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) {
Expand All @@ -102,21 +99,25 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
},

/**
* Initializes "mousedown" event handler
* Default event handler for the basic functionalities needed on mousedown:before
* can be overridden to do something different.
* Scope of this implementation is: verify the object is already selected when mousing down
*/
initMousedownHandler: function() {
this.on('mousedown', this._mouseDownHandler);
_mouseDownHandlerBefore: function(options) {
if (!this.canvas || !this.editable || (options.e.button && options.e.button !== 1)) {
return;
}
if (this === this.canvas._activeObject) {
this.selected = true;
}
},

/**
* detect if object moved
* @private
* Initializes "mousedown" event handler
*/
_isObjectMoved: function(e) {
var pointer = this.canvas.getPointer(e);

return this.__mousedownX !== pointer.x ||
this.__mousedownY !== pointer.y;
initMousedownHandler: function() {
this.on('mousedown', this._mouseDownHandler);
this.on('mousedown:before', this._mouseDownHandlerBefore);
},

/**
Expand All @@ -132,7 +133,9 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
*/
mouseUpHandler: function(options) {
this.__isMousedown = false;
if (!this.editable || this._isObjectMoved(options.e) || (options.e.button && options.e.button !== 1)) {
if (!this.editable ||
(options.transform && options.transform.actionPerformed) ||
(options.e.button && options.e.button !== 1)) {
return;
}

Expand Down
7 changes: 0 additions & 7 deletions src/mixins/object_origin.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,6 @@
_getLeftTopCoords: function() {
return this.translateToOriginPoint(this.getCenterPoint(), 'left', 'top');
},

/**
* Callback; invoked right before object is about to go from active to inactive
*/
onDeselect: function() {
/* NOOP */
}
});

})();
9 changes: 9 additions & 0 deletions test/unit/itext_click_behaviour.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,13 @@
var selection = iText._getNewSelectionStartFromOffset({ y: 1, x: 1000 }, 500, 520, index, jlen);
assert.equal(selection, index, 'index value was NOT moved to next char, since is already at end of text');
});
QUnit.test('_mouseDownHandlerBefore set up selected property', function(assert) {
var iText = new fabric.IText('test need some word\nsecond line');
assert.equal(iText.selected, undefined, 'iText has no selected property');
iText.canvas = {
_activeObject: iText,
};
iText._mouseDownHandlerBefore({ e: {} });
assert.equal(iText.selected, true, 'iText has selected property');
});
})();