@@ -19,13 +19,9 @@ export type CropBox = {
19
19
*/
20
20
type EditorCallbacks = {
21
21
onCropBoxChange : Set < ( crop : Readonly < CropBox > ) => void > ;
22
- onCropReset : Set < ( ) => void > ;
23
22
onZoomChange : Set < ( zoom : number ) => void > ;
24
- onImageLoad : Set < ( ) => void > ;
25
23
} ;
26
24
27
- type SetElement < T > = T extends Set < infer U > ? U : never ;
28
-
29
25
/**
30
26
* Crop box resize handle names.
31
27
*/
@@ -183,9 +179,7 @@ export class Editor {
183
179
184
180
private callbacks : EditorCallbacks = {
185
181
onCropBoxChange : new Set ( ) ,
186
- onCropReset : new Set ( ) ,
187
182
onZoomChange : new Set ( ) ,
188
- onImageLoad : new Set ( ) ,
189
183
} ;
190
184
191
185
private cropBox : CropBox | null = null ;
@@ -603,7 +597,9 @@ export class Editor {
603
597
this . updateKonvaCropInteractionRect ( ) ;
604
598
this . updateKonvaCropInteractionGuides ( ) ;
605
599
this . updateKonvaCropInteractionHandlePositions ( ) ;
606
- this . _invokeCallbacks ( 'onCropBoxChange' , cropBox ) ;
600
+ for ( const cb of this . callbacks . onCropBoxChange ) {
601
+ cb ( cropBox ) ;
602
+ }
607
603
} ;
608
604
609
605
/**
@@ -1007,7 +1003,9 @@ export class Editor {
1007
1003
// Update handle scaling to maintain constant screen size
1008
1004
this . updateKonvaCropInteractionHandleScales ( ) ;
1009
1005
this . updateKonvaBg ( ) ;
1010
- this . _invokeCallbacks ( 'onZoomChange' , newScale ) ;
1006
+ for ( const cb of this . callbacks . onZoomChange ) {
1007
+ cb ( newScale ) ;
1008
+ }
1011
1009
} ;
1012
1010
1013
1011
/**
@@ -1103,7 +1101,6 @@ export class Editor {
1103
1101
img . onload = ( ) => {
1104
1102
this . originalImage = img ;
1105
1103
this . updateImage ( initial ) ;
1106
- this . _invokeCallbacks ( 'onImageLoad' ) ;
1107
1104
resolve ( ) ;
1108
1105
} ;
1109
1106
@@ -1119,14 +1116,14 @@ export class Editor {
1119
1116
* Reset the crop box to encompass the entire image.
1120
1117
*/
1121
1118
resetCrop = ( ) => {
1122
- if ( this . konva ?. image . image ) {
1123
- this . updateCropBox ( {
1124
- x : 0 ,
1125
- y : 0 ,
1126
- ...this . konva . image . image . size ( ) ,
1127
- } ) ;
1119
+ if ( ! this . konva ?. image . image ) {
1120
+ return ;
1128
1121
}
1129
- this . _invokeCallbacks ( 'onCropReset' ) ;
1122
+ this . updateCropBox ( {
1123
+ x : 0 ,
1124
+ y : 0 ,
1125
+ ...this . konva . image . image . size ( ) ,
1126
+ } ) ;
1130
1127
} ;
1131
1128
1132
1129
/**
@@ -1276,7 +1273,9 @@ export class Editor {
1276
1273
1277
1274
this . updateKonvaBg ( ) ;
1278
1275
1279
- this . _invokeCallbacks ( 'onZoomChange' , scale ) ;
1276
+ for ( const cb of this . callbacks . onZoomChange ) {
1277
+ cb ( scale ) ;
1278
+ }
1280
1279
} ;
1281
1280
1282
1281
/**
@@ -1330,7 +1329,9 @@ export class Editor {
1330
1329
1331
1330
this . updateKonvaBg ( ) ;
1332
1331
1333
- this . _invokeCallbacks ( 'onZoomChange' , 1 ) ;
1332
+ for ( const cb of this . callbacks . onZoomChange ) {
1333
+ cb ( 1 ) ;
1334
+ }
1334
1335
} ;
1335
1336
1336
1337
/**
@@ -1366,7 +1367,9 @@ export class Editor {
1366
1367
1367
1368
this . updateKonvaBg ( ) ;
1368
1369
1369
- this . _invokeCallbacks ( 'onZoomChange' , scale ) ;
1370
+ for ( const cb of this . callbacks . onZoomChange ) {
1371
+ cb ( scale ) ;
1372
+ }
1370
1373
} ;
1371
1374
1372
1375
/**
@@ -1476,56 +1479,24 @@ export class Editor {
1476
1479
} ;
1477
1480
1478
1481
/**
1479
- * Helper to build a callback registrar function for a specific event name.
1480
- * @param name The callback event name.
1482
+ * Register a callback for when the crop box changes (moved or resized).
1481
1483
*/
1482
- _buildCallbackRegistrar = < T extends keyof EditorCallbacks > ( name : T ) => {
1483
- return ( cb : SetElement < EditorCallbacks [ T ] > ) : ( ( ) => void ) => {
1484
- ( this . callbacks [ name ] as Set < typeof cb > ) . add ( cb ) ;
1485
- return ( ) => {
1486
- ( this . callbacks [ name ] as Set < typeof cb > ) . delete ( cb ) ;
1487
- } ;
1484
+ onCropBoxChange = ( cb : ( crop : Readonly < CropBox > ) => void ) : ( ( ) => void ) => {
1485
+ this . callbacks . onCropBoxChange . add ( cb ) ;
1486
+ return ( ) => {
1487
+ this . callbacks . onCropBoxChange . delete ( cb ) ;
1488
1488
} ;
1489
1489
} ;
1490
1490
1491
- /**
1492
- * Invoke all callbacks registered for a specific event.
1493
- * @param name The callback event name.
1494
- * @param args The arguments to pass to each callback.
1495
- */
1496
- private _invokeCallbacks = < T extends keyof EditorCallbacks > (
1497
- name : T ,
1498
- ...args : EditorCallbacks [ T ] extends Set < ( ...args : infer P ) => void > ? P : never
1499
- ) : void => {
1500
- const callbacks = this . callbacks [ name ] ;
1501
- if ( callbacks && callbacks . size > 0 ) {
1502
- callbacks . forEach ( ( cb ) => {
1503
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1504
- ( cb as ( ...args : any [ ] ) => void ) ( ...args ) ;
1505
- } ) ;
1506
- }
1507
- } ;
1508
-
1509
- /**
1510
- * Register a callback for when the crop is reset.
1511
- */
1512
- onCropReset = this . _buildCallbackRegistrar ( 'onCropReset' ) ;
1513
-
1514
- /**
1515
- * Register a callback for when the crop box changes (moved or resized).
1516
- */
1517
- onCropBoxChange = this . _buildCallbackRegistrar ( 'onCropBoxChange' ) ;
1518
-
1519
- /**
1520
- * Register a callback for when a new image is loaded.
1521
- */
1522
- onImageLoad = this . _buildCallbackRegistrar ( 'onImageLoad' ) ;
1523
-
1524
1491
/**
1525
1492
* Register a callback for when the zoom level changes.
1526
1493
*/
1527
- onZoomChange = this . _buildCallbackRegistrar ( 'onZoomChange' ) ;
1528
-
1494
+ onZoomChange = ( cb : ( zoom : number ) => void ) : ( ( ) => void ) => {
1495
+ this . callbacks . onZoomChange . add ( cb ) ;
1496
+ return ( ) => {
1497
+ this . callbacks . onZoomChange . delete ( cb ) ;
1498
+ } ;
1499
+ } ;
1529
1500
/**
1530
1501
* Resize the editor container and adjust the Konva stage accordingly.
1531
1502
*
0 commit comments