@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.
Files changed (196) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/README.md +2 -1
  3. package/build/components/media-editor/index.cjs +136 -63
  4. package/build/components/media-editor/index.cjs.map +3 -3
  5. package/build/components/media-editor/use-crop-options.cjs +2 -3
  6. package/build/components/media-editor/use-crop-options.cjs.map +2 -2
  7. package/build/components/media-editor/use-save-media-editor.cjs +36 -20
  8. package/build/components/media-editor/use-save-media-editor.cjs.map +2 -2
  9. package/build/components/media-editor-canvas/index.cjs +8 -8
  10. package/build/components/media-editor-canvas/index.cjs.map +3 -3
  11. package/build/components/media-editor-crop-panel/index.cjs +12 -8
  12. package/build/components/media-editor-crop-panel/index.cjs.map +3 -3
  13. package/build/components/media-editor-fine-rotation/index.cjs +1 -1
  14. package/build/components/media-editor-fine-rotation/index.cjs.map +2 -2
  15. package/build/components/media-editor-image-controls/index.cjs +46 -29
  16. package/build/components/media-editor-image-controls/index.cjs.map +2 -2
  17. package/build/components/media-editor-modal/index.cjs +19 -9
  18. package/build/components/media-editor-modal/index.cjs.map +2 -2
  19. package/build/components/media-editor-provider/index.cjs +2 -1
  20. package/build/components/media-editor-provider/index.cjs.map +2 -2
  21. package/build/components/media-form/index.cjs +28 -28
  22. package/build/components/media-form/index.cjs.map +3 -3
  23. package/build/components/media-preview/index.cjs.map +3 -3
  24. package/build/hooks/use-crop-gesture-handlers.cjs +1 -1
  25. package/build/hooks/use-crop-gesture-handlers.cjs.map +2 -2
  26. package/build/image-editor/core/interaction-controller.cjs +5 -0
  27. package/build/image-editor/core/interaction-controller.cjs.map +2 -2
  28. package/build/image-editor/react/components/cropper.cjs +4 -14
  29. package/build/image-editor/react/components/cropper.cjs.map +2 -2
  30. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +1 -1
  31. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
  32. package/build/image-editor/react/hooks/use-aria-announcer.cjs +3 -1
  33. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
  34. package/build/image-editor/react/hooks/use-interaction.cjs +1 -1
  35. package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
  36. package/build/state/composite-reducer.cjs +35 -10
  37. package/build/state/composite-reducer.cjs.map +3 -3
  38. package/build/state/index.cjs.map +1 -1
  39. package/build/state/media-editor-state-provider.cjs +1 -6
  40. package/build/state/media-editor-state-provider.cjs.map +2 -2
  41. package/build/state/types.cjs.map +1 -1
  42. package/build/state/use-media-editor-state.cjs +57 -42
  43. package/build/state/use-media-editor-state.cjs.map +2 -2
  44. package/build/store/actions.cjs +2 -0
  45. package/build/store/actions.cjs.map +2 -2
  46. package/build/store/reducer.cjs +3 -1
  47. package/build/store/reducer.cjs.map +2 -2
  48. package/build/store/selectors.cjs +5 -0
  49. package/build/store/selectors.cjs.map +2 -2
  50. package/build-module/components/media-editor/index.mjs +151 -67
  51. package/build-module/components/media-editor/index.mjs.map +2 -2
  52. package/build-module/components/media-editor/use-crop-options.mjs +2 -3
  53. package/build-module/components/media-editor/use-crop-options.mjs.map +2 -2
  54. package/build-module/components/media-editor/use-save-media-editor.mjs +36 -20
  55. package/build-module/components/media-editor/use-save-media-editor.mjs.map +2 -2
  56. package/build-module/components/media-editor-canvas/index.mjs +10 -10
  57. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  58. package/build-module/components/media-editor-crop-panel/index.mjs +13 -9
  59. package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
  60. package/build-module/components/media-editor-fine-rotation/index.mjs +1 -1
  61. package/build-module/components/media-editor-fine-rotation/index.mjs.map +2 -2
  62. package/build-module/components/media-editor-image-controls/index.mjs +47 -36
  63. package/build-module/components/media-editor-image-controls/index.mjs.map +2 -2
  64. package/build-module/components/media-editor-modal/index.mjs +19 -9
  65. package/build-module/components/media-editor-modal/index.mjs.map +2 -2
  66. package/build-module/components/media-editor-provider/index.mjs +2 -1
  67. package/build-module/components/media-editor-provider/index.mjs.map +2 -2
  68. package/build-module/components/media-form/index.mjs +33 -30
  69. package/build-module/components/media-form/index.mjs.map +2 -2
  70. package/build-module/components/media-preview/index.mjs +2 -2
  71. package/build-module/components/media-preview/index.mjs.map +2 -2
  72. package/build-module/hooks/use-crop-gesture-handlers.mjs +1 -1
  73. package/build-module/hooks/use-crop-gesture-handlers.mjs.map +2 -2
  74. package/build-module/image-editor/core/interaction-controller.mjs +5 -0
  75. package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
  76. package/build-module/image-editor/react/components/cropper.mjs +4 -14
  77. package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
  78. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +1 -1
  79. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
  80. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +3 -1
  81. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
  82. package/build-module/image-editor/react/hooks/use-interaction.mjs +1 -1
  83. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
  84. package/build-module/state/composite-reducer.mjs +25 -10
  85. package/build-module/state/composite-reducer.mjs.map +2 -2
  86. package/build-module/state/index.mjs.map +1 -1
  87. package/build-module/state/media-editor-state-provider.mjs +1 -6
  88. package/build-module/state/media-editor-state-provider.mjs.map +2 -2
  89. package/build-module/state/types.mjs.map +1 -1
  90. package/build-module/state/use-media-editor-state.mjs +57 -42
  91. package/build-module/state/use-media-editor-state.mjs.map +2 -2
  92. package/build-module/store/actions.mjs +2 -0
  93. package/build-module/store/actions.mjs.map +2 -2
  94. package/build-module/store/reducer.mjs +3 -1
  95. package/build-module/store/reducer.mjs.map +2 -2
  96. package/build-module/store/selectors.mjs +4 -0
  97. package/build-module/store/selectors.mjs.map +2 -2
  98. package/build-style/style-rtl.css +5 -5
  99. package/build-style/style.css +5 -5
  100. package/build-types/components/media-editor/index.d.ts +4 -3
  101. package/build-types/components/media-editor/index.d.ts.map +1 -1
  102. package/build-types/components/media-editor/use-crop-options.d.ts +0 -1
  103. package/build-types/components/media-editor/use-crop-options.d.ts.map +1 -1
  104. package/build-types/components/media-editor/use-save-media-editor.d.ts +15 -3
  105. package/build-types/components/media-editor/use-save-media-editor.d.ts.map +1 -1
  106. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  107. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  108. package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -1
  109. package/build-types/components/media-editor-modal/index.d.ts +7 -2
  110. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  111. package/build-types/components/media-editor-provider/index.d.ts +7 -1
  112. package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
  113. package/build-types/components/media-form/index.d.ts +5 -2
  114. package/build-types/components/media-form/index.d.ts.map +1 -1
  115. package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -1
  116. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
  117. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  118. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  119. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
  120. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
  121. package/build-types/state/composite-reducer.d.ts +5 -5
  122. package/build-types/state/composite-reducer.d.ts.map +1 -1
  123. package/build-types/state/index.d.ts +2 -2
  124. package/build-types/state/index.d.ts.map +1 -1
  125. package/build-types/state/media-editor-state-provider.d.ts +8 -16
  126. package/build-types/state/media-editor-state-provider.d.ts.map +1 -1
  127. package/build-types/state/types.d.ts +34 -5
  128. package/build-types/state/types.d.ts.map +1 -1
  129. package/build-types/state/use-media-editor-state.d.ts +53 -35
  130. package/build-types/state/use-media-editor-state.d.ts.map +1 -1
  131. package/build-types/store/actions.d.ts +9 -1
  132. package/build-types/store/actions.d.ts.map +1 -1
  133. package/build-types/store/reducer.d.ts +3 -0
  134. package/build-types/store/reducer.d.ts.map +1 -1
  135. package/build-types/store/selectors.d.ts +1 -0
  136. package/build-types/store/selectors.d.ts.map +1 -1
  137. package/package.json +41 -25
  138. package/src/components/media-editor/index.tsx +213 -79
  139. package/src/components/media-editor/use-crop-options.ts +1 -3
  140. package/src/components/media-editor/use-save-media-editor.ts +61 -20
  141. package/src/components/media-editor-canvas/index.tsx +9 -10
  142. package/src/components/media-editor-crop-panel/index.tsx +17 -9
  143. package/src/components/media-editor-fine-rotation/index.tsx +1 -1
  144. package/src/components/media-editor-image-controls/index.tsx +41 -41
  145. package/src/components/media-editor-image-controls/style.scss +1 -1
  146. package/src/components/media-editor-modal/index.tsx +26 -11
  147. package/src/components/media-editor-provider/index.tsx +8 -1
  148. package/src/components/media-form/index.tsx +48 -42
  149. package/src/components/media-preview/index.tsx +2 -2
  150. package/src/hooks/use-crop-gesture-handlers.ts +3 -1
  151. package/src/image-editor/core/interaction-controller.ts +8 -0
  152. package/src/image-editor/docs/architecture.md +10 -2
  153. package/src/image-editor/docs/recipes.md +8 -8
  154. package/src/image-editor/react/components/cropper.scss +5 -5
  155. package/src/image-editor/react/components/cropper.tsx +9 -18
  156. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +3 -1
  157. package/src/image-editor/react/hooks/use-aria-announcer.ts +3 -1
  158. package/src/image-editor/react/hooks/use-interaction.ts +3 -2
  159. package/src/state/composite-reducer.ts +29 -17
  160. package/src/state/index.ts +3 -1
  161. package/src/state/media-editor-state-provider.tsx +9 -22
  162. package/src/state/types.ts +37 -5
  163. package/src/state/use-media-editor-state.ts +124 -78
  164. package/src/store/actions.ts +9 -0
  165. package/src/store/reducer.ts +6 -1
  166. package/src/store/selectors.ts +4 -0
  167. package/global.d.ts +0 -5
  168. package/src/components/media-editor/test/use-crop-options.jsdom.test.tsx +0 -104
  169. package/src/components/media-editor-crop-panel/test/index.jsdom.test.tsx +0 -74
  170. package/src/components/media-editor-image-controls/test/index.jsdom.test.tsx +0 -180
  171. package/src/components/media-editor-modal/test/build-modifiers.test.ts +0 -359
  172. package/src/components/media-editor-modal/test/index.jsdom.test.tsx +0 -165
  173. package/src/components/rotation-ruler/stories/index.story.tsx +0 -43
  174. package/src/components/rotation-ruler/test/index.jsdom.test.tsx +0 -140
  175. package/src/image-editor/core/export/test/canvas-renderer.jsdom.test.ts +0 -823
  176. package/src/image-editor/core/math/test/sanitize.ts +0 -242
  177. package/src/image-editor/core/test/camera.ts +0 -1218
  178. package/src/image-editor/core/test/interaction-controller.ts +0 -1254
  179. package/src/image-editor/core/test/preview-export-parity.ts +0 -249
  180. package/src/image-editor/core/test/render-camera-bridge.ts +0 -349
  181. package/src/image-editor/core/test/state.ts +0 -876
  182. package/src/image-editor/core/test/stencil-math.ts +0 -665
  183. package/src/image-editor/core/transforms/test/pipeline.ts +0 -267
  184. package/src/image-editor/react/components/stencils/test/rectangle-stencil.browser.test.tsx +0 -438
  185. package/src/image-editor/react/components/test/cropper.browser.test.tsx +0 -1460
  186. package/src/image-editor/react/hooks/test/use-aria-announcer.jsdom.test.ts +0 -491
  187. package/src/image-editor/react/hooks/test/use-cropper-reducer.jsdom.test.ts +0 -1049
  188. package/src/image-editor/react/hooks/test/use-interaction.jsdom.test.ts +0 -213
  189. package/src/image-editor/react/hooks/test/use-transform-style.jsdom.test.ts +0 -140
  190. package/src/image-editor/stories/rectangle-crop.story.tsx +0 -1073
  191. package/src/image-editor/stories/style.module.css +0 -65
  192. package/src/state/test/use-media-editor-state.jsdom.test.ts +0 -324
  193. package/src/store/test/reducer.ts +0 -64
  194. package/tsconfig.build.json +0 -26
  195. package/tsconfig.build.tsbuildinfo +0 -1
  196. package/tsconfig.json +0 -37
