@wordpress/media-editor 0.8.0 → 0.8.1-next.v.202605131032.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 (217) hide show
  1. package/build/components/media-editor/index.cjs +560 -0
  2. package/build/components/media-editor/index.cjs.map +7 -0
  3. package/build/components/media-editor-canvas/index.cjs +14 -2
  4. package/build/components/media-editor-canvas/index.cjs.map +2 -2
  5. package/build/components/media-editor-crop-panel/index.cjs +34 -27
  6. package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
  7. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +162 -0
  8. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +7 -0
  9. package/build/components/media-editor-modal/index.cjs +42 -430
  10. package/build/components/media-editor-modal/index.cjs.map +3 -3
  11. package/build/components/media-editor-toolbar/index.cjs +143 -75
  12. package/build/components/media-editor-toolbar/index.cjs.map +3 -3
  13. package/build/components/media-form/index.cjs +3 -0
  14. package/build/components/media-form/index.cjs.map +2 -2
  15. package/build/components/rotation-ruler/index.cjs +197 -0
  16. package/build/components/rotation-ruler/index.cjs.map +7 -0
  17. package/build/components/rotation-ruler/use-ruler-drag.cjs +154 -0
  18. package/build/components/rotation-ruler/use-ruler-drag.cjs.map +7 -0
  19. package/build/hooks/use-crop-gesture-handlers.cjs +43 -0
  20. package/build/hooks/use-crop-gesture-handlers.cjs.map +7 -0
  21. package/build/image-editor/core/constants.cjs +9 -0
  22. package/build/image-editor/core/constants.cjs.map +2 -2
  23. package/build/image-editor/core/containment.cjs +7 -13
  24. package/build/image-editor/core/containment.cjs.map +2 -2
  25. package/build/image-editor/core/interaction-controller.cjs +43 -7
  26. package/build/image-editor/core/interaction-controller.cjs.map +2 -2
  27. package/build/image-editor/core/state.cjs +5 -1
  28. package/build/image-editor/core/state.cjs.map +2 -2
  29. package/build/image-editor/core/transform-style.cjs.map +1 -1
  30. package/build/image-editor/core/types.cjs.map +1 -1
  31. package/build/image-editor/core/viewport-state.cjs +50 -0
  32. package/build/image-editor/core/viewport-state.cjs.map +7 -0
  33. package/build/image-editor/react/components/cropper.cjs +192 -72
  34. package/build/image-editor/react/components/cropper.cjs.map +3 -3
  35. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +5 -2
  36. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +2 -2
  37. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +16 -4
  38. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
  39. package/build/image-editor/react/components/viewport-provider.cjs +51 -0
  40. package/build/image-editor/react/components/viewport-provider.cjs.map +7 -0
  41. package/build/image-editor/react/hooks/use-aria-announcer.cjs +53 -6
  42. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
  43. package/build/image-editor/react/hooks/use-cropper-state.cjs +127 -8
  44. package/build/image-editor/react/hooks/use-cropper-state.cjs.map +2 -2
  45. package/build/image-editor/react/hooks/use-interaction.cjs +26 -5
  46. package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
  47. package/build/image-editor/react/hooks/use-viewport-state.cjs +51 -0
  48. package/build/image-editor/react/hooks/use-viewport-state.cjs.map +7 -0
  49. package/build/image-editor/react/visually-hidden-style.cjs +41 -0
  50. package/build/image-editor/react/visually-hidden-style.cjs.map +7 -0
  51. package/build/private-apis.cjs +2 -0
  52. package/build/private-apis.cjs.map +2 -2
  53. package/build-module/components/media-editor/index.mjs +545 -0
  54. package/build-module/components/media-editor/index.mjs.map +7 -0
  55. package/build-module/components/media-editor-canvas/index.mjs +14 -2
  56. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  57. package/build-module/components/media-editor-crop-panel/index.mjs +35 -28
  58. package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
  59. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +144 -0
  60. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +7 -0
  61. package/build-module/components/media-editor-modal/index.mjs +46 -455
  62. package/build-module/components/media-editor-modal/index.mjs.map +2 -2
  63. package/build-module/components/media-editor-toolbar/index.mjs +137 -77
  64. package/build-module/components/media-editor-toolbar/index.mjs.map +2 -2
  65. package/build-module/components/media-form/index.mjs +3 -0
  66. package/build-module/components/media-form/index.mjs.map +2 -2
  67. package/build-module/components/rotation-ruler/index.mjs +166 -0
  68. package/build-module/components/rotation-ruler/index.mjs.map +7 -0
  69. package/build-module/components/rotation-ruler/use-ruler-drag.mjs +126 -0
  70. package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +7 -0
  71. package/build-module/hooks/use-crop-gesture-handlers.mjs +17 -0
  72. package/build-module/hooks/use-crop-gesture-handlers.mjs.map +7 -0
  73. package/build-module/image-editor/core/constants.mjs +6 -0
  74. package/build-module/image-editor/core/constants.mjs.map +2 -2
  75. package/build-module/image-editor/core/containment.mjs +6 -12
  76. package/build-module/image-editor/core/containment.mjs.map +2 -2
  77. package/build-module/image-editor/core/interaction-controller.mjs +50 -8
  78. package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
  79. package/build-module/image-editor/core/state.mjs +5 -1
  80. package/build-module/image-editor/core/state.mjs.map +2 -2
  81. package/build-module/image-editor/core/transform-style.mjs.map +1 -1
  82. package/build-module/image-editor/core/viewport-state.mjs +24 -0
  83. package/build-module/image-editor/core/viewport-state.mjs.map +7 -0
  84. package/build-module/image-editor/react/components/cropper.mjs +195 -73
  85. package/build-module/image-editor/react/components/cropper.mjs.map +3 -3
  86. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +5 -2
  87. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +2 -2
  88. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +26 -5
  89. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
  90. package/build-module/image-editor/react/components/viewport-provider.mjs +27 -0
  91. package/build-module/image-editor/react/components/viewport-provider.mjs.map +7 -0
  92. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +59 -7
  93. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
  94. package/build-module/image-editor/react/hooks/use-cropper-state.mjs +134 -9
  95. package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +2 -2
  96. package/build-module/image-editor/react/hooks/use-interaction.mjs +26 -5
  97. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
  98. package/build-module/image-editor/react/hooks/use-viewport-state.mjs +29 -0
  99. package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +7 -0
  100. package/build-module/image-editor/react/visually-hidden-style.mjs +16 -0
  101. package/build-module/image-editor/react/visually-hidden-style.mjs.map +7 -0
  102. package/build-module/private-apis.mjs +2 -0
  103. package/build-module/private-apis.mjs.map +2 -2
  104. package/build-style/style-rtl.css +254 -29
  105. package/build-style/style.css +254 -29
  106. package/build-types/components/media-editor/index.d.ts +34 -0
  107. package/build-types/components/media-editor/index.d.ts.map +1 -0
  108. package/build-types/components/media-editor-canvas/index.d.ts +10 -1
  109. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  110. package/build-types/components/media-editor-crop-panel/index.d.ts +1 -1
  111. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  112. package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts +6 -0
  113. package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts.map +1 -0
  114. package/build-types/components/media-editor-modal/index.d.ts +2 -2
  115. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  116. package/build-types/components/media-editor-provider/index.d.ts +1 -1
  117. package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
  118. package/build-types/components/media-editor-toolbar/index.d.ts +4 -1
  119. package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -1
  120. package/build-types/components/media-form/index.d.ts +1 -1
  121. package/build-types/components/media-form/index.d.ts.map +1 -1
  122. package/build-types/components/rotation-ruler/index.d.ts +46 -0
  123. package/build-types/components/rotation-ruler/index.d.ts.map +1 -0
  124. package/build-types/components/rotation-ruler/stories/index.story.d.ts +14 -0
  125. package/build-types/components/rotation-ruler/stories/index.story.d.ts.map +1 -0
  126. package/build-types/components/rotation-ruler/use-ruler-drag.d.ts +61 -0
  127. package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -0
  128. package/build-types/hooks/use-crop-gesture-handlers.d.ts +42 -0
  129. package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -0
  130. package/build-types/image-editor/core/constants.d.ts +9 -0
  131. package/build-types/image-editor/core/constants.d.ts.map +1 -1
  132. package/build-types/image-editor/core/containment.d.ts +11 -7
  133. package/build-types/image-editor/core/containment.d.ts.map +1 -1
  134. package/build-types/image-editor/core/interaction-controller.d.ts +14 -2
  135. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
  136. package/build-types/image-editor/core/state.d.ts.map +1 -1
  137. package/build-types/image-editor/core/transform-style.d.ts +1 -1
  138. package/build-types/image-editor/core/types.d.ts +25 -0
  139. package/build-types/image-editor/core/types.d.ts.map +1 -1
  140. package/build-types/image-editor/core/viewport-state.d.ts +7 -0
  141. package/build-types/image-editor/core/viewport-state.d.ts.map +1 -0
  142. package/build-types/image-editor/react/components/cropper-provider.d.ts +1 -1
  143. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
  144. package/build-types/image-editor/react/components/cropper.d.ts +2 -1
  145. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  146. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +3 -13
  147. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -1
  148. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +1 -1
  149. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -1
  150. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +1 -1
  151. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  152. package/build-types/image-editor/react/components/viewport-provider.d.ts +9 -0
  153. package/build-types/image-editor/react/components/viewport-provider.d.ts.map +1 -0
  154. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
  155. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +29 -0
  156. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -1
  157. package/build-types/image-editor/react/hooks/use-interaction.d.ts +5 -2
  158. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
  159. package/build-types/image-editor/react/hooks/use-viewport-state.d.ts +19 -0
  160. package/build-types/image-editor/react/hooks/use-viewport-state.d.ts.map +1 -0
  161. package/build-types/image-editor/react/visually-hidden-style.d.ts +6 -0
  162. package/build-types/image-editor/react/visually-hidden-style.d.ts.map +1 -0
  163. package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -1
  164. package/build-types/private-apis.d.ts.map +1 -1
  165. package/build-types/store/actions.d.ts +3 -3
  166. package/build-types/store/actions.d.ts.map +1 -1
  167. package/package.json +16 -15
  168. package/src/components/media-editor/index.tsx +709 -0
  169. package/src/components/media-editor/style.scss +89 -0
  170. package/src/components/media-editor-canvas/index.tsx +24 -0
  171. package/src/components/media-editor-crop-panel/index.tsx +39 -27
  172. package/src/components/media-editor-crop-panel/test/index.tsx +109 -0
  173. package/src/components/media-editor-keyboard-shortcuts-modal/index.tsx +177 -0
  174. package/src/components/media-editor-keyboard-shortcuts-modal/style.scss +53 -0
  175. package/src/components/media-editor-modal/index.tsx +49 -589
  176. package/src/components/media-editor-modal/style.scss +13 -112
  177. package/src/components/media-editor-toolbar/index.tsx +122 -63
  178. package/src/components/media-editor-toolbar/style.scss +39 -11
  179. package/src/components/media-form/index.tsx +5 -0
  180. package/src/components/rotation-ruler/README.md +37 -0
  181. package/src/components/rotation-ruler/index.tsx +266 -0
  182. package/src/components/rotation-ruler/stories/index.story.tsx +54 -0
  183. package/src/components/rotation-ruler/style.scss +147 -0
  184. package/src/components/rotation-ruler/test/index.tsx +109 -0
  185. package/src/components/rotation-ruler/use-ruler-drag.ts +230 -0
  186. package/src/hooks/use-crop-gesture-handlers.ts +54 -0
  187. package/src/image-editor/core/constants.ts +12 -0
  188. package/src/image-editor/core/containment.ts +16 -28
  189. package/src/image-editor/core/interaction-controller.ts +66 -10
  190. package/src/image-editor/core/state.ts +15 -8
  191. package/src/image-editor/core/test/camera.ts +15 -26
  192. package/src/image-editor/core/test/interaction-controller.ts +158 -7
  193. package/src/image-editor/core/test/state.ts +21 -1
  194. package/src/image-editor/core/transform-style.ts +1 -1
  195. package/src/image-editor/core/types.ts +16 -0
  196. package/src/image-editor/core/viewport-state.ts +33 -0
  197. package/src/image-editor/docs/architecture.md +1 -1
  198. package/src/image-editor/docs/recipes.md +2 -2
  199. package/src/image-editor/react/components/cropper.scss +28 -0
  200. package/src/image-editor/react/components/cropper.tsx +268 -72
  201. package/src/image-editor/react/components/overlays/dimming-overlay.tsx +12 -12
  202. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +27 -8
  203. package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +29 -3
  204. package/src/image-editor/react/components/test/cropper.tsx +294 -5
  205. package/src/image-editor/react/components/viewport-provider.tsx +39 -0
  206. package/src/image-editor/react/hooks/test/use-aria-announcer.ts +81 -0
  207. package/src/image-editor/react/hooks/test/use-cropper-state.ts +286 -0
  208. package/src/image-editor/react/hooks/test/use-interaction.ts +1 -0
  209. package/src/image-editor/react/hooks/use-aria-announcer.ts +68 -7
  210. package/src/image-editor/react/hooks/use-cropper-state.ts +230 -11
  211. package/src/image-editor/react/hooks/use-interaction.ts +36 -6
  212. package/src/image-editor/react/hooks/use-viewport-state.ts +48 -0
  213. package/src/image-editor/react/visually-hidden-style.ts +15 -0
  214. package/src/private-apis.ts +2 -0
  215. package/src/style.scss +3 -0
  216. package/tsconfig.json +1 -0
  217. package/tsconfig.tsbuildinfo +1 -1
