Skip to content

Commit

Permalink
only if changed
Browse files Browse the repository at this point in the history
Update SelectableCanvas.ts
  • Loading branch information
ShaMan123 committed Sep 15, 2023
1 parent 8f67b96 commit 5fc9171
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/canvas/SelectableCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,9 @@ export class SelectableCanvas<EventSpec extends CanvasEvents = CanvasEvents>
return false;
}
this._activeObject = object;
if (object instanceof ActiveSelection) {

if (object instanceof ActiveSelection && this._activeSelection !== object) {
this._activeSelection.dispose();
this._activeSelection = object;
this._activeSelection.set('canvas', this);
this._activeSelection.setCoords();
Expand Down
11 changes: 10 additions & 1 deletion src/shapes/ActiveSelection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 5fc9171

Please sign in to comment.