Skip to content

Commit

Permalink
fire deselected on activeObject switch. (#3689)
Browse files Browse the repository at this point in the history
* misc changes

* fire the selection on target swap

* manag exitEditing without events

* onDeselect

* onDeselect
  • Loading branch information
asturur authored Feb 13, 2017
1 parent f2b3cc8 commit 91c851a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 46 deletions.
69 changes: 45 additions & 24 deletions src/canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1350,8 +1350,12 @@
* @param {Object} object
*/
_setActiveObject: function(object) {
if (this._activeObject) {
this._activeObject.set('active', false);
var obj = this._activeObject;
if (obj) {
obj.set('active', false);
if (object !== obj && obj.onDeselect && typeof obj.onDeselect === 'function') {
obj.onDeselect();
}
}
this._activeObject = object;
object.set('active', true);
Expand All @@ -1365,6 +1369,10 @@
* @chainable
*/
setActiveObject: function (object, e) {
var currentActiveObject = this.getActiveObject();
if (currentActiveObject && currentActiveObject !== object) {
currentActiveObject.fire('deselected', { e: e });
}
this._setActiveObject(object);
this.renderAll();
this.fire('object:selected', { target: object, e: e });
Expand Down Expand Up @@ -1402,24 +1410,33 @@
* @private
*/
_discardActiveObject: function() {
if (this._activeObject) {
this._activeObject.set('active', false);
var obj = this._activeObject;
if (obj) {
obj.set('active', false);
if (obj.onDeselect && typeof obj.onDeselect === 'function') {
obj.onDeselect();
}
}
this._activeObject = null;
},

/**
* Discards currently active object and fire events
* Discards currently active object and fire events. If the function is called by fabric
* as a consequence of a mouse event, the event is passed as a parmater and
* sent to the fire function for the custom events. When used as a method the
* e param does not have any application.
* @param {event} e
* @return {fabric.Canvas} thisArg
* @chainable
*/
discardActiveObject: function (e) {
var activeObject = this._activeObject;
this.fire('before:selection:cleared', { target: activeObject, e: e });
this._discardActiveObject();
this.fire('selection:cleared', { e: e });
activeObject && activeObject.fire('deselected', { e: e });
if (activeObject) {
this.fire('before:selection:cleared', { target: activeObject, e: e });
this._discardActiveObject();
this.fire('selection:cleared', { e: e });
activeObject.fire('deselected', { e: e });
}
return this;
},

Expand All @@ -1435,7 +1452,10 @@
},

/**
* Sets active group to a specified one
* Sets active group to a specified one. If the function is called by fabric
* as a consequence of a mouse event, the event is passed as a parmater and
* sent to the fire function for the custom events. When used as a method the
* e param does not have any application.
* @param {fabric.Group} group Group to set as a current one
* @param {Event} e Event object
* @return {fabric.Canvas} thisArg
Expand Down Expand Up @@ -1470,15 +1490,20 @@
},

/**
* Discards currently active group and fire events
* Discards currently active group and fire events If the function is called by fabric
* as a consequence of a mouse event, the event is passed as a parmater and
* sent to the fire function for the custom events. When used as a method the
* e param does not have any application.
* @return {fabric.Canvas} thisArg
* @chainable
*/
discardActiveGroup: function (e) {
var g = this.getActiveGroup();
this.fire('before:selection:cleared', { e: e, target: g });
this._discardActiveGroup();
this.fire('selection:cleared', { e: e });
if (g) {
this.fire('before:selection:cleared', { e: e, target: g });
this._discardActiveGroup();
this.fire('selection:cleared', { e: e });
}
return this;
},

Expand All @@ -1502,21 +1527,17 @@
},

/**
* Deactivates all objects and dispatches appropriate events
* Deactivates all objects and dispatches appropriate events If the function is called by fabric
* as a consequence of a mouse event, the event is passed as a parmater and
* sent to the fire function for the custom events. When used as a method the
* e param does not have any application.
* @return {fabric.Canvas} thisArg
* @chainable
*/
deactivateAllWithDispatch: function (e) {
var activeGroup = this.getActiveGroup(),
activeObject = this.getActiveObject();
if (activeObject || activeGroup) {
this.fire('before:selection:cleared', { target: activeObject || activeGroup, e: e });
}
this.discardActiveGroup(e);
this.discardActiveObject(e);
this.deactivateAll();
if (activeObject || activeGroup) {
this.fire('selection:cleared', { e: e, target: activeObject });
activeObject && activeObject.fire('deselected');
}
return this;
},

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

/**
* Initializes "selected" event handler
*/
initSelectedHandler: function() {
this.on('selected', function() {

var _this = this;
setTimeout(function() {
_this.selected = true;
}, 100);
});
onDeselect: function() {
this.isEditing && this.exitEditing();
this.selected = false;
},

/**
Expand Down Expand Up @@ -66,18 +58,13 @@
* @private
*/
_initCanvasHandlers: function(canvas) {
canvas._canvasITextSelectionClearedHanlder = (function() {
fabric.IText.prototype.exitEditingOnOthers(canvas);
}).bind(this);
canvas._mouseUpITextHandler = (function() {
if (canvas._iTextInstances) {
canvas._iTextInstances.forEach(function(obj) {
obj.__isMousedown = false;
});
}
}).bind(this);
canvas.on('selection:cleared', canvas._canvasITextSelectionClearedHanlder);
canvas.on('object:selected', canvas._canvasITextSelectionClearedHanlder);
canvas.on('mouse:up', canvas._mouseUpITextHandler);
},

Expand All @@ -86,8 +73,6 @@
* @private
*/
_removeCanvasHandlers: function(canvas) {
canvas.off('selection:cleared', canvas._canvasITextSelectionClearedHanlder);
canvas.off('object:selected', canvas._canvasITextSelectionClearedHanlder);
canvas.off('mouse:up', canvas._mouseUpITextHandler);
},

Expand Down Expand Up @@ -581,7 +566,6 @@
this.canvas.fire('text:editing:exited', { target: this });
isTextChanged && this.canvas.fire('object:modified', { target: this });
}

return this;
},

Expand Down
1 change: 0 additions & 1 deletion src/mixins/itext_click_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
* Initializes event handlers related to cursor or selection
*/
initCursorSelectionHandlers: function() {
this.initSelectedHandler();
this.initMousedownHandler();
this.initMouseupHandler();
this.initClicks();
Expand Down
2 changes: 0 additions & 2 deletions src/static_canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,8 +783,6 @@
this.backgroundColor = '';
this.overlayColor = '';
if (this._hasITextHandlers) {
this.off('selection:cleared', this._canvasITextSelectionClearedHanlder);
this.off('object:selected', this._canvasITextSelectionClearedHanlder);
this.off('mouse:up', this._mouseUpITextHandler);
this._iTextInstances = null;
this._hasITextHandlers = false;
Expand Down

0 comments on commit 91c851a

Please sign in to comment.