@@ -1,80 +1,19 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import apiFetch from '@wordpress/api-fetch';
5
- import {
6
- Button,
7
- Flex,
8
- Modal,
9
- Spinner,
10
- __experimentalConfirmDialog as ConfirmDialog,
11
- privateApis as componentsPrivateApis,
12
- } from '@wordpress/components';
13
- import { Stack } from '@wordpress/ui';
14
- import { useDispatch, useRegistry, useSelect } from '@wordpress/data';
15
- import { store as coreStore } from '@wordpress/core-data';
16
- import {
17
- createPortal,
18
- useCallback,
19
- useContext,
20
- useEffect,
21
- useMemo,
22
- useRef,
23
- useState,
24
- } from '@wordpress/element';
25
- import { __, sprintf } from '@wordpress/i18n';
26
- import { close, drawerRight } from '@wordpress/icons';
27
- import { SnackbarNotices, store as noticesStore } from '@wordpress/notices';
4
+ import { Modal } from '@wordpress/components';
5
+ import { useDispatch, useSelect } from '@wordpress/data';
6
+ import { __ } from '@wordpress/i18n';
28
7
  import type { Field } from '@wordpress/dataviews';
29
- import {
30
- ComplementaryArea,
31
- InterfaceSkeleton,
32
- PinnedItems,
33
- // No type declarations available for @wordpress/interface.
34
- // @ts-expect-error
35
- } from '@wordpress/interface';
36
8
 
