Skip to content

Commit

Permalink
fix(fabri.Canvas): Add target to each selection event (#6858)
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur authored Feb 8, 2021
1 parent ccd9954 commit 4b5577f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
53 changes: 38 additions & 15 deletions src/canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1082,38 +1082,50 @@
*/
_fireSelectionEvents: function(oldObjects, e) {
var somethingChanged = false, objects = this.getActiveObjects(),
added = [], removed = [], opt = { e: e };
added = [], removed = [];
oldObjects.forEach(function(oldObject) {
if (objects.indexOf(oldObject) === -1) {
somethingChanged = true;
oldObject.fire('deselected', opt);
oldObject.fire('deselected', {
e: e,
target: oldObject
});
removed.push(oldObject);
}
});
objects.forEach(function(object) {
if (oldObjects.indexOf(object) === -1) {
somethingChanged = true;
object.fire('selected', opt);
object.fire('selected', {
e: e,
target: object
});
added.push(object);
}
});
if (oldObjects.length > 0 && objects.length > 0) {
opt.selected = added;
opt.deselected = removed;
// added for backward compatibility
opt.updated = added[0] || removed[0];
opt.target = this._activeObject;
somethingChanged && this.fire('selection:updated', opt);
somethingChanged && this.fire('selection:updated', {
e: e,
selected: added,
deselected: removed,
// added for backward compatibility
// deprecated
updated: added[0] || removed[0],
target: this._activeObject,
});
}
else if (objects.length > 0) {
opt.selected = added;
// added for backward compatibility
opt.target = this._activeObject;
this.fire('selection:created', opt);
this.fire('selection:created', {
e: e,
selected: added,
target: this._activeObject,
});
}
else if (oldObjects.length > 0) {
opt.deselected = removed;
this.fire('selection:cleared', opt);
this.fire('selection:cleared', {
e: e,
deselected: removed,
});
}
},

Expand All @@ -1132,6 +1144,10 @@
},

/**
* This is a private method for now.
* This is supposed to be equivalent to setActiveObject but without firing
* any event. There is commitment to have this stay this way.
* This is the functional part of setActiveObject.
* @private
* @param {Object} object to set as active
* @param {Event} [e] Event (passed along when firing "object:selected")
Expand All @@ -1152,6 +1168,13 @@
},

/**
* This is a private method for now.
* This is supposed to be equivalent to discardActiveObject but without firing
* any events. There is commitment to have this stay this way.
* This is the functional part of discardActiveObject.
* @param {Event} [e] Event (passed along when firing "object:deselected")
* @param {Object} object to set as active
* @return {Boolean} true if the selection happened
* @private
*/
_discardActiveObject: function(e, object) {
Expand Down
6 changes: 5 additions & 1 deletion src/shapes/image.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@

/**
* Constructor
* @param {HTMLImageElement | String} element Image element
* Image can be initialized with any canvas drawable or a string.
* The string should be a url and will be loaded as an image.
* Canvas and Image element work out of the box, while videos require extra code to work.
* Please check video element events for seeking.
* @param {HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | String} element Image element
* @param {Object} [options] Options object
* @param {function} [callback] callback function to call after eventual filters applied.
* @return {fabric.Image} thisArg
Expand Down
5 changes: 3 additions & 2 deletions src/shapes/itext.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,9 @@
* High level function to know the color of the cursor.
* the currentChar is the one that precedes the cursor
* Returns color (fill) of char at the current cursor
* Unused from the library, is for the end user
* @return {String} Character color (fill)
* if the text object has a pattern or gradient for filler, it will return that.
* Unused by the library, is for the end user
* @return {String | fabric.Gradient | fabric.Pattern} Character color (fill)
*/
getCurrentCharColor: function() {
var cp = this._getCurrentCharIndex();
Expand Down

0 comments on commit 4b5577f

Please sign in to comment.