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

Fix 'before:selection:cleared' event in order to receive as target the whole activeSelection if present #5658

Merged
merged 3 commits into from
Apr 28, 2019
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
4 changes: 2 additions & 2 deletions src/canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1588,9 +1588,9 @@
* @chainable
*/
discardActiveObject: function (e) {
var currentActives = this.getActiveObjects();
var currentActives = this.getActiveObjects(), activeObject = this.getActiveObject();
if (currentActives.length) {
this.fire('before:selection:cleared', { target: currentActives[0], e: e });
this.fire('before:selection:cleared', { target: activeObject, e: e });
}
this._discardActiveObject(e);
this._fireSelectionEvents(currentActives, e);
Expand Down
28 changes: 25 additions & 3 deletions test/unit/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@
afterEach: function() {
canvas.viewportTransform = [1, 0, 0, 1, 0, 0];
canvas.clear();
canvas.cancelRequestedRender();
canvas.backgroundColor = fabric.Canvas.prototype.backgroundColor;
canvas.overlayColor = fabric.Canvas.prototype.overlayColor;
canvas._collectObjects = fabric.Canvas.prototype._collectObjects;
canvas.off();
canvas.calcOffset();
canvas.cancelRequestedRender();
upperCanvasEl.style.display = 'none';
}
});
Expand Down Expand Up @@ -295,6 +297,25 @@
assert.equal(isFired, true, 'removing active object should fire "before:selection:cleared"');
});

QUnit.test('before:selection:cleared gets target the active object', function(assert) {
var passedTarget;
canvas.on('before:selection:cleared', function(options) {
passedTarget = options.target;
});
var rect = new fabric.Rect();
canvas.add(rect);
canvas.setActiveObject(rect);
canvas.discardActiveObject();
assert.equal(passedTarget, rect, 'options.target was the removed object');
var rect1 = new fabric.Rect();
var rect2 = new fabric.Rect();
canvas.add(rect1, rect2);
var activeSelection = new fabric.ActiveSelection([rect1, rect2], { canvas: canvas });
canvas.setActiveObject(activeSelection);
canvas.discardActiveObject();
assert.equal(passedTarget, activeSelection, 'removing an activeSelection pass that as a target');
});

QUnit.test('selection:cleared', function(assert) {
var isFired = false;
canvas.on('selection:cleared', function( ) { isFired = true; });
Expand Down Expand Up @@ -2043,7 +2064,7 @@
assert.equal(parentEl.firstChild, el, 'canvas should be appended at partentEl');
assert.equal(parentEl.childNodes.length, 1, 'parentEl has 1 child only');

var canvas = new fabric.Canvas(el, {enableRetinaScaling: false });
var canvas = new fabric.Canvas(el, {enableRetinaScaling: false, renderOnAddRemove: false });
wrapperEl = canvas.wrapperEl;
lowerCanvasEl = canvas.lowerCanvasEl;
upperCanvasEl = canvas.upperCanvasEl;
Expand All @@ -2065,6 +2086,7 @@
assert.ok(typeof canvas.dispose === 'function');
canvas.add(makeRect(), makeRect(), makeRect());
canvas.dispose();
canvas.cancelRequestedRender();
assert.equal(canvas.getObjects().length, 0, 'dispose should clear canvas');
assert.equal(parentEl.childNodes.length, 1, 'parent has always 1 child');
if (!fabric.isLikelyNode) {
Expand Down Expand Up @@ -2134,7 +2156,7 @@

assert.equal(canvas.getWidth(), clone.getWidth());
assert.equal(canvas.getHeight(), clone.getHeight());

clone.renderAll();
done();
});
});
Expand All @@ -2153,7 +2175,7 @@

assert.equal(canvas.getWidth(), clone.getWidth());
assert.equal(canvas.getHeight(), clone.getHeight());

clone.renderAll();
done();
});
});
Expand Down