37
9
  /**
38
10
  * Internal dependencies
39
11
  */
40
- import { MediaEditorProvider } from '../media-editor-provider';
12
+ import MediaEditor from '../media-editor';
41
13
  import type { Media } from '../media-editor-provider';
42
- import MediaPreview from '../media-preview';
43
- import MediaEditorCanvas from '../media-editor-canvas';
44
- import MediaEditorToolbar from '../media-editor-toolbar';
45
- import MediaEditorCropPanel, {
46
- resolveAspectRatio,
47
- } from '../media-editor-crop-panel';
48
- import MediaForm from '../media-form';
49
14
  import { store as mediaEditorStore } from '../../store';
50
15
  import type { MediaEditorModalUpdate } from '../../store/actions';
51
- import { unlock } from '../../lock-unlock';
52
- import { getMediaTypeFromMimeType } from '../../utils';
53
- import { CropperProvider, useCropper } from '../../image-editor';
54
16
  import type { AspectRatioPreset } from '../../image-editor/core/constants';
55
- import { buildModifiers } from './build-modifiers';
56
-
57
- // Details-tab edits the modal bundles into a transformed `/edit` request.
58
- // Matches Core's `WP_REST_Attachments_Controller::get_edit_media_item_args`
59
- // — that endpoint's arg schema explicitly whitelists only these fields
60
- // (title / caption / description / alt_text / post), so forwarding any
61
- // others would fail REST validation with `rest_invalid_param`. Staged
62
- // edits to fields outside this list are not forwarded; a follow-up could
63
- // persist them via a separate `saveEditedEntityRecord` call.
64
- const METADATA_EDIT_KEYS = [
65
- 'title',
66
- 'caption',
67
- 'description',
68
- 'alt_text',
69
- 'post',
70
- ] as const;
71
-
72
- // Scope save-failure snackbars to this modal so they don't leak into the
73
- // host editor's notices tray (and vice versa).
74
- const NOTICES_CONTEXT = 'media-editor';
75
- const PLACEMENT_CONTROL_IDLE_MS = 300;
76
-
77
- const { Tabs } = unlock( componentsPrivateApis );
78
17
 
