@wordpress/media-editor 0.11.1 → 0.11.2-next.v.202606191442.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/build/components/media-editor/index.cjs.map +1 -1
  2. package/build/components/media-editor/use-crop-options.cjs.map +1 -1
  3. package/build/components/media-editor-canvas/index.cjs +63 -15
  4. package/build/components/media-editor-canvas/index.cjs.map +3 -3
  5. package/build/components/media-editor-fine-rotation/index.cjs.map +1 -1
  6. package/build/components/media-editor-image-controls/index.cjs +2 -2
  7. package/build/components/media-editor-image-controls/index.cjs.map +2 -2
  8. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +4 -4
  9. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +2 -2
  10. package/build/components/media-editor-modal/build-modifiers.cjs.map +1 -1
  11. package/build/components/rotation-ruler/index.cjs +1 -1
  12. package/build/components/rotation-ruler/index.cjs.map +1 -1
  13. package/build/components/rotation-ruler/use-ruler-drag.cjs.map +1 -1
  14. package/build/image-editor/core/camera.cjs +13 -0
  15. package/build/image-editor/core/camera.cjs.map +2 -2
  16. package/build/image-editor/core/constants.cjs +12 -3
  17. package/build/image-editor/core/constants.cjs.map +2 -2
  18. package/build/image-editor/core/containment.cjs.map +1 -1
  19. package/build/image-editor/core/crop-rect.cjs.map +1 -1
  20. package/build/image-editor/core/export/canvas-renderer.cjs.map +1 -1
  21. package/build/image-editor/core/interaction-controller.cjs +9 -7
  22. package/build/image-editor/core/interaction-controller.cjs.map +2 -2
  23. package/build/image-editor/core/math/sanitize.cjs.map +1 -1
  24. package/build/image-editor/core/setter-helpers.cjs.map +1 -1
  25. package/build/image-editor/core/source-region.cjs +111 -2
  26. package/build/image-editor/core/source-region.cjs.map +2 -2
  27. package/build/image-editor/core/state.cjs.map +1 -1
  28. package/build/image-editor/core/stencil-math.cjs +3 -4
  29. package/build/image-editor/core/stencil-math.cjs.map +2 -2
  30. package/build/image-editor/core/transform-style.cjs.map +1 -1
  31. package/build/image-editor/core/types.cjs.map +1 -1
  32. package/build/image-editor/react/components/cropper.cjs +232 -55
  33. package/build/image-editor/react/components/cropper.cjs.map +2 -2
  34. package/build/image-editor/react/components/overlays/dimensions-overlay.cjs.map +1 -1
  35. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +1 -0
  36. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +2 -2
  37. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +65 -19
  38. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
  39. package/build/image-editor/react/hooks/build-cropper-setters.cjs.map +1 -1
  40. package/build/image-editor/react/hooks/use-cropper-reducer.cjs +1 -1
  41. package/build/image-editor/react/hooks/use-cropper-reducer.cjs.map +2 -2
  42. package/build/image-editor/react/hooks/use-interaction.cjs.map +1 -1
  43. package/build/image-editor/react/hooks/use-viewport-state.cjs.map +1 -1
  44. package/build/index.cjs.map +1 -1
  45. package/build/state/types.cjs.map +1 -1
  46. package/build/state/use-media-editor-state.cjs +1 -1
  47. package/build/state/use-media-editor-state.cjs.map +2 -2
  48. package/build-module/components/media-editor/index.mjs.map +1 -1
  49. package/build-module/components/media-editor/use-crop-options.mjs.map +1 -1
  50. package/build-module/components/media-editor-canvas/index.mjs +55 -17
  51. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  52. package/build-module/components/media-editor-fine-rotation/index.mjs.map +1 -1
  53. package/build-module/components/media-editor-image-controls/index.mjs +2 -2
  54. package/build-module/components/media-editor-image-controls/index.mjs.map +2 -2
  55. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +4 -4
  56. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +2 -2
  57. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +1 -1
  58. package/build-module/components/rotation-ruler/index.mjs +1 -1
  59. package/build-module/components/rotation-ruler/index.mjs.map +1 -1
  60. package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +1 -1
  61. package/build-module/image-editor/core/camera.mjs +12 -0
  62. package/build-module/image-editor/core/camera.mjs.map +2 -2
  63. package/build-module/image-editor/core/constants.mjs +8 -2
  64. package/build-module/image-editor/core/constants.mjs.map +2 -2
  65. package/build-module/image-editor/core/containment.mjs.map +1 -1
  66. package/build-module/image-editor/core/crop-rect.mjs.map +1 -1
  67. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +1 -1
  68. package/build-module/image-editor/core/interaction-controller.mjs +9 -7
  69. package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
  70. package/build-module/image-editor/core/math/sanitize.mjs.map +1 -1
  71. package/build-module/image-editor/core/setter-helpers.mjs.map +1 -1
  72. package/build-module/image-editor/core/source-region.mjs +108 -1
  73. package/build-module/image-editor/core/source-region.mjs.map +2 -2
  74. package/build-module/image-editor/core/state.mjs.map +1 -1
  75. package/build-module/image-editor/core/stencil-math.mjs +3 -4
  76. package/build-module/image-editor/core/stencil-math.mjs.map +2 -2
  77. package/build-module/image-editor/core/transform-style.mjs.map +1 -1
  78. package/build-module/image-editor/react/components/cropper.mjs +242 -57
  79. package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
  80. package/build-module/image-editor/react/components/overlays/dimensions-overlay.mjs.map +1 -1
  81. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +1 -0
  82. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +2 -2
  83. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +65 -19
  84. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
  85. package/build-module/image-editor/react/hooks/build-cropper-setters.mjs.map +1 -1
  86. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs +1 -1
  87. package/build-module/image-editor/react/hooks/use-cropper-reducer.mjs.map +2 -2
  88. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +1 -1
  89. package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +1 -1
  90. package/build-module/index.mjs.map +1 -1
  91. package/build-module/state/types.mjs.map +1 -1
  92. package/build-module/state/use-media-editor-state.mjs +1 -1
  93. package/build-module/state/use-media-editor-state.mjs.map +2 -2
  94. package/build-style/style-rtl.css +49 -6
  95. package/build-style/style.css +49 -6
  96. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  97. package/build-types/image-editor/core/camera.d.ts +25 -1
  98. package/build-types/image-editor/core/camera.d.ts.map +1 -1
  99. package/build-types/image-editor/core/constants.d.ts +20 -1
  100. package/build-types/image-editor/core/constants.d.ts.map +1 -1
  101. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
  102. package/build-types/image-editor/core/source-region.d.ts +23 -1
  103. package/build-types/image-editor/core/source-region.d.ts.map +1 -1
  104. package/build-types/image-editor/core/stencil-math.d.ts +10 -5
  105. package/build-types/image-editor/core/stencil-math.d.ts.map +1 -1
  106. package/build-types/image-editor/core/types.d.ts +14 -0
  107. package/build-types/image-editor/core/types.d.ts.map +1 -1
  108. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  109. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -1
  110. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +17 -14
  111. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  112. package/package.json +25 -20
  113. package/src/components/media-editor/style.scss +21 -9
  114. package/src/components/media-editor-canvas/index.tsx +82 -13
  115. package/src/components/media-editor-canvas/style.scss +41 -0
  116. package/src/image-editor/core/camera.ts +60 -1
  117. package/src/image-editor/core/constants.ts +23 -1
  118. package/src/image-editor/core/interaction-controller.ts +25 -11
  119. package/src/image-editor/core/source-region.ts +202 -1
  120. package/src/image-editor/core/stencil-math.ts +23 -14
  121. package/src/image-editor/core/test/camera.ts +272 -1
  122. package/src/image-editor/core/test/interaction-controller.ts +100 -0
  123. package/src/image-editor/core/test/stencil-math.ts +120 -6
  124. package/src/image-editor/core/types.ts +17 -0
  125. package/src/image-editor/react/components/cropper.tsx +362 -72
  126. package/src/image-editor/react/components/overlays/dimming-overlay.tsx +1 -0
  127. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +101 -32
  128. package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +169 -17
  129. package/src/image-editor/react/components/test/cropper.tsx +689 -6
  130. package/tsconfig.tsbuildinfo +1 -1
  131. package/build-types/components/media-editor-toolbar/index.d.ts +0 -24
  132. package/build-types/components/media-editor-toolbar/index.d.ts.map +0 -1
  133. package/build-types/components/media-editor-transform-controls/index.d.ts +0 -21
  134. package/build-types/components/media-editor-transform-controls/index.d.ts.map +0 -1
  135. package/build-types/components/media-form/sidebar-datetime-view.d.ts +0 -10
  136. package/build-types/components/media-form/sidebar-datetime-view.d.ts.map +0 -1
  137. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +0 -108
  138. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +0 -1
