@wordpress/media-editor 0.19.0 → 0.21.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.
- package/CHANGELOG.md +34 -0
- package/README.md +2 -1
- package/build/components/media-editor/index.cjs +136 -63
- package/build/components/media-editor/index.cjs.map +3 -3
- package/build/components/media-editor/use-crop-options.cjs +2 -3
- package/build/components/media-editor/use-crop-options.cjs.map +2 -2
- package/build/components/media-editor/use-save-media-editor.cjs +36 -20
- package/build/components/media-editor/use-save-media-editor.cjs.map +2 -2
- package/build/components/media-editor-canvas/index.cjs +8 -8
- package/build/components/media-editor-canvas/index.cjs.map +3 -3
- package/build/components/media-editor-crop-panel/index.cjs +12 -8
- package/build/components/media-editor-crop-panel/index.cjs.map +3 -3
- package/build/components/media-editor-fine-rotation/index.cjs +1 -1
- package/build/components/media-editor-fine-rotation/index.cjs.map +2 -2
- package/build/components/media-editor-image-controls/index.cjs +46 -29
- package/build/components/media-editor-image-controls/index.cjs.map +2 -2
- package/build/components/media-editor-modal/index.cjs +19 -9
- package/build/components/media-editor-modal/index.cjs.map +2 -2
- package/build/components/media-editor-provider/index.cjs +2 -1
- package/build/components/media-editor-provider/index.cjs.map +2 -2
- package/build/components/media-form/index.cjs +28 -28
- package/build/components/media-form/index.cjs.map +3 -3
- package/build/components/media-preview/index.cjs.map +3 -3
- package/build/hooks/use-crop-gesture-handlers.cjs +1 -1
- package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
- package/build/image-editor/core/interaction-controller.cjs +5 -0
- package/build/image-editor/core/interaction-controller.cjs.map +2 -2
- package/build/image-editor/react/components/cropper.cjs +4 -14
- package/build/image-editor/react/components/cropper.cjs.map +2 -2
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +1 -1
- package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-aria-announcer.cjs +3 -1
- package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
- package/build/image-editor/react/hooks/use-interaction.cjs +1 -1
- package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
- package/build/state/composite-reducer.cjs +35 -10
- package/build/state/composite-reducer.cjs.map +3 -3
- package/build/state/index.cjs.map +1 -1
- package/build/state/media-editor-state-provider.cjs +1 -6
- package/build/state/media-editor-state-provider.cjs.map +2 -2
- package/build/state/types.cjs.map +1 -1
- package/build/state/use-media-editor-state.cjs +57 -42
- package/build/state/use-media-editor-state.cjs.map +2 -2
- package/build/store/actions.cjs +2 -0
- package/build/store/actions.cjs.map +2 -2
- package/build/store/reducer.cjs +3 -1
- package/build/store/reducer.cjs.map +2 -2
- package/build/store/selectors.cjs +5 -0
- package/build/store/selectors.cjs.map +2 -2
- package/build-module/components/media-editor/index.mjs +151 -67
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs +2 -3
- package/build-module/components/media-editor/use-crop-options.mjs.map +2 -2
- package/build-module/components/media-editor/use-save-media-editor.mjs +36 -20
- package/build-module/components/media-editor/use-save-media-editor.mjs.map +2 -2
- package/build-module/components/media-editor-canvas/index.mjs +10 -10
- package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
- package/build-module/components/media-editor-crop-panel/index.mjs +13 -9
- package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
- package/build-module/components/media-editor-fine-rotation/index.mjs +1 -1
- package/build-module/components/media-editor-fine-rotation/index.mjs.map +2 -2
- package/build-module/components/media-editor-image-controls/index.mjs +47 -36
- package/build-module/components/media-editor-image-controls/index.mjs.map +2 -2
- package/build-module/components/media-editor-modal/index.mjs +19 -9
- package/build-module/components/media-editor-modal/index.mjs.map +2 -2
- package/build-module/components/media-editor-provider/index.mjs +2 -1
- package/build-module/components/media-editor-provider/index.mjs.map +2 -2
- package/build-module/components/media-form/index.mjs +33 -30
- package/build-module/components/media-form/index.mjs.map +2 -2
- package/build-module/components/media-preview/index.mjs +2 -2
- package/build-module/components/media-preview/index.mjs.map +2 -2
- package/build-module/hooks/use-crop-gesture-handlers.mjs +1 -1
- package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
- package/build-module/image-editor/core/interaction-controller.mjs +5 -0
- package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
- package/build-module/image-editor/react/components/cropper.mjs +4 -14
- package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +1 -1
- package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +3 -1
- package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
- package/build-module/image-editor/react/hooks/use-interaction.mjs +1 -1
- package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
- package/build-module/state/composite-reducer.mjs +25 -10
- package/build-module/state/composite-reducer.mjs.map +2 -2
- package/build-module/state/index.mjs.map +1 -1
- package/build-module/state/media-editor-state-provider.mjs +1 -6
- package/build-module/state/media-editor-state-provider.mjs.map +2 -2
- package/build-module/state/types.mjs.map +1 -1
- package/build-module/state/use-media-editor-state.mjs +57 -42
- package/build-module/state/use-media-editor-state.mjs.map +2 -2
- package/build-module/store/actions.mjs +2 -0
- package/build-module/store/actions.mjs.map +2 -2
- package/build-module/store/reducer.mjs +3 -1
- package/build-module/store/reducer.mjs.map +2 -2
- package/build-module/store/selectors.mjs +4 -0
- package/build-module/store/selectors.mjs.map +2 -2
- package/build-style/style-rtl.css +5 -5
- package/build-style/style.css +5 -5
- package/build-types/components/media-editor/index.d.ts +4 -3
- package/build-types/components/media-editor/index.d.ts.map +1 -1
- package/build-types/components/media-editor/use-crop-options.d.ts +0 -1
- package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
- package/build-types/components/media-editor/use-save-media-editor.d.ts +15 -3
- package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -1
- package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
- package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
- package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
- package/build-types/components/media-editor-modal/index.d.ts +7 -2
- package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
- package/build-types/components/media-editor-provider/index.d.ts +7 -1
- package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
- package/build-types/components/media-form/index.d.ts +5 -2
- package/build-types/components/media-form/index.d.ts.map +1 -1
- package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -1
- package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
- package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
- package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
- package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
- package/build-types/state/composite-reducer.d.ts +5 -5
- package/build-types/state/composite-reducer.d.ts.map +1 -1
- package/build-types/state/index.d.ts +2 -2
- package/build-types/state/index.d.ts.map +1 -1
- package/build-types/state/media-editor-state-provider.d.ts +8 -16
- package/build-types/state/media-editor-state-provider.d.ts.map +1 -1
- package/build-types/state/types.d.ts +34 -5
- package/build-types/state/types.d.ts.map +1 -1
- package/build-types/state/use-media-editor-state.d.ts +53 -35
- package/build-types/state/use-media-editor-state.d.ts.map +1 -1
- package/build-types/store/actions.d.ts +9 -1
- package/build-types/store/actions.d.ts.map +1 -1
- package/build-types/store/reducer.d.ts +3 -0
- package/build-types/store/reducer.d.ts.map +1 -1
- package/build-types/store/selectors.d.ts +1 -0
- package/build-types/store/selectors.d.ts.map +1 -1
- package/package.json +41 -25
- package/src/components/media-editor/index.tsx +213 -79
- package/src/components/media-editor/use-crop-options.ts +1 -3
- package/src/components/media-editor/use-save-media-editor.ts +61 -20
- package/src/components/media-editor-canvas/index.tsx +9 -10
- package/src/components/media-editor-crop-panel/index.tsx +17 -9
- package/src/components/media-editor-fine-rotation/index.tsx +1 -1
- package/src/components/media-editor-image-controls/index.tsx +41 -41
- package/src/components/media-editor-image-controls/style.scss +1 -1
- package/src/components/media-editor-modal/index.tsx +26 -11
- package/src/components/media-editor-provider/index.tsx +8 -1
- package/src/components/media-form/index.tsx +48 -42
- package/src/components/media-preview/index.tsx +2 -2
- package/src/hooks/use-crop-gesture-handlers.ts +3 -1
- package/src/image-editor/core/interaction-controller.ts +8 -0
- package/src/image-editor/docs/architecture.md +10 -2
- package/src/image-editor/docs/recipes.md +8 -8
- package/src/image-editor/react/components/cropper.scss +5 -5
- package/src/image-editor/react/components/cropper.tsx +9 -18
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +3 -1
- package/src/image-editor/react/hooks/use-aria-announcer.ts +3 -1
- package/src/image-editor/react/hooks/use-interaction.ts +3 -2
- package/src/state/composite-reducer.ts +29 -17
- package/src/state/index.ts +3 -1
- package/src/state/media-editor-state-provider.tsx +9 -22
- package/src/state/types.ts +37 -5
- package/src/state/use-media-editor-state.ts +124 -78
- package/src/store/actions.ts +9 -0
- package/src/store/reducer.ts +6 -1
- package/src/store/selectors.ts +4 -0
- package/global.d.ts +0 -5
- package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +0 -104
- package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +0 -74
- package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +0 -180
- package/src/components/media-editor-modal/test/build-modifiers.test.ts +0 -359
- package/src/components/media-editor-modal/test/index.jsdom.test.tsx +0 -165
- package/src/components/rotation-ruler/stories/index.story.tsx +0 -43
- package/src/components/rotation-ruler/test/index.jsdom.test.tsx +0 -140
- package/src/image-editor/core/export/test/canvas-renderer.jsdom.test.ts +0 -823
- package/src/image-editor/core/math/test/sanitize.ts +0 -242
- package/src/image-editor/core/test/camera.ts +0 -1218
- package/src/image-editor/core/test/interaction-controller.ts +0 -1254
- package/src/image-editor/core/test/preview-export-parity.ts +0 -249
- package/src/image-editor/core/test/render-camera-bridge.ts +0 -349
- package/src/image-editor/core/test/state.ts +0 -876
- package/src/image-editor/core/test/stencil-math.ts +0 -665
- package/src/image-editor/core/transforms/test/pipeline.ts +0 -267
- package/src/image-editor/react/components/stencils/test/rectangle-stencil.browser.test.tsx +0 -438
- package/src/image-editor/react/components/test/cropper.browser.test.tsx +0 -1460
- package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +0 -491
- package/src/image-editor/react/hooks/test/use-cropper-reducer.jsdom.test.ts +0 -1049
- package/src/image-editor/react/hooks/test/use-interaction.jsdom.test.ts +0 -213
- package/src/image-editor/react/hooks/test/use-transform-style.jsdom.test.ts +0 -140
- package/src/image-editor/stories/rectangle-crop.story.tsx +0 -1073
- package/src/image-editor/stories/style.module.css +0 -65
- package/src/state/test/use-media-editor-state.jsdom.test.ts +0 -324
- package/src/store/test/reducer.ts +0 -64
- package/tsconfig.build.json +0 -26
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.json +0 -37
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
| React cropper | `src/image-editor/react/` | DOM measurement, interaction hooks, overlays, stencil rendering, and the pure `useCropperReducer` hook. |
|
|
9
9
|
| Media editor state | `src/state/` | Composite state for the media editor: cropper state, crop options, undo/redo, and gesture boundaries. |
|
|
10
10
|
|
|
11
|
-
The cropper layer does not own editor history. History belongs to the
|
|
11
|
+
The cropper layer does not own editor history. History belongs to the media editor session because sidebar controls and cropper gestures need one shared undo stack.
|
|
12
12
|
|
|
13
13
|
## Coordinate Spaces
|
|
14
14
|
|
|
@@ -37,6 +37,14 @@ The render path uses focused calculations for CSS transforms, stencil placement,
|
|
|
37
37
|
|
|
38
38
|
The core reducer is framework-agnostic and serializable. React and media-editor layers add DOM measurement, gestures, undo/redo, save behavior, and UI state.
|
|
39
39
|
|
|
40
|
+
### Session
|
|
41
|
+
|
|
42
|
+
`useMediaEditorState()` returns the media editor session. The session owns the source image, every editing slice, the shared undo/redo history, gesture boundaries, and whether the edits change the saved image (`hasOutputEdits`). The cropper is one participant: `session.cropper` is a geometry-only `CropperController` that a `<Cropper>` takes via its `controller` prop, and its `isDirty` covers geometry alone. Replacing the source image (`setSourceImage`, which the cropper's `setImage` also calls) starts a fresh session with default geometry and crop options, a clean baseline, and no history. Save code asks the session, not the cropper, whether image output needs saving.
|
|
43
|
+
|
|
44
|
+
The session also holds named edits other than crop: `session.edits`, set with `session.setEdit( name, value )` and removed by passing `undefined`. They share the crop history, so undo, redo and gestures treat them like any crop change, and a different source image clears them. Values are plain data (`EditValue`: strings, numbers, booleans, `null`, and arrays and objects of those), because the session compares them by content to tell a real change from a no-op. Edits do not yet count towards `hasOutputEdits`; nothing saves them.
|
|
45
|
+
|
|
46
|
+
`session.reset()` puts every slice back to its starting state in one undo entry: default geometry and crop options, no edits, same image. The Reset button calls it; it does not compose per-slice resets itself.
|
|
47
|
+
|
|
40
48
|
### Viewport
|
|
41
49
|
|
|
42
50
|
Viewport pan/zoom lets the user inspect or follow the crop area without changing the crop output. It is not part of export and should not create undo entries.
|
|
@@ -52,4 +60,4 @@ These APIs let code inside `@wordpress/media-editor` compose the cropper. They a
|
|
|
52
60
|
| Crop data for REST requests | `getSourceRegion()` and `getSourceRegionPercent()`. |
|
|
53
61
|
| Canvas export | `exportCroppedImage()` or `applyToCanvas()`. |
|
|
54
62
|
| Shared cropper state across internal components | `CropperProvider` / `useCropper()`. |
|
|
55
|
-
| Media editor history | `useMediaEditorState()`
|
|
63
|
+
| Media editor history | `useMediaEditorState()` session. |
|
|
@@ -76,29 +76,29 @@ function CropperPanel( { src }: { src: string } ) {
|
|
|
76
76
|
}
|
|
77
77
|
```
|
|
78
78
|
|
|
79
|
-
## Use the Media Editor
|
|
79
|
+
## Use the Media Editor Session
|
|
80
80
|
|
|
81
|
-
Use the media editor
|
|
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
|
|
88
|
+
const session = useMediaEditor();
|
|
89
89
|
const aspectRatio = resolveAspectRatio(
|
|
90
|
-
|
|
91
|
-
|
|
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={
|
|
97
|
+
controller={ session.cropper }
|
|
98
98
|
aspectRatio={ aspectRatio }
|
|
99
99
|
freeformCrop
|
|
100
|
-
onGestureStart={
|
|
101
|
-
onGestureEnd={
|
|
100
|
+
onGestureStart={ session.beginGesture }
|
|
101
|
+
onGestureEnd={ session.endGesture }
|
|
102
102
|
/>
|
|
103
103
|
);
|
|
104
104
|
}
|
|
@@ -149,8 +149,8 @@ $handle-touch-target-size: 44px;
|
|
|
149
149
|
// on the canvas ensures the outline appears only when the canvas
|
|
150
150
|
// itself is focused, not when focus is on a child handle.
|
|
151
151
|
&__canvas--focus-visible:focus &__stencil-rect {
|
|
152
|
-
border-color: var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #
|
|
153
|
-
box-shadow: 0 0 0 1px var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #
|
|
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));
|
|
154
154
|
}
|
|
155
155
|
|
|
156
156
|
&__handle {
|
|
@@ -169,7 +169,7 @@ $handle-touch-target-size: 44px;
|
|
|
169
169
|
// Solid white fill with an accent-blue border so the handles read
|
|
170
170
|
// clearly against the light grey wash.
|
|
171
171
|
background: $white;
|
|
172
|
-
border: 1px solid var(--wp-admin-theme-color, #
|
|
172
|
+
border: 1px solid var(--wp-admin-theme-color, #3858e9);
|
|
173
173
|
border-radius: 50%;
|
|
174
174
|
box-sizing: border-box;
|
|
175
175
|
pointer-events: auto;
|
|
@@ -185,9 +185,9 @@ $handle-touch-target-size: 44px;
|
|
|
185
185
|
// keyboard session — e.g., clicking a handle to start a drag —
|
|
186
186
|
// don't leave the active ring stuck on the handle.
|
|
187
187
|
.wp-media-editor-image-editor__canvas--focus-visible &:focus {
|
|
188
|
-
background: var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #
|
|
188
|
+
background: var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #3858e9));
|
|
189
189
|
border-color: transparent;
|
|
190
|
-
box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #
|
|
190
|
+
box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--wp-image-editor-focus-color, var(--wp-admin-theme-color, #3858e9));
|
|
191
191
|
}
|
|
192
192
|
|
|
193
193
|
// Extend touch target to the minimum accessible hit area.
|
|
@@ -571,14 +571,17 @@ function CropperInner(
|
|
|
571
571
|
return;
|
|
572
572
|
}
|
|
573
573
|
|
|
574
|
-
|
|
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<
|
|
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;
|
|
@@ -962,23 +967,9 @@ function CropperInner(
|
|
|
962
967
|
};
|
|
963
968
|
}
|
|
964
969
|
|
|
965
|
-
// Forward the root element to the consumer's ref.
|
|
966
|
-
const setContainerRef = useCallback(
|
|
967
|
-
( element: HTMLDivElement | null ) => {
|
|
968
|
-
if ( typeof ref === 'function' ) {
|
|
969
|
-
ref( element );
|
|
970
|
-
} else if ( ref ) {
|
|
971
|
-
(
|
|
972
|
-
ref as React.MutableRefObject< HTMLDivElement | null >
|
|
973
|
-
).current = element;
|
|
974
|
-
}
|
|
975
|
-
},
|
|
976
|
-
[ ref ]
|
|
977
|
-
);
|
|
978
|
-
|
|
979
970
|
return (
|
|
980
971
|
<div
|
|
981
|
-
ref={
|
|
972
|
+
ref={ ref }
|
|
982
973
|
className={ clsx(
|
|
983
974
|
'wp-media-editor-image-editor',
|
|
984
975
|
isDragging && 'wp-media-editor-image-editor--dragging',
|
|
@@ -134,7 +134,9 @@ export function RectangleStencil( {
|
|
|
134
134
|
} ),
|
|
135
135
|
[ boundsMinX, boundsMinY, boundsMaxX, boundsMaxY ]
|
|
136
136
|
);
|
|
137
|
-
const keyboardSettleTimerRef = useRef<
|
|
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 );
|
|
@@ -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
|
-
|
|
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 );
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import fastDeepEqual from 'fast-deep-equal/es6/index.js';
|
|
1
2
|
import {
|
|
2
3
|
cropperReducer,
|
|
3
4
|
areCropperStatesEqual,
|
|
@@ -64,21 +65,31 @@ export function mediaEditorReducer(
|
|
|
64
65
|
payload: newCropRect,
|
|
65
66
|
} );
|
|
66
67
|
return {
|
|
68
|
+
...state,
|
|
67
69
|
cropper: nextCropper,
|
|
68
70
|
cropOptions: nextCropOptions,
|
|
69
71
|
};
|
|
70
72
|
}
|
|
71
|
-
case '
|
|
72
|
-
|
|
73
|
-
state.
|
|
74
|
-
|
|
75
|
-
)
|
|
73
|
+
case 'RESET': {
|
|
74
|
+
const next = buildInitialMediaEditorState(
|
|
75
|
+
cropperReducer( state.cropper, { type: 'RESET' } )
|
|
76
|
+
);
|
|
77
|
+
return areMediaEditorStatesEqual( state, next ) ? state : next;
|
|
78
|
+
}
|
|
79
|
+
case 'SET_EDIT': {
|
|
80
|
+
const { name, value } = action.payload;
|
|
81
|
+
if ( value === undefined ) {
|
|
82
|
+
if ( ! Object.hasOwn( state.edits, name ) ) {
|
|
83
|
+
return state;
|
|
84
|
+
}
|
|
85
|
+
const edits = { ...state.edits };
|
|
86
|
+
delete edits[ name ];
|
|
87
|
+
return { ...state, edits };
|
|
88
|
+
}
|
|
89
|
+
if ( fastDeepEqual( state.edits[ name ], value ) ) {
|
|
76
90
|
return state;
|
|
77
91
|
}
|
|
78
|
-
return {
|
|
79
|
-
...state,
|
|
80
|
-
cropOptions: { ...DEFAULT_CROP_OPTIONS },
|
|
81
|
-
};
|
|
92
|
+
return { ...state, edits: { ...state.edits, [ name ]: value } };
|
|
82
93
|
}
|
|
83
94
|
case 'RESTORE_SNAPSHOT':
|
|
84
95
|
return action.payload;
|
|
@@ -112,23 +123,24 @@ export function areMediaEditorStatesEqual(
|
|
|
112
123
|
}
|
|
113
124
|
return (
|
|
114
125
|
areCropperStatesEqual( a.cropper, b.cropper ) &&
|
|
115
|
-
a.cropOptions.aspectRatioValue === b.cropOptions.aspectRatioValue
|
|
126
|
+
a.cropOptions.aspectRatioValue === b.cropOptions.aspectRatioValue &&
|
|
127
|
+
fastDeepEqual( a.edits, b.edits )
|
|
116
128
|
);
|
|
117
129
|
}
|
|
118
130
|
|
|
119
131
|
/**
|
|
120
|
-
* Build the
|
|
132
|
+
* Build the composite state a session starts from: the given cropper
|
|
133
|
+
* slice, default crop options, and no edits.
|
|
121
134
|
*
|
|
122
|
-
* @param
|
|
123
|
-
* @param initialOptions Initial cropOptions-slice fields to merge.
|
|
135
|
+
* @param cropper The cropper slice.
|
|
124
136
|
* @return Composite initial state.
|
|
125
137
|
*/
|
|
126
138
|
export function buildInitialMediaEditorState(
|
|
127
|
-
|
|
128
|
-
initialOptions?: Partial< CropOptionsSlice >
|
|
139
|
+
cropper: CropperState
|
|
129
140
|
): MediaEditorState {
|
|
130
141
|
return {
|
|
131
|
-
cropper
|
|
132
|
-
cropOptions: { ...DEFAULT_CROP_OPTIONS
|
|
142
|
+
cropper,
|
|
143
|
+
cropOptions: { ...DEFAULT_CROP_OPTIONS },
|
|
144
|
+
edits: {},
|
|
133
145
|
};
|
|
134
146
|
}
|
package/src/state/index.ts
CHANGED
|
@@ -2,7 +2,7 @@ export {
|
|
|
2
2
|
useMediaEditorState,
|
|
3
3
|
resolveAspectRatio,
|
|
4
4
|
} from './use-media-editor-state';
|
|
5
|
-
export type {
|
|
5
|
+
export type { MediaEditorSession } from './use-media-editor-state';
|
|
6
6
|
export {
|
|
7
7
|
MediaEditorStateProvider,
|
|
8
8
|
useMediaEditor,
|
|
@@ -15,6 +15,8 @@ export {
|
|
|
15
15
|
export { DEFAULT_CROP_OPTIONS } from './types';
|
|
16
16
|
export type {
|
|
17
17
|
CropOptionsSlice,
|
|
18
|
+
EditsSlice,
|
|
19
|
+
EditValue,
|
|
18
20
|
MediaEditorState,
|
|
19
21
|
MediaEditorAction,
|
|
20
22
|
} from './types';
|
|
@@ -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
|
|
4
|
+
type MediaEditorSession,
|
|
6
5
|
} from './use-media-editor-state';
|
|
7
|
-
import type { CropOptionsSlice } from './types';
|
|
8
6
|
|
|
9
|
-
const MediaEditorStateContext = createContext<
|
|
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
|
|
24
|
-
*
|
|
25
|
-
*
|
|
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
|
|
37
|
+
* Consume the media editor session. Throws if used
|
|
51
38
|
* outside a `<MediaEditorStateProvider>`.
|
|
52
39
|
*
|
|
53
|
-
* @return The
|
|
40
|
+
* @return The media editor session.
|
|
54
41
|
*/
|
|
55
|
-
export function useMediaEditor():
|
|
42
|
+
export function useMediaEditor(): MediaEditorSession {
|
|
56
43
|
const context = useContext( MediaEditorStateContext );
|
|
57
44
|
if ( ! context ) {
|
|
58
45
|
throw new Error(
|
package/src/state/types.ts
CHANGED
|
@@ -27,9 +27,31 @@ export const DEFAULT_CROP_OPTIONS: CropOptionsSlice = {
|
|
|
27
27
|
};
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
30
|
+
* A value an edit can hold: plain data only, because the session
|
|
31
|
+
* compares values by content to tell a real change from a no-op.
|
|
32
|
+
* Anything else, such as a `Blob`, would compare equal to any other
|
|
33
|
+
* and the change would be dropped — keep bytes behind a URL instead.
|
|
34
|
+
*
|
|
35
|
+
* Objects must satisfy the index signature, so type a value with a
|
|
36
|
+
* `type` alias rather than an `interface`.
|
|
37
|
+
*/
|
|
38
|
+
export type EditValue =
|
|
39
|
+
| string
|
|
40
|
+
| number
|
|
41
|
+
| boolean
|
|
42
|
+
| null
|
|
43
|
+
| EditValue[]
|
|
44
|
+
| { [ key: string ]: EditValue };
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Named edits other than crop, keyed by edit name (for example
|
|
48
|
+
* `my-plugin/vignette`). An edit that is not present means "no edit".
|
|
49
|
+
*/
|
|
50
|
+
export type EditsSlice = Record< string, EditValue >;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The full composite state for the media editor — cropper geometry,
|
|
54
|
+
* the sidebar control slice, and named edits.
|
|
33
55
|
*
|
|
34
56
|
* Every undo/redo snapshot is a full `MediaEditorState`. Slices stay
|
|
35
57
|
* referentially equal across actions that don't touch them.
|
|
@@ -37,6 +59,7 @@ export const DEFAULT_CROP_OPTIONS: CropOptionsSlice = {
|
|
|
37
59
|
export interface MediaEditorState {
|
|
38
60
|
cropper: CropperState;
|
|
39
61
|
cropOptions: CropOptionsSlice;
|
|
62
|
+
edits: EditsSlice;
|
|
40
63
|
}
|
|
41
64
|
|
|
42
65
|
/**
|
|
@@ -79,8 +102,17 @@ export type MediaEditorAction =
|
|
|
79
102
|
visualSize: Size;
|
|
80
103
|
};
|
|
81
104
|
}
|
|
82
|
-
/**
|
|
83
|
-
|
|
105
|
+
/**
|
|
106
|
+
* Put every slice back to its starting state, keeping the image:
|
|
107
|
+
* default geometry, default crop options, no edits. One action,
|
|
108
|
+
* so one undo entry.
|
|
109
|
+
*/
|
|
110
|
+
| { type: 'RESET' }
|
|
111
|
+
/** Set a named edit, or remove it when `value` is `undefined`. */
|
|
112
|
+
| {
|
|
113
|
+
type: 'SET_EDIT';
|
|
114
|
+
payload: { name: string; value: EditValue | undefined };
|
|
115
|
+
}
|
|
84
116
|
/**
|
|
85
117
|
* Replace the entire composite state with `payload`. Used by
|
|
86
118
|
* undo/redo to restore a full snapshot atomically.
|