79
18
  interface MediaEditorModalProps {
80
19
  /**
@@ -86,494 +25,11 @@ interface MediaEditorModalProps {
86
25
  fields?: Field< Media >[];
87
26
  /**
88
27
  * Fixed aspect-ratio presets for image cropping. Free and Original are
89
- * always provided by the modal.
28
+ * always provided by the media editor.
90
29
  */
91
30
  aspectRatioPresets?: AspectRatioPreset[];
92
31
  }
93
32
 
94
- interface ModalTab {
95
- id: string;
96
- title: string;
97
- panel: JSX.Element;
98
- }
99
-
100
- // Renders the `ComplementaryArea` with a tab list in its header, mirroring
101
- // the post editor's pattern in
102
- // `packages/editor/src/components/sidebar/index.js`. The `header` prop
103
- // replaces `ComplementaryArea`'s default `<h2>{ title }</h2>` row — `title`
104
- // is still passed so it can label the pinned toolbar button. Tabs context
105
- // must be re-provided on both sides of the Slot/Fill because the Fill
106
- // doesn't preserve the Tabs React context across to the Slot.
107
- function MediaEditorModalSidebar( { tabs }: { tabs: ModalTab[] } ) {
108
- const tabsContextValue = useContext( Tabs.Context );
109
- return (
110
- <ComplementaryArea
111
- scope="media-editor"
112
- identifier="media-editor/details"
113
- title={ __( 'Details' ) }
114
- icon={ drawerRight }
115
- isActiveByDefault
116
- className="media-editor-modal__sidebar"
117
- panelClassName="media-editor-modal__sidebar-panel"
118
- headerClassName="media-editor-modal__sidebar-header"
119
- header={
120
- <Tabs.Context.Provider value={ tabsContextValue }>
121
- <Tabs.TabList>
122
- { tabs.map( ( tab ) => (
123
- <Tabs.Tab key={ tab.id } tabId={ tab.id }>
124
- { tab.title }
125
- </Tabs.Tab>
126
- ) ) }
127
- </Tabs.TabList>
128
- </Tabs.Context.Provider>
129
- }
130
- >
131
- <Tabs.Context.Provider value={ tabsContextValue }>
132
- { tabs.map( ( tab ) => (
133
- <Tabs.TabPanel
134
- key={ tab.id }
135
- tabId={ tab.id }
136
- focusable={ false }
137
- >
138
- { tab.panel }
139
- </Tabs.TabPanel>
140
- ) ) }
141
- </Tabs.Context.Provider>
142
- </ComplementaryArea>
143
- );
144
- }
145
-
146
- interface HeaderActionsProps {
147
- isSaving: boolean;
148
- hasMedia: boolean;
149
- hasChanges: boolean;
150
- onCancel: () => void;
151
- onSave: () => void;
152
- }
153
-
154
- function HeaderActions( {
155
- isSaving,
156
- hasMedia,
157
- hasChanges,
158
- onCancel,
159
- onSave,
160
- }: HeaderActionsProps ) {
161
- const saveDisabled = isSaving || ! hasMedia || ! hasChanges;
162
- return (
163
- <Flex
164
- className="media-editor-modal__header-actions"
165
- justify="flex-end"
166
- expanded={ false }
167
- gap={ 2 }
168
- >
169
- <PinnedItems.Slot scope="media-editor" />
170
- <Button
171
- size="compact"
172
- variant="tertiary"
173
- onClick={ onCancel }
174
- disabled={ isSaving }
175
- accessibleWhenDisabled
176
- >
177
- { __( 'Cancel' ) }
178
- </Button>
179
- <Button
180
- size="compact"
181
- variant="primary"
182
- onClick={ onSave }
183
- isBusy={ isSaving }
184
- disabled={ saveDisabled }
185
- accessibleWhenDisabled
186
- >
187
- { __( 'Save' ) }
188
- </Button>
189
- <Button
190
- size="compact"
191
- icon={ close }
192
- label={ __( 'Close' ) }
193
- onClick={ onCancel }
194
- disabled={ isSaving }
195
- accessibleWhenDisabled
196
- />
197
- </Flex>
198
- );
199
- }
200
-
201
- interface MediaEditorModalContentProps {
202
- fields: Field< Media >[];
203
- id: number;
204
- media: Media | null;
205
- hasEdits: boolean;
206
- aspectRatioPresets?: AspectRatioPreset[];
207
- onUpdate: ( ( updated: MediaEditorModalUpdate ) => void ) | null;
208
- }
209
-
210
- // Inner component rendered inside `CropperProvider` so it can read
211
- // `isDirty` from the cropper. The outer `MediaEditorModal` keeps the
212
- // store reads and provider tree above this.
213
- function MediaEditorModalContent( {
214
- fields,
215
- id,
216
- media,
217
- hasEdits,
218
- aspectRatioPresets,
219
- onUpdate,
220
- }: MediaEditorModalContentProps ) {
221
- const cropper = useCropper();
222
- const hasChanges = cropper.isDirty || hasEdits;
223
-
224
- const registry = useRegistry();
225
- const {
226
- clearEntityRecordEdits,
227
- editEntityRecord,
228
- receiveEntityRecords,
229
- saveEditedEntityRecord,
230
- } = useDispatch( coreStore );
231
- const { closeMediaEditorModal } = useDispatch( mediaEditorStore );
232
- const { createErrorNotice, removeAllNotices } = useDispatch( noticesStore );
233
-
234
- const [ isSaving, setIsSaving ] = useState( false );
235
- const [ isDiscardDialogOpen, setIsDiscardDialogOpen ] = useState( false );
236
- const [ isPlacementActive, setIsPlacementActive ] = useState( false );
237
- const placementControlTimerRef =
238
- useRef< ReturnType< typeof setTimeout > >();
239
-
240
- const [ aspectRatioValue, setAspectRatioValue ] = useState( '0' );
241
- const [ freeformCrop, setFreeformCrop ] = useState( true );
242
-
243
- const signalPlacementControlInteraction = useCallback( () => {
244
- setIsPlacementActive( true );
245
- clearTimeout( placementControlTimerRef.current );
246
- placementControlTimerRef.current = setTimeout( () => {
247
- setIsPlacementActive( false );
248
- }, PLACEMENT_CONTROL_IDLE_MS );
249
- }, [] );
250
-
251
- useEffect( () => {
252
- return () => {
253
- clearTimeout( placementControlTimerRef.current );
254
- };
255
- }, [] );
256
-
257
- // Reset aspect-ratio / freeform state when the media changes, so the
258
- // next image starts from the defaults.
259
- useEffect( () => {
260
- setAspectRatioValue( '0' );
261
- setFreeformCrop( true );
262
- }, [ id ] );
263
-
264
- const mediaType = getMediaTypeFromMimeType( media?.mime_type ).type;
265
- const isImage = !! media && mediaType === 'image';
266
-
267
- const imageAspectRatio = useMemo( () => {
268
- if ( ! isImage ) {
269
- return null;
270
- }
271
- const naturalWidth = Number( media?.media_details?.width );
272
- const naturalHeight = Number( media?.media_details?.height );
273
- if (
274
- Number.isFinite( naturalWidth ) &&
275
- Number.isFinite( naturalHeight ) &&
276
- naturalHeight > 0
277
- ) {
278
- return naturalWidth / naturalHeight;
279
- }
280
- return null;
281
- }, [ isImage, media ] );
282
-
283
- const tabs = useMemo< ModalTab[] >( () => {
284
- const detailsTab: ModalTab = {
285
- id: 'details',
286
- title: __( 'Details' ),
287
- panel: (
288
- <Stack
289
- className="media-editor-modal__panel"
290
- direction="column"
291
- gap="lg"
292
- >
293
- <MediaForm />
294
- </Stack>
295
- ),
296
- };
297
- if ( ! isImage ) {
298
- return [ detailsTab ];
299
- }
300
- return [
301
- {
302
- id: 'crop',
303
- title: __( 'Crop' ),
304
- panel: (
305
- <Stack
306
- className="media-editor-modal__panel"
307
- direction="column"
308
- gap="lg"
309
- >
310
- <MediaEditorCropPanel
311
- aspectRatioValue={ aspectRatioValue }
312
- onAspectRatioChange={ setAspectRatioValue }
313
- freeformCrop={ freeformCrop }
314
- onFreeformChange={ setFreeformCrop }
315
- onPlacementControlInteraction={
316
- signalPlacementControlInteraction
317
- }
318
- aspectRatioPresets={ aspectRatioPresets }
319
- />
320
- </Stack>
321
- ),
322
- },
323
- detailsTab,
324
- ];
325
- }, [
326
- isImage,
327
- aspectRatioValue,
328
- freeformCrop,
329
- aspectRatioPresets,
330
- signalPlacementControlInteraction,
331
- ] );
332
-
333
- const handleChange = ( updates: Partial< Media > ) => {
334
- editEntityRecord( 'postType', 'attachment', id, updates );
335
- };
336
-
337
- const discardAndClose = () => {
338
- removeAllNotices( 'snackbar', NOTICES_CONTEXT );
339
- clearEntityRecordEdits( 'postType', 'attachment', id );
340
- closeMediaEditorModal();
341
- };
342
-
343
- const handleRequestClose = () => {
344
- // Disallow closing while a save is in flight so the in-progress
345
- // request can settle without the modal unmounting under it.
346
- if ( isSaving ) {
347
- return;
348
- }
349
- if ( hasChanges ) {
350
- setIsDiscardDialogOpen( true );
351
- return;
352
- }
353
- discardAndClose();
354
- };
355
-
356
- const handleSave = async () => {
357
- // Clear any prior failure snackbar so a successful retry doesn't
358
- // leave a stale "Could not save image" hovering.
359
- removeAllNotices( 'snackbar', NOTICES_CONTEXT );
360
- setIsSaving( true );
361
- try {
362
- let saved: Media | null | undefined;
363
-
364
- const modifiers =
365
- cropper.isDirty && cropper.state.image
366
- ? buildModifiers( cropper.state, {
367
- width: cropper.state.image.naturalWidth,
368
- height: cropper.state.image.naturalHeight,
369
- } )
370
- : [];
371
-
372
- if ( modifiers.length > 0 ) {
373
- // Bundle staged Details-tab edits into the same /edit
374
- // request. Transformed saves duplicate the attachment,
375
- // and the prior core-data edits were staged against the
376
- // old id — if we don't forward them here they'd be
377
- // silently lost. Core's `edit_media_item` honors these
378
- // keys via `prepare_item_for_database` and `alt_text`.
379
- const pendingEdits = registry
380
- .select( coreStore )
381
- .getEntityRecordNonTransientEdits(
382
- 'postType',
383
- 'attachment',
384
- id
385
- ) as Record< string, unknown > | undefined;
386
- const metadataEdits: Record< string, unknown > = {};
387
- for ( const key of METADATA_EDIT_KEYS ) {
388
- if ( pendingEdits && key in pendingEdits ) {
389
- metadataEdits[ key ] = pendingEdits[ key ];
390
- }
391
- }
392
-
393
- saved = ( await apiFetch( {
394
- path: `/wp/v2/media/${ id }/edit`,
395
- method: 'POST',
396
- data: {
397
- src: media?.source_url,
398
- modifiers,
399
- ...metadataEdits,
400
- },
401
- } ) ) as Media;
402
-
403
- if ( saved ) {
404
- // Put the newly-created attachment into the core-data
405
- // cache so downstream consumers see it without an
406
- // extra fetch round-trip.
407
- receiveEntityRecords(
408
- 'postType',
409
- 'attachment',
410
- saved,
411
- undefined,
412
- true
413
- );
414
- }
415
- } else {
416
- saved = ( await saveEditedEntityRecord(
417
- 'postType',
418
- 'attachment',
419
- id
420
- ) ) as Media | undefined;
421
- }
422
-
423
- const next = ( saved ?? media ) as Media | null;
424
-
425
- // A transformed save creates a new attachment; clear staged
426
- // edits on the old record so it doesn't appear dirty in the
427
- // Media Library afterwards.
428
- if ( next && next.id !== id ) {
429
- clearEntityRecordEdits( 'postType', 'attachment', id );
430
- }
431
-
432
- if ( next && next.id && onUpdate ) {
433
- // Normalize to the public callback shape — see
434
- // `MediaEditorModalUpdate` in `../../store/actions.ts`.
435
- onUpdate( { id: next.id, url: next.source_url } );
436
- }
437
- closeMediaEditorModal();
438
- } catch ( error ) {
439
- const message =
440
- error instanceof Error
441
- ? error.message
442
- : ( error as { message?: string } )?.message ??
443
- __( 'An unknown error occurred.' );
444
- createErrorNotice(
445
- sprintf(
446
- /* translators: %s: Error message. */
447
- __( 'Could not save image. %s' ),
448
- message
449
- ),
450
- { type: 'snackbar', context: NOTICES_CONTEXT }
451
- );
452
- } finally {
453
- setIsSaving( false );
454
- }
455
- };
456
-
457
- return (
458
- <Modal
459
- className="media-editor-modal"
460
- title={ __( 'Edit media' ) }
461
- size="fill"
462
- isDismissible={ false }
463
- shouldCloseOnClickOutside={ ! hasChanges && ! isSaving }
464
- onKeyDown={ ( event ) => {
465
- if ( event.code !== 'Escape' && event.key !== 'Escape' ) {
466
- return;
467
- }
468
- // While saving, swallow ESC so the in-progress request
469
- // can settle without the modal closing under it.
470
- if ( isSaving ) {
471
- event.preventDefault();
472
- return;
473
- }
474
- // When there are pending changes, intercept ESC and
475
- // open the confirm dialog ourselves. `preventDefault`
476
- // short-circuits Modal's own ESC-to-close handler on
477
- // the overlay so the modal doesn't animate out before
478
- // the dialog appears.
479
- if ( hasChanges ) {
480
- event.preventDefault();
481
- setIsDiscardDialogOpen( true );
482
- }
483
- } }
484
- onRequestClose={ handleRequestClose }
485
- headerActions={
486
- <HeaderActions
487
- isSaving={ isSaving }
488
- hasMedia={ !! media }
489
- hasChanges={ hasChanges }
490
- onCancel={ handleRequestClose }
491
- onSave={ handleSave }
492
- />
493
- }
494
- >
495
- <MediaEditorProvider
496
- value={ media ?? undefined }
497
- onChange={ handleChange }
498
- settings={ { fields } }
499
- >
500
- { ! media ? (
501
- <div className="media-editor-modal__loading">
502
- <Spinner />
503
- </div>
504
- ) : (
505
- <>
506
- <Tabs>
507
- <MediaEditorModalSidebar tabs={ tabs } />
508
- </Tabs>
509
- <InterfaceSkeleton
510
- className="media-editor-modal__skeleton"
511
- content={
512
- <div className="media-editor-modal__canvas">
513
- { isImage ? (
514
- <MediaEditorCanvas
515
- aspectRatio={ resolveAspectRatio(
516
- aspectRatioValue,
517
- imageAspectRatio
518
- ) }
519
- freeformCrop={ freeformCrop }
520
- isPlacementActive={
521
- isPlacementActive
522
- }
523
- />
524
- ) : (
525
- <MediaPreview />
526
- ) }
527
- </div>
528
- }
529
- footer={
530
- isImage ? (
531
- <MediaEditorToolbar
532
- onReset={ () => {
533
- setAspectRatioValue( '0' );
534
- setFreeformCrop( true );
535
- } }
536
- onPlacementControlInteraction={
537
- signalPlacementControlInteraction
538
- }
539
- />
540
- ) : undefined
541
- }
542
- sidebar={
543
- <ComplementaryArea.Slot scope="media-editor" />
544
- }
545
- />
546
- </>
547
- ) }
548
- { /* Rendered inside the parent Modal so it's tracked as
549
- a nested dismisser. As a top-level sibling it would,
550
- on mount, request the parent Modal close. */ }
551
- <ConfirmDialog
552
- isOpen={ isDiscardDialogOpen }
553
- confirmButtonText={ __( 'Discard' ) }
554
- cancelButtonText={ __( 'Keep editing' ) }
555
- onCancel={ () => setIsDiscardDialogOpen( false ) }
556
- onConfirm={ () => {
557
- setIsDiscardDialogOpen( false );
558
- discardAndClose();
559
- } }
560
- >
561
- { __(
562
- 'Are you sure you want to discard your unsaved changes?'
563
- ) }
564
- </ConfirmDialog>
565
- </MediaEditorProvider>
566
- { createPortal(
567
- <SnackbarNotices
568
- className="media-editor-modal__snackbar"
569
- context={ NOTICES_CONTEXT }
570
- />,
571
- document.body
572
- ) }
573
- </Modal>
574
- );
575
- }
576
-
577
33
  export function MediaEditorModal( {
578
34
  fields = [],
579
35
  aspectRatioPresets,
@@ -587,52 +43,56 @@ export function MediaEditorModal( {
587
43
  };
588
44
  }, [] );
589
45
 
590
- const { media, hasEdits } = useSelect(
591
- ( select ) => {
592
- if ( ! id ) {
593
- return { media: null, hasEdits: false };
594
- }
595
- const { getEditedEntityRecord, hasEditsForEntityRecord } =
596
- select( coreStore );
597
- return {
598
- media: getEditedEntityRecord(
599
- 'postType',
600
- 'attachment',
601
- id
602
- ) as Media,
603
- hasEdits: hasEditsForEntityRecord(
604
- 'postType',
605
- 'attachment',
606
- id
607
- ),
608
- };
609
- },
610
- [ id ]
611
- );
46
+ const { closeMediaEditorModal } = useDispatch( mediaEditorStore );
612
47
 
613
48
  if ( ! isModalOpen || ! id ) {
614
49
  return null;
615
50
  }
616
51
 
617
- // `CropperProvider` is always mounted while the modal is open — it's
618
- // just a `useReducer` with no side effects — so the inner component
619
- // can read `isDirty` for images without forking on media type. The
620
- // `key` remounts the provider when the edited attachment changes,
621
- // discarding the previous cropper state. Keying on `id` (rather than
622
- // `media?.id`) avoids a remount when `media` resolves from `null` to
623
- // the loaded record on open — that flip would otherwise re-run the
624
- // modal's entry animation and cause a visible flicker.
52
+ const portalElement =
53
+ typeof document === 'undefined' ? null : document.body;
54
+
625
55
  return (
626
- <CropperProvider key={ id }>
627
- <MediaEditorModalContent
628
- fields={ fields }
629
- id={ id }
630
- media={ media }
631
- hasEdits={ hasEdits }
632
- aspectRatioPresets={ aspectRatioPresets }
633
- onUpdate={ onUpdate }
634
- />
635
- </CropperProvider>
56
+ <MediaEditor
57
+ id={ id }
58
+ fields={ fields }
59
+ aspectRatioPresets={ aspectRatioPresets }
60
+ showCloseButton
61
+ shouldCloseOnEsc
62
+ noticesClassName="media-editor-modal__snackbar"
63
+ noticesPortalElement={ portalElement }
64
+ onClose={ closeMediaEditorModal }
65
+ onSaved={ ( { id: savedId, url } ) => {
66
+ if ( savedId && onUpdate ) {
67
+ const update: MediaEditorModalUpdate = {
68
+ id: savedId,
69
+ url,
70
+ };
71
+ onUpdate( update );
72
+ }
73
+ closeMediaEditorModal();
74
+ } }
75
+ renderFrame={ ( {
76
+ children,
77
+ headerActions,
78
+ onRequestClose,
79
+ onKeyDown,
80
+ shouldCloseOnClickOutside,
81
+ } ) => (
82
+ <Modal
83
+ className="media-editor-modal"
84
+ title={ __( 'Edit media' ) }
85
+ size="fill"
86
+ isDismissible={ false }
87
+ shouldCloseOnClickOutside={ shouldCloseOnClickOutside }
88
+ onKeyDown={ onKeyDown }
89
+ onRequestClose={ onRequestClose }
90
+ headerActions={ headerActions }
91
+ >
92
+ { children }
93
+ </Modal>
94
+ ) }
95
+ />
636
96
  );
637
97
  }
638
98