@@ -121,7 +121,7 @@ export class Editor {
121
121
private lastPointerPosition : { x : number ; y : number } | null = null ;
122
122
private isSpacePressed = false ;
123
123
124
- private subscriptions : Set < ( ) => void > = new Set ( ) ;
124
+ private cleanupFunctions : Set < ( ) => void > = new Set ( ) ;
125
125
126
126
init = ( container : HTMLDivElement , config ?: Partial < EditorConfig > ) => {
127
127
this . config = { ...this . config , ...config } ;
@@ -147,7 +147,7 @@ export class Editor {
147
147
crop,
148
148
} ;
149
149
150
- this . setupStageEvents ( ) ;
150
+ this . setupListeners ( ) ;
151
151
} ;
152
152
153
153
private createKonvaBgObjects = ( ) : KonvaObjects [ 'bg' ] => {
@@ -507,41 +507,31 @@ export class Editor {
507
507
} ;
508
508
509
509
//#region Event Handling
510
- private setupStageEvents = ( ) => {
510
+ private setupListeners = ( ) => {
511
511
if ( ! this . konva ) {
512
512
return ;
513
513
}
514
514
const stage = this . konva . stage ;
515
515
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 ) ;
525
518
stage . on ( 'pointerdown' , this . onPointerDown ) ;
526
- this . subscriptions . add ( ( ) => {
527
- stage . off ( 'pointerdown' , this . onPointerDown ) ;
528
- } ) ;
529
519
stage . on ( 'pointerup' , this . onPointerUp ) ;
530
- this . subscriptions . add ( ( ) => {
531
- stage . off ( 'pointerup' , this . onPointerUp ) ;
532
- } ) ;
533
520
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 ) ;
535
527
stage . off ( 'pointermove' , this . onPointerMove ) ;
536
528
} ) ;
537
529
538
530
window . addEventListener ( 'keydown' , this . onKeyDown ) ;
539
- this . subscriptions . add ( ( ) => {
540
- window . removeEventListener ( 'keydown' , this . onKeyDown ) ;
541
- } ) ;
542
-
543
531
window . addEventListener ( 'keyup' , this . onKeyUp ) ;
544
- this . subscriptions . add ( ( ) => {
532
+
533
+ this . cleanupFunctions . add ( ( ) => {
534
+ window . removeEventListener ( 'keydown' , this . onKeyDown ) ;
545
535
window . removeEventListener ( 'keyup' , this . onKeyUp ) ;
546
536
} ) ;
547
537
} ;
@@ -559,11 +549,11 @@ export class Editor {
559
549
} ;
560
550
561
551
// Zoom with mouse wheel
562
- private onWheel = ( e : WheelEvent ) => {
552
+ private onWheel = ( e : KonvaEventObject < WheelEvent > ) => {
563
553
if ( ! this . konva ?. stage ) {
564
554
return ;
565
555
}
566
- e . preventDefault ( ) ;
556
+ e . evt . preventDefault ( ) ;
567
557
568
558
const oldScale = this . konva . stage . scaleX ( ) ;
569
559
const pointer = this . konva . stage . getPointerPosition ( ) ;
@@ -577,7 +567,7 @@ export class Editor {
577
567
y : ( pointer . y - this . konva . stage . y ( ) ) / oldScale ,
578
568
} ;
579
569
580
- const direction = e . deltaY > 0 ? - 1 : 1 ;
570
+ const direction = e . evt . deltaY > 0 ? - 1 : 1 ;
581
571
const scaleBy = this . config . ZOOM_WHEEL_FACTOR ;
582
572
let newScale = direction > 0 ? oldScale * scaleBy : oldScale / scaleBy ;
583
573
@@ -671,9 +661,9 @@ export class Editor {
671
661
}
672
662
} ;
673
663
674
- private onContextMenu = ( e : MouseEvent ) => {
664
+ private onContextMenu = ( e : KonvaEventObject < MouseEvent > ) => {
675
665
// Prevent context menu on right-click
676
- e . preventDefault ( ) ;
666
+ e . evt . preventDefault ( ) ;
677
667
} ;
678
668
//#endregion
679
669
@@ -1288,17 +1278,15 @@ export class Editor {
1288
1278
} ;
1289
1279
1290
1280
destroy = ( ) => {
1291
- for ( const unsubscribe of this . subscriptions ) {
1292
- unsubscribe ( ) ;
1281
+ for ( const cleanup of this . cleanupFunctions ) {
1282
+ cleanup ( ) ;
1293
1283
}
1294
1284
1295
1285
// Cancel any ongoing crop operation
1296
1286
if ( this . isCropping ) {
1297
1287
this . cancelCrop ( ) ;
1298
1288
}
1299
1289
1300
- // Remove all Konva event listeners by destroying the stage
1301
- // This automatically removes all Konva event handlers
1302
1290
this . konva ?. stage . destroy ( ) ;
1303
1291
1304
1292
// Clear all references
0 commit comments