@wordpress/media-editor 0.8.0 → 0.8.1-next.v.202605131006.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
@@ -0,0 +1,709 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import apiFetch from '@wordpress/api-fetch';
5
+ import {
6
+ Button,
7
+ Flex,
8
+ Spinner,
9
+ __experimentalConfirmDialog as ConfirmDialog,
10
+ privateApis as componentsPrivateApis,
11
+ } from '@wordpress/components';
12
+ import { Stack } from '@wordpress/ui';
13
+ import { useDispatch, useRegistry, useSelect } from '@wordpress/data';
14
+ import { store as coreStore } from '@wordpress/core-data';
15
+ import {
16
+ createPortal,
17
+ useCallback,
18
+ useContext,
19
+ useEffect,
20
+ useMemo,
21
+ useRef,
22
+ useState,
23
+ } from '@wordpress/element';
24
+ import { __, sprintf } from '@wordpress/i18n';
25
+ import { close, drawerRight, keyboard } from '@wordpress/icons';
26
+ import { isAppleOS, isKeyboardEvent } from '@wordpress/keycodes';
27
+ import { SnackbarNotices, store as noticesStore } from '@wordpress/notices';
28
+ 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
+ import type { KeyboardEvent as ReactKeyboardEvent, ReactNode } from 'react';
37
+
38
+ /**
39
+ * Internal dependencies
40
+ */
41
+ import { MediaEditorProvider } from '../media-editor-provider';
42
+ import type { Media } from '../media-editor-provider';
43
+ import MediaPreview from '../media-preview';
44
+ import MediaEditorCanvas from '../media-editor-canvas';
45
+ import MediaEditorToolbar from '../media-editor-toolbar';
46
+ import MediaEditorCropPanel, {
47
+ resolveAspectRatio,
48
+ } from '../media-editor-crop-panel';
49
+ import MediaForm from '../media-form';
50
+ import { unlock } from '../../lock-unlock';
51
+ import { getMediaTypeFromMimeType } from '../../utils';
52
+ import { CropperProvider, useCropper } from '../../image-editor';
53
+ import type { AspectRatioPreset } from '../../image-editor/core/constants';
54
+ import { CROP_CONTROL_ATTR } from '../../hooks/use-crop-gesture-handlers';
55
+ import { buildModifiers } from '../media-editor-modal/build-modifiers';
56
+ import MediaEditorKeyboardShortcutsModal from '../media-editor-keyboard-shortcuts-modal';
57
+
58
+ // Details-tab edits are bundled into transformed `/edit` requests. Core's
59
+ // endpoint only accepts this whitelist.
60
+ const METADATA_EDIT_KEYS = [
61
+ 'title',
62
+ 'caption',
63
+ 'description',
64
+ 'alt_text',
65
+ 'post',
66
+ ] as const;
67
+
68
+ // Embed query for the attachment's author and parent post. Shared between
69
+ // the `getEntityRecord` read and the matching `invalidateResolution` so the
70
+ // two stay in lockstep.
71
+ const ATTACHMENT_EMBED_QUERY = { _embed: 'author,wp:attached-to' } as const;
72
+
73
+ // Scope save-failure snackbars so they don't leak into the host editor/page.
74
+ const NOTICES_CONTEXT = 'media-editor';
75
+ const PLACEMENT_CONTROL_IDLE_MS = 300;
76
+
77
+ const { Tabs } = unlock( componentsPrivateApis );
78
+
79
+ interface EditorTab {
80
+ id: string;
81
+ title: string;
82
+ panel: JSX.Element;
83
+ }
84
+
85
+ export interface MediaEditorSaveResult {
86
+ id: number;
87
+ url?: string;
88
+ media: Media;
89
+ }
90
+
91
+ export interface MediaEditorFrameProps {
92
+ children: ReactNode;
93
+ headerActions: ReactNode;
94
+ onRequestClose: () => void;
95
+ onKeyDown: ( event: ReactKeyboardEvent< HTMLElement > ) => void;
96
+ shouldCloseOnClickOutside: boolean;
97
+ isSaving: boolean;
98
+ hasChanges: boolean;
99
+ hasMedia: boolean;
100
+ }
101
+
102
+ export interface MediaEditorProps {
103
+ fields?: Field< Media >[];
104
+ id: number;
105
+ aspectRatioPresets?: AspectRatioPreset[];
106
+ onClose?: () => void;
107
+ onSaved?: ( result: MediaEditorSaveResult ) => void;
108
+ renderFrame: ( props: MediaEditorFrameProps ) => JSX.Element;
109
+ noticesClassName?: string;
110
+ noticesPortalElement?: Element | null;
111
+ showCloseButton?: boolean;
112
+ shouldCloseOnEsc?: boolean;
113
+ }
114
+
115
+ function MediaEditorSidebar( { tabs }: { tabs: EditorTab[] } ) {
116
+ const tabsContextValue = useContext( Tabs.Context );
117
+ return (
118
+ <ComplementaryArea
119
+ scope="media-editor"
120
+ identifier="media-editor/details"
121
+ title={ __( 'Details' ) }
122
+ icon={ drawerRight }
123
+ isActiveByDefault
124
+ className="media-editor__sidebar"
125
+ panelClassName="media-editor__sidebar-panel"
126
+ headerClassName="media-editor__sidebar-header"
127
+ header={
128
+ <Tabs.Context.Provider value={ tabsContextValue }>
129
+ <Tabs.TabList>
130
+ { tabs.map( ( tab ) => (
131
+ <Tabs.Tab key={ tab.id } tabId={ tab.id }>
132
+ { tab.title }
133
+ </Tabs.Tab>
134
+ ) ) }
135
+ </Tabs.TabList>
136
+ </Tabs.Context.Provider>
137
+ }
138
+ >
139
+ <Tabs.Context.Provider value={ tabsContextValue }>
140
+ { tabs.map( ( tab ) => (
141
+ <Tabs.TabPanel
142
+ key={ tab.id }
143
+ tabId={ tab.id }
144
+ focusable={ false }
145
+ >
146
+ { tab.panel }
147
+ </Tabs.TabPanel>
148
+ ) ) }
149
+ </Tabs.Context.Provider>
150
+ </ComplementaryArea>
151
+ );
152
+ }
153
+
154
+ interface HeaderActionsProps {
155
+ isSaving: boolean;
156
+ hasMedia: boolean;
157
+ hasChanges: boolean;
158
+ isImage: boolean;
159
+ showCloseButton?: boolean;
160
+ onCancel: () => void;
161
+ onSave: () => void;
162
+ }
163
+
164
+ function HeaderActions( {
165
+ isSaving,
166
+ hasMedia,
167
+ hasChanges,
168
+ isImage,
169
+ showCloseButton = false,
170
+ onCancel,
171
+ onSave,
172
+ }: HeaderActionsProps ) {
173
+ const saveDisabled = isSaving || ! hasMedia || ! hasChanges;
174
+ const [ isShortcutsModalOpen, setIsShortcutsModalOpen ] = useState( false );
175
+ return (
176
+ <Flex
177
+ className="media-editor__header-actions"
178
+ justify="flex-end"
179
+ expanded={ false }
180
+ gap={ 2 }
181
+ >
182
+ { isImage && (
183
+ <Button
184
+ size="compact"
185
+ icon={ keyboard }
186
+ label={ __( 'Keyboard shortcuts' ) }
187
+ onClick={ () => setIsShortcutsModalOpen( true ) }
188
+ />
189
+ ) }
190
+ <PinnedItems.Slot scope="media-editor" />
191
+ <Button
192
+ size="compact"
193
+ variant="tertiary"
194
+ onClick={ onCancel }
195
+ disabled={ isSaving }
196
+ accessibleWhenDisabled
197
+ >
198
+ { __( 'Cancel' ) }
199
+ </Button>
200
+ <Button
201
+ size="compact"
202
+ variant="primary"
203
+ onClick={ onSave }
204
+ isBusy={ isSaving }
205
+ disabled={ saveDisabled }
206
+ accessibleWhenDisabled
207
+ >
208
+ { __( 'Save' ) }
209
+ </Button>
210
+ { showCloseButton && (
211
+ <Button
212
+ size="compact"
213
+ icon={ close }
214
+ label={ __( 'Close' ) }
215
+ onClick={ onCancel }
216
+ disabled={ isSaving }
217
+ accessibleWhenDisabled
218
+ />
219
+ ) }
220
+ { isShortcutsModalOpen && (
221
+ <MediaEditorKeyboardShortcutsModal
222
+ onClose={ () => setIsShortcutsModalOpen( false ) }
223
+ />
224
+ ) }
225
+ </Flex>
226
+ );
227
+ }
228
+
229
+ type PendingMetadataEdits = Record< string, unknown > | undefined;
230
+
231
+ function MediaEditorContent( {
232
+ fields = [],
233
+ id,
234
+ aspectRatioPresets,
235
+ onClose,
236
+ onSaved,
237
+ renderFrame,
238
+ noticesClassName = 'media-editor__snackbar',
239
+ noticesPortalElement,
240
+ showCloseButton = false,
241
+ shouldCloseOnEsc = false,
242
+ }: MediaEditorProps ) {
243
+ const cropper = useCropper();
244
+
245
+ const { media, hasEdits } = useSelect(
246
+ ( select ) => {
247
+ const {
248
+ getEditedEntityRecord,
249
+ getEntityRecord,
250
+ hasEditsForEntityRecord,
251
+ } = select( coreStore );
252
+ // Trigger an _embed fetch so `_embedded.author` and
253
+ // `_embedded['wp:attached-to']` land on the record for the Details
254
+ // fields to read. `getEditedEntityRecord` doesn't formally accept a
255
+ // query, so we can't embed via that selector directly.
256
+ getEntityRecord(
257
+ 'postType',
258
+ 'attachment',
259
+ id,
260
+ ATTACHMENT_EMBED_QUERY
261
+ );
262
+ return {
263
+ media: getEditedEntityRecord(
264
+ 'postType',
265
+ 'attachment',
266
+ id
267
+ ) as Media,
268
+ hasEdits: hasEditsForEntityRecord(
269
+ 'postType',
270
+ 'attachment',
271
+ id
272
+ ),
273
+ };
274
+ },
275
+ [ id ]
276
+ );
277
+
278
+ const hasChanges = cropper.isDirty || hasEdits;
279
+
280
+ const registry = useRegistry();
281
+ const {
282
+ clearEntityRecordEdits,
283
+ editEntityRecord,
284
+ invalidateResolution,
285
+ receiveEntityRecords,
286
+ saveEditedEntityRecord,
287
+ } = useDispatch( coreStore );
288
+ const { createErrorNotice, removeAllNotices } = useDispatch( noticesStore );
289
+
290
+ const [ isSaving, setIsSaving ] = useState( false );
291
+ const [ isDiscardDialogOpen, setIsDiscardDialogOpen ] = useState( false );
292
+ const [ isPlacementActive, setIsPlacementActive ] = useState( false );
293
+ const [ isCanvasGestureActive, setIsCanvasGestureActive ] =
294
+ useState( false );
295
+ const placementControlTimerRef =
296
+ useRef< ReturnType< typeof setTimeout > >();
297
+
298
+ const [ aspectRatioValue, setAspectRatioValue ] = useState( '0' );
299
+ const [ freeformCrop, setFreeformCrop ] = useState( true );
300
+
301
+ const signalPlacementControlInteraction = useCallback( () => {
302
+ setIsPlacementActive( true );
303
+ clearTimeout( placementControlTimerRef.current );
304
+ placementControlTimerRef.current = setTimeout( () => {
305
+ setIsPlacementActive( false );
306
+ }, PLACEMENT_CONTROL_IDLE_MS );
307
+ }, [] );
308
+ const handleCanvasGestureStart = useCallback( () => {
309
+ setIsCanvasGestureActive( true );
310
+ }, [] );
311
+ const handleCanvasGestureEnd = useCallback( () => {
312
+ setIsCanvasGestureActive( false );
313
+ }, [] );
314
+ const isCropInteractionActive = isPlacementActive || isCanvasGestureActive;
315
+
316
+ useEffect( () => {
317
+ return () => {
318
+ clearTimeout( placementControlTimerRef.current );
319
+ };
320
+ }, [] );
321
+
322
+ useEffect( () => {
323
+ setAspectRatioValue( '0' );
324
+ setFreeformCrop( true );
325
+ setIsPlacementActive( false );
326
+ setIsCanvasGestureActive( false );
327
+ }, [ id ] );
328
+
329
+ // Bust the cached `_embed` resolution each time the editor mounts (or the
330
+ // id changes) so embedded data such as the attached post's title or the
331
+ // author's name reflects any edits made elsewhere since the last open.
332
+ useEffect( () => {
333
+ invalidateResolution( 'getEntityRecord', [
334
+ 'postType',
335
+ 'attachment',
336
+ id,
337
+ ATTACHMENT_EMBED_QUERY,
338
+ ] );
339
+ }, [ id, invalidateResolution ] );
340
+
341
+ const mediaType = getMediaTypeFromMimeType( media?.mime_type ).type;
342
+ const isImage = !! media && mediaType === 'image';
343
+
344
+ const imageAspectRatio = useMemo( () => {
345
+ if ( ! isImage ) {
346
+ return null;
347
+ }
348
+ const naturalWidth = Number( media?.media_details?.width );
349
+ const naturalHeight = Number( media?.media_details?.height );
350
+ if (
351
+ Number.isFinite( naturalWidth ) &&
352
+ Number.isFinite( naturalHeight ) &&
353
+ naturalHeight > 0
354
+ ) {
355
+ return naturalWidth / naturalHeight;
356
+ }
357
+ return null;
358
+ }, [ isImage, media ] );
359
+
360
+ const tabs = useMemo< EditorTab[] >( () => {
361
+ const detailsTab: EditorTab = {
362
+ id: 'details',
363
+ title: __( 'Details' ),
364
+ panel: (
365
+ <Stack
366
+ className="media-editor__panel"
367
+ direction="column"
368
+ gap="lg"
369
+ >
370
+ <MediaForm />
371
+ </Stack>
372
+ ),
373
+ };
374
+ if ( ! isImage ) {
375
+ return [ detailsTab ];
376
+ }
377
+ return [
378
+ {
379
+ id: 'crop',
380
+ title: __( 'Crop' ),
381
+ panel: (
382
+ <Stack
383
+ className="media-editor__panel"
384
+ direction="column"
385
+ gap="lg"
386
+ >
387
+ <MediaEditorCropPanel
388
+ aspectRatioValue={ aspectRatioValue }
389
+ onAspectRatioChange={ setAspectRatioValue }
390
+ freeformCrop={ freeformCrop }
391
+ onFreeformChange={ setFreeformCrop }
392
+ onPlacementControlInteraction={
393
+ signalPlacementControlInteraction
394
+ }
395
+ aspectRatioPresets={ aspectRatioPresets }
396
+ />
397
+ </Stack>
398
+ ),
399
+ },
400
+ detailsTab,
401
+ ];
402
+ }, [
403
+ isImage,
404
+ aspectRatioValue,
405
+ freeformCrop,
406
+ aspectRatioPresets,
407
+ signalPlacementControlInteraction,
408
+ ] );
409
+
410
+ const handleChange = ( updates: Partial< Media > ) => {
411
+ editEntityRecord( 'postType', 'attachment', id, updates );
412
+ };
413
+
414
+ const discardAndClose = () => {
415
+ removeAllNotices( 'snackbar', NOTICES_CONTEXT );
416
+ clearEntityRecordEdits( 'postType', 'attachment', id );
417
+ onClose?.();
418
+ };
419
+
420
+ const handleRequestClose = () => {
421
+ if ( isSaving ) {
422
+ return;
423
+ }
424
+ if ( hasChanges ) {
425
+ setIsDiscardDialogOpen( true );
426
+ return;
427
+ }
428
+ discardAndClose();
429
+ };
430
+
431
+ const handleSave = async () => {
432
+ removeAllNotices( 'snackbar', NOTICES_CONTEXT );
433
+ setIsSaving( true );
434
+ try {
435
+ let saved: Media | null | undefined;
436
+
437
+ const modifiers =
438
+ cropper.isDirty && cropper.state.image
439
+ ? buildModifiers( cropper.state, {
440
+ width: cropper.state.image.naturalWidth,
441
+ height: cropper.state.image.naturalHeight,
442
+ } )
443
+ : [];
444
+
445
+ if ( modifiers.length > 0 ) {
446
+ const pendingEdits = registry
447
+ .select( coreStore )
448
+ .getEntityRecordNonTransientEdits(
449
+ 'postType',
450
+ 'attachment',
451
+ id
452
+ ) as PendingMetadataEdits;
453
+ const metadataEdits: Record< string, unknown > = {};
454
+ for ( const key of METADATA_EDIT_KEYS ) {
455
+ if ( pendingEdits && key in pendingEdits ) {
456
+ metadataEdits[ key ] = pendingEdits[ key ];
457
+ }
458
+ }
459
+ // The `/edit` endpoint creates a new attachment for the crop
460
+ // and doesn't inherit `post_parent` from the source (unlike
461
+ // title/caption/etc.), so carry the existing value across when
462
+ // the user hasn't explicitly edited it. Use a defined-check so
463
+ // an explicit `0` (unattached) is also preserved.
464
+ if (
465
+ ! ( 'post' in metadataEdits ) &&
466
+ media?.post !== undefined
467
+ ) {
468
+ metadataEdits.post = media.post;
469
+ }
470
+
471
+ saved = ( await apiFetch( {
472
+ path: `/wp/v2/media/${ id }/edit`,
473
+ method: 'POST',
474
+ data: {
475
+ src: media?.source_url,
476
+ modifiers,
477
+ ...metadataEdits,
478
+ },
479
+ } ) ) as Media;
480
+
481
+ if ( saved ) {
482
+ receiveEntityRecords(
483
+ 'postType',
484
+ 'attachment',
485
+ saved,
486
+ undefined,
487
+ true
488
+ );
489
+ }
490
+ } else {
491
+ saved = ( await saveEditedEntityRecord(
492
+ 'postType',
493
+ 'attachment',
494
+ id
495
+ ) ) as Media | undefined;
496
+ }
497
+
498
+ const next = ( saved ?? media ) as Media | null;
499
+
500
+ if ( next && next.id !== id ) {
501
+ clearEntityRecordEdits( 'postType', 'attachment', id );
502
+ }
503
+
504
+ if ( next && next.id ) {
505
+ if ( next.id === id ) {
506
+ cropper.reset();
507
+ }
508
+ onSaved?.( {
509
+ id: next.id,
510
+ url: next.source_url,
511
+ media: next,
512
+ } );
513
+ }
514
+ } catch ( error ) {
515
+ const message =
516
+ error instanceof Error
517
+ ? error.message
518
+ : ( error as { message?: string } )?.message ??
519
+ __( 'An unknown error occurred.' );
520
+ createErrorNotice(
521
+ isImage
522
+ ? sprintf(
523
+ /* translators: %s: Error message. */
524
+ __( 'Could not save image. %s' ),
525
+ message
526
+ )
527
+ : sprintf(
528
+ /* translators: %s: Error message. */
529
+ __( 'Could not save media. %s' ),
530
+ message
531
+ ),
532
+ { type: 'snackbar', context: NOTICES_CONTEXT }
533
+ );
534
+ } finally {
535
+ setIsSaving( false );
536
+ }
537
+ };
538
+
539
+ const handleKeyDown = ( event: ReactKeyboardEvent< HTMLElement > ) => {
540
+ const isUndoShortcut = isKeyboardEvent.primary( event, 'z' );
541
+ const isRedoShortcut =
542
+ isKeyboardEvent.primaryShift( event, 'z' ) ||
543
+ ( ! isAppleOS() && isKeyboardEvent.primary( event, 'y' ) );
544
+ if ( ( isUndoShortcut || isRedoShortcut ) && isImage ) {
545
+ const target = event.target as HTMLElement;
546
+ const isMetadataField =
547
+ ( target.tagName === 'INPUT' ||
548
+ target.tagName === 'TEXTAREA' ||
549
+ target.isContentEditable ) &&
550
+ ! target.closest( `[${ CROP_CONTROL_ATTR }]` );
551
+ if ( ! isMetadataField ) {
552
+ event.preventDefault();
553
+ if ( isCropInteractionActive ) {
554
+ return;
555
+ }
556
+ if ( isRedoShortcut ) {
557
+ cropper.redo();
558
+ } else {
559
+ cropper.undo();
560
+ }
561
+ }
562
+ }
563
+
564
+ if ( shouldCloseOnEsc ) {
565
+ if ( event.code !== 'Escape' && event.key !== 'Escape' ) {
566
+ return;
567
+ }
568
+ if ( isSaving ) {
569
+ event.preventDefault();
570
+ return;
571
+ }
572
+ if ( hasChanges ) {
573
+ event.preventDefault();
574
+ setIsDiscardDialogOpen( true );
575
+ }
576
+ }
577
+ };
578
+
579
+ const snackbar = (
580
+ <SnackbarNotices
581
+ className={ noticesClassName }
582
+ context={ NOTICES_CONTEXT }
583
+ />
584
+ );
585
+
586
+ const children = (
587
+ <MediaEditorProvider
588
+ value={ media ?? undefined }
589
+ onChange={ handleChange }
590
+ settings={ { fields } }
591
+ >
592
+ <div className="media-editor">
593
+ { ! media ? (
594
+ <div className="media-editor__loading">
595
+ <Spinner />
596
+ </div>
597
+ ) : (
598
+ <>
599
+ <Tabs>
600
+ <MediaEditorSidebar tabs={ tabs } />
601
+ </Tabs>
602
+ <InterfaceSkeleton
603
+ className="media-editor__skeleton"
604
+ labels={ {
605
+ body: isImage
606
+ ? __( 'Image editor' )
607
+ : __( 'Media preview' ),
608
+ sidebar: __( 'Media details' ),
609
+ footer: __( 'Image editing tools' ),
610
+ } }
611
+ content={
612
+ <div className="media-editor__canvas">
613
+ { isImage ? (
614
+ <MediaEditorCanvas
615
+ aspectRatio={ resolveAspectRatio(
616
+ aspectRatioValue,
617
+ imageAspectRatio
618
+ ) }
619
+ freeformCrop={ freeformCrop }
620
+ focusOnMount
621
+ isPlacementActive={
622
+ isPlacementActive
623
+ }
624
+ onGestureStart={
625
+ handleCanvasGestureStart
626
+ }
627
+ onGestureEnd={
628
+ handleCanvasGestureEnd
629
+ }
630
+ />
631
+ ) : (
632
+ <MediaPreview />
633
+ ) }
634
+ </div>
635
+ }
636
+ footer={
637
+ isImage ? (
638
+ <MediaEditorToolbar
639
+ onReset={ () => {
640
+ setAspectRatioValue( '0' );
641
+ setFreeformCrop( true );
642
+ } }
643
+ onPlacementControlInteraction={
644
+ signalPlacementControlInteraction
645
+ }
646
+ isUndoRedoDisabled={
647
+ isCropInteractionActive
648
+ }
649
+ />
650
+ ) : undefined
651
+ }
652
+ sidebar={
653
+ <ComplementaryArea.Slot scope="media-editor" />
654
+ }
655
+ />
656
+ </>
657
+ ) }
658
+ </div>
659
+ <ConfirmDialog
660
+ isOpen={ isDiscardDialogOpen }
661
+ confirmButtonText={ __( 'Discard' ) }
662
+ cancelButtonText={ __( 'Keep editing' ) }
663
+ onCancel={ () => setIsDiscardDialogOpen( false ) }
664
+ onConfirm={ () => {
665
+ setIsDiscardDialogOpen( false );
666
+ discardAndClose();
667
+ } }
668
+ >
669
+ { __(
670
+ 'Are you sure you want to discard your unsaved changes?'
671
+ ) }
672
+ </ConfirmDialog>
673
+ { noticesPortalElement
674
+ ? createPortal( snackbar, noticesPortalElement )
675
+ : snackbar }
676
+ </MediaEditorProvider>
677
+ );
678
+
679
+ return renderFrame( {
680
+ children,
681
+ headerActions: (
682
+ <HeaderActions
683
+ isSaving={ isSaving }
684
+ hasMedia={ !! media }
685
+ hasChanges={ hasChanges }
686
+ isImage={ isImage }
687
+ showCloseButton={ showCloseButton }
688
+ onCancel={ handleRequestClose }
689
+ onSave={ handleSave }
690
+ />
691
+ ),
692
+ onRequestClose: handleRequestClose,
693
+ onKeyDown: handleKeyDown,
694
+ shouldCloseOnClickOutside: ! hasChanges && ! isSaving,
695
+ isSaving,
696
+ hasChanges,
697
+ hasMedia: !! media,
698
+ } );
699
+ }
700
+
701
+ export function MediaEditor( props: MediaEditorProps ) {
702
+ return (
703
+ <CropperProvider key={ props.id }>
704
+ <MediaEditorContent { ...props } />
705
+ </CropperProvider>
706
+ );
707
+ }
708
+
709
+ export default MediaEditor;