@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.
- package/CHANGELOG.md +25 -0
- package/build/components/media-editor/index.cjs +132 -50
- package/build/components/media-editor/index.cjs.map +2 -2
- package/build/components/media-editor/use-crop-options.cjs +1 -1
- 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 +2 -2
- 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 +17 -9
- package/build/components/media-editor-modal/index.cjs.map +2 -2
- 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 +3 -3
- 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 +3 -3
- package/build/state/composite-reducer.cjs.map +2 -2
- 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/use-media-editor-state.cjs +38 -36
- 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 +145 -52
- package/build-module/components/media-editor/index.mjs.map +2 -2
- package/build-module/components/media-editor/use-crop-options.mjs +1 -1
- 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 +8 -8
- package/build-module/components/media-editor-canvas/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 +17 -9
- package/build-module/components/media-editor-modal/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 +3 -3
- 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 +3 -3
- 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/use-media-editor-state.mjs +38 -36
- 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 +26 -27
- package/build-style/style.css +11 -5
- package/build-types/components/media-editor/index.d.ts +2 -2
- package/build-types/components/media-editor/index.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-image-controls/index.d.ts.map +1 -1
- package/build-types/components/media-editor-modal/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 +1 -1
- 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/use-media-editor-state.d.ts +37 -30
- 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 +29 -25
- package/src/components/media-editor/index.tsx +203 -59
- package/src/components/media-editor/test/restore-original.browser.test.tsx +227 -0
- package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +12 -10
- package/src/components/media-editor/use-crop-options.ts +1 -1
- package/src/components/media-editor/use-save-media-editor.ts +61 -20
- package/src/components/media-editor-canvas/index.tsx +7 -8
- package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +2 -4
- 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/test/index.jsdom.test.tsx +19 -13
- package/src/components/media-editor-modal/index.tsx +18 -10
- package/src/components/media-editor-modal/test/index.jsdom.test.tsx +18 -1
- package/src/components/rotation-ruler/stories/index.story.tsx +2 -2
- package/src/components/rotation-ruler/style.scss +9 -0
- package/src/hooks/use-crop-gesture-handlers.ts +3 -1
- package/src/image-editor/core/interaction-controller.ts +8 -0
- package/src/image-editor/core/test/interaction-controller.ts +45 -0
- package/src/image-editor/docs/architecture.md +6 -2
- package/src/image-editor/docs/recipes.md +8 -8
- package/src/image-editor/react/components/cropper.scss +11 -5
- package/src/image-editor/react/components/cropper.tsx +8 -3
- package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +3 -1
- package/src/image-editor/react/components/test/cropper.browser.test.tsx +7 -5
- 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/image-editor/stories/rectangle-crop.story.tsx +15 -14
- package/src/image-editor/stories/{style.css → style.module.css} +8 -37
- package/src/state/composite-reducer.ts +6 -7
- package/src/state/index.ts +1 -1
- package/src/state/media-editor-state-provider.tsx +9 -22
- package/src/state/test/use-media-editor-state.browser.test.tsx +79 -0
- package/src/state/test/use-media-editor-state.jsdom.test.ts +126 -64
- package/src/state/use-media-editor-state.ts +79 -68
- package/src/store/actions.ts +9 -0
- package/src/store/reducer.ts +6 -1
- package/src/store/selectors.ts +4 -0
- package/tsconfig.build.tsbuildinfo +1 -1
|
@@ -8,12 +8,13 @@ const IMAGE = {
|
|
|
8
8
|
naturalHeight: 600,
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
+
// Loading the image through the session, as the canvas does, so the
|
|
12
|
+
// baseline and history start from the loaded image.
|
|
11
13
|
function setupHook( visualSize = { width: 600, height: 400 } ) {
|
|
12
|
-
const view = renderHook( () =>
|
|
13
|
-
useMediaEditorState( { cropper: { image: IMAGE } } )
|
|
14
|
-
);
|
|
14
|
+
const view = renderHook( () => useMediaEditorState() );
|
|
15
15
|
act( () => {
|
|
16
|
-
view.result.current.
|
|
16
|
+
view.result.current.setSourceImage( IMAGE );
|
|
17
|
+
view.result.current.cropper.setVisualSize( visualSize );
|
|
17
18
|
} );
|
|
18
19
|
return view;
|
|
19
20
|
}
|
|
@@ -26,42 +27,53 @@ describe( 'useMediaEditorState', () => {
|
|
|
26
27
|
expect( result.current.cropOptions.aspectRatioValue ).toBe( '0' );
|
|
27
28
|
} );
|
|
28
29
|
|
|
29
|
-
it( 'merges initial cropper and cropOptions overrides', () => {
|
|
30
|
-
const { result } = renderHook( () =>
|
|
31
|
-
useMediaEditorState( {
|
|
32
|
-
cropper: { image: IMAGE },
|
|
33
|
-
cropOptions: { aspectRatioValue: '1' },
|
|
34
|
-
} )
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
expect( result.current.state.image ).toEqual( IMAGE );
|
|
38
|
-
expect( result.current.cropOptions ).toEqual( {
|
|
39
|
-
aspectRatioValue: '1',
|
|
40
|
-
} );
|
|
41
|
-
} );
|
|
42
|
-
|
|
43
30
|
it( 'starts with isDirty=false and no undo / redo', () => {
|
|
44
31
|
const { result } = setupHook();
|
|
45
32
|
|
|
46
33
|
expect( result.current.isDirty ).toBe( false );
|
|
47
|
-
expect( result.current.
|
|
34
|
+
expect( result.current.cropper.isDirty ).toBe( false );
|
|
48
35
|
expect( result.current.hasUndo ).toBe( false );
|
|
49
36
|
expect( result.current.hasRedo ).toBe( false );
|
|
50
37
|
} );
|
|
51
38
|
} );
|
|
52
39
|
|
|
40
|
+
describe( 'session edits versus image output edits', () => {
|
|
41
|
+
it( 'counts an aspect-ratio preset change as a session edit only', () => {
|
|
42
|
+
// No visual size is reported, so picking a ratio changes the
|
|
43
|
+
// preset without reshaping the crop.
|
|
44
|
+
const { result } = renderHook( () => useMediaEditorState() );
|
|
45
|
+
act( () => result.current.setSourceImage( IMAGE ) );
|
|
46
|
+
|
|
47
|
+
act( () => result.current.setAspectRatioValue( '1' ) );
|
|
48
|
+
|
|
49
|
+
expect( result.current.isDirty ).toBe( true );
|
|
50
|
+
expect( result.current.cropper.isDirty ).toBe( false );
|
|
51
|
+
expect( result.current.hasOutputEdits ).toBe( false );
|
|
52
|
+
} );
|
|
53
|
+
|
|
54
|
+
it( 'counts a geometry change as an image output edit', () => {
|
|
55
|
+
const { result } = setupHook();
|
|
56
|
+
|
|
57
|
+
act( () => result.current.cropper.setRotation( 45 ) );
|
|
58
|
+
|
|
59
|
+
expect( result.current.isDirty ).toBe( true );
|
|
60
|
+
expect( result.current.cropper.isDirty ).toBe( true );
|
|
61
|
+
expect( result.current.hasOutputEdits ).toBe( true );
|
|
62
|
+
} );
|
|
63
|
+
} );
|
|
64
|
+
|
|
53
65
|
describe( 'history — undo / redo across slices', () => {
|
|
54
66
|
it( 'records the pre-state of each action and undoes it', () => {
|
|
55
67
|
const { result } = setupHook();
|
|
56
68
|
|
|
57
|
-
act( () => result.current.setRotation( 45 ) );
|
|
69
|
+
act( () => result.current.cropper.setRotation( 45 ) );
|
|
58
70
|
|
|
59
|
-
expect( result.current.state.rotation ).toBe( 45 );
|
|
71
|
+
expect( result.current.cropper.state.rotation ).toBe( 45 );
|
|
60
72
|
expect( result.current.hasUndo ).toBe( true );
|
|
61
73
|
|
|
62
74
|
act( () => result.current.undo() );
|
|
63
75
|
|
|
64
|
-
expect( result.current.state.rotation ).toBe( 0 );
|
|
76
|
+
expect( result.current.cropper.state.rotation ).toBe( 0 );
|
|
65
77
|
expect( result.current.hasUndo ).toBe( false );
|
|
66
78
|
expect( result.current.hasRedo ).toBe( true );
|
|
67
79
|
} );
|
|
@@ -69,19 +81,19 @@ describe( 'useMediaEditorState', () => {
|
|
|
69
81
|
it( 'redo re-applies an undone action', () => {
|
|
70
82
|
const { result } = setupHook();
|
|
71
83
|
|
|
72
|
-
act( () => result.current.setRotation( 45 ) );
|
|
84
|
+
act( () => result.current.cropper.setRotation( 45 ) );
|
|
73
85
|
act( () => result.current.undo() );
|
|
74
86
|
act( () => result.current.redo() );
|
|
75
87
|
|
|
76
|
-
expect( result.current.state.rotation ).toBe( 45 );
|
|
88
|
+
expect( result.current.cropper.state.rotation ).toBe( 45 );
|
|
77
89
|
} );
|
|
78
90
|
|
|
79
91
|
it( 'redo stack clears on a new action after undo', () => {
|
|
80
92
|
const { result } = setupHook();
|
|
81
93
|
|
|
82
|
-
act( () => result.current.setRotation( 45 ) );
|
|
94
|
+
act( () => result.current.cropper.setRotation( 45 ) );
|
|
83
95
|
act( () => result.current.undo() );
|
|
84
|
-
act( () => result.current.setRotation( 90 ) );
|
|
96
|
+
act( () => result.current.cropper.setRotation( 90 ) );
|
|
85
97
|
|
|
86
98
|
expect( result.current.hasRedo ).toBe( false );
|
|
87
99
|
} );
|
|
@@ -96,17 +108,17 @@ describe( 'useMediaEditorState', () => {
|
|
|
96
108
|
|
|
97
109
|
it( 'a single undo restores both cropOptions and cropRect after aspect-ratio change', () => {
|
|
98
110
|
const { result } = setupHook();
|
|
99
|
-
const initialCropRect = result.current.state.cropRect;
|
|
111
|
+
const initialCropRect = result.current.cropper.state.cropRect;
|
|
100
112
|
const initialPreset = result.current.cropOptions.aspectRatioValue;
|
|
101
113
|
|
|
102
114
|
act( () => result.current.setAspectRatioValue( '1' ) ); // Square
|
|
103
115
|
|
|
104
116
|
// Both slices changed atomically: preset key and cropRect.
|
|
105
117
|
expect( result.current.cropOptions.aspectRatioValue ).toBe( '1' );
|
|
106
|
-
expect( result.current.state.cropRect ).not.toEqual(
|
|
118
|
+
expect( result.current.cropper.state.cropRect ).not.toEqual(
|
|
107
119
|
initialCropRect
|
|
108
120
|
);
|
|
109
|
-
expect( result.current.
|
|
121
|
+
expect( result.current.cropper.isDirty ).toBe( true );
|
|
110
122
|
// Only ONE undo entry — atomic transaction.
|
|
111
123
|
expect( result.current.hasUndo ).toBe( true );
|
|
112
124
|
|
|
@@ -115,7 +127,9 @@ describe( 'useMediaEditorState', () => {
|
|
|
115
127
|
expect( result.current.cropOptions.aspectRatioValue ).toBe(
|
|
116
128
|
initialPreset
|
|
117
129
|
);
|
|
118
|
-
expect( result.current.state.cropRect ).toEqual(
|
|
130
|
+
expect( result.current.cropper.state.cropRect ).toEqual(
|
|
131
|
+
initialCropRect
|
|
132
|
+
);
|
|
119
133
|
} );
|
|
120
134
|
} );
|
|
121
135
|
|
|
@@ -124,17 +138,17 @@ describe( 'useMediaEditorState', () => {
|
|
|
124
138
|
const { result } = setupHook();
|
|
125
139
|
|
|
126
140
|
act( () => result.current.beginGesture() );
|
|
127
|
-
act( () => result.current.setRotation( 10 ) );
|
|
128
|
-
act( () => result.current.setRotation( 20 ) );
|
|
129
|
-
act( () => result.current.setRotation( 30 ) );
|
|
141
|
+
act( () => result.current.cropper.setRotation( 10 ) );
|
|
142
|
+
act( () => result.current.cropper.setRotation( 20 ) );
|
|
143
|
+
act( () => result.current.cropper.setRotation( 30 ) );
|
|
130
144
|
act( () => result.current.endGesture() );
|
|
131
145
|
|
|
132
|
-
expect( result.current.state.rotation ).toBe( 30 );
|
|
146
|
+
expect( result.current.cropper.state.rotation ).toBe( 30 );
|
|
133
147
|
expect( result.current.hasUndo ).toBe( true );
|
|
134
148
|
|
|
135
149
|
// One undo restores all the way to the pre-gesture state.
|
|
136
150
|
act( () => result.current.undo() );
|
|
137
|
-
expect( result.current.state.rotation ).toBe( 0 );
|
|
151
|
+
expect( result.current.cropper.state.rotation ).toBe( 0 );
|
|
138
152
|
} );
|
|
139
153
|
|
|
140
154
|
it( 'an empty gesture (no changes) records no history', () => {
|
|
@@ -150,11 +164,11 @@ describe( 'useMediaEditorState', () => {
|
|
|
150
164
|
const { result } = setupHook();
|
|
151
165
|
|
|
152
166
|
act( () => result.current.beginGesture() );
|
|
153
|
-
act( () => result.current.setRotation( 45 ) );
|
|
167
|
+
act( () => result.current.cropper.setRotation( 45 ) );
|
|
154
168
|
// Undo without endGesture should still flush the in-flight change.
|
|
155
169
|
act( () => result.current.undo() );
|
|
156
170
|
|
|
157
|
-
expect( result.current.state.rotation ).toBe( 0 );
|
|
171
|
+
expect( result.current.cropper.state.rotation ).toBe( 0 );
|
|
158
172
|
expect( result.current.hasRedo ).toBe( true );
|
|
159
173
|
} );
|
|
160
174
|
|
|
@@ -163,17 +177,17 @@ describe( 'useMediaEditorState', () => {
|
|
|
163
177
|
|
|
164
178
|
act( () => {
|
|
165
179
|
result.current.beginGesture();
|
|
166
|
-
result.current.setRotation( 10 );
|
|
180
|
+
result.current.cropper.setRotation( 10 );
|
|
167
181
|
result.current.beginGesture();
|
|
168
|
-
result.current.setRotation( 20 );
|
|
182
|
+
result.current.cropper.setRotation( 20 );
|
|
169
183
|
result.current.endGesture();
|
|
170
184
|
} );
|
|
171
185
|
|
|
172
|
-
expect( result.current.state.rotation ).toBe( 20 );
|
|
186
|
+
expect( result.current.cropper.state.rotation ).toBe( 20 );
|
|
173
187
|
|
|
174
188
|
act( () => result.current.undo() );
|
|
175
189
|
|
|
176
|
-
expect( result.current.state.rotation ).toBe( 0 );
|
|
190
|
+
expect( result.current.cropper.state.rotation ).toBe( 0 );
|
|
177
191
|
} );
|
|
178
192
|
|
|
179
193
|
it( 'does not record a gesture that returns to its starting state', () => {
|
|
@@ -181,12 +195,12 @@ describe( 'useMediaEditorState', () => {
|
|
|
181
195
|
|
|
182
196
|
act( () => {
|
|
183
197
|
result.current.beginGesture();
|
|
184
|
-
result.current.setRotation( 10 );
|
|
185
|
-
result.current.setRotation( 0 );
|
|
198
|
+
result.current.cropper.setRotation( 10 );
|
|
199
|
+
result.current.cropper.setRotation( 0 );
|
|
186
200
|
result.current.endGesture();
|
|
187
201
|
} );
|
|
188
202
|
|
|
189
|
-
expect( result.current.state.rotation ).toBe( 0 );
|
|
203
|
+
expect( result.current.cropper.state.rotation ).toBe( 0 );
|
|
190
204
|
expect( result.current.hasUndo ).toBe( false );
|
|
191
205
|
} );
|
|
192
206
|
} );
|
|
@@ -200,32 +214,37 @@ describe( 'useMediaEditorState', () => {
|
|
|
200
214
|
// At visualSize 600x400, the inscribed square has w/h such that
|
|
201
215
|
// pixel ratio = 1. normalizedRatio = 1 * 400 / 600 = 0.666...
|
|
202
216
|
// So normalized w = 0.666..., h = 1.
|
|
203
|
-
expect( result.current.state.cropRect.width ).toBeCloseTo(
|
|
217
|
+
expect( result.current.cropper.state.cropRect.width ).toBeCloseTo(
|
|
204
218
|
2 / 3,
|
|
205
219
|
5
|
|
206
220
|
);
|
|
207
|
-
expect( result.current.state.cropRect.height ).toBeCloseTo(
|
|
221
|
+
expect( result.current.cropper.state.cropRect.height ).toBeCloseTo(
|
|
222
|
+
1,
|
|
223
|
+
5
|
|
224
|
+
);
|
|
208
225
|
} );
|
|
209
226
|
|
|
210
227
|
it( 'Free leaves the cropRect alone', () => {
|
|
211
228
|
const { result } = setupHook();
|
|
212
|
-
const original = result.current.state.cropRect;
|
|
229
|
+
const original = result.current.cropper.state.cropRect;
|
|
213
230
|
|
|
214
231
|
// User-picked custom crop first.
|
|
215
232
|
act( () =>
|
|
216
|
-
result.current.setCropRect( {
|
|
233
|
+
result.current.cropper.setCropRect( {
|
|
217
234
|
x: 0.1,
|
|
218
235
|
y: 0.1,
|
|
219
236
|
width: 0.5,
|
|
220
237
|
height: 0.5,
|
|
221
238
|
} )
|
|
222
239
|
);
|
|
223
|
-
const beforeFree = result.current.state.cropRect;
|
|
240
|
+
const beforeFree = result.current.cropper.state.cropRect;
|
|
224
241
|
|
|
225
242
|
// Switch to Free — cropRect should NOT snap back to anything.
|
|
226
243
|
act( () => result.current.setAspectRatioValue( '0' ) );
|
|
227
244
|
|
|
228
|
-
expect( result.current.state.cropRect ).toEqual(
|
|
245
|
+
expect( result.current.cropper.state.cropRect ).toEqual(
|
|
246
|
+
beforeFree
|
|
247
|
+
);
|
|
229
248
|
expect( original ).not.toEqual( beforeFree );
|
|
230
249
|
} );
|
|
231
250
|
|
|
@@ -236,8 +255,11 @@ describe( 'useMediaEditorState', () => {
|
|
|
236
255
|
|
|
237
256
|
// Image is 1200x600 = ratio 2. inscribedRect at visualSize 600x400:
|
|
238
257
|
// normalizedRatio = 2 * 400 / 600 = 1.333... > 1, so h = 1/1.333 = 0.75.
|
|
239
|
-
expect( result.current.state.cropRect.width ).toBeCloseTo(
|
|
240
|
-
|
|
258
|
+
expect( result.current.cropper.state.cropRect.width ).toBeCloseTo(
|
|
259
|
+
1,
|
|
260
|
+
5
|
|
261
|
+
);
|
|
262
|
+
expect( result.current.cropper.state.cropRect.height ).toBeCloseTo(
|
|
241
263
|
0.75,
|
|
242
264
|
5
|
|
243
265
|
);
|
|
@@ -247,23 +269,23 @@ describe( 'useMediaEditorState', () => {
|
|
|
247
269
|
const { result } = setupHook();
|
|
248
270
|
|
|
249
271
|
act( () => result.current.setAspectRatioValue( '1' ) );
|
|
250
|
-
act( () => result.current.setRotation( 45 ) );
|
|
272
|
+
act( () => result.current.cropper.setRotation( 45 ) );
|
|
251
273
|
|
|
252
274
|
act( () => {
|
|
253
275
|
result.current.beginGesture();
|
|
254
|
-
result.current.reset();
|
|
276
|
+
result.current.cropper.reset();
|
|
255
277
|
result.current.resetCropOptions();
|
|
256
278
|
result.current.endGesture();
|
|
257
279
|
} );
|
|
258
280
|
|
|
259
|
-
expect( result.current.state.rotation ).toBe( 0 );
|
|
281
|
+
expect( result.current.cropper.state.rotation ).toBe( 0 );
|
|
260
282
|
expect( result.current.cropOptions ).toEqual( {
|
|
261
283
|
aspectRatioValue: '0',
|
|
262
284
|
} );
|
|
263
285
|
|
|
264
286
|
act( () => result.current.undo() );
|
|
265
287
|
|
|
266
|
-
expect( result.current.state.rotation ).toBe( 45 );
|
|
288
|
+
expect( result.current.cropper.state.rotation ).toBe( 45 );
|
|
267
289
|
expect( result.current.cropOptions ).toEqual( {
|
|
268
290
|
aspectRatioValue: '1',
|
|
269
291
|
} );
|
|
@@ -275,7 +297,7 @@ describe( 'useMediaEditorState', () => {
|
|
|
275
297
|
const { result } = setupHook();
|
|
276
298
|
|
|
277
299
|
act( () =>
|
|
278
|
-
result.current.adjustCropRectForViewport( {
|
|
300
|
+
result.current.cropper.adjustCropRectForViewport( {
|
|
279
301
|
x: 0,
|
|
280
302
|
y: 0,
|
|
281
303
|
width: 0.5,
|
|
@@ -287,16 +309,16 @@ describe( 'useMediaEditorState', () => {
|
|
|
287
309
|
} );
|
|
288
310
|
} );
|
|
289
311
|
|
|
290
|
-
describe( '
|
|
312
|
+
describe( 'setSourceImage', () => {
|
|
291
313
|
it( 'clears history and resets isDirty', () => {
|
|
292
314
|
const { result } = setupHook();
|
|
293
315
|
|
|
294
|
-
act( () => result.current.setRotation( 45 ) );
|
|
316
|
+
act( () => result.current.cropper.setRotation( 45 ) );
|
|
295
317
|
expect( result.current.hasUndo ).toBe( true );
|
|
296
318
|
expect( result.current.isDirty ).toBe( true );
|
|
297
319
|
|
|
298
320
|
act( () =>
|
|
299
|
-
result.current.
|
|
321
|
+
result.current.setSourceImage( {
|
|
300
322
|
src: 'other.jpg',
|
|
301
323
|
naturalWidth: 800,
|
|
302
324
|
naturalHeight: 800,
|
|
@@ -308,17 +330,57 @@ describe( 'useMediaEditorState', () => {
|
|
|
308
330
|
expect( result.current.isDirty ).toBe( false );
|
|
309
331
|
} );
|
|
310
332
|
|
|
333
|
+
it( 'resets geometry and crop options for a new image', () => {
|
|
334
|
+
const { result } = setupHook();
|
|
335
|
+
|
|
336
|
+
act( () => result.current.cropper.setRotation( 45 ) );
|
|
337
|
+
act( () => result.current.setAspectRatioValue( '1' ) );
|
|
338
|
+
expect( result.current.cropper.state.rotation ).toBe( 45 );
|
|
339
|
+
expect( result.current.cropOptions.aspectRatioValue ).toBe( '1' );
|
|
340
|
+
|
|
341
|
+
act( () =>
|
|
342
|
+
result.current.setSourceImage( {
|
|
343
|
+
src: 'other.jpg',
|
|
344
|
+
naturalWidth: 800,
|
|
345
|
+
naturalHeight: 800,
|
|
346
|
+
} )
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
// Edits made against one image must not survive onto another.
|
|
350
|
+
expect( result.current.cropper.state.rotation ).toBe( 0 );
|
|
351
|
+
expect( result.current.cropOptions.aspectRatioValue ).toBe( '0' );
|
|
352
|
+
} );
|
|
353
|
+
|
|
354
|
+
it( 'starts a fresh session when the cropper reports a different image', () => {
|
|
355
|
+
const { result } = setupHook();
|
|
356
|
+
|
|
357
|
+
act( () => result.current.cropper.setRotation( 45 ) );
|
|
358
|
+
act( () =>
|
|
359
|
+
result.current.cropper.setImage( {
|
|
360
|
+
src: 'other.jpg',
|
|
361
|
+
naturalWidth: 800,
|
|
362
|
+
naturalHeight: 800,
|
|
363
|
+
} )
|
|
364
|
+
);
|
|
365
|
+
|
|
366
|
+
expect( result.current.cropper.state.image?.src ).toBe(
|
|
367
|
+
'other.jpg'
|
|
368
|
+
);
|
|
369
|
+
expect( result.current.cropper.state.rotation ).toBe( 0 );
|
|
370
|
+
expect( result.current.hasUndo ).toBe( false );
|
|
371
|
+
} );
|
|
372
|
+
|
|
311
373
|
it( 'does not clear history when the same image is reported again', () => {
|
|
312
374
|
const { result } = setupHook();
|
|
313
375
|
|
|
314
|
-
act( () => result.current.setRotation( 45 ) );
|
|
376
|
+
act( () => result.current.cropper.setRotation( 45 ) );
|
|
315
377
|
expect( result.current.hasUndo ).toBe( true );
|
|
316
|
-
expect( result.current.
|
|
378
|
+
expect( result.current.cropper.isDirty ).toBe( true );
|
|
317
379
|
|
|
318
|
-
act( () => result.current.
|
|
380
|
+
act( () => result.current.setSourceImage( IMAGE ) );
|
|
319
381
|
|
|
320
382
|
expect( result.current.hasUndo ).toBe( true );
|
|
321
|
-
expect( result.current.
|
|
383
|
+
expect( result.current.cropper.isDirty ).toBe( true );
|
|
322
384
|
} );
|
|
323
385
|
} );
|
|
324
386
|
} );
|
|
@@ -81,13 +81,23 @@ function areCropperImagesEqual(
|
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
/**
|
|
84
|
-
* The
|
|
84
|
+
* The media editor session exposed by `useMediaEditorState`.
|
|
85
85
|
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
86
|
+
* The session owns what is being edited and how: the source image,
|
|
87
|
+
* every editing slice, the shared undo/redo history, gesture
|
|
88
|
+
* boundaries, and whether the edits change the saved image. The
|
|
89
|
+
* cropper is one participant in the session — `cropper` is a
|
|
90
|
+
* geometry-only controller that a `<Cropper>` takes via its
|
|
91
|
+
* `controller` prop — rather than the session itself.
|
|
89
92
|
*/
|
|
90
|
-
export interface
|
|
93
|
+
export interface MediaEditorSession {
|
|
94
|
+
/**
|
|
95
|
+
* Geometry controller for the cropper slice. Its setters record
|
|
96
|
+
* history in the session; its `isDirty` covers geometry only.
|
|
97
|
+
* Its `setImage` is `setSourceImage`, so the `<Cropper>` reporting
|
|
98
|
+
* a newly loaded image starts a fresh session.
|
|
99
|
+
*/
|
|
100
|
+
cropper: CropperController;
|
|
91
101
|
/** The cropOptions slice. */
|
|
92
102
|
cropOptions: CropOptionsSlice;
|
|
93
103
|
/** Set the aspect-ratio preset. Atomic with the cropRect reshape. */
|
|
@@ -95,9 +105,22 @@ export interface MediaEditorController extends CropperController {
|
|
|
95
105
|
/** Reset cropOptions to defaults. */
|
|
96
106
|
resetCropOptions: () => void;
|
|
97
107
|
/**
|
|
98
|
-
*
|
|
108
|
+
* Replace the image being edited. A different image starts a
|
|
109
|
+
* fresh session: default geometry and crop options, a clean
|
|
110
|
+
* baseline, and no history. Reporting the same image again is a
|
|
111
|
+
* no-op.
|
|
112
|
+
*/
|
|
113
|
+
setSourceImage: ( image: CropperState[ 'image' ] ) => void;
|
|
114
|
+
/**
|
|
115
|
+
* Whether anything in the session differs from its baseline,
|
|
116
|
+
* including editor-only state such as the aspect-ratio preset.
|
|
117
|
+
*/
|
|
118
|
+
isDirty: boolean;
|
|
119
|
+
/**
|
|
120
|
+
* Whether the edits change the saved image, so saving has to
|
|
121
|
+
* produce new image output rather than only metadata.
|
|
99
122
|
*/
|
|
100
|
-
|
|
123
|
+
hasOutputEdits: boolean;
|
|
101
124
|
/** Whether there's an undoable change in history. */
|
|
102
125
|
hasUndo: boolean;
|
|
103
126
|
/** Whether there's a redoable change in history. */
|
|
@@ -114,27 +137,10 @@ export interface MediaEditorController extends CropperController {
|
|
|
114
137
|
beginGesture: () => void;
|
|
115
138
|
/** Close a gesture boundary and commit the pre-gesture snapshot. */
|
|
116
139
|
endGesture: () => void;
|
|
117
|
-
/**
|
|
118
|
-
* Report the current rendered image size to the controller. The
|
|
119
|
-
* Cropper component calls this whenever its visualSize changes;
|
|
120
|
-
* the controller needs it to compute aspect-ratio reshapes.
|
|
121
|
-
*/
|
|
122
|
-
setVisualSize: ( size: Size ) => void;
|
|
123
|
-
/**
|
|
124
|
-
* Reshape the cropRect in response to a viewport change. Behaves
|
|
125
|
-
* like `setCropRect` but does NOT record an undo entry — window
|
|
126
|
-
* resizes aren't editor actions.
|
|
127
|
-
*/
|
|
128
|
-
adjustCropRectForViewport: ( rect: NormalizedRect ) => void;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
interface InitialMediaEditorState {
|
|
132
|
-
cropper?: Partial< CropperState >;
|
|
133
|
-
cropOptions?: Partial< CropOptionsSlice >;
|
|
134
140
|
}
|
|
135
141
|
|
|
136
142
|
/**
|
|
137
|
-
*
|
|
143
|
+
* The media editor session.
|
|
138
144
|
*
|
|
139
145
|
* Owns the only undo/redo history for the editor. Cropper geometry
|
|
140
146
|
* actions and sidebar cropOptions actions flow through the same
|
|
@@ -152,35 +158,28 @@ interface InitialMediaEditorState {
|
|
|
152
158
|
* undo entries.
|
|
153
159
|
*
|
|
154
160
|
* `isDirty` tracks the full editor session, including undoable UI
|
|
155
|
-
* state. Use `
|
|
161
|
+
* state. Use `hasOutputEdits` when deciding whether the image output
|
|
156
162
|
* needs saving.
|
|
157
163
|
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
164
|
+
* The session starts empty. Call `setSourceImage` to load an image.
|
|
165
|
+
*
|
|
166
|
+
* @return The media editor session.
|
|
160
167
|
*/
|
|
161
|
-
export function useMediaEditorState(
|
|
162
|
-
initialState?: InitialMediaEditorState
|
|
163
|
-
): MediaEditorController {
|
|
168
|
+
export function useMediaEditorState(): MediaEditorSession {
|
|
164
169
|
const [ state, dispatch ] = useReducer( mediaEditorReducer, null, () =>
|
|
165
|
-
buildInitialMediaEditorState(
|
|
166
|
-
enforceContainment( {
|
|
167
|
-
...DEFAULT_STATE,
|
|
168
|
-
...initialState?.cropper,
|
|
169
|
-
} ),
|
|
170
|
-
initialState?.cropOptions
|
|
171
|
-
)
|
|
170
|
+
buildInitialMediaEditorState( { ...DEFAULT_STATE } )
|
|
172
171
|
);
|
|
173
172
|
|
|
174
173
|
// The "clean" snapshot the current state is compared against for
|
|
175
174
|
// `isDirty`. Stored in state (not a ref) so the comparison reads
|
|
176
|
-
// a value, not a ref's `current`. `
|
|
175
|
+
// a value, not a ref's `current`. `setSourceImage` refreshes this when
|
|
177
176
|
// a new image establishes a fresh baseline.
|
|
178
177
|
const [ initialBaseline, setInitialBaseline ] =
|
|
179
178
|
useState< MediaEditorState >( () => state );
|
|
180
179
|
|
|
181
180
|
// Latest-state ref used by setters that need to read fresh state
|
|
182
181
|
// (focal-point zoom, dispatch wrapper). Every mutation path
|
|
183
|
-
// (`dispatchWithHistory`, `
|
|
182
|
+
// (`dispatchWithHistory`, `setSourceImage`, `undo`, `redo`) writes this
|
|
184
183
|
// synchronously, so multiple actions in one event see the latest
|
|
185
184
|
// reducer output before React commits.
|
|
186
185
|
const stateRef = useRef( state );
|
|
@@ -281,19 +280,20 @@ export function useMediaEditorState(
|
|
|
281
280
|
[ dispatchCropperAction ]
|
|
282
281
|
);
|
|
283
282
|
|
|
284
|
-
const
|
|
283
|
+
const setSourceImage = useCallback( ( image: CropperState[ 'image' ] ) => {
|
|
285
284
|
if ( areCropperImagesEqual( stateRef.current.cropper.image, image ) ) {
|
|
286
285
|
return;
|
|
287
286
|
}
|
|
288
|
-
// New image = fresh
|
|
289
|
-
//
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
287
|
+
// New image = fresh session: default geometry and crop options, a
|
|
288
|
+
// clean baseline, and no history. Built from defaults rather than
|
|
289
|
+
// dispatching SET_IMAGE, which keeps the current pan / zoom /
|
|
290
|
+
// rotation / flip / cropRect — edits made against one image must
|
|
291
|
+
// not survive onto another.
|
|
292
|
+
const next = buildInitialMediaEditorState(
|
|
293
|
+
enforceContainment( { ...DEFAULT_STATE, image } )
|
|
294
|
+
);
|
|
295
295
|
stateRef.current = next;
|
|
296
|
-
dispatch(
|
|
296
|
+
dispatch( { type: 'RESTORE_SNAPSHOT', payload: next } );
|
|
297
297
|
setInitialBaseline( next );
|
|
298
298
|
isGestureOpenRef.current = false;
|
|
299
299
|
gestureSnapshotRef.current = null;
|
|
@@ -403,7 +403,7 @@ export function useMediaEditorState(
|
|
|
403
403
|
( mimeType?: string, quality?: number ): Promise< Blob > => {
|
|
404
404
|
if ( ! state.cropper.image ) {
|
|
405
405
|
return Promise.reject(
|
|
406
|
-
new Error( 'No image loaded — call
|
|
406
|
+
new Error( 'No image loaded — call setSourceImage first.' )
|
|
407
407
|
);
|
|
408
408
|
}
|
|
409
409
|
return exportCroppedImage(
|
|
@@ -416,40 +416,53 @@ export function useMediaEditorState(
|
|
|
416
416
|
[ state.cropper ]
|
|
417
417
|
);
|
|
418
418
|
|
|
419
|
-
const
|
|
419
|
+
const cropper = useMemo< CropperController >(
|
|
420
420
|
() => ( {
|
|
421
|
-
// CropperController surface (state is the cropper slice so a
|
|
422
|
-
// <Cropper> takes this controller as-is).
|
|
423
421
|
...cropperSetters,
|
|
424
422
|
state: state.cropper,
|
|
425
|
-
setImage,
|
|
423
|
+
setImage: setSourceImage,
|
|
426
424
|
reset,
|
|
427
|
-
isDirty,
|
|
425
|
+
isDirty: isCropperDirty,
|
|
426
|
+
getCroppedImage,
|
|
427
|
+
setVisualSize,
|
|
428
|
+
adjustCropRectForViewport,
|
|
429
|
+
} ),
|
|
430
|
+
[
|
|
431
|
+
cropperSetters,
|
|
432
|
+
state.cropper,
|
|
433
|
+
setSourceImage,
|
|
434
|
+
reset,
|
|
435
|
+
isCropperDirty,
|
|
428
436
|
getCroppedImage,
|
|
429
|
-
|
|
437
|
+
setVisualSize,
|
|
438
|
+
adjustCropRectForViewport,
|
|
439
|
+
]
|
|
440
|
+
);
|
|
441
|
+
|
|
442
|
+
const session = useMemo< MediaEditorSession >(
|
|
443
|
+
() => ( {
|
|
444
|
+
cropper,
|
|
430
445
|
cropOptions: state.cropOptions,
|
|
431
446
|
setAspectRatioValue,
|
|
432
447
|
resetCropOptions,
|
|
433
|
-
|
|
448
|
+
setSourceImage,
|
|
449
|
+
isDirty,
|
|
450
|
+
// Only geometry changes the saved image so far.
|
|
451
|
+
hasOutputEdits: isCropperDirty,
|
|
434
452
|
hasUndo,
|
|
435
453
|
hasRedo,
|
|
436
454
|
undo,
|
|
437
455
|
redo,
|
|
438
456
|
beginGesture,
|
|
439
457
|
endGesture,
|
|
440
|
-
setVisualSize,
|
|
441
|
-
adjustCropRectForViewport,
|
|
442
458
|
} ),
|
|
443
459
|
[
|
|
444
|
-
|
|
445
|
-
state.cropper,
|
|
446
|
-
setImage,
|
|
447
|
-
reset,
|
|
448
|
-
isDirty,
|
|
449
|
-
getCroppedImage,
|
|
460
|
+
cropper,
|
|
450
461
|
state.cropOptions,
|
|
451
462
|
setAspectRatioValue,
|
|
452
463
|
resetCropOptions,
|
|
464
|
+
setSourceImage,
|
|
465
|
+
isDirty,
|
|
453
466
|
isCropperDirty,
|
|
454
467
|
hasUndo,
|
|
455
468
|
hasRedo,
|
|
@@ -457,9 +470,7 @@ export function useMediaEditorState(
|
|
|
457
470
|
redo,
|
|
458
471
|
beginGesture,
|
|
459
472
|
endGesture,
|
|
460
|
-
setVisualSize,
|
|
461
|
-
adjustCropRectForViewport,
|
|
462
473
|
]
|
|
463
474
|
);
|
|
464
|
-
return
|
|
475
|
+
return session;
|
|
465
476
|
}
|
package/src/store/actions.ts
CHANGED
|
@@ -15,18 +15,27 @@ export interface MediaEditorModalUpdate {
|
|
|
15
15
|
interface OpenMediaEditorModalArgs {
|
|
16
16
|
id: number;
|
|
17
17
|
onUpdate?: ( updated: MediaEditorModalUpdate ) => void;
|
|
18
|
+
/**
|
|
19
|
+
* Restore the caller's state after a saved attachment change. May run
|
|
20
|
+
* before an asynchronous `onUpdate` finishes; the caller should cancel
|
|
21
|
+
* that update. Without this callback, Undo calls `onUpdate` with the
|
|
22
|
+
* previous attachment's id and URL.
|
|
23
|
+
*/
|
|
24
|
+
onUndo?: () => void;
|
|
18
25
|
onClose?: () => void;
|
|
19
26
|
}
|
|
20
27
|
|
|
21
28
|
export function openMediaEditorModal( {
|
|
22
29
|
id,
|
|
23
30
|
onUpdate,
|
|
31
|
+
onUndo,
|
|
24
32
|
onClose,
|
|
25
33
|
}: OpenMediaEditorModalArgs ) {
|
|
26
34
|
return {
|
|
27
35
|
type: 'OPEN_MEDIA_EDITOR_MODAL' as const,
|
|
28
36
|
id,
|
|
29
37
|
onUpdate: onUpdate ?? null,
|
|
38
|
+
onUndo: onUndo ?? null,
|
|
30
39
|
onClose: onClose ?? null,
|
|
31
40
|
};
|
|
32
41
|
}
|