From 86601848e9202d6dc2ff4fd5aa91b9ab545d1f80 Mon Sep 17 00:00:00 2001 From: ShaMan123 Date: Fri, 15 Sep 2023 21:39:59 +0530 Subject: [PATCH] only if changed --- src/canvas/SelectableCanvas.ts | 5 ++++- src/shapes/ActiveSelection.spec.ts | 11 ++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/canvas/SelectableCanvas.ts b/src/canvas/SelectableCanvas.ts index ad498c65ba6..ffad76257e5 100644 --- a/src/canvas/SelectableCanvas.ts +++ b/src/canvas/SelectableCanvas.ts @@ -1104,7 +1104,10 @@ export class SelectableCanvas return false; } this._activeObject = object; - if (object instanceof ActiveSelection) { + + const prevActiveSelection = this._activeSelection; + if (object instanceof ActiveSelection && prevActiveSelection !== object) { + prevActiveSelection.dispose(); this._activeSelection = object; this._activeSelection.set('canvas', this); this._activeSelection.setCoords(); diff --git a/src/shapes/ActiveSelection.spec.ts b/src/shapes/ActiveSelection.spec.ts index ee5a9588fba..d6f74bea367 100644 --- a/src/shapes/ActiveSelection.spec.ts +++ b/src/shapes/ActiveSelection.spec.ts @@ -88,18 +88,27 @@ describe('ActiveSelection', () => { expect(canvas.getActiveSelection().aCoords).toMatchSnapshot(); }); - it('`setActiveObject` should update the active selection ref on canvas', () => { + it('`setActiveObject` should update the active selection ref on canvas if it changed', () => { const canvas = new Canvas(null); const obj1 = new FabricObject(); const obj2 = new FabricObject(); canvas.add(obj1, obj2); + const existingActiveSelection = canvas.getActiveSelection(); + const disposeSpy = jest.spyOn(existingActiveSelection, 'dispose'); const activeSelection = new ActiveSelection([obj1, obj2]); const spy = jest.spyOn(activeSelection, 'setCoords'); canvas.setActiveObject(activeSelection); expect(canvas.getActiveSelection()).toBe(activeSelection); expect(canvas.getActiveObjects()).toEqual([obj1, obj2]); + expect(disposeSpy).toHaveBeenCalled(); expect(spy).toHaveBeenCalled(); expect(activeSelection.canvas).toBe(canvas); + + spy.mockClear(); + const disposeSpy2 = jest.spyOn(activeSelection, 'dispose'); + canvas.setActiveObject(activeSelection); + expect(disposeSpy2).not.toHaveBeenCalled(); + expect(spy).not.toHaveBeenCalled(); }); it('transferring an object between active selections keeps its owning group', () => {