@@ -6,6 +6,7 @@ import {
6
6
  createExportCamera,
7
7
  getImageFit,
8
8
  getRotatedBBox,
9
+ getViewScale,
9
10
  } from '../camera';
10
11
  import {
11
12
  restrictPanZoom,
@@ -13,7 +14,12 @@ import {
13
14
  getImageCropBounds,
14
15
  getMinZoom,
15
16
  } from '../containment';
16
- import { getSourceRegion, getSourceRegionPercent } from '../source-region';
17
+ import {
18
+ getSourceRegion,
19
+ getSourceRegionPercent,
20
+ snapCropRectToSourcePixelGrid,
21
+ snapCropRectToSourcePixels,
22
+ } from '../source-region';
17
23
  import { computeTransformStyle } from '../transform-style';
18
24
  import { DEFAULT_STATE, MAX_ZOOM, MIN_ZOOM } from '../constants';
19
25
  import type { CropperState, Size } from '../types';
@@ -576,6 +582,114 @@ describe( 'getSourceRegion', () => {
576
582
  } );
577
583
  } );
578
584
 
585
+ describe( 'snapCropRectToSourcePixels', () => {
586
+ const getSourceEdges = ( state: CropperState ) => {
587
+ const region = getSourceRegion( state, IMAGE );
588
+ return {
589
+ left: region.x,
590
+ top: region.y,
591
+ right: region.x + region.width,
592
+ bottom: region.y + region.height,
593
+ };
594
+ };
595
+
596
+ it( 'snaps the selected source-region edges to whole pixels under pan and zoom', () => {
597
+ const state = makeState( {
598
+ zoom: 2.25,
599
+ pan: { x: 0.013, y: -0.017 },
600
+ cropRect: { x: 0.12, y: 0.18, width: 0.33, height: 0.41 },
601
+ } );
602
+ const candidate = { x: 0.123, y: 0.187, width: 0.337, height: 0.419 };
603
+
604
+ const unsnapped = getSourceRegion(
605
+ { ...state, cropRect: candidate },
606
+ IMAGE
607
+ );
608
+ expect(
609
+ Math.abs(
610
+ unsnapped.x +
611
+ unsnapped.width -
612
+ Math.round( unsnapped.x + unsnapped.width )
613
+ )
614
+ ).toBeGreaterThan( 0.01 );
615
+ expect(
616
+ Math.abs(
617
+ unsnapped.y +
618
+ unsnapped.height -
619
+ Math.round( unsnapped.y + unsnapped.height )
620
+ )
621
+ ).toBeGreaterThan( 0.01 );
622
+
623
+ const snappedCropRect = snapCropRectToSourcePixels(
624
+ state,
625
+ IMAGE,
626
+ candidate,
627
+ 'se'
628
+ );
629
+
630
+ const snapped = getSourceEdges( {
631
+ ...state,
632
+ cropRect: snappedCropRect,
633
+ } );
634
+ expect( snapped.left ).toBeCloseTo( unsnapped.x, 3 );
635
+ expect( snapped.top ).toBeCloseTo( unsnapped.y, 3 );
636
+ expect( snapped.right ).toBeCloseTo( Math.round( snapped.right ), 3 );
637
+ expect( snapped.bottom ).toBeCloseTo( Math.round( snapped.bottom ), 3 );
638
+ } );
639
+
640
+ it( 'snaps all source-region edges to whole pixels', () => {
641
+ const state = makeState( {
642
+ rotation: 30,
643
+ zoom: 2.25,
644
+ pan: { x: 0.013, y: -0.017 },
645
+ cropRect: { x: 0.12, y: 0.18, width: 0.33, height: 0.41 },
646
+ } );
647
+ const candidate = { x: 0.123, y: 0.187, width: 0.337, height: 0.419 };
648
+
649
+ const snappedCropRect = snapCropRectToSourcePixelGrid(
650
+ state,
651
+ IMAGE,
652
+ candidate
653
+ );
654
+ const snapped = getSourceEdges( {
655
+ ...state,
656
+ cropRect: snappedCropRect,
657
+ } );
658
+
659
+ expect( snapped.left ).toBeCloseTo( Math.round( snapped.left ), 3 );
660
+ expect( snapped.top ).toBeCloseTo( Math.round( snapped.top ), 3 );
661
+ expect( snapped.right ).toBeCloseTo( Math.round( snapped.right ), 3 );
662
+ expect( snapped.bottom ).toBeCloseTo( Math.round( snapped.bottom ), 3 );
663
+ } );
664
+
665
+ it( 'does not snap selected edges while fine rotation couples source axes', () => {
666
+ const state = makeState( {
667
+ rotation: 30,
668
+ zoom: 2.25,
669
+ pan: { x: 0.013, y: -0.017 },
670
+ cropRect: { x: 0.12, y: 0.18, width: 0.33, height: 0.41 },
671
+ } );
672
+ const candidate = { x: 0.123, y: 0.187, width: 0.337, height: 0.419 };
673
+
674
+ expect(
675
+ snapCropRectToSourcePixels( state, IMAGE, candidate, 'e' )
676
+ ).toBe( candidate );
677
+ } );
678
+
679
+ it( 'returns the input crop rect when the image dimensions are invalid', () => {
680
+ const cropRect = { x: 0.1, y: 0.2, width: 0.3, height: 0.4 };
681
+
682
+ expect(
683
+ snapCropRectToSourcePixels(
684
+ makeState(),
685
+ { width: 0, height: 0 },
686
+ cropRect,
687
+ 'se'
688
+ )
689
+ ).toBe( cropRect );
690
+ } );
691
+ } );
692
+
579
693
  describe( 'getSourceRegionPercent', () => {
580
694
  it( 'at default state, returns 0/0/100/100', () => {
581
695
  const state = makeState();
@@ -946,3 +1060,160 @@ describe( 'getSourceRegion — non-finite input regression', () => {
946
1060
  expect( out.zoom ).toBeGreaterThanOrEqual( 1 );
947
1061
  } );
948
1062
  } );
1063
+
1064
+ describe( 'getViewScale — presentational magnification to fill the canvas', () => {
1065
+ // A tall image (1400x2500) fit into a short, wide canvas (1000x500):
1066
+ // contain fit = min(1000/1400, 500/2500) = 0.2, so the rendered footprint
1067
+ // is 280x500 — it fills the canvas height but only 280 of 1000 px wide.
1068
+ const CANVAS: Size = { width: 1000, height: 500 };
1069
+ const FOOTPRINT: Size = { width: 280, height: 500 };
1070
+ const TARGET = 0.8;
1071
+ const MAX = 8;
1072
+
1073
+ it( 'returns 1 when the crop already fills at least the target on its binding axis', () => {
1074
+ // Full-footprint crop is 280x500 on screen: 500 == canvas height, well
1075
+ // above 0.8 of it. No magnification needed.
1076
+ const full = { x: 0, y: 0, width: 1, height: 1 };
1077
+ expect( getViewScale( full, CANVAS, FOOTPRINT, TARGET, MAX ) ).toBe(
1078
+ 1
1079
+ );
1080
+ } );
1081
+
1082
+ it( 'magnifies an under-filling crop so its binding axis reaches target * canvas', () => {
1083
+ // A square crop in the 280x500 footprint: width fills (cropRect.width=1
1084
+ // -> 280px), height is 280/500 = 0.56 (-> 280px). On-screen 280x280.
1085
+ // kW = 0.8*1000/280 = 2.857, kH = 0.8*500/280 = 1.4286, k = min = 1.4286.
1086
+ const square = { x: 0, y: 0.22, width: 1, height: 280 / 500 };
1087
+ const scale = getViewScale( square, CANVAS, FOOTPRINT, TARGET, MAX );
1088
+ expect( scale ).toBeCloseTo( ( TARGET * 500 ) / 280, 5 );
1089
+ // After magnification the binding (height) axis reaches 0.8 * canvas.
1090
+ expect( square.height * FOOTPRINT.height * scale ).toBeCloseTo(
1091
+ TARGET * CANVAS.height,
1092
+ 5
1093
+ );
1094
+ } );
1095
+
1096
+ it( 'clamps magnification at maxScale for a near-degenerate crop', () => {
1097
+ // Tiny crop would need an enormous scale to fill; cap it.
1098
+ const tiny = { x: 0.5, y: 0.5, width: 0.02, height: 0.02 };
1099
+ expect( getViewScale( tiny, CANVAS, FOOTPRINT, TARGET, MAX ) ).toBe(
1100
+ MAX
1101
+ );
1102
+ } );
1103
+
1104
+ it( 'never shrinks below 1 even when the crop overfills the target', () => {
1105
+ const full = { x: 0, y: 0, width: 1, height: 1 };
1106
+ expect( getViewScale( full, CANVAS, FOOTPRINT, 0.5, MAX ) ).toBe( 1 );
1107
+ } );
1108
+
1109
+ it( 'returns 1 for zero or degenerate inputs', () => {
1110
+ const full = { x: 0, y: 0, width: 1, height: 1 };
1111
+ const zeroRect = { x: 0, y: 0, width: 0, height: 0 };
1112
+ expect( getViewScale( zeroRect, CANVAS, FOOTPRINT, TARGET, MAX ) ).toBe(
1113
+ 1
1114
+ );
1115
+ expect(
1116
+ getViewScale(
1117
+ full,
1118
+ { width: 0, height: 0 },
1119
+ FOOTPRINT,
1120
+ TARGET,
1121
+ MAX
1122
+ )
1123
+ ).toBe( 1 );
1124
+ expect(
1125
+ getViewScale( full, CANVAS, { width: 0, height: 0 }, TARGET, MAX )
1126
+ ).toBe( 1 );
1127
+ } );
1128
+
1129
+ it( 'returns 1 (never NaN) for non-finite inputs', () => {
1130
+ const full = { x: 0, y: 0, width: 1, height: 1 };
1131
+ const nanRect = { x: 0, y: 0, width: NaN, height: 1 };
1132
+ expect( getViewScale( nanRect, CANVAS, FOOTPRINT, TARGET, MAX ) ).toBe(
1133
+ 1
1134
+ );
1135
+ expect(
1136
+ getViewScale(
1137
+ full,
1138
+ { width: NaN, height: 500 },
1139
+ FOOTPRINT,
1140
+ TARGET,
1141
+ MAX
1142
+ )
1143
+ ).toBe( 1 );
1144
+ expect(
1145
+ getViewScale(
1146
+ full,
1147
+ CANVAS,
1148
+ { width: Infinity, height: 500 },
1149
+ TARGET,
1150
+ MAX
1151
+ )
1152
+ ).toBe( 1 );
1153
+ expect( getViewScale( full, CANVAS, FOOTPRINT, NaN, MAX ) ).toBe( 1 );
1154
+ expect( getViewScale( full, CANVAS, FOOTPRINT, TARGET, NaN ) ).toBe(
1155
+ 1
1156
+ );
1157
+ } );
1158
+ } );
1159
+
1160
+ describe( 'getViewScale — property invariants', () => {
1161
+ const ITERATIONS = 100;
1162
+
1163
+ it( `holds across ${ ITERATIONS } random valid inputs`, () => {
1164
+ for ( let i = 0; i < ITERATIONS; i++ ) {
1165
+ const canvasW = 200 + ( ( i * 37 ) % 1800 );
1166
+ const canvasH = 200 + ( ( i * 53 ) % 1200 );
1167
+ const footprintW = 50 + ( ( i * 19 ) % 800 );
1168
+ const footprintH = 50 + ( ( i * 29 ) % 1000 );
1169
+ const cropW = 0.05 + ( ( i * 3 ) % 95 ) / 100;
1170
+ const cropH = 0.05 + ( ( i * 5 ) % 95 ) / 100;
1171
+ const cropX = Math.min( ( ( i * 11 ) % 100 ) / 100, 1 - cropW );
1172
+ const cropY = Math.min( ( ( i * 17 ) % 100 ) / 100, 1 - cropH );
1173
+ const targetFill = 0.1 + ( ( i * 7 ) % 90 ) / 100;
1174
+ const maxScale = 1 + ( ( i * 13 ) % 20 );
1175
+
1176
+ const canvas: Size = { width: canvasW, height: canvasH };
1177
+ const footprint: Size = { width: footprintW, height: footprintH };
1178
+ const cropRect = {
1179
+ x: cropX,
1180
+ y: cropY,
1181
+ width: cropW,
1182
+ height: cropH,
1183
+ };
1184
+
1185
+ const scale = getViewScale(
1186
+ cropRect,
1187
+ canvas,
1188
+ footprint,
1189
+ targetFill,
1190
+ maxScale
1191
+ );
1192
+
1193
+ expect( Number.isFinite( scale ) ).toBe( true );
1194
+ expect( scale ).toBeGreaterThanOrEqual( 1 );
1195
+ expect( scale ).toBeLessThanOrEqual( maxScale );
1196
+
1197
+ const cropScreenW = cropRect.width * footprintW;
1198
+ const cropScreenH = cropRect.height * footprintH;
1199
+ const kW = ( targetFill * canvasW ) / cropScreenW;
1200
+ const kH = ( targetFill * canvasH ) / cropScreenH;
1201
+ const k = Math.min( kW, kH );
1202
+ const unclamped = Math.max( 1, k );
1203
+
1204
+ expect( scale ).toBeCloseTo( Math.min( maxScale, unclamped ), 10 );
1205
+
1206
+ const shouldCheckFill = scale > 1 && scale < maxScale - 1e-10;
1207
+ const bindingIsWidth = kW <= kH + 1e-10;
1208
+ const magnifiedBinding = bindingIsWidth
1209
+ ? cropScreenW * scale
1210
+ : cropScreenH * scale;
1211
+ const targetBinding = bindingIsWidth
1212
+ ? targetFill * canvasW
1213
+ : targetFill * canvasH;
1214
+ expect(
1215
+ shouldCheckFill ? magnifiedBinding / targetBinding : 1
1216
+ ).toBeCloseTo( 1, shouldCheckFill ? 4 : 10 );
1217
+ }
1218
+ } );
1219
+ } );
@@ -291,6 +291,38 @@ describe( 'InteractionController', () => {
291
291
  el._fire( 'pointerup', createPointerEvent() );
292
292
  } );
293
293
 
294
+ it( 'ignores touch pointerdown so touch gestures own touch input', () => {
295
+ const state = makeState( { zoom: 2 } );
296
+ const onGestureStart = jest.fn();
297
+ const onStatusChange = jest.fn();
298
+ const { controller } = createController( state, {
299
+ onGestureStart,
300
+ onStatusChange,
301
+ } );
302
+ const el = createMockElement();
303
+ const event = createPointerEvent( {
304
+ clientX: 100,
305
+ clientY: 100,
306
+ pointerType: 'touch',
307
+ } );
308
+
309
+ controller.handlePointerDown( event, el );
310
+
311
+ expect( event.preventDefault ).not.toHaveBeenCalled();
312
+ expect( el.focus ).not.toHaveBeenCalled();
313
+ expect( el.setPointerCapture ).not.toHaveBeenCalled();
314
+ expect( el.addEventListener ).not.toHaveBeenCalled();
315
+ expect( onGestureStart ).not.toHaveBeenCalled();
316
+ expect( onStatusChange ).not.toHaveBeenCalled();
317
+
318
+ el._fire(
319
+ 'pointermove',
320
+ createPointerEvent( { clientX: 150, clientY: 120 } )
321
+ );
322
+
323
+ expect( actionMocks.setPan ).not.toHaveBeenCalled();
324
+ } );
325
+
294
326
  it( 'stops dispatching after pointerup', () => {
295
327
  const state = makeState( { zoom: 2 } );
296
328
  const { controller } = createController( state );
@@ -797,6 +829,26 @@ describe( 'InteractionController', () => {
797
829
  expect( actionMocks.setPan ).toHaveBeenCalled();
798
830
  } );
799
831
 
832
+ it( 'prevents default on touchmove so the page does not scroll mid-gesture', () => {
833
+ const state = makeState( { zoom: 2 } );
834
+ const { controller } = createController( state );
835
+ const doc = createMockDocument();
836
+ const rect = createContainerRect();
837
+
838
+ controller.handleTouchStart(
839
+ createTouchEvent( [ { clientX: 100, clientY: 100 } ] ),
840
+ rect,
841
+ doc
842
+ );
843
+
844
+ const moveEvent = createTouchEvent( [
845
+ { clientX: 150, clientY: 120 },
846
+ ] );
847
+ doc._fire( 'touchmove', moveEvent );
848
+
849
+ expect( moveEvent.preventDefault ).toHaveBeenCalled();
850
+ } );
851
+
800
852
  it( 'calls onGestureStart/onGestureEnd for single-finger pan', () => {
801
853
  const state = makeState( { zoom: 2 } );
802
854
  const onGestureStart = jest.fn();
@@ -888,6 +940,54 @@ describe( 'InteractionController', () => {
888
940
  expect( actionMocks.setZoomAtPoint ).toHaveBeenCalled();
889
941
  } );
890
942
 
943
+ it( 'keeps a repeated pinch zoom anchored to the pinch-start midpoint from a zoomed and panned state', () => {
944
+ let state = makeState( {
945
+ zoom: 2,
946
+ pan: { x: 0.1, y: -0.05 },
947
+ } );
948
+ actionMocks.setZoomAtPoint.mockImplementation( ( zoom, pan ) => {
949
+ state = { ...state, zoom, pan };
950
+ } );
951
+ const { controller } = createController( state, {
952
+ getState: () => state,
953
+ } );
954
+ const doc = createMockDocument();
955
+ const rect = createContainerRect();
956
+
957
+ controller.handleTouchStart(
958
+ createTouchEvent( [
959
+ { clientX: 300, clientY: 180 },
960
+ { clientX: 400, clientY: 180 },
961
+ ] ),
962
+ rect,
963
+ doc
964
+ );
965
+
966
+ doc._fire(
967
+ 'touchmove',
968
+ createTouchEvent( [
969
+ { clientX: 275, clientY: 180 },
970
+ { clientX: 425, clientY: 180 },
971
+ ] )
972
+ );
973
+ doc._fire(
974
+ 'touchmove',
975
+ createTouchEvent( [
976
+ { clientX: 250, clientY: 180 },
977
+ { clientX: 450, clientY: 180 },
978
+ ] )
979
+ );
980
+
981
+ expect( actionMocks.setZoomAtPoint ).toHaveBeenCalledTimes( 2 );
982
+ const [ zoom, pan ] =
983
+ actionMocks.setZoomAtPoint.mock.calls[
984
+ actionMocks.setZoomAtPoint.mock.calls.length - 1
985
+ ];
986
+ expect( zoom ).toBeCloseTo( 4 );
987
+ expect( pan.x ).toBeCloseTo( 0 );
988
+ expect( pan.y ).toBeCloseTo( -0.2 );
989
+ } );
990
+
891
991
  it( 'calls onGestureStart for pinch, onGestureEnd on touchend', () => {
892
992
  const state = makeState( { zoom: 1 } );
893
993
  const onGestureStart = jest.fn();
@@ -9,20 +9,23 @@ import {
9
9
  type CropBounds,
10
10
  type ResizeDragState,
11
11
  } from '../stencil-math';
12
- import { MIN_CROP_PIXELS, MIN_CROP_SCREEN_PX } from '../constants';
12
+ import {
13
+ DEFAULT_KEYBOARD_STEP,
14
+ MIN_CROP_PIXELS,
15
+ MIN_CROP_SCREEN_PX,
16
+ } from '../constants';
13
17
  import type { Size } from '../types';
14
18
 
15
19
  describe( 'getMinCropPixels — operable on-screen floor', () => {
16
20
  it( 'returns the source-pixel hard floor when the image is shown large enough', () => {
17
- // displayScale = 2 CSS px per source px (zoomed in). The usability
18
- // term is 44 / 2 = 22 px, below the 24-source-px hard floor, so the
19
- // hard floor wins.
20
- expect( getMinCropPixels( 2 ) ).toBe( MIN_CROP_PIXELS );
21
+ const displayScale = ( MIN_CROP_SCREEN_PX / MIN_CROP_PIXELS ) * 1.1;
22
+
23
+ expect( getMinCropPixels( displayScale ) ).toBe( MIN_CROP_PIXELS );
21
24
  } );
22
25
 
23
26
  it( 'raises the floor so the crop stays operable when the image is shown small', () => {
24
27
  // A large image fit into a small window: ~0.2 CSS px per source px.
25
- // 24 source px would render as ~4.8 px on screen — unusable. The
28
+ // The source-pixel floor would render too small on screen. The
26
29
  // usability term (44 / 0.2 = 220 source px) must win.
27
30
  expect( getMinCropPixels( 0.2 ) ).toBeCloseTo(
28
31
  MIN_CROP_SCREEN_PX / 0.2,
@@ -150,6 +153,117 @@ describe( 'computeLockedResizeRect — driver-axis selection', () => {
150
153
  expect( pixelW ).toBeCloseTo( 200, 5 );
151
154
  expect( pixelH ).toBeCloseTo( 200, 5 );
152
155
  } );
156
+
157
+ it( 'keeps pointer resize continuous near the pixel-motion threshold', () => {
158
+ const originalImageSize: Size = { width: 600, height: 400 };
159
+ const fullImageCrop = { x: 0, y: 0, width: 1, height: 1 };
160
+ const drag: ResizeDragState = {
161
+ handle: 'nw',
162
+ startX: 0,
163
+ startY: 0,
164
+ startRect: fullImageCrop,
165
+ };
166
+
167
+ const beforeThreshold = computeLockedResizeRect(
168
+ drag,
169
+ 60,
170
+ -39.6,
171
+ originalImageSize,
172
+ FULL_BOUNDS,
173
+ 1
174
+ );
175
+ const afterThreshold = computeLockedResizeRect(
176
+ drag,
177
+ 60,
178
+ -40.4,
179
+ originalImageSize,
180
+ FULL_BOUNDS,
181
+ 1
182
+ );
183
+
184
+ expect(
185
+ Math.abs( beforeThreshold.width - afterThreshold.width )
186
+ ).toBeLessThan( 0.01 );
187
+ expect(
188
+ Math.abs( beforeThreshold.height - afterThreshold.height )
189
+ ).toBeLessThan( 0.01 );
190
+ } );
191
+
192
+ it( 'shrinks a locked-ratio crop from horizontal keyboard movement', () => {
193
+ const squareImageSize: Size = { width: 500, height: 500 };
194
+ const lockedStartRect = { x: 0.1, y: 0.1, width: 0.8, height: 0.8 };
195
+ const drag: ResizeDragState = {
196
+ handle: 'nw',
197
+ startX: 0,
198
+ startY: 0,
199
+ startRect: lockedStartRect,
200
+ };
201
+ const rect = computeLockedResizeRect(
202
+ drag,
203
+ DEFAULT_KEYBOARD_STEP * squareImageSize.width,
204
+ 0,
205
+ squareImageSize,
206
+ FULL_BOUNDS,
207
+ 1,
208
+ undefined,
209
+ 'width'
210
+ );
211
+
212
+ expect( rect.x ).toBeCloseTo(
213
+ lockedStartRect.x + DEFAULT_KEYBOARD_STEP,
214
+ 5
215
+ );
216
+ expect( rect.y ).toBeCloseTo(
217
+ lockedStartRect.y + DEFAULT_KEYBOARD_STEP,
218
+ 5
219
+ );
220
+ expect( rect.width ).toBeCloseTo(
221
+ lockedStartRect.width - DEFAULT_KEYBOARD_STEP,
222
+ 5
223
+ );
224
+ expect( rect.height ).toBeCloseTo(
225
+ lockedStartRect.height - DEFAULT_KEYBOARD_STEP,
226
+ 5
227
+ );
228
+ } );
229
+
230
+ it( 'shrinks a locked-ratio crop from vertical keyboard movement', () => {
231
+ const squareImageSize: Size = { width: 500, height: 500 };
232
+ const lockedStartRect = { x: 0.1, y: 0.1, width: 0.8, height: 0.8 };
233
+ const drag: ResizeDragState = {
234
+ handle: 'nw',
235
+ startX: 0,
236
+ startY: 0,
237
+ startRect: lockedStartRect,
238
+ };
239
+ const rect = computeLockedResizeRect(
240
+ drag,
241
+ 0,
242
+ DEFAULT_KEYBOARD_STEP * squareImageSize.height,
243
+ squareImageSize,
244
+ FULL_BOUNDS,
245
+ 1,
246
+ undefined,
247
+ 'height'
248
+ );
249
+
250
+ expect( rect.x ).toBeCloseTo(
251
+ lockedStartRect.x + DEFAULT_KEYBOARD_STEP,
252
+ 5
253
+ );
254
+ expect( rect.y ).toBeCloseTo(
255
+ lockedStartRect.y + DEFAULT_KEYBOARD_STEP,
256
+ 5
257
+ );
258
+ expect( rect.width ).toBeCloseTo(
259
+ lockedStartRect.width - DEFAULT_KEYBOARD_STEP,
260
+ 5
261
+ );
262
+ expect( rect.height ).toBeCloseTo(
263
+ lockedStartRect.height - DEFAULT_KEYBOARD_STEP,
264
+ 5
265
+ );
266
+ } );
153
267
  } );
154
268
 
155
269
  const IMAGE: Size = { width: 1000, height: 500 };
@@ -193,6 +193,8 @@ export interface StencilProps {
193
193
  aspectRatio?: number;
194
194
  /** Whether the crop handles are shown for freeform resizing. */
195
195
  freeformCrop?: boolean;
196
+ /** Whether resize handles should ignore pointer and keyboard input. */
197
+ isResizeDisabled?: boolean;
196
198
  /** CSS transition string for settle animation. */
197
199
  stencilTransition?: string;
198
200
  /** Maximum crop rect bounds based on current zoom/rotation. */
@@ -210,4 +212,19 @@ export interface StencilProps {
210
212
  * the source image dimensions. Omit to use the stencil's default.
211
213
  */
212
214
  minCropSize?: Size;
215
+ /**
216
+ * Optional post-processor for freeform resize output. Hosts use this for
217
+ * display-gated behavior such as snapping visible pixel grids to source
218
+ * pixel boundaries.
219
+ */
220
+ snapCropRect?: (
221
+ rect: NormalizedRect,
222
+ handle: HandlePosition
223
+ ) => NormalizedRect;
224
+ /**
225
+ * Optional keyboard resize step in normalized space, per axis. Hosts use
226
+ * this when keyboard resize should follow the rendered image scale instead
227
+ * of the default percentage-based step.
228
+ */
229
+ keyboardResizeStep?: Size;
213
230
  }