Skip to content

Commit 9d3d2a3

Browse files
tidy(ui): editor listeners
1 parent ed23104 commit 9d3d2a3

File tree

1 file changed

+21
-33
lines changed
  • invokeai/frontend/web/src/features/editImageModal/lib

1 file changed

+21
-33
lines changed

invokeai/frontend/web/src/features/editImageModal/lib/editor.ts

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export class Editor {
121121
private lastPointerPosition: { x: number; y: number } | null = null;
122122
private isSpacePressed = false;
123123

124-
private subscriptions: Set<() => void> = new Set();
124+
private cleanupFunctions: Set<() => void> = new Set();
125125

126126
init = (container: HTMLDivElement, config?: Partial<EditorConfig>) => {
127127
this.config = { ...this.config, ...config };
@@ -147,7 +147,7 @@ export class Editor {
147147
crop,
148148
};
149149

150-
this.setupStageEvents();
150+
this.setupListeners();
151151
};
152152

153153
private createKonvaBgObjects = (): KonvaObjects['bg'] => {
@@ -507,41 +507,31 @@ export class Editor {
507507
};
508508

509509
//#region Event Handling
510-
private setupStageEvents = () => {
510+
private setupListeners = () => {
511511
if (!this.konva) {
512512
return;
513513
}
514514
const stage = this.konva.stage;
515515

516-
stage.container().addEventListener('wheel', this.onWheel, { passive: false });
517-
this.subscriptions.add(() => {
518-
stage.container().removeEventListener('wheel', this.onWheel);
519-
});
520-
stage.container().addEventListener('contextmenu', this.onContextMenu);
521-
this.subscriptions.add(() => {
522-
stage.container().removeEventListener('contextmenu', this.onContextMenu);
523-
});
524-
516+
stage.on('wheel', this.onWheel);
517+
stage.on('contextmenu', this.onContextMenu);
525518
stage.on('pointerdown', this.onPointerDown);
526-
this.subscriptions.add(() => {
527-
stage.off('pointerdown', this.onPointerDown);
528-
});
529519
stage.on('pointerup', this.onPointerUp);
530-
this.subscriptions.add(() => {
531-
stage.off('pointerup', this.onPointerUp);
532-
});
533520
stage.on('pointermove', this.onPointerMove);
534-
this.subscriptions.add(() => {
521+
522+
this.cleanupFunctions.add(() => {
523+
stage.off('wheel', this.onWheel);
524+
stage.off('contextmenu', this.onContextMenu);
525+
stage.off('pointerdown', this.onPointerDown);
526+
stage.off('pointerup', this.onPointerUp);
535527
stage.off('pointermove', this.onPointerMove);
536528
});
537529

538530
window.addEventListener('keydown', this.onKeyDown);
539-
this.subscriptions.add(() => {
540-
window.removeEventListener('keydown', this.onKeyDown);
541-
});
542-
543531
window.addEventListener('keyup', this.onKeyUp);
544-
this.subscriptions.add(() => {
532+
533+
this.cleanupFunctions.add(() => {
534+
window.removeEventListener('keydown', this.onKeyDown);
545535
window.removeEventListener('keyup', this.onKeyUp);
546536
});
547537
};
@@ -559,11 +549,11 @@ export class Editor {
559549
};
560550

561551
// Zoom with mouse wheel
562-
private onWheel = (e: WheelEvent) => {
552+
private onWheel = (e: KonvaEventObject<WheelEvent>) => {
563553
if (!this.konva?.stage) {
564554
return;
565555
}
566-
e.preventDefault();
556+
e.evt.preventDefault();
567557

568558
const oldScale = this.konva.stage.scaleX();
569559
const pointer = this.konva.stage.getPointerPosition();
@@ -577,7 +567,7 @@ export class Editor {
577567
y: (pointer.y - this.konva.stage.y()) / oldScale,
578568
};
579569

580-
const direction = e.deltaY > 0 ? -1 : 1;
570+
const direction = e.evt.deltaY > 0 ? -1 : 1;
581571
const scaleBy = this.config.ZOOM_WHEEL_FACTOR;
582572
let newScale = direction > 0 ? oldScale * scaleBy : oldScale / scaleBy;
583573

@@ -671,9 +661,9 @@ export class Editor {
671661
}
672662
};
673663

674-
private onContextMenu = (e: MouseEvent) => {
664+
private onContextMenu = (e: KonvaEventObject<MouseEvent>) => {
675665
// Prevent context menu on right-click
676-
e.preventDefault();
666+
e.evt.preventDefault();
677667
};
678668
//#endregion
679669

@@ -1288,17 +1278,15 @@ export class Editor {
12881278
};
12891279

12901280
destroy = () => {
1291-
for (const unsubscribe of this.subscriptions) {
1292-
unsubscribe();
1281+
for (const cleanup of this.cleanupFunctions) {
1282+
cleanup();
12931283
}
12941284

12951285
// Cancel any ongoing crop operation
12961286
if (this.isCropping) {
12971287
this.cancelCrop();
12981288
}
12991289

1300-
// Remove all Konva event listeners by destroying the stage
1301-
// This automatically removes all Konva event handlers
13021290
this.konva?.stage.destroy();
13031291

13041292
// Clear all references

0 commit comments

Comments
 (0)