@wordpress/media-editor 0.18.1-next.v.202609171837.0 → 0.20.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 (146) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/build/components/media-editor/index.cjs +132 -50
  3. package/build/components/media-editor/index.cjs.map +2 -2
  4. package/build/components/media-editor/use-crop-options.cjs +1 -1
  5. package/build/components/media-editor/use-crop-options.cjs.map +2 -2
  6. package/build/components/media-editor/use-save-media-editor.cjs +36 -20
  7. package/build/components/media-editor/use-save-media-editor.cjs.map +2 -2
  8. package/build/components/media-editor-canvas/index.cjs +8 -8
  9. package/build/components/media-editor-canvas/index.cjs.map +2 -2
  10. package/build/components/media-editor-fine-rotation/index.cjs +1 -1
  11. package/build/components/media-editor-fine-rotation/index.cjs.map +2 -2
  12. package/build/components/media-editor-image-controls/index.cjs +46 -29
  13. package/build/components/media-editor-image-controls/index.cjs.map +2 -2
  14. package/build/components/media-editor-modal/index.cjs +17 -9
  15. package/build/components/media-editor-modal/index.cjs.map +2 -2
  16. package/build/hooks/use-crop-gesture-handlers.cjs +1 -1
  17. package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
  18. package/build/image-editor/core/interaction-controller.cjs +5 -0
  19. package/build/image-editor/core/interaction-controller.cjs.map +2 -2
  20. package/build/image-editor/react/components/cropper.cjs +3 -3
  21. package/build/image-editor/react/components/cropper.cjs.map +2 -2
  22. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +1 -1
  23. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
  24. package/build/image-editor/react/hooks/use-aria-announcer.cjs +3 -1
  25. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
  26. package/build/image-editor/react/hooks/use-interaction.cjs +1 -1
  27. package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
  28. package/build/state/composite-reducer.cjs +3 -3
  29. package/build/state/composite-reducer.cjs.map +2 -2
  30. package/build/state/index.cjs.map +1 -1
  31. package/build/state/media-editor-state-provider.cjs +1 -6
  32. package/build/state/media-editor-state-provider.cjs.map +2 -2
  33. package/build/state/use-media-editor-state.cjs +38 -36
  34. package/build/state/use-media-editor-state.cjs.map +2 -2
  35. package/build/store/actions.cjs +2 -0
  36. package/build/store/actions.cjs.map +2 -2
  37. package/build/store/reducer.cjs +3 -1
  38. package/build/store/reducer.cjs.map +2 -2
  39. package/build/store/selectors.cjs +5 -0
  40. package/build/store/selectors.cjs.map +2 -2
  41. package/build-module/components/media-editor/index.mjs +145 -52
  42. package/build-module/components/media-editor/index.mjs.map +2 -2
  43. package/build-module/components/media-editor/use-crop-options.mjs +1 -1
  44. package/build-module/components/media-editor/use-crop-options.mjs.map +2 -2
  45. package/build-module/components/media-editor/use-save-media-editor.mjs +36 -20
  46. package/build-module/components/media-editor/use-save-media-editor.mjs.map +2 -2
  47. package/build-module/components/media-editor-canvas/index.mjs +8 -8
  48. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  49. package/build-module/components/media-editor-fine-rotation/index.mjs +1 -1
  50. package/build-module/components/media-editor-fine-rotation/index.mjs.map +2 -2
  51. package/build-module/components/media-editor-image-controls/index.mjs +47 -36
  52. package/build-module/components/media-editor-image-controls/index.mjs.map +2 -2
  53. package/build-module/components/media-editor-modal/index.mjs +17 -9
  54. package/build-module/components/media-editor-modal/index.mjs.map +2 -2
  55. package/build-module/hooks/use-crop-gesture-handlers.mjs +1 -1
  56. package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
  57. package/build-module/image-editor/core/interaction-controller.mjs +5 -0
  58. package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
  59. package/build-module/image-editor/react/components/cropper.mjs +3 -3
  60. package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
  61. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +1 -1
  62. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
  63. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +3 -1
  64. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
  65. package/build-module/image-editor/react/hooks/use-interaction.mjs +1 -1
  66. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
  67. package/build-module/state/composite-reducer.mjs +3 -3
  68. package/build-module/state/composite-reducer.mjs.map +2 -2
  69. package/build-module/state/index.mjs.map +1 -1
  70. package/build-module/state/media-editor-state-provider.mjs +1 -6
  71. package/build-module/state/media-editor-state-provider.mjs.map +2 -2
  72. package/build-module/state/use-media-editor-state.mjs +38 -36
  73. package/build-module/state/use-media-editor-state.mjs.map +2 -2
  74. package/build-module/store/actions.mjs +2 -0
  75. package/build-module/store/actions.mjs.map +2 -2
  76. package/build-module/store/reducer.mjs +3 -1
  77. package/build-module/store/reducer.mjs.map +2 -2
  78. package/build-module/store/selectors.mjs +4 -0
  79. package/build-module/store/selectors.mjs.map +2 -2
  80. package/build-style/style-rtl.css +26 -27
  81. package/build-style/style.css +11 -5
  82. package/build-types/components/media-editor/index.d.ts +2 -2
  83. package/build-types/components/media-editor/index.d.ts.map +1 -1
  84. package/build-types/components/media-editor/use-save-media-editor.d.ts +15 -3
  85. package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -1
  86. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  87. package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
  88. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  89. package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -1
  90. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
  91. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  92. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  93. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
  94. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
  95. package/build-types/state/composite-reducer.d.ts +5 -5
  96. package/build-types/state/composite-reducer.d.ts.map +1 -1
  97. package/build-types/state/index.d.ts +1 -1
  98. package/build-types/state/index.d.ts.map +1 -1
  99. package/build-types/state/media-editor-state-provider.d.ts +8 -16
  100. package/build-types/state/media-editor-state-provider.d.ts.map +1 -1
  101. package/build-types/state/use-media-editor-state.d.ts +37 -30
  102. package/build-types/state/use-media-editor-state.d.ts.map +1 -1
  103. package/build-types/store/actions.d.ts +9 -1
  104. package/build-types/store/actions.d.ts.map +1 -1
  105. package/build-types/store/reducer.d.ts +3 -0
  106. package/build-types/store/reducer.d.ts.map +1 -1
  107. package/build-types/store/selectors.d.ts +1 -0
  108. package/build-types/store/selectors.d.ts.map +1 -1
  109. package/package.json +29 -25
  110. package/src/components/media-editor/index.tsx +203 -59
  111. package/src/components/media-editor/test/restore-original.browser.test.tsx +227 -0
  112. package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +12 -10
  113. package/src/components/media-editor/use-crop-options.ts +1 -1
  114. package/src/components/media-editor/use-save-media-editor.ts +61 -20
  115. package/src/components/media-editor-canvas/index.tsx +7 -8
  116. package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +2 -4
  117. package/src/components/media-editor-fine-rotation/index.tsx +1 -1
  118. package/src/components/media-editor-image-controls/index.tsx +41 -41
  119. package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +19 -13
  120. package/src/components/media-editor-modal/index.tsx +18 -10
  121. package/src/components/media-editor-modal/test/index.jsdom.test.tsx +18 -1
  122. package/src/components/rotation-ruler/stories/index.story.tsx +2 -2
  123. package/src/components/rotation-ruler/style.scss +9 -0
  124. package/src/hooks/use-crop-gesture-handlers.ts +3 -1
  125. package/src/image-editor/core/interaction-controller.ts +8 -0
  126. package/src/image-editor/core/test/interaction-controller.ts +45 -0
  127. package/src/image-editor/docs/architecture.md +6 -2
  128. package/src/image-editor/docs/recipes.md +8 -8
  129. package/src/image-editor/react/components/cropper.scss +11 -5
  130. package/src/image-editor/react/components/cropper.tsx +8 -3
  131. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +3 -1
  132. package/src/image-editor/react/components/test/cropper.browser.test.tsx +7 -5
  133. package/src/image-editor/react/hooks/use-aria-announcer.ts +3 -1
  134. package/src/image-editor/react/hooks/use-interaction.ts +3 -2
  135. package/src/image-editor/stories/rectangle-crop.story.tsx +15 -14
  136. package/src/image-editor/stories/{style.css → style.module.css} +8 -37
  137. package/src/state/composite-reducer.ts +6 -7
  138. package/src/state/index.ts +1 -1
  139. package/src/state/media-editor-state-provider.tsx +9 -22
  140. package/src/state/test/use-media-editor-state.browser.test.tsx +79 -0
  141. package/src/state/test/use-media-editor-state.jsdom.test.ts +126 -64
  142. package/src/state/use-media-editor-state.ts +79 -68
  143. package/src/store/actions.ts +9 -0
  144. package/src/store/reducer.ts +6 -1
  145. package/src/store/selectors.ts +4 -0
  146. package/tsconfig.build.tsbuildinfo +1 -1
