From a4c0f62827ac8ca3d98624d7891558ac11906c60 Mon Sep 17 00:00:00 2001 From: ShaMan123 Date: Fri, 19 May 2023 16:00:24 +0900 Subject: [PATCH] patch(): `Control#shouldActivate` --- src/controls/Control.ts | 8 ++++++++ src/shapes/Object/InteractiveObject.ts | 11 +++-------- test/unit/object_interactivity.js | 25 +++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/controls/Control.ts b/src/controls/Control.ts index 3da565648e8..6bf3544ea8f 100644 --- a/src/controls/Control.ts +++ b/src/controls/Control.ts @@ -171,6 +171,14 @@ export class Control { */ declare mouseUpHandler?: ControlActionHandler; + shouldActivate(controlKey: string, fabricObject: InteractiveFabricObject) { + // TODO: locking logic can be handled here instead of in the control handler logic + return ( + fabricObject.canvas?.getActiveObject() === fabricObject && + fabricObject.isControlVisible(controlKey) + ); + } + /** * Returns control actionHandler * @param {Event} eventData the native mouse event diff --git a/src/shapes/Object/InteractiveObject.ts b/src/shapes/Object/InteractiveObject.ts index bbee16255e7..ca0697fe2ca 100644 --- a/src/shapes/Object/InteractiveObject.ts +++ b/src/shapes/Object/InteractiveObject.ts @@ -185,11 +185,7 @@ export class InteractiveFabricObject< * @return {String} corner code (tl, tr, bl, br, etc.), or an empty string if nothing is found. */ _findTargetCorner(pointer: Point, forTouch = false): string { - if ( - !this.hasControls || - !this.canvas || - (this.canvas._activeObject as unknown as this) !== this - ) { + if (!this.hasControls || !this.canvas) { return ''; } @@ -198,10 +194,8 @@ export class InteractiveFabricObject< const cornerEntries = Object.entries(this.controlCoords); for (let i = cornerEntries.length - 1; i >= 0; i--) { const [key, coord] = cornerEntries[i]; - if (!this.isControlVisible(key)) { - continue; - } if ( + this.controls[key].shouldActivate(key, this) && PlaneBBox.build( forTouch ? coord.touchCorner : coord.corner ).containsPoint(pointer) @@ -210,6 +204,7 @@ export class InteractiveFabricObject< return key; } } + return ''; } diff --git a/test/unit/object_interactivity.js b/test/unit/object_interactivity.js index 2ddcd13d318..1bd5ba13c01 100644 --- a/test/unit/object_interactivity.js +++ b/test/unit/object_interactivity.js @@ -205,7 +205,7 @@ assert.ok(typeof cObj._findTargetCorner === 'function', '_findTargetCorner should exist'); cObj.setCoords(); cObj.canvas = { - _activeObject: cObj + getActiveObject() { return cObj } }; assert.equal(cObj._findTargetCorner(cObj.controlCoords.br.position), 'br'); assert.equal(cObj._findTargetCorner(cObj.controlCoords.tl.position), 'tl'); @@ -223,7 +223,7 @@ var cObj = new fabric.Object({ top: 10, left: 10, width: 30, height: 30, strokeWidth: 0 }); cObj.setCoords(); cObj.canvas = { - _activeObject: cObj + getActiveObject() { return cObj } }; var pointNearBr = { x: cObj.controlCoords.br.position.x + cObj.cornerSize / 3, @@ -239,6 +239,27 @@ assert.equal(cObj._findTargetCorner(pointNearBr, false), false, 'not touch event touchCornerSize/3 near br returns false'); }); + QUnit.test('_findTargetCorner for non active object', function (assert) { + var cObj = new fabric.Object({ top: 10, left: 10, width: 30, height: 30, strokeWidth: 0 }); + assert.ok(typeof cObj._findTargetCorner === 'function', '_findTargetCorner should exist'); + cObj.setCoords(); + cObj.canvas = { + getActiveObject() { return } + }; + assert.equal(cObj._findTargetCorner(cObj.oCoords.mtr), '', 'object is not active'); + }); + + QUnit.test('_findTargetCorner for non visible control', function (assert) { + var cObj = new fabric.Object({ top: 10, left: 10, width: 30, height: 30, strokeWidth: 0 }); + assert.ok(typeof cObj._findTargetCorner === 'function', '_findTargetCorner should exist'); + cObj.setCoords(); + cObj.canvas = { + getActiveObject() { return cObj } + }; + cObj.isControlVisible = () => false; + assert.equal(cObj._findTargetCorner(cObj.oCoords.mtr), '', 'object is not active'); + }); + QUnit.test.skip('_calculateCurrentDimensions', function(assert) { var cObj = new fabric.Object({ width: 10, height: 15, strokeWidth: 0 }), dim; assert.ok(typeof cObj._calculateCurrentDimensions === 'function', '_calculateCurrentDimensions should exist');