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

Added activeOn 'up/down' property #6807

Merged
merged 11 commits into from
Jan 30, 2021
26 changes: 16 additions & 10 deletions src/mixins/canvas_events.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,15 +451,21 @@
}
}
if (target) {
var corner = target._findTargetCorner(
this.getPointer(e, true),
fabric.util.isTouchEvent(e)
);
var control = target.controls[corner],
mouseUpHandler = control && control.getMouseUpHandler(e, target, control);
if (mouseUpHandler) {
var pointer = this.getPointer(e);
mouseUpHandler(e, transform, pointer.x, pointer.y);
if (target.selectable && target !== this._activeObject && target.activeOn === 'up') {
this.setActiveObject(target, e);
shouldRender = true;
}
else {
var corner = target._findTargetCorner(
this.getPointer(e, true),
fabric.util.isTouchEvent(e)
);
var control = target.controls[corner],
mouseUpHandler = control && control.getMouseUpHandler(e, target, control);
if (mouseUpHandler) {
var pointer = this.getPointer(e);
mouseUpHandler(e, transform, pointer.x, pointer.y);
}
}
target.isMoving = false;
}
Expand Down Expand Up @@ -715,7 +721,7 @@

if (target) {
var alreadySelected = target === this._activeObject;
if (target.selectable) {
if (target.selectable && target.activeOn === 'down') {
this.setActiveObject(target, e);
}
var corner = target._findTargetCorner(
Expand Down
11 changes: 11 additions & 0 deletions src/shapes/object.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,17 @@
*/
paintFirst: 'fill',

/**
* When 'down', object is set to active on mousedown/touchstart
* When 'up', object is set to active on mouseup/touchend
* Experimental. Let's see if this breaks anything before supporting officially
* @private
* since 4.4.0
* @type String
* @default 'down'
*/
activeOn: 'down',

/**
* List of properties to consider when checking if state
* of an object is changed (fabric.Object#hasStateChanged)
Expand Down
15 changes: 15 additions & 0 deletions test/unit/canvas_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,21 @@
canvas.__onMouseUp(e);
});

QUnit.test('activeOn object selection', function(assert) {
var rect = new fabric.Rect({ width: 200, height: 200, activeOn: 'down' });
canvas.add(rect);
var e = { clientX: 30, clientY: 15, which: 1, target: canvas.upperCanvasEl };
canvas.__onMouseDown(e);
assert.equal(canvas._activeObject, rect, 'with activeOn of down object is selected on mouse down');
canvas.__onMouseUp(e);
canvas.discardActiveObject();
rect.activeOn = 'up';
canvas.__onMouseDown(e);
assert.equal(canvas._activeObject, null, 'with activeOn of up object is not selected on mouse down');
canvas.__onMouseUp(e);
assert.equal(canvas._activeObject, rect, 'with activeOn of up object is selected on mouse up');
});

QUnit.test('specific bug #5317 for shift+click and active selection', function(assert) {
var greenRect = new fabric.Rect({
width: 300,
Expand Down