@@ -76,29 +76,29 @@ function CropperPanel( { src }: { src: string } ) {
76
76
  }
77
77
  ```
78
78
 
79
- ## Use the Media Editor Composite Controller
79
+ ## Use the Media Editor Session
80
80
 
81
- Use the media editor controller when cropper state must share history with controls such as aspect ratio.
81
+ Use the media editor session when cropper state must share history with controls such as aspect ratio. The session owns history and gesture boundaries; pass its `cropper` controller to `<Cropper>`.
82
82
 
83
83
  ```tsx
84
84
  import { resolveAspectRatio, useMediaEditor } from '../../state';
85
85
  import { Cropper } from '../image-editor';
86
86
 
87
87
  function MediaEditorCanvas( { src }: { src: string } ) {
88
- const controller = useMediaEditor();
88
+ const session = useMediaEditor();
89
89
  const aspectRatio = resolveAspectRatio(
90
- controller.cropOptions.aspectRatioValue,
91
- controller.state.image
90
+ session.cropOptions.aspectRatioValue,
91
+ session.cropper.state.image
92
92
  );
93
93
 
94
94
  return (
95
95
  <Cropper
96
96
  src={ src }
97
- controller={ controller }
97
+ controller={ session.cropper }
98
98
  aspectRatio={ aspectRatio }
99
99
  freeformCrop
100
- onGestureStart={ controller.beginGesture }
101
- onGestureEnd={ controller.endGesture }
100
+ onGestureStart={ session.beginGesture }
101
+ onGestureEnd={ session.endGesture }
102
102
  />
103
103
  );
104
104
  }
