@@ -4,14 +4,6 @@ import Konva from 'konva';
4
4
import type { KonvaEventObject } from 'konva/lib/Node' ;
5
5
import { objectEntries } from 'tsafe' ;
6
6
7
- type CropConstraints = {
8
- minWidth ?: number ;
9
- minHeight ?: number ;
10
- maxWidth ?: number ;
11
- maxHeight ?: number ;
12
- aspectRatio ?: number ;
13
- } ;
14
-
15
7
export type CropBox = {
16
8
x : number ;
17
9
y : number ;
@@ -119,10 +111,8 @@ export class Editor {
119
111
private isCropping = false ;
120
112
private config : EditorConfig = DEFAULT_CONFIG ;
121
113
122
- private cropConstraints : CropConstraints = {
123
- minWidth : this . config . MIN_CROP_DIMENSION ,
124
- minHeight : this . config . MIN_CROP_DIMENSION ,
125
- } ;
114
+ private aspectRatio : number | null = null ;
115
+
126
116
private callbacks : EditorCallbacks = { } ;
127
117
private cropBox : CropBox | null = null ;
128
118
@@ -718,18 +708,10 @@ export class Editor {
718
708
return ;
719
709
}
720
710
721
- let { newX, newY, newWidth, newHeight } = this . cropConstraints . aspectRatio
711
+ let { newX, newY, newWidth, newHeight } = this . aspectRatio
722
712
? this . _resizeCropBoxWithAspectRatio ( handleName , handleRect )
723
713
: this . _resizeCropBoxFree ( handleName , handleRect ) ;
724
714
725
- // Apply general constraints
726
- if ( this . cropConstraints . maxWidth ) {
727
- newWidth = Math . min ( newWidth , this . cropConstraints . maxWidth ) ;
728
- }
729
- if ( this . cropConstraints . maxHeight ) {
730
- newHeight = Math . min ( newHeight , this . cropConstraints . maxHeight ) ;
731
- }
732
-
733
715
this . updateCropBox ( {
734
716
x : newX ,
735
717
y : newY ,
@@ -754,8 +736,8 @@ export class Editor {
754
736
const handleX = handleRect . x ( ) + handleRect . width ( ) / 2 ;
755
737
const handleY = handleRect . y ( ) + handleRect . height ( ) / 2 ;
756
738
757
- const minWidth = this . cropConstraints . minWidth ?? this . config . MIN_CROP_DIMENSION ;
758
- const minHeight = this . cropConstraints . minHeight ?? this . config . MIN_CROP_DIMENSION ;
739
+ const minWidth = this . config . MIN_CROP_DIMENSION ;
740
+ const minHeight = this . config . MIN_CROP_DIMENSION ;
759
741
760
742
// Update dimensions based on handle type
761
743
if ( handleName . includes ( 'left' ) ) {
@@ -779,18 +761,18 @@ export class Editor {
779
761
} ;
780
762
781
763
private _resizeCropBoxWithAspectRatio = ( handleName : HandleName , handleRect : Konva . Rect ) => {
782
- if ( ! this . konva ?. image . image || ! this . cropConstraints . aspectRatio || ! this . cropBox ) {
764
+ if ( ! this . konva ?. image . image || ! this . aspectRatio || ! this . cropBox ) {
783
765
throw new Error ( 'Crop box, image, or aspect ratio not found' ) ;
784
766
}
785
767
const imgWidth = this . konva . image . image . width ( ) ;
786
768
const imgHeight = this . konva . image . image . height ( ) ;
787
- const ratio = this . cropConstraints . aspectRatio ;
769
+ const ratio = this . aspectRatio ;
788
770
789
771
const handleX = handleRect . x ( ) + handleRect . width ( ) / 2 ;
790
772
const handleY = handleRect . y ( ) + handleRect . height ( ) / 2 ;
791
773
792
- const minWidth = this . cropConstraints . minWidth ?? this . config . MIN_CROP_DIMENSION ;
793
- const minHeight = this . cropConstraints . minHeight ?? this . config . MIN_CROP_DIMENSION ;
774
+ const minWidth = this . config . MIN_CROP_DIMENSION ;
775
+ const minHeight = this . config . MIN_CROP_DIMENSION ;
794
776
795
777
// Early boundary check for aspect ratio mode
796
778
const atLeftEdge = this . cropBox . x <= 0 ;
@@ -1218,9 +1200,9 @@ export class Editor {
1218
1200
}
1219
1201
} ;
1220
1202
1221
- setCropAspectRatio = ( ratio : number | undefined ) => {
1203
+ setCropAspectRatio = ( ratio : number | null ) => {
1222
1204
// Update the constraint
1223
- this . cropConstraints . aspectRatio = ratio ;
1205
+ this . aspectRatio = ratio ;
1224
1206
1225
1207
if ( ! this . konva ?. image . image || ! this . cropBox ) {
1226
1208
return ;
@@ -1230,7 +1212,7 @@ export class Editor {
1230
1212
const currentHeight = this . cropBox . height ;
1231
1213
const currentArea = currentWidth * currentHeight ;
1232
1214
1233
- if ( ratio === undefined ) {
1215
+ if ( ratio === null ) {
1234
1216
// Just removed the aspect ratio constraint, no need to adjust
1235
1217
return ;
1236
1218
}
@@ -1258,8 +1240,8 @@ export class Editor {
1258
1240
}
1259
1241
1260
1242
// Apply minimum size constraints
1261
- const minWidth = this . cropConstraints . minWidth ?? this . config . MIN_CROP_DIMENSION ;
1262
- const minHeight = this . cropConstraints . minHeight ?? this . config . MIN_CROP_DIMENSION ;
1243
+ const minWidth = this . config . MIN_CROP_DIMENSION ;
1244
+ const minHeight = this . config . MIN_CROP_DIMENSION ;
1263
1245
1264
1246
if ( newWidth < minWidth ) {
1265
1247
newWidth = minWidth ;
@@ -1289,8 +1271,8 @@ export class Editor {
1289
1271
} ) ;
1290
1272
} ;
1291
1273
1292
- getCropAspectRatio = ( ) : number | undefined => {
1293
- return this . cropConstraints . aspectRatio ;
1274
+ getCropAspectRatio = ( ) : number | null => {
1275
+ return this . aspectRatio ;
1294
1276
} ;
1295
1277
1296
1278
// Utility
0 commit comments