@@ -5,7 +5,7 @@ import { useCallback, useState } from '@wordpress/element';
5
5
  import { __, sprintf } from '@wordpress/i18n';
6
6
  import { store as noticesStore } from '@wordpress/notices';
7
7
  import type { Media } from '../media-editor-provider';
8
- import type { MediaEditorController } from '../../state';
8
+ import type { MediaEditorSession } from '../../state';
9
9
  import {
10
10
  buildModifiers,
11
11
  type Modifier,
@@ -37,11 +37,23 @@ export interface MediaEditorSaveResult {
37
37
  }
38
38
 
39
39
  interface UseSaveMediaEditorArgs {
40
- cropper: MediaEditorController;
40
+ session: MediaEditorSession;
41
41
  id: number;
42
42
  isImage: boolean;
43
43
  media?: Media | null;
44
44
  onSaved?: ( result: MediaEditorSaveResult ) => void;
45
+ /**
46
+ * When another attachment has replaced the edited one (today, the original
47
+ * via "Restore original image"), the save targets the replacement:
48
+ * - with no fresh crop, the block is repointed at the replacement and any
49
+ * changed details are saved there (no `/edit`);
50
+ * - with a fresh crop, `/edit` runs against the replacement's id and url.
51
+ */
52
+ replacementSource?: {
53
+ id: number;
54
+ url?: string;
55
+ media: Media;
56
+ };
45
57
  }
46
58
 
47
59
  interface UseSaveMediaEditorReturn {
@@ -49,13 +61,14 @@ interface UseSaveMediaEditorReturn {
49
61
  save: () => Promise< void >;
50
62
  }
51
63
 
52
- function getCropModifiers( cropper: MediaEditorController ): Modifier[] {
53
- if ( ! cropper.isCropperDirty || ! cropper.state.image ) {
64
+ function getCropModifiers( session: MediaEditorSession ): Modifier[] {
65
+ const { state } = session.cropper;
66
+ if ( ! session.hasOutputEdits || ! state.image ) {
54
67
  return [];
55
68
  }
56
- return buildModifiers( cropper.state, {
57
- width: cropper.state.image.naturalWidth,
58
- height: cropper.state.image.naturalHeight,
69
+ return buildModifiers( state, {
70
+ width: state.image.naturalWidth,
71
+ height: state.image.naturalHeight,
59
72
  } );
60
73
  }
61
74
 
@@ -80,11 +93,12 @@ function getMetadataEdits(
80
93
  }
81
94
 
82
95
  export function useSaveMediaEditor( {
83
- cropper,
96
+ session,
84
97
  id,
85
98
  isImage,
86
99
  media,
87
100
  onSaved,
101
+ replacementSource,
88
102
  }: UseSaveMediaEditorArgs ): UseSaveMediaEditorReturn {
89
103
  const registry = useRegistry();
90
104
  const {
@@ -100,9 +114,18 @@ export function useSaveMediaEditor( {
100
114
  setIsSaving( true );
101
115
  try {
102
116
  let saved: Media | null | undefined;
103
- const modifiers = getCropModifiers( cropper );
117
+ const modifiers = getCropModifiers( session );
118
+
119
+ // A replacement retargets the save; without one the current
120
+ // attachment is both source and target as before.
121
+ const targetId = replacementSource?.id ?? id;
122
+ const targetUrl = replacementSource?.url ?? media?.source_url;
123
+ const targetMedia = replacementSource?.media ?? media;
124
+
125
+ // Both a fresh crop and a bare replacement swap the block's image,
126
+ // so both offer an Undo back to the current attachment.
104
127
  const previous =
105
- modifiers.length > 0 && media
128
+ ( modifiers.length > 0 || replacementSource ) && media
106
129
  ? {
107
130
  id,
108
131
  url: media.source_url,
@@ -115,15 +138,18 @@ export function useSaveMediaEditor( {
115
138
  .getEntityRecordNonTransientEdits(
116
139
  'postType',
117
140
  'attachment',
118
- id
141
+ targetId
119
142
  ) as PendingMetadataEdits;
120
- const metadataEdits = getMetadataEdits( pendingEdits, media );
143
+ const metadataEdits = getMetadataEdits(
144
+ pendingEdits,
145
+ targetMedia
146
+ );
121
147
 
122
148
  saved = ( await apiFetch( {
123
- path: `/wp/v2/media/${ id }/edit`,
149
+ path: `/wp/v2/media/${ targetId }/edit`,
124
150
  method: 'POST',
125
151
  data: {
126
- src: media?.source_url,
152
+ src: targetUrl,
127
153
  modifiers,
128
154
  ...metadataEdits,
129
155
  },
@@ -138,23 +164,37 @@ export function useSaveMediaEditor( {
138
164
  true
139
165
  );
140
166
  }
167
+ } else if (
168
+ replacementSource &&
169
+ ! registry
170
+ .select( coreStore )
171
+ .hasEditsForEntityRecord(
172
+ 'postType',
173
+ 'attachment',
174
+ targetId
175
+ )
176
+ ) {
177
+ // A bare replacement only repoints the block. The replacement
178
+ // already exists and has no changes to persist.
179
+ saved = replacementSource.media;
141
180
  } else {
142
181
  saved = ( await saveEditedEntityRecord(
143
182
  'postType',
144
183
  'attachment',
145
- id
184
+ targetId,
185
+ { throwOnError: true }
146
186
  ) ) as Media | undefined;
147
187
  }
148
188
 
149
- const next = ( saved ?? media ) as Media | null;
189
+ const next = ( saved ?? targetMedia ) as Media | null;
150
190
 
151
- if ( next && next.id !== id ) {
152
- clearEntityRecordEdits( 'postType', 'attachment', id );
191
+ if ( next && next.id !== targetId ) {
192
+ clearEntityRecordEdits( 'postType', 'attachment', targetId );
153
193
  }
154
194
 
155
195
  if ( next && next.id ) {
156
196
  if ( next.id === id ) {
157
- cropper.reset();
197
+ session.cropper.reset();
158
198
  }
159
199
  onSaved?.( {
160
200
  id: next.id,
@@ -192,7 +232,6 @@ export function useSaveMediaEditor( {
192
232
  }, [
193
233
  clearEntityRecordEdits,
194
234
  createErrorNotice,
195
- cropper,
196
235
  id,
197
236
  isImage,
198
237
  media,
@@ -200,7 +239,9 @@ export function useSaveMediaEditor( {
200
239
  receiveEntityRecords,
201
240
  registry,
202
241
  removeAllNotices,
242
+ replacementSource,
203
243
  saveEditedEntityRecord,
244
+ session,
204
245
  ] );
205
246
 
206
247
  return { isSaving, save };
@@ -1,6 +1,6 @@
1
1
  import clsx from 'clsx';
2
2
  import { useCallback, useEffect, useMemo, useState } from '@wordpress/element';
3
- import { Spinner } from '@wordpress/components';
3
+ import { Spinner as WCSpinner } from '@wordpress/components';
4
4
  import { __ } from '@wordpress/i18n';
5
5
  import { useMediaEditorContext } from '../media-editor-provider';
6
6
  import { getMediaTypeFromMimeType } from '../../utils';
@@ -42,10 +42,10 @@ export default function MediaEditorCanvas( {
42
42
  disabled = false,
43
43
  }: MediaEditorCanvasProps ) {
44
44
  const { media } = useMediaEditorContext();
45
- const controller = useMediaEditor();
46
- const { aspectRatioValue } = controller.cropOptions;
47
- const cropperImage = controller.state.image;
48
- const { beginGesture, endGesture, setImage } = controller;
45
+ const session = useMediaEditor();
46
+ const { aspectRatioValue } = session.cropOptions;
47
+ const cropperImage = session.cropper.state.image;
48
+ const { beginGesture, endGesture, setSourceImage } = session;
49
49
 
50
50
  // Tracks whether the image pixels have actually loaded. The cropper's
51
51
  // geometry is driven by the known media dimensions, so its handles and
@@ -80,7 +80,6 @@ export default function MediaEditorCanvas( {
80
80
 
81
81
  useEffect( () => {
82
82
  if (
83
- cropperImage ||
84
83
  ! mediaUrl ||
85
84
  ! Number.isFinite( mediaWidth ) ||
86
85
  ! Number.isFinite( mediaHeight ) ||
@@ -89,12 +88,12 @@ export default function MediaEditorCanvas( {
89
88
  ) {
90
89
  return;
91
90
  }
92
- setImage( {
91
+ setSourceImage( {
93
92
  src: mediaUrl,
94
93
  naturalWidth: mediaWidth,
95
94
  naturalHeight: mediaHeight,
96
95
  } );
97
- }, [ cropperImage, mediaUrl, mediaWidth, mediaHeight, setImage ] );
96
+ }, [ mediaUrl, mediaWidth, mediaHeight, setSourceImage ] );
98
97
 
99
98
  const isImage = mediaType.type === 'image';
100
99
 
@@ -139,7 +138,7 @@ export default function MediaEditorCanvas( {
139
138
  <div className="media-editor-canvas">
140
139
  { status === 'loading' && (
141
140
  <div className="media-editor-canvas__spinner">
142
- <Spinner />
141
+ <WCSpinner />
143
142
  </div>
144
143
  ) }
145
144
  { /*
@@ -159,7 +158,7 @@ export default function MediaEditorCanvas( {
159
158
  >
160
159
  <Cropper
161
160
  src={ mediaUrl }
162
- controller={ controller }
161
+ controller={ session.cropper }
163
162
  aspectRatio={ aspectRatio }
164
163
  freeformCrop
165
164
  showGrid="interactive"
@@ -1,5 +1,4 @@
1
- import { SelectControl as WCSelectControl } from '@wordpress/components';
2
- import { Stack, VisuallyHidden } from '@wordpress/ui';
1
+ import { SelectControl, Stack, VisuallyHidden } from '@wordpress/ui';
3
2
  import { __ } from '@wordpress/i18n';
4
3
  import { CROP_CONTROL_ATTR } from '../../hooks/use-crop-gesture-handlers';
5
4
  import MediaEditorImageControls from '../media-editor-image-controls';
@@ -36,6 +35,11 @@ export default function MediaEditorCropPanel( {
36
35
  aspectRatioOptions,
37
36
  disabled = false,
38
37
  }: MediaEditorCropPanelProps ) {
38
+ const items = aspectRatioOptions.map( ( preset ) => ( {
39
+ label: preset.label,
40
+ value: preset.value.toString(),
41
+ } ) );
42
+
39
43
  return (
40
44
  // Tag the whole panel as a crop-control region so the modal's
41
45
  // Cmd+Z handler doesn't mistake the SelectControl input for a
@@ -49,15 +53,19 @@ export default function MediaEditorCropPanel( {
49
53
  { __( 'Crop options' ) }
50
54
  </VisuallyHidden>
51
55
  <MediaEditorImageControls withLabels disabled={ disabled } />
52
- <WCSelectControl
56
+ <SelectControl
53
57
  label={ __( 'Aspect ratio' ) }
54
- value={ aspectRatioValue }
55
- onChange={ onAspectRatioChange }
58
+ value={
59
+ items.find( ( item ) => item.value === aspectRatioValue ) ??
60
+ null
61
+ }
62
+ onValueChange={ ( item ) => {
63
+ if ( item && item.value !== null ) {
64
+ onAspectRatioChange( item.value );
65
+ }
66
+ } }
56
67
  disabled={ disabled }
57
- options={ aspectRatioOptions.map( ( preset ) => ( {
58
- label: preset.label,
59
- value: preset.value.toString(),
60
- } ) ) }
68
+ items={ items }
61
69
  />
62
70
  </Stack>
63
71
  );
@@ -28,7 +28,7 @@ export default function MediaEditorFineRotation( {
28
28
  onPlacementControlInteraction,
29
29
  disabled = false,
30
30
  }: MediaEditorFineRotationProps ) {
31
- const { state, setRotation } = useMediaEditor();
31
+ const { state, setRotation } = useMediaEditor().cropper;
32
32
  // `commitOnKeyUp: false` lets rapid arrow-key adjustments coalesce
33
33
  // into one undo entry via the gesture idle window. Pointer-up still
34
34
  // closes pointer drags immediately.
@@ -1,13 +1,9 @@
1
- import {
2
- Button,
3
- DropdownMenu,
4
- MenuGroup,
5
- MenuItem,
6
- } from '@wordpress/components';
1
+ import { Button } from '@wordpress/components';
2
+ // eslint-disable-next-line @wordpress/use-recommended-components -- Intentional early adoption of the new Menu, pending WordPress/gutenberg#76135.
3
+ import { Menu } from '@wordpress/ui';
7
4
  import { __ } from '@wordpress/i18n';
8
5
  import {
9
6
  aspectRatio as aspectRatioIcon,
10
- check,
11
7
  rotateLeft,
12
8
  rotateRight,
13
9
  flipHorizontal,
@@ -81,7 +77,7 @@ export default function MediaEditorImageControls( {
81
77
  zoomFactor = DEFAULT_ZOOM_FACTOR,
82
78
  disabled = false,
83
79
  }: MediaEditorImageControlsProps ) {
84
- const { state, setFlip, snapRotate90, setZoom } = useMediaEditor();
80
+ const { state, setFlip, snapRotate90, setZoom } = useMediaEditor().cropper;
85
81
  const { aspectRatioValue, setAspectRatioValue, aspectRatioOptions } =
86
82
  useCropOptions( { aspectRatioPresets } );
87
83
  const hasAspectRatioControl = ! withLabels && showAspectRatioControl;
@@ -174,39 +170,43 @@ export default function MediaEditorImageControls( {
174
170
  );
175
171
 
176
172
  const aspectRatioDropdown = hasAspectRatioControl ? (
177
- <DropdownMenu
178
- icon={ aspectRatioIcon }
179
- label={ __( 'Aspect ratio' ) }
180
- popoverProps={ { placement: 'top' } }
181
- toggleProps={ { size: 'compact', disabled } }
182
- >
183
- { ( { onClose } ) => (
184
- <MenuGroup label={ __( 'Aspect ratio' ) }>
185
- { aspectRatioOptions.map( ( preset ) => {
186
- const value = preset.value.toString();
187
- const isSelected = value === aspectRatioValue;
188
- return (
189
- <MenuItem
190
- key={ value }
191
- role="menuitemradio"
192
- isSelected={ isSelected }
193
- icon={ isSelected ? check : undefined }
194
- disabled={ disabled }
195
- onClick={ () => {
196
- if ( disabled ) {
197
- return;
198
- }
199
- setAspectRatioValue( value );
200
- onClose();
201
- } }
202
- >
203
- { preset.label }
204
- </MenuItem>
205
- );
206
- } ) }
207
- </MenuGroup>
208
- ) }
209
- </DropdownMenu>
173
+ <Menu.Root>
174
+ <Menu.Trigger
175
+ disabled={ disabled }
176
+ render={
177
+ <Button
178
+ size="compact"
179
+ icon={ aspectRatioIcon }
180
+ label={ __( 'Aspect ratio' ) }
181
+ accessibleWhenDisabled
182
+ />
183
+ }
184
+ />
185
+ <Menu.Popup
186
+ positioner={ <Menu.Positioner side="top" align="center" /> }
187
+ >
188
+ <Menu.RadioGroup
189
+ value={ aspectRatioValue }
190
+ onValueChange={ ( value ) => {
191
+ if ( ! disabled ) {
192
+ setAspectRatioValue( value );
193
+ }
194
+ } }
195
+ >
196
+ <Menu.GroupLabel>{ __( 'Aspect ratio' ) }</Menu.GroupLabel>
197
+ { aspectRatioOptions.map( ( preset ) => (
198
+ <Menu.RadioItem
199
+ key={ preset.value }
200
+ value={ preset.value.toString() }
201
+ closeOnClick
202
+ disabled={ disabled }
203
+ >
204
+ <Menu.ItemLabel>{ preset.label }</Menu.ItemLabel>
205
+ </Menu.RadioItem>
206
+ ) ) }
207
+ </Menu.RadioGroup>
208
+ </Menu.Popup>
209
+ </Menu.Root>
210
210
  ) : null;
211
211
 
212
212
  if ( withLabels ) {
@@ -30,7 +30,7 @@
30
30
 
31
31
  // Match the uppercase 11px heading the panel's other controls use for
32
32
  // their labels (Zoom, Aspect ratio) so the groups read as section
33
- // headings. Mirrors `baseLabelTypography` from `@wordpress/components`.
33
+ // headings. Matches InputControl's label typography in `@wordpress/components`.
34
34
  &__label {
35
35
  font-size: 11px;
36
36
  font-weight: var(--wpds-typography-font-weight-emphasis);
@@ -3,7 +3,7 @@ import { useDispatch, useSelect } from '@wordpress/data';
3
3
  import { __ } from '@wordpress/i18n';
4
4
  import { ShortcutProvider } from '@wordpress/keyboard-shortcuts';
5
5
  import { store as noticesStore } from '@wordpress/notices';
6
- import type { Field } from '@wordpress/dataviews';
6
+ import type { Field, Form } from '@wordpress/dataviews';
7
7
  import type { KeyboardEvent as ReactKeyboardEvent } from 'react';
8
8
  import MediaEditor from '../media-editor';
9
9
  import type { Media } from '../media-editor-provider';
@@ -19,6 +19,11 @@ interface MediaEditorModalProps {
19
19
  * since `@wordpress/media-editor` cannot depend on `@wordpress/editor`.
20
20
  */
21
21
  fields?: Field< Media >[];
22
+ /**
23
+ * Form the fields are laid out with in the Details tab. When omitted,
24
+ * a default layout of the core attachment fields is used.
25
+ */
26
+ form?: Form;
22
27
  /**
23
28
  * Fixed aspect-ratio presets for image cropping. Free and Original are
24
29
  * always provided by the media editor.
@@ -47,18 +52,23 @@ function ModalFooter() {
47
52
 
48
53
  export function MediaEditorModal( {
49
54
  fields = [],
55
+ form,
50
56
  aspectRatioPresets,
51
57
  }: MediaEditorModalProps ) {
52
- const { isModalOpen, id, onUpdate, onClose } = useSelect( ( select ) => {
53
- const { isOpen, getId, getOnUpdate, getOnClose } =
54
- select( mediaEditorStore );
55
- return {
56
- isModalOpen: isOpen(),
57
- id: getId(),
58
- onUpdate: getOnUpdate(),
59
- onClose: getOnClose(),
60
- };
61
- }, [] );
58
+ const { isModalOpen, id, onUpdate, onUndo, onClose } = useSelect(
59
+ ( select ) => {
60
+ const { isOpen, getId, getOnUpdate, getOnUndo, getOnClose } =
61
+ select( mediaEditorStore );
62
+ return {
63
+ isModalOpen: isOpen(),
64
+ id: getId(),
65
+ onUpdate: getOnUpdate(),
66
+ onUndo: getOnUndo(),
67
+ onClose: getOnClose(),
68
+ };
69
+ },
70
+ []
71
+ );
62
72
 
63
73
  const { closeMediaEditorModal } = useDispatch( mediaEditorStore );
64
74
  const { createSuccessNotice } = useDispatch( noticesStore );
@@ -89,6 +99,7 @@ export function MediaEditorModal( {
89
99
  <MediaEditor
90
100
  id={ id }
91
101
  fields={ fields }
102
+ form={ form }
92
103
  aspectRatioPresets={ aspectRatioPresets }
93
104
  shouldCloseOnEsc
94
105
  noticesClassName="media-editor-modal__snackbar"
@@ -113,6 +124,10 @@ export function MediaEditorModal( {
113
124
  {
114
125
  label: __( 'Undo' ),
115
126
  onClick: () => {
127
+ if ( onUndo ) {
128
+ onUndo();
129
+ return;
130
+ }
116
131
  onUpdate( {
117
132
  id: previous.id,
118
133
  url: previous.url,
@@ -1,5 +1,5 @@
1
1
  import { createContext, useContext } from '@wordpress/element';
2
- import type { Field } from '@wordpress/dataviews';
2
+ import type { Field, Form } from '@wordpress/dataviews';
3
3
  import type { ReactNode } from 'react';
4
4
 
5
5
  /**
@@ -23,6 +23,7 @@ export interface MediaEditorContextValue {
23
23
  media?: Media;
24
24
  onChange?: ( updates: Partial< Media > ) => void;
25
25
  fields: Field< Media >[];
26
+ form?: Form;
26
27
  }
27
28
 
28
29
  /**
@@ -41,6 +42,11 @@ export interface MediaEditorProviderProps {
41
42
  /** Configuration settings for the media editor. */
42
43
  settings?: {
43
44
  fields?: Field< Media >[];
45
+ /**
46
+ * The form the fields are laid out with. When omitted, `MediaForm`
47
+ * uses a default layout of the core attachment fields.
48
+ */
49
+ form?: Form;
44
50
  };
45
51
  /** Child components. */
46
52
  children: ReactNode;
@@ -60,6 +66,7 @@ export function MediaEditorProvider( {
60
66
  media: value,
61
67
  onChange,
62
68
  fields: settings.fields || [],
69
+ form: settings.form,
63
70
  };
64
71
 
65
72
  return (
@@ -1,11 +1,43 @@
1
1
  import { DataForm } from '@wordpress/dataviews';
2
- import type { Form, Field } from '@wordpress/dataviews';
3
- import { Spinner, __experimentalVStack as VStack } from '@wordpress/components';
2
+ import type { Form } from '@wordpress/dataviews';
3
+ import {
4
+ Spinner as WCSpinner,
5
+ __experimentalVStack as VStack,
6
+ } from '@wordpress/components';
4
7
  import { VisuallyHidden } from '@wordpress/ui';
5
8
  import { __ } from '@wordpress/i18n';
6
9
  import type { ReactNode } from 'react';
7
10
  import { useMediaEditorContext } from '../media-editor-provider';
8
- import type { Media } from '../media-editor-provider';
11
+
12
+ /*
13
+ * The form used when neither the component nor the provider settings have
14
+ * one, mirroring the `form` of the `attachment` view configuration on the
15
+ * server. It lists the core attachment fields in a fixed order, so the layout
16
+ * does not depend on the order the fields are passed in: the editable fields
17
+ * first, in a regular layout, then the metadata and the file information, in
18
+ * panels. A field it lists that is not passed renders nothing.
19
+ */
20
+ const DEFAULT_FORM: Form = {
21
+ layout: {
22
+ type: 'panel',
23
+ },
24
+ fields: [
25
+ ...[ 'title', 'alt_text', 'caption', 'description' ].map( ( id ) => ( {
26
+ id,
27
+ layout: {
28
+ type: 'regular' as const,
29
+ labelPosition: 'top' as const,
30
+ },
31
+ } ) ),
32
+ 'date',
33
+ 'author',
34
+ 'filename',
35
+ 'mime_type',
36
+ 'filesize',
37
+ 'media_dimensions',
38
+ 'attached_to',
39
+ ],
40
+ };
9
41
 
10
42
  /**
11
43
  * Props for MediaForm component.
@@ -19,10 +51,13 @@ export interface MediaFormProps {
19
51
  * MediaForm component for editing media metadata.
20
52
  *
21
53
  * Renders a DataForm with fields for editing media properties like
22
- * title, alt text, caption, description, etc.
54
+ * title, alt text, caption, description, etc. The fields are laid out with
55
+ * the `form` prop, else the form of the provider settings, else a default
56
+ * form of the core attachment fields.
23
57
  *
24
58
  * @param props - Component props.
25
- * @param props.form - Optional form configuration.
59
+ * @param props.form - Optional form configuration, which takes precedence
60
+ * over the form of the provider settings.
26
61
  * @param props.header - Optional header content to display above the form.
27
62
  * @return The MediaForm component.
28
63
  */
@@ -30,51 +65,22 @@ export default function MediaForm( {
30
65
  form: formOverrides,
31
66
  header,
32
67
  }: MediaFormProps ) {
33
- const { media, fields, onChange } = useMediaEditorContext();
68
+ const {
69
+ media,
70
+ fields,
71
+ form: settingsForm,
72
+ onChange,
73
+ } = useMediaEditorContext();
34
74
 
35
75
  if ( ! media || ! onChange ) {
36
76
  return (
37
77
  <div className="media-editor-form media-editor-form--loading">
38
- <Spinner />
78
+ <WCSpinner />
39
79
  </div>
40
80
  );
41
81
  }
42
82
 
43
- // Fields that use a regular (non-panel) layout, rendered at the top.
44
- const regularFieldIds = [ 'title', 'alt_text', 'caption', 'description' ];
45
-
46
- // Place the non-panel (regular layout) fields at the top of the array,
47
- // with the remaining panel fields below.
48
- const sortedFields = [
49
- ...fields.filter( ( field: Field< Media > ) =>
50
- regularFieldIds.includes( field.id )
51
- ),
52
- ...fields.filter(
53
- ( field: Field< Media > ) => ! regularFieldIds.includes( field.id )
54
- ),
55
- ];
56
-
57
- // Default form structure with panel layout
58
- const defaultForm: Form = {
59
- layout: {
60
- type: 'panel',
61
- },
62
- fields: sortedFields.map( ( field: Field< Media > ) => {
63
- // Use regular layout for main editable fields
64
- if ( regularFieldIds.includes( field.id ) ) {
65
- return {
66
- id: field.id,
67
- layout: {
68
- type: 'regular',
69
- labelPosition: 'top',
70
- },
71
- };
72
- }
73
- return field.id;
74
- } ),
75
- };
76
-
77
- const form = formOverrides || defaultForm;
83
+ const form = formOverrides || settingsForm || DEFAULT_FORM;
78
84
 
79
85
  return (
80
86
  <div className="media-editor-form">
@@ -1,4 +1,4 @@
1
- import { Spinner } from '@wordpress/components';
1
+ import { Spinner as WCSpinner } from '@wordpress/components';
2
2
  import { useState } from '@wordpress/element';
3
3
  import { __ } from '@wordpress/i18n';
4
4
  import { useMediaEditorContext } from '../media-editor-provider';
@@ -130,7 +130,7 @@ export default function MediaPreview( props: MediaPreviewProps ) {
130
130
  >
131
131
  { mediaType.type === 'image' && loadingState === 'loading' && (
132
132
  <div className="media-editor-preview__spinner">
133
- <Spinner />
133
+ <WCSpinner />
134
134
  </div>
135
135
  ) }
136
136
  <MediaPreviewContent
@@ -43,7 +43,9 @@ export function useCropGestureHandlers(
43
43
  ) {
44
44
  const { commitOnKeyUp = true } = options;
45
45
  const { beginGesture, endGesture } = useMediaEditor();
46
- const keyboardTimerRef = useRef< ReturnType< typeof setTimeout > >();
46
+ const keyboardTimerRef = useRef<
47
+ ReturnType< typeof setTimeout > | undefined
48
+ >( undefined );
47
49
 
48
50
  const clearKeyboardTimer = useCallback( () => {
49
51
  clearTimeout( keyboardTimerRef.current );
@@ -305,6 +305,14 @@ export class InteractionController {
305
305
  // Capture pointer so drag works across iframe boundaries.
306
306
  el.setPointerCapture( e.pointerId );
307
307
 
308
+ // End a pending wheel gesture now, so its debounce timer cannot
309
+ // fire mid-drag and end the drag's gesture along with it.
310
+ if ( this.wheelGestureActive ) {
311
+ clearTimeout( this.wheelGestureTimer );
312
+ this.wheelGestureActive = false;
313
+ this.options.onGestureEnd?.();
314
+ }
315
+
308
316
  this.setStatus( { isDragging: true } );
309
317
  this.options.onGestureStart?.();
310
318