@@ -6,6 +6,11 @@
6
6
  // reach isn't clipped by the root.
7
7
  $handle-touch-target-size: 44px;
8
8
 
9
+ // Geometry, not text direction: the camera and `stencil-math.ts` address
10
+ // this canvas in physical pixels, and JS sets the image's `left` inline.
11
+ // Mirrored, `nw` would resize the right edge and the image would pin to the
12
+ // stage edge. ResizableBox ignores its own handle offsets for the same reason.
13
+ /*!rtl:begin:ignore*/
9
14
  .wp-media-editor-image-editor {
10
15
  position: relative;
11
16
  overflow: hidden;
@@ -144,8 +149,8 @@ $handle-touch-target-size: 44px;
144
149
  // on the canvas ensures the outline appears only when the canvas
145
150
  // itself is focused, not when focus is on a child handle.
146
151
  &__canvas--focus-visible:focus &__stencil-rect {
147
- border-color: var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #007cba));
148
- box-shadow: 0 0 0 1px var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #007cba));
152
+ border-color: var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #3858e9));
153
+ box-shadow: 0 0 0 1px var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #3858e9));
149
154
  }
150
155
 
151
156
  &__handle {
@@ -164,7 +169,7 @@ $handle-touch-target-size: 44px;
164
169
  // Solid white fill with an accent-blue border so the handles read
165
170
  // clearly against the light grey wash.
166
171
  background: $white;
167
- border: 1px solid var(--wp-admin-theme-color, #007cba);
172
+ border: 1px solid var(--wp-admin-theme-color, #3858e9);
168
173
  border-radius: 50%;
169
174
  box-sizing: border-box;
170
175
  pointer-events: auto;
@@ -180,9 +185,9 @@ $handle-touch-target-size: 44px;
180
185
  // keyboard session — e.g., clicking a handle to start a drag —
181
186
  // don't leave the active ring stuck on the handle.
182
187
  .wp-media-editor-image-editor__canvas--focus-visible &:focus {
183
- background: var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #007cba));
188
+ background: var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #3858e9));
184
189
  border-color: transparent;
185
- box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #007cba));
190
+ box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #3858e9));
186
191
  }
187
192
 
188
193
  // Extend touch target to the minimum accessible hit area.
@@ -251,3 +256,4 @@ $handle-touch-target-size: 44px;
251
256
  }
252
257
 
253
258
  }
259
+ /*!rtl:end:ignore*/
@@ -571,14 +571,17 @@ function CropperInner(
571
571
  return;
572
572
  }
573
573
 
574
- setCropRect( snappedCropRect );
574
+ // The display scale crossing the threshold is not an edit, so the
575
+ // snap must not record an undo entry. Recording it here would also
576
+ // clear the redo stack whenever an undo or redo crosses it.
577
+ adjustCropRectForViewport( snappedCropRect );
575
578
  }, [
579
+ adjustCropRectForViewport,
576
580
  aspectRatio,
577
581
  displayScale,
578
582
  freeformCrop,
579
583
  naturalWidth,
580
584
  naturalHeight,
581
- setCropRect,
582
585
  state,
583
586
  ] );
