Skip to content

Commit

Permalink
Stop text from firing mouse up and mouse down on non left clicks (#3888)
Browse files Browse the repository at this point in the history
* fix right click mouse up

* block mouse up/down for text on non left click

* fixes also deactivteAllWithDispatch
  • Loading branch information
asturur authored Apr 26, 2017
1 parent ca95fb5 commit 6139af3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
9 changes: 8 additions & 1 deletion src/canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1545,9 +1545,16 @@
* @chainable
*/
deactivateAllWithDispatch: function (e) {
var allObjects = this.getObjects(),
i = 0,
len = allObjects.length,
obj;
for ( ; i < len; i++) {
obj = allObjects[i];
obj && obj.set('active', false);
}
this.discardActiveGroup(e);
this.discardActiveObject(e);
this.deactivateAll();
return this;
},

Expand Down
46 changes: 34 additions & 12 deletions src/mixins/canvas_events.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
tl: 7 // nw
},
addListener = fabric.util.addListener,
removeListener = fabric.util.removeListener;
removeListener = fabric.util.removeListener,
RIGHT_CLICK = 3, MIDDLE_CLICK = 2, LEFT_CLICK = 1;

function checkClick(e, value) {
return 'which' in e ? e.which === value : e.button === value - 1;
}

fabric.util.object.extend(fabric.Canvas.prototype, /** @lends fabric.Canvas.prototype */ {

Expand Down Expand Up @@ -303,15 +308,33 @@
* @param {Event} e Event object fired on mouseup
*/
__onMouseUp: function (e) {
var target, searchTarget = true, transform = this._currentTransform,
groupSelector = this._groupSelector,
isClick = (!groupSelector || (groupSelector.left === 0 && groupSelector.top === 0));

var target;
// if right/middle click just fire events and return
// target undefined will make the _handleEvent search the target
if (checkClick(e, RIGHT_CLICK)) {
if (this.fireRightClick) {
this._handleEvent(e, 'up', target, RIGHT_CLICK);
}
return;
}

if (checkClick(e, MIDDLE_CLICK)) {
if (this.fireMiddleClick) {
this._handleEvent(e, 'up', target, MIDDLE_CLICK);
}
return;
}

if (this.isDrawingMode && this._isCurrentlyDrawing) {
this._onMouseUpInDrawingMode(e);
return;
}

var searchTarget = true, transform = this._currentTransform,
groupSelector = this._groupSelector,
isClick = (!groupSelector || (groupSelector.left === 0 && groupSelector.top === 0));

if (transform) {
this._finalizeCurrentTransform();
searchTarget = !transform.actionPerformed;
Expand Down Expand Up @@ -352,15 +375,16 @@
},

/**
* @private
* Handle event firing for target and subtargets
* @param {Event} e event from mouse
* @param {String} eventType event to fire (up, down or move)
* @param {fabric.Object} targetObj receiving event
*/
_handleEvent: function(e, eventType, targetObj) {
_handleEvent: function(e, eventType, targetObj, button) {
var target = typeof targetObj === 'undefined' ? this.findTarget(e) : targetObj,
targets = this.targets || [],
options = { e: e, target: target, subTargets: targets };
options = { e: e, target: target, subTargets: targets, button: button || LEFT_CLICK };
this.fire('mouse:' + eventType, options);
target && target.fire('mouse' + eventType, options);
for (var i = 0; i < targets.length; i++) {
Expand Down Expand Up @@ -466,18 +490,16 @@
var target = this.findTarget(e);

// if right click just fire events
var isRightClick = 'which' in e ? e.which === 3 : e.button === 2;
if (isRightClick) {
if (checkClick(e, RIGHT_CLICK)) {
if (this.fireRightClick) {
this._handleEvent(e, 'down', target ? target : null);
this._handleEvent(e, 'down', target ? target : null, RIGHT_CLICK);
}
return;
}

var isMiddleClick = 'which' in e ? e.which === 2 : e.button === 1;
if (isMiddleClick) {
if (checkClick(e, MIDDLE_CLICK)) {
if (this.fireMiddleClick) {
this._handleEvent(e, 'down', target ? target : null);
this._handleEvent(e, 'down', target ? target : null, MIDDLE_CLICK);
}
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/mixins/itext_click_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
this.__newClickTime = +new Date();
var newPointer = this.canvas.getPointer(options.e);

if (this.isTripleClick(newPointer)) {
if (this.isTripleClick(newPointer, options.e)) {
this.fire('tripleclick', options);
this._stopEvent(options.e);
}
Expand Down Expand Up @@ -83,7 +83,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
*/
initMousedownHandler: function() {
this.on('mousedown', function(options) {
if (!this.editable) {
if (!this.editable || (options.e.button && options.e.button !== 1)) {
return;
}
var pointer = this.canvas.getPointer(options.e);
Expand Down Expand Up @@ -122,7 +122,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
initMouseupHandler: function() {
this.on('mouseup', function(options) {
this.__isMousedown = false;
if (!this.editable || this._isObjectMoved(options.e)) {
if (!this.editable || this._isObjectMoved(options.e) || (options.e.button && options.e.button !== 1)) {
return;
}

Expand Down

0 comments on commit 6139af3

Please sign in to comment.