584
587
 
@@ -760,7 +763,9 @@ function CropperInner(
760
763
 
761
764
  // Settling animation: brief transition after resize end.
762
765
  const [ settling, setSettling ] = useState( false );
763
- const settleTimerRef = useRef< ReturnType< typeof setTimeout > >();
766
+ const settleTimerRef = useRef<
767
+ ReturnType< typeof setTimeout > | undefined
768
+ >( undefined );
764
769
  const finishSettling = useCallback( () => {
765
770
  clearTimeout( settleTimerRef.current );
766
771
  isSettlingRef.current = false;
@@ -134,7 +134,9 @@ export function RectangleStencil( {
134
134
  } ),
135
135
  [ boundsMinX, boundsMinY, boundsMaxX, boundsMaxY ]
136
136
  );
137
- const keyboardSettleTimerRef = useRef< ReturnType< typeof setTimeout > >();
137
+ const keyboardSettleTimerRef = useRef<
138
+ ReturnType< typeof setTimeout > | undefined
139
+ >( undefined );
138
140
  const keyboardResizeActiveRef = useRef( false );
139
141
  const resizeHandleDescriptionId = useId();
140
142
  const hasLockedRatio = !! ( aspectRatio && aspectRatio > 0 );
@@ -7,7 +7,6 @@ import type { CropperController } from '../../hooks/use-cropper-reducer';
7
7
  import { DEFAULT_STATE } from '../../../core/constants';
8
8
  import { getSourceRegion } from '../../../core/source-region';
9
9
  // Browser Mode needs the package's real styles for layout and transitions.
10
- // eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports
11
10
  import '../../../style.scss';
12
11
 
13
12
  const GRID_TEST_ID = 'cropper-grid';
@@ -1345,7 +1344,7 @@ describe( 'Cropper', () => {
1345
1344
  );
1346
1345
 
1347
1346
  await screen.findByTestId( 'cropper-image' );
1348
- expect( controller.setCropRect ).not.toHaveBeenCalled();
1347
+ expect( controller.adjustCropRectForViewport ).not.toHaveBeenCalled();
1349
1348
 
1350
1349
  controller.state = { ...controller.state, zoom: 3 };
1351
1350
  await rerender(
@@ -1357,9 +1356,12 @@ describe( 'Cropper', () => {
1357
1356
  );
1358
1357
 
1359
1358
  await waitFor( () =>
1360
- expect( controller.setCropRect ).toHaveBeenCalledTimes( 1 )
1359
+ expect(
1360
+ controller.adjustCropRectForViewport
1361
+ ).toHaveBeenCalledTimes( 1 )
1361
1362
  );
1362
- const rect = ( controller.setCropRect as Mock ).mock.calls[ 0 ][ 0 ];
1363
+ const rect = ( controller.adjustCropRectForViewport as Mock ).mock
1364
+ .calls[ 0 ][ 0 ];
1363
1365
  const region = getSourceRegion(
1364
1366
  { ...controller.state, cropRect: rect },
1365
1367
  { width: image.naturalWidth, height: image.naturalHeight }
@@ -1412,7 +1414,7 @@ describe( 'Cropper', () => {
1412
1414
  />
1413
1415
  );
1414
1416
 
1415
- expect( controller.setCropRect ).not.toHaveBeenCalled();
1417
+ expect( controller.adjustCropRectForViewport ).not.toHaveBeenCalled();
1416
1418
  } );
1417
1419
 
1418
1420
  function imageRendering(): string {
@@ -157,7 +157,9 @@ function buildAnnouncement(
157
157
  * @param state The current cropper state to announce.
158
158
  */
159
159
  export function useAriaAnnouncer( state: CropperState ): void {
160
- const timerRef = useRef< ReturnType< typeof setTimeout > >();
160
+ const timerRef = useRef< ReturnType< typeof setTimeout > | undefined >(
161
+ undefined
162
+ );
161
163
  const prevMessageRef = useRef( '' );
162
164
  const prevStateRef = useRef< CropperState | null >( null );
163
165
  const latestStateRef = useRef( state );
@@ -106,8 +106,9 @@ export function useInteraction(
106
106
  const [ isZooming, setIsZooming ] = useState( false );
107
107
  const [ isGestureActive, setIsGestureActive ] = useState( false );
108
108
  const [ isKeyboardPanning, setIsKeyboardPanning ] = useState( false );
109
- const keyboardInteractionTimerRef =
110
- useRef< ReturnType< typeof setTimeout > >();
109
+ const keyboardInteractionTimerRef = useRef<
110
+ ReturnType< typeof setTimeout > | undefined
111
+ >( undefined );
111
112
  // Tracks whether a keyboard gesture (pan or zoom) is currently active so
112
113
  // onGestureStart is only fired once per gesture, not on every key repeat.
113
114
  const isKeyboardGestureActiveRef = useRef( false );
@@ -40,7 +40,7 @@ import {
40
40
  getVisibleBounds,
41
41
  } from '../core/camera';
42
42
  import { getSourceRegion } from '../core/source-region';
43
- import './style.css';
43
+ import styles from './style.module.css';
44
44
 
45
45
  const SAMPLE_IMAGE = 'image-editor-demo.jpeg';
46
46
 
@@ -118,7 +118,8 @@ function resolveAspectRatio(
118
118
  }
119
119
 
120
120
  const meta: Meta< typeof Cropper > = {
121
- title: 'MediaEditor/ImageEditor',
121
+ id: 'mediaeditor-imageeditor',
122
+ title: 'Editor/Media Editor/ImageEditor',
122
123
  component: Cropper,
123
124
  tags: [ 'status-experimental' ],
124
125
  };
@@ -135,7 +136,7 @@ const DefaultComponent = () => {
135
136
 
136
137
  return (
137
138
  <div>
138
- <div className="image-editor-story__container">
139
+ <div className={ styles.container }>
139
140
  <Cropper
140
141
  src={ SAMPLE_IMAGE }
141
142
  controller={ controller }
@@ -320,9 +321,9 @@ const WithControlsComponent = () => {
320
321
  type="file"
321
322
  accept="image/*"
322
323
  onChange={ handleFileChange }
323
- className="image-editor-story__hidden-file"
324
+ className={ styles[ 'hidden-file' ] }
324
325
  />
325
- <div className="image-editor-story__toolbar">
326
+ <div className={ styles.toolbar }>
326
327
  <Flex align="center" gap={ 2 } wrap>
327
328
  <FlexItem>
328
329
  <Button
@@ -440,7 +441,7 @@ const WithControlsComponent = () => {
440
441
  </Button>
441
442
  </FlexItem>
442
443
  </Flex>
443
- <div className="image-editor-story__sliders">
444
+ <div className={ styles.sliders }>
444
445
  <RangeControl
445
446
  label="Fine rotation"
446
447
  min={ -MAX_ROTATION_OFFSET }
@@ -460,7 +461,7 @@ const WithControlsComponent = () => {
460
461
  </div>
461
462
  </div>
462
463
 
463
- <div className="image-editor-story__resizable">
464
+ <div className={ styles.resizable }>
464
465
  <Cropper
465
466
  src={ src }
466
467
  controller={ controller }
@@ -491,7 +492,7 @@ const WithControlsComponent = () => {
491
492
 
492
493
  <div style={ { marginTop: 16 } }>
493
494
  <strong>Current State:</strong>
494
- <pre className="image-editor-story__state">
495
+ <pre className={ styles.state }>
495
496
  { JSON.stringify(
496
497
  {
497
498
  rotation: state.rotation,
@@ -737,9 +738,9 @@ const DebugComponent = () => {
737
738
  type="file"
738
739
  accept="image/*"
739
740
  onChange={ handleFileChange }
740
- className="image-editor-story__hidden-file"
741
+ className={ styles[ 'hidden-file' ] }
741
742
  />
742
- <div className="image-editor-story__toolbar">
743
+ <div className={ styles.toolbar }>
743
744
  <Flex align="center" gap={ 2 } wrap>
744
745
  <FlexItem>
745
746
  <Button
@@ -858,7 +859,7 @@ const DebugComponent = () => {
858
859
  </Button>
859
860
  </FlexItem>
860
861
  </Flex>
861
- <div className="image-editor-story__sliders">
862
+ <div className={ styles.sliders }>
862
863
  <RangeControl
863
864
  label="Fine rotation"
864
865
  min={ -MAX_ROTATION_OFFSET }
@@ -885,7 +886,7 @@ const DebugComponent = () => {
885
886
  ref={ containerRef }
886
887
  style={ { flex: '1 1 60%', minWidth: 0 } }
887
888
  >
888
- <div className="image-editor-story__container">
889
+ <div className={ styles.container }>
889
890
  <Cropper
890
891
  src={ src }
891
892
  controller={ controller }
@@ -907,10 +908,10 @@ const DebugComponent = () => {
907
908
  maxHeight: 500,
908
909
  } }
909
910
  >
910
- <div className="image-editor-story__export-preview">
911
+ <div className={ styles[ 'export-preview' ] }>
911
912
  { previewSrc ? (
912
913
  <img
913
- className="image-editor-story__export-image"
914
+ className={ styles[ 'export-image' ] }
914
915
  src={ previewSrc }
915
916
  alt="Crop preview"
916
917
  />
@@ -1,4 +1,4 @@
1
- .image-editor-story__container {
1
+ .container {
2
2
  width: 100%;
3
3
  max-width: 600px;
4
4
  aspect-ratio: 3 / 2;
@@ -6,7 +6,7 @@
6
6
  position: relative;
7
7
  }
8
8
 
9
- .image-editor-story__resizable {
9
+ .resizable {
10
10
  width: 600px;
11
11
  height: 400px;
12
12
  max-width: 100%;
@@ -18,7 +18,7 @@
18
18
  position: relative;
19
19
  }
20
20
 
21
- .image-editor-story__toolbar {
21
+ .toolbar {
22
22
  display: flex;
23
23
  flex-direction: column;
24
24
  gap: 12px;
@@ -29,25 +29,18 @@
29
29
  background: #fafafa;
30
30
  }
31
31
 
32
- .image-editor-story__sliders {
32
+ .sliders {
33
33
  display: grid;
34
34
  grid-template-columns: 1fr 1fr;
35
35
  gap: 16px;
36
36
  padding: 0 4px;
37
37
  }
38
38
 
39
- .image-editor-story__hidden-file {
39
+ .hidden-file {
40
40
  display: none;
41
41
  }
42
42
 
43
- .image-editor-story__json {
44
- width: 100%;
45
- min-height: 80px;
46
- font-family: monospace;
47
- font-size: 12px;
48
- }
49
-
50
- .image-editor-story__state {
43
+ .state {
51
44
  font-family: monospace;
52
45
  font-size: 11px;
53
46
  background: #f5f5f5;
@@ -58,34 +51,12 @@
58
51
  overflow: auto;
59
52
  }
60
53
 
61
- .image-editor-story__vertex-count {
62
- font-variant-numeric: tabular-nums;
63
- min-width: 120px;
64
- }
65
-
66
- .image-editor-story__instructions {
67
- background: #f9f9f9;
68
- border: 1px solid #e0e0e0;
69
- border-radius: 4px;
70
- padding: 12px;
71
- font-size: 13px;
72
- line-height: 1.5;
73
- }
74
-
75
- .image-editor-story__instructions p {
76
- margin: 0 0 4px;
77
- }
78
-
79
- .image-editor-story__instructions p:last-child {
80
- margin-bottom: 0;
81
- }
82
-
83
- .image-editor-story__export-preview {
54
+ .export-preview {
84
55
  margin-bottom: 16px;
85
56
  max-width: 200px;
86
57
  }
87
58
 
88
- .image-editor-story__export-image {
59
+ .export-image {
89
60
  display: block;
90
61
  max-width: 100%;
91
62
  border: 1px solid #ccc;
@@ -117,18 +117,17 @@ export function areMediaEditorStatesEqual(
117
117
  }
118
118
 
119
119
  /**
120
- * Build the initial composite state from optional partial overrides.
120
+ * Build the composite state a session starts from: the given cropper
121
+ * slice and default crop options.
121
122
  *
122
- * @param initialCropper Initial cropper-slice fields to merge.
123
- * @param initialOptions Initial cropOptions-slice fields to merge.
123
+ * @param cropper The cropper slice.
124
124
  * @return Composite initial state.
125
125
  */
126
126
  export function buildInitialMediaEditorState(
127
- initialCropper: CropperState,
128
- initialOptions?: Partial< CropOptionsSlice >
127
+ cropper: CropperState
129
128
  ): MediaEditorState {
130
129
  return {
131
- cropper: initialCropper,
132
- cropOptions: { ...DEFAULT_CROP_OPTIONS, ...initialOptions },
130
+ cropper,
131
+ cropOptions: { ...DEFAULT_CROP_OPTIONS },
133
132
  };
134
133
  }
@@ -2,7 +2,7 @@ export {
2
2
  useMediaEditorState,
3
3
  resolveAspectRatio,
4
4
  } from './use-media-editor-state';
5
- export type { MediaEditorController } from './use-media-editor-state';
5
+ export type { MediaEditorSession } from './use-media-editor-state';
6
6
  export {
7
7
  MediaEditorStateProvider,
8
8
  useMediaEditor,
@@ -1,43 +1,30 @@
1
1
  import { createContext, useContext } from '@wordpress/element';
2
- import type { CropperState } from '../image-editor';
3
2
  import {
4
3
  useMediaEditorState,
5
- type MediaEditorController,
4
+ type MediaEditorSession,
6
5
  } from './use-media-editor-state';
7
- import type { CropOptionsSlice } from './types';
8
6
 
9
- const MediaEditorStateContext = createContext< MediaEditorController | null >(
7
+ const MediaEditorStateContext = createContext< MediaEditorSession | null >(
10
8
  null
11
9
  );
12
10
 
13
11
  interface MediaEditorStateProviderProps {
14
- /** Optional cropper-slice initial state. */
15
- initialCropperState?: Partial< CropperState >;
16
- /** Optional cropOptions-slice initial state. */
17
- initialCropOptions?: Partial< CropOptionsSlice >;
18
12
  /** Child components. */
19
13
  children: React.ReactNode;
20
14
  }
21
15
 
22
16
  /**
23
- * Provider that vends the composite media-editor controller via
24
- * context. Wrap the media editor in this so every consumer
25
- * (canvas, toolbar, sidebar) reads from the same store.
17
+ * Provider that vends the media editor session via context. Wrap
18
+ * the media editor in this so every consumer (canvas, toolbar,
19
+ * sidebar) reads from the same store.
26
20
  *
27
21
  * @param props
28
- * @param props.initialCropperState
29
- * @param props.initialCropOptions
30
22
  * @param props.children
31
23
  */
32
24
  export function MediaEditorStateProvider( {
33
- initialCropperState,
34
- initialCropOptions,
35
25
  children,
36
26
  }: MediaEditorStateProviderProps ) {
37
- const controller = useMediaEditorState( {
38
- cropper: initialCropperState,
39
- cropOptions: initialCropOptions,
40
- } );
27
+ const controller = useMediaEditorState();
41
28
 
42
29
  return (
43
30
  <MediaEditorStateContext.Provider value={ controller }>
@@ -47,12 +34,12 @@ export function MediaEditorStateProvider( {
47
34
  }
48
35
 
49
36
  /**
50
- * Consume the composite media-editor controller. Throws if used
37
+ * Consume the media editor session. Throws if used
51
38
  * outside a `<MediaEditorStateProvider>`.
52
39
  *
53
- * @return The composite controller.
40
+ * @return The media editor session.
54
41
  */
55
- export function useMediaEditor(): MediaEditorController {
42
+ export function useMediaEditor(): MediaEditorSession {
56
43
  const context = useContext( MediaEditorStateContext );
57
44
  if ( ! context ) {
58
45
  throw new Error(
@@ -0,0 +1,79 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { userEvent } from 'vitest/browser';
3
+ import { act, screen, waitFor } from '@testing-library/react';
4
+ import { render } from 'vitest-browser-react';
5
+ import { useEffect } from '@wordpress/element';
6
+ import { Cropper } from '../../image-editor';
7
+ import {
8
+ useMediaEditorState,
9
+ type MediaEditorSession,
10
+ } from '../use-media-editor-state';
11
+ // Browser Mode needs the package's real styles for layout and transitions.
12
+ import '../../image-editor/style.scss';
13
+
14
+ // Shown below 1:1 in the fixture, so pixel snapping stays off until a crop
15
+ // magnifies the image past one screen pixel per source pixel.
16
+ const IMAGE = {
17
+ src: 'large.png',
18
+ naturalWidth: 999,
19
+ naturalHeight: 999,
20
+ };
21
+
22
+ function CropperWithHistory( {
23
+ onController,
24
+ }: {
25
+ onController: ( controller: MediaEditorSession ) => void;
26
+ } ) {
27
+ const controller = useMediaEditorState();
28
+ const { setSourceImage } = controller;
29
+ useEffect( () => setSourceImage( IMAGE ), [ setSourceImage ] );
30
+ useEffect( () => onController( controller ), [ controller, onController ] );
31
+ return (
32
+ <div style={ { width: 648, height: 448 } }>
33
+ <Cropper
34
+ src={ IMAGE.src }
35
+ controller={ controller.cropper }
36
+ freeformCrop
37
+ onGestureStart={ controller.beginGesture }
38
+ onGestureEnd={ controller.endGesture }
39
+ />
40
+ </div>
41
+ );
42
+ }
43
+
44
+ describe( 'useMediaEditorState with a Cropper', () => {
45
+ it( 'keeps redo steps after redoing a handle crop that turns on pixel snapping', async () => {
46
+ let controller!: MediaEditorSession;
47
+ const onController = ( next: MediaEditorSession ) => {
48
+ controller = next;
49
+ };
50
+ await render( <CropperWithHistory onController={ onController } /> );
51
+ const handle = await screen.findByRole( 'button', {
52
+ name: 'Resize from bottom-right corner',
53
+ } );
54
+
55
+ // Shrink the crop to 30% in steps of 10% of a 999px image, so its
56
+ // edges fall between source pixels, then wait for it to settle.
57
+ handle.focus();
58
+ await userEvent.keyboard(
59
+ '{Shift>}' +
60
+ '{ArrowLeft}'.repeat( 7 ) +
61
+ '{ArrowUp}'.repeat( 7 ) +
62
+ '{/Shift}'
63
+ );
64
+ await waitFor( () =>
65
+ expect( controller.cropper.state.zoom ).toBeGreaterThan( 1 )
66
+ );
67
+
68
+ act( () =>
69
+ controller.cropper.setFlip( { horizontal: true, vertical: false } )
70
+ );
71
+
72
+ for ( let i = 0; i < 10 && controller.hasUndo; i++ ) {
73
+ act( () => controller.undo() );
74
+ }
75
+ act( () => controller.redo() );
76
+
77
+ expect( controller.hasRedo ).toBe( true );
78
+ } );
79
+ } );