@wordpress/media-editor 0.23.0 → 0.24.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 (87) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/build/components/media-editor/index.cjs +157 -107
  3. package/build/components/media-editor/index.cjs.map +1 -1
  4. package/build/components/media-editor/use-save-media-editor.cjs +18 -8
  5. package/build/components/media-editor/use-save-media-editor.cjs.map +1 -1
  6. package/build/components/media-editor-canvas/index.cjs +9 -7
  7. package/build/components/media-editor-canvas/index.cjs.map +1 -1
  8. package/build/components/media-editor-crop-panel/index.cjs +9 -7
  9. package/build/components/media-editor-crop-panel/index.cjs.map +1 -1
  10. package/build/components/media-editor-fine-rotation/index.cjs +9 -7
  11. package/build/components/media-editor-fine-rotation/index.cjs.map +1 -1
  12. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +32 -33
  13. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +1 -1
  14. package/build/components/media-editor-modal/index.cjs +10 -7
  15. package/build/components/media-editor-modal/index.cjs.map +1 -1
  16. package/build/components/media-editor-panel/error-boundary.cjs +68 -0
  17. package/build/components/media-editor-panel/error-boundary.cjs.map +1 -0
  18. package/build/components/media-editor-panel/index.cjs +158 -0
  19. package/build/components/media-editor-panel/index.cjs.map +1 -0
  20. package/build/components/media-preview/index.cjs +143 -36
  21. package/build/components/media-preview/index.cjs.map +1 -1
  22. package/build/components/rotation-ruler/index.cjs +9 -7
  23. package/build/components/rotation-ruler/index.cjs.map +1 -1
  24. package/build/components/rotation-ruler/use-ruler-drag.cjs.map +1 -1
  25. package/build/hooks/use-crop-gesture-handlers.cjs.map +1 -1
  26. package/build/index.cjs +10 -8
  27. package/build/private-apis.cjs.map +1 -1
  28. package/build/state/aspect-ratio-presets.cjs +2 -2
  29. package/build/state/aspect-ratio-presets.cjs.map +1 -1
  30. package/build/state/composite-reducer.cjs +9 -7
  31. package/build/state/composite-reducer.cjs.map +1 -1
  32. package/build/state/use-media-editor-state.cjs.map +1 -1
  33. package/build/store/index.cjs +11 -9
  34. package/build/store/index.cjs.map +1 -1
  35. package/build/utils/get-media-type.cjs +8 -0
  36. package/build/utils/get-media-type.cjs.map +1 -1
  37. package/build/utils/index.cjs +6 -0
  38. package/build-module/components/media-editor/index.mjs +142 -95
  39. package/build-module/components/media-editor/index.mjs.map +1 -1
  40. package/build-module/components/media-editor/use-save-media-editor.mjs +9 -1
  41. package/build-module/components/media-editor/use-save-media-editor.mjs.map +1 -1
  42. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +32 -33
  43. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +1 -1
  44. package/build-module/components/media-editor-modal/index.mjs +1 -0
  45. package/build-module/components/media-editor-modal/index.mjs.map +1 -1
  46. package/build-module/components/media-editor-panel/error-boundary.mjs +63 -0
  47. package/build-module/components/media-editor-panel/error-boundary.mjs.map +1 -0
  48. package/build-module/components/media-editor-panel/index.mjs +123 -0
  49. package/build-module/components/media-editor-panel/index.mjs.map +1 -0
  50. package/build-module/components/media-preview/index.mjs +123 -40
  51. package/build-module/components/media-preview/index.mjs.map +1 -1
  52. package/build-module/state/aspect-ratio-presets.mjs +2 -2
  53. package/build-module/state/aspect-ratio-presets.mjs.map +1 -1
  54. package/build-module/state/use-media-editor-state.mjs.map +1 -1
  55. package/build-module/utils/get-media-type.mjs +8 -1
  56. package/build-module/utils/get-media-type.mjs.map +1 -1
  57. package/build-module/utils/index.mjs +1 -1
  58. package/build-style/style-rtl.css +18 -30
  59. package/build-style/style.css +18 -30
  60. package/build-types/components/media-editor/index.d.ts +8 -0
  61. package/build-types/components/media-editor/index.d.ts.map +1 -1
  62. package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts.map +1 -1
  63. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  64. package/build-types/components/media-editor-panel/error-boundary.d.ts +23 -0
  65. package/build-types/components/media-editor-panel/error-boundary.d.ts.map +1 -0
  66. package/build-types/components/media-editor-panel/index.d.ts +46 -0
  67. package/build-types/components/media-editor-panel/index.d.ts.map +1 -0
  68. package/build-types/components/media-preview/index.d.ts.map +1 -1
  69. package/build-types/state/use-media-editor-state.d.ts +7 -2
  70. package/build-types/state/use-media-editor-state.d.ts.map +1 -1
  71. package/build-types/utils/get-media-type.d.ts +3 -1
  72. package/build-types/utils/get-media-type.d.ts.map +1 -1
  73. package/build-types/utils/index.d.ts +1 -1
  74. package/build-types/utils/index.d.ts.map +1 -1
  75. package/package.json +26 -22
  76. package/src/components/media-editor/index.tsx +209 -114
  77. package/src/components/media-editor/style.scss +21 -1
  78. package/src/components/media-editor-keyboard-shortcuts-modal/index.tsx +55 -63
  79. package/src/components/media-editor-modal/index.tsx +3 -0
  80. package/src/components/media-editor-panel/error-boundary.tsx +88 -0
  81. package/src/components/media-editor-panel/index.tsx +211 -0
  82. package/src/components/media-preview/index.tsx +195 -36
  83. package/src/components/media-preview/style.scss +11 -32
  84. package/src/state/README.md +3 -1
  85. package/src/state/use-media-editor-state.ts +7 -2
  86. package/src/utils/get-media-type.ts +9 -1
  87. package/src/utils/index.ts +1 -1
@@ -9,8 +9,11 @@ import {
9
9
  __experimentalConfirmDialog as ConfirmDialog,
10
10
  __unstableAnimatePresence as AnimatePresence,
11
11
  __unstableMotion as motion,
12
+ Slot,
13
+ SlotFillProvider,
12
14
  } from '@wordpress/components';
13
15
  import { Stack, Tabs, VisuallyHidden } from '@wordpress/ui';
16
+ import { PluginArea } from '@wordpress/plugins';
14
17
  import { useReducedMotion, useViewportMatch } from '@wordpress/compose';
15
18
  import { useDispatch, useRegistry, useSelect } from '@wordpress/data';
16
19
  import { store as coreStore } from '@wordpress/core-data';
@@ -39,8 +42,10 @@ import {
39
42
  displayShortcut,
40
43
  isAppleOS,
41
44
  isKeyboardEvent,
45
+ withIgnoreIMEEvents,
42
46
  } from '@wordpress/keycodes';
43
47
  import { SnackbarNotices, store as noticesStore } from '@wordpress/notices';
48
+ import { inertValue } from '@wordpress/react-inert-value';
44
49
  import type { Action, ActionModal, Field, Form } from '@wordpress/dataviews';
45
50
  import type {
46
51
  KeyboardEvent as ReactKeyboardEvent,
@@ -56,6 +61,13 @@ import MediaEditorFineRotation from '../media-editor-fine-rotation';
56
61
  import MediaEditorCropPanel from '../media-editor-crop-panel';
57
62
  import MediaEditorImageControls from '../media-editor-image-controls';
58
63
  import MediaForm from '../media-form';
64
+ import MediaEditorPanel, {
65
+ MEDIA_EDITOR_PLUGIN_SCOPE,
66
+ MediaEditorPanelsProvider,
67
+ getPanelSlotName,
68
+ useMediaEditorPanels,
69
+ type MediaEditorPanelFillProps,
70
+ } from '../media-editor-panel';
59
71
  import { getMediaTypeFromMimeType } from '../../utils';
60
72
  import { MediaEditorStateProvider, useMediaEditor } from '../../state';
61
73
  import { CROP_CONTROL_ATTR } from '../../hooks/use-crop-gesture-handlers';
@@ -77,15 +89,18 @@ const ATTACHMENT_EMBED_QUERY = { _embed: 'author,wp:attached-to' } as const;
77
89
  const PLACEMENT_CONTROL_IDLE_MS = 300;
78
90
 
79
91
  /**
80
- * Identifier for the details panel, and the panel the sidebar opens on before
81
- * the user picks another. The sidebar tracks which panel is open rather than
82
- * whether one is, so a further panel is a new id here and a new entry in the
83
- * tab list — not a change to the state's shape.
92
+ * The Details panel's id. Every media type has this panel, so the sidebar
93
+ * shows it when the requested panel doesn't exist.
94
+ *
95
+ * The sidebar stores the open panel's id, so adding a panel only needs a new
96
+ * id and a new tab.
84
97
  */
85
- const DETAILS_PANEL = 'details';
98
+ const DETAILS_PANEL = 'core/details';
86
99
 
87
100
  /** Identifier for the crop panel. Images only; other media has nothing to crop. */
88
- const CROP_PANEL = 'crop';
101
+ const CROP_PANEL = 'core/crop';
102
+
103
+ const CROP_MEDIA_TYPES = [ 'image' ] as const;
89
104
 
90
105
  // Matches the editor's sidebar in `ComplementaryArea`.
91
106
  const SIDEBAR_ANIMATION_DURATION = 0.3;
@@ -151,6 +166,14 @@ export interface MediaEditorProps {
151
166
  noticesClassName?: string;
152
167
  noticesPortalElement?: Element | null;
153
168
  shouldCloseOnEsc?: boolean;
169
+ /**
170
+ * The id of the panel to open on, which is also shown as the first tab.
171
+ * Below `small`, or when no tab has this id, the sidebar opens on
172
+ * Details.
173
+ *
174
+ * @default 'core/details'
175
+ */
176
+ defaultPanel?: string;
154
177
  }
155
178
 
156
179
  interface MediaEditorSidebarProps {
@@ -210,6 +233,10 @@ function MediaEditorSidebar( {
210
233
  value={ tab.id }
211
234
  tabIndex={ -1 }
212
235
  className="media-editor__panel"
236
+ // A save reads the session once and then waits, so
237
+ // nothing in the panel may act on it meanwhile.
238
+ // `inert` covers panels that ignore `disabled`.
239
+ inert={ inertValue( disabled ) }
213
240
  >
214
241
  <Stack direction="column" gap="lg">
215
242
  { tab.render() }
@@ -285,6 +312,20 @@ function useMediaEditorFrameContext(): MediaEditorFrameContextValue {
285
312
  return context;
286
313
  }
287
314
 
315
+ function CropPanelContent( { disabled }: MediaEditorPanelFillProps ) {
316
+ const { aspectRatioPresets } = useMediaEditorFrameContext();
317
+ const { aspectRatioValue, setAspectRatioValue, aspectRatioOptions } =
318
+ useCropOptions( { aspectRatioPresets } );
319
+ return (
320
+ <MediaEditorCropPanel
321
+ aspectRatioValue={ aspectRatioValue }
322
+ onAspectRatioChange={ setAspectRatioValue }
323
+ aspectRatioOptions={ aspectRatioOptions }
324
+ disabled={ disabled }
325
+ />
326
+ );
327
+ }
328
+
288
329
  export interface HeaderActionsProps {
289
330
  /**
290
331
  * Whether to include a Close button. Frames with a dismissal affordance of
@@ -680,6 +721,7 @@ function MediaEditorContent( {
680
721
  noticesClassName = 'media-editor__snackbar',
681
722
  noticesPortalElement,
682
723
  shouldCloseOnEsc = false,
724
+ defaultPanel = DETAILS_PANEL,
683
725
  }: MediaEditorProps ) {
684
726
  const session = useMediaEditor();
685
727
  // Width decides whether the settings panel docks beside the canvas or
@@ -731,7 +773,23 @@ function MediaEditorContent( {
731
773
 
732
774
  const { clearEntityRecordEdits, editEntityRecord, invalidateResolution } =
733
775
  useDispatch( coreStore );
734
- const { removeAllNotices } = useDispatch( noticesStore );
776
+ const { createErrorNotice, removeAllNotices } = useDispatch( noticesStore );
777
+ // `PluginArea` drops a plugin whose render throws; tell the user.
778
+ const handlePluginError = useCallback(
779
+ ( name: string ) => {
780
+ createErrorNotice(
781
+ sprintf(
782
+ /* translators: %s: plugin name */
783
+ __(
784
+ 'The "%s" plugin has encountered an error and cannot be rendered.'
785
+ ),
786
+ name
787
+ ),
788
+ { type: 'snackbar', context: MEDIA_EDITOR_NOTICES_CONTEXT }
789
+ );
790
+ },
791
+ [ createErrorNotice ]
792
+ );
735
793
 
736
794
  const [ isDiscardDialogOpen, setIsDiscardDialogOpen ] = useState( false );
737
795
  const [ isPlacementActive, setIsPlacementActive ] = useState( false );
@@ -865,18 +923,17 @@ function MediaEditorContent( {
865
923
  }, [ clearEntityRecordEdits, id, onEditOriginal, originalId ] );
866
924
 
867
925
  // A bare replacement has no image editor change, so OR the flag in explicitly.
926
+ // Nothing saves named edits (`session.edits`), so `hasOutputEdits` leaves
927
+ // them out: counting them would enable Save for a change it then drops.
868
928
  const hasChanges = session.hasOutputEdits || hasEdits || isSourceReplaced;
869
929
 
870
930
  const mediaType = getMediaTypeFromMimeType( media?.mime_type ).type;
871
931
  const isImage = !! media && mediaType === 'image';
872
932
 
873
- // Docked, the panel opens on Crop: the canvas is still beside it and
874
- // cropping is what the editor is for. Below `small` an open panel covers
875
- // the canvas, and crop controls with no crop in sight are not worth
876
- // opening on, so Details leads there. Non-image media has no Crop tab at
877
- // all (see `tabs`), so it always gets Details.
878
- const currentTab =
879
- chosenTab ?? ( isWide && isImage ? CROP_PANEL : DETAILS_PANEL );
933
+ // Below `small` the panel covers the canvas, so open on Details rather
934
+ // than a panel that needs the canvas in view. If no tab matches
935
+ // `defaultPanel`, the sidebar shows Details (see `activeTab` below).
936
+ const currentTab = chosenTab ?? ( isWide ? defaultPanel : DETAILS_PANEL );
880
937
  // `null` when nothing is open, otherwise the open panel's id. Until the
881
938
  // user opens or closes the panel, follow the breakpoint: dragging a window
882
939
  // narrow hands the canvas the full width instead of leaving a panel
@@ -897,8 +954,6 @@ function MediaEditorContent( {
897
954
  const togglePanel = useCallback( () => {
898
955
  setChosenPanel( activePanel ? null : currentTab );
899
956
  }, [ activePanel, currentTab ] );
900
- const { aspectRatioValue, setAspectRatioValue, aspectRatioOptions } =
901
- useCropOptions( { aspectRatioPresets } );
902
957
  const { isSaving, save: saveMediaEditor } = useSaveMediaEditor( {
903
958
  session,
904
959
  id,
@@ -938,45 +993,47 @@ function MediaEditorContent( {
938
993
  discardAndClose();
939
994
  };
940
995
 
941
- const handleKeyDown = ( event: ReactKeyboardEvent< HTMLElement > ) => {
942
- const isUndoShortcut = isKeyboardEvent.primary( event, 'z' );
943
- const isRedoShortcut =
944
- isKeyboardEvent.primaryShift( event, 'z' ) ||
945
- ( ! isAppleOS() && isKeyboardEvent.primary( event, 'y' ) );
946
- if ( ( isUndoShortcut || isRedoShortcut ) && isImage ) {
947
- const target = event.target as HTMLElement;
948
- const isMetadataField =
949
- ( target.tagName === 'INPUT' ||
950
- target.tagName === 'TEXTAREA' ||
951
- target.isContentEditable ) &&
952
- ! target.closest( `[${ CROP_CONTROL_ATTR }]` );
953
- if ( ! isMetadataField ) {
954
- event.preventDefault();
955
- if ( isCropInteractionActive || isSaving ) {
956
- return;
957
- }
958
- if ( isRedoShortcut ) {
959
- session.redo();
960
- } else {
961
- session.undo();
996
+ const handleKeyDown = withIgnoreIMEEvents(
997
+ ( event: ReactKeyboardEvent< HTMLElement > ) => {
998
+ const isUndoShortcut = isKeyboardEvent.primary( event, 'z' );
999
+ const isRedoShortcut =
1000
+ isKeyboardEvent.primaryShift( event, 'z' ) ||
1001
+ ( ! isAppleOS() && isKeyboardEvent.primary( event, 'y' ) );
1002
+ if ( ( isUndoShortcut || isRedoShortcut ) && isImage ) {
1003
+ const target = event.target as HTMLElement;
1004
+ const isMetadataField =
1005
+ ( target.tagName === 'INPUT' ||
1006
+ target.tagName === 'TEXTAREA' ||
1007
+ target.isContentEditable ) &&
1008
+ ! target.closest( `[${ CROP_CONTROL_ATTR }]` );
1009
+ if ( ! isMetadataField ) {
1010
+ event.preventDefault();
1011
+ if ( isCropInteractionActive || isSaving ) {
1012
+ return;
1013
+ }
1014
+ if ( isRedoShortcut ) {
1015
+ session.redo();
1016
+ } else {
1017
+ session.undo();
1018
+ }
962
1019
  }
963
1020
  }
964
- }
965
1021
 
966
- if ( shouldCloseOnEsc ) {
967
- if ( event.code !== 'Escape' && event.key !== 'Escape' ) {
968
- return;
969
- }
970
- if ( isSaving ) {
971
- event.preventDefault();
972
- return;
973
- }
974
- if ( hasChanges ) {
975
- event.preventDefault();
976
- setIsDiscardDialogOpen( true );
1022
+ if ( shouldCloseOnEsc ) {
1023
+ if ( event.code !== 'Escape' && event.key !== 'Escape' ) {
1024
+ return;
1025
+ }
1026
+ if ( isSaving ) {
1027
+ event.preventDefault();
1028
+ return;
1029
+ }
1030
+ if ( hasChanges ) {
1031
+ event.preventDefault();
1032
+ setIsDiscardDialogOpen( true );
1033
+ }
977
1034
  }
978
1035
  }
979
- };
1036
+ );
980
1037
 
981
1038
  const snackbar = (
982
1039
  <SnackbarNotices
@@ -999,32 +1056,31 @@ function MediaEditorContent( {
999
1056
  />
1000
1057
  ) : null;
1001
1058
 
1002
- // Crop leads, since it is what the docked panel opens on. Non-image media
1003
- // has nothing to crop and gets Details alone, which is why the sidebar
1004
- // takes this list rather than deriving it.
1005
- const tabs: MediaEditorTab[] = [
1006
- ...( isImage
1007
- ? [
1008
- {
1009
- id: CROP_PANEL,
1010
- title: __( 'Crop' ),
1011
- render: () => (
1012
- <MediaEditorCropPanel
1013
- aspectRatioValue={ aspectRatioValue }
1014
- onAspectRatioChange={ setAspectRatioValue }
1015
- aspectRatioOptions={ aspectRatioOptions }
1016
- disabled={ isSaving }
1017
- />
1018
- ),
1019
- },
1020
- ]
1021
- : [] ),
1022
- {
1023
- id: DETAILS_PANEL,
1024
- title: __( 'Details' ),
1025
- render: () => <MediaForm />,
1026
- },
1027
- ];
1059
+ const { panels, contextValue: panelsContextValue } = useMediaEditorPanels();
1060
+ const availableTabs: MediaEditorTab[] = panels
1061
+ .filter(
1062
+ ( panel ) =>
1063
+ ! panel.mediaTypes || panel.mediaTypes.includes( mediaType )
1064
+ )
1065
+ .map( ( panel ) => ( {
1066
+ id: panel.name,
1067
+ title: panel.title,
1068
+ render: () => (
1069
+ <Slot
1070
+ bubblesVirtually
1071
+ name={ getPanelSlotName( panel.name ) }
1072
+ fillProps={ { disabled: isSaving } }
1073
+ />
1074
+ ),
1075
+ } ) );
1076
+ // Put the default panel first and keep the others in order.
1077
+ const defaultTab = availableTabs.find( ( tab ) => tab.id === defaultPanel );
1078
+ const tabs = defaultTab
1079
+ ? [
1080
+ defaultTab,
1081
+ ...availableTabs.filter( ( tab ) => tab !== defaultTab ),
1082
+ ]
1083
+ : availableTabs;
1028
1084
 
1029
1085
  const ruler = isImage ? (
1030
1086
  <MediaEditorFineRotation
@@ -1034,23 +1090,7 @@ function MediaEditorContent( {
1034
1090
  ) : null;
1035
1091
 
1036
1092
  const children = (
1037
- <MediaEditorProvider
1038
- value={ media ?? undefined }
1039
- onChange={ handleChange }
1040
- settings={ {
1041
- // Disable the fields while saving, so the guard in
1042
- // `handleChange` is not silently swallowing typing.
1043
- // `readOnly` would swap the field's layout mid-save;
1044
- // disabled keeps it in place and greys it out.
1045
- fields: isSaving
1046
- ? fields.map( ( field ) => ( {
1047
- ...field,
1048
- isDisabled: true,
1049
- } ) )
1050
- : fields,
1051
- form,
1052
- } }
1053
- >
1093
+ <>
1054
1094
  <div className="media-editor">
1055
1095
  { ! media ? (
1056
1096
  <div className="media-editor__loading">
@@ -1104,25 +1144,31 @@ function MediaEditorContent( {
1104
1144
  exit="closed"
1105
1145
  transition={ sidebarTransition }
1106
1146
  >
1107
- <MediaEditorSidebar
1108
- tabs={ tabs }
1109
- // Swapping the edited media can drop the
1110
- // Crop tab out from under an open panel —
1111
- // saving a crop moves the editor to a new
1112
- // id, which may resolve as non-image.
1113
- // Fall back rather than hand `Tabs.Root`
1114
- // a value no tab matches.
1115
- activeTab={
1116
- tabs.some(
1117
- ( tab ) =>
1118
- tab.id === activePanel
1119
- )
1120
- ? activePanel
1121
- : DETAILS_PANEL
1122
- }
1123
- onSelectTab={ selectPanel }
1124
- disabled={ isSaving }
1125
- />
1147
+ { /* Panels announce their tabs after the
1148
+ first render. Waiting for them outside
1149
+ this container would slide it in on
1150
+ every open. */ }
1151
+ { tabs.length > 0 && (
1152
+ <MediaEditorSidebar
1153
+ tabs={ tabs }
1154
+ // Swapping the edited media can drop the
1155
+ // Crop tab out from under an open panel —
1156
+ // saving a crop moves the editor to a new
1157
+ // id, which may resolve as non-image.
1158
+ // Fall back rather than hand `Tabs.Root`
1159
+ // a value no tab matches.
1160
+ activeTab={
1161
+ tabs.some(
1162
+ ( tab ) =>
1163
+ tab.id === activePanel
1164
+ )
1165
+ ? activePanel
1166
+ : DETAILS_PANEL
1167
+ }
1168
+ onSelectTab={ selectPanel }
1169
+ disabled={ isSaving }
1170
+ />
1171
+ ) }
1126
1172
  </motion.div>
1127
1173
  ) }
1128
1174
  </AnimatePresence>
@@ -1146,7 +1192,7 @@ function MediaEditorContent( {
1146
1192
  { noticesPortalElement
1147
1193
  ? createPortal( snackbar, noticesPortalElement )
1148
1194
  : snackbar }
1149
- </MediaEditorProvider>
1195
+ </>
1150
1196
  );
1151
1197
 
1152
1198
  // Rebuilt each render, like the elements it replaces: consumers re-render
@@ -1175,7 +1221,56 @@ function MediaEditorContent( {
1175
1221
  return (
1176
1222
  <MediaEditorFrameContext.Provider value={ contextValue }>
1177
1223
  { renderFrame( {
1178
- children,
1224
+ children: (
1225
+ <MediaEditorProvider
1226
+ value={ media ?? undefined }
1227
+ onChange={ handleChange }
1228
+ settings={ {
1229
+ // Disable the fields while saving, so the guard in
1230
+ // `handleChange` is not silently swallowing typing.
1231
+ // `readOnly` would swap the field's layout mid-save;
1232
+ // disabled keeps it in place and greys it out.
1233
+ fields: isSaving
1234
+ ? fields.map( ( field ) => ( {
1235
+ ...field,
1236
+ isDisabled: true,
1237
+ } ) )
1238
+ : fields,
1239
+ form,
1240
+ } }
1241
+ >
1242
+ <SlotFillProvider>
1243
+ <MediaEditorPanelsProvider
1244
+ value={ panelsContextValue }
1245
+ >
1246
+ { /* Inside the session and `MediaEditorProvider`, so
1247
+ panel content can read both. */ }
1248
+ <MediaEditorPanel
1249
+ name={ CROP_PANEL }
1250
+ title={ __( 'Crop' ) }
1251
+ mediaTypes={ CROP_MEDIA_TYPES }
1252
+ >
1253
+ { ( { disabled } ) => (
1254
+ <CropPanelContent
1255
+ disabled={ disabled }
1256
+ />
1257
+ ) }
1258
+ </MediaEditorPanel>
1259
+ <MediaEditorPanel
1260
+ name={ DETAILS_PANEL }
1261
+ title={ __( 'Details' ) }
1262
+ >
1263
+ <MediaForm />
1264
+ </MediaEditorPanel>
1265
+ <PluginArea
1266
+ scope={ MEDIA_EDITOR_PLUGIN_SCOPE }
1267
+ onError={ handlePluginError }
1268
+ />
1269
+ { children }
1270
+ </MediaEditorPanelsProvider>
1271
+ </SlotFillProvider>
1272
+ </MediaEditorProvider>
1273
+ ),
1179
1274
  isImage,
1180
1275
  layout,
1181
1276
  onRequestClose: handleRequestClose,
@@ -120,6 +120,13 @@
120
120
  height: 100%;
121
121
  }
122
122
 
123
+ // The grey stage is for previewing media. Without a preview, e.g. for
124
+ // a zip file or a file that failed to load, the placeholder sits on
125
+ // the surface instead.
126
+ &:has(.media-editor-preview.is-placeholder) {
127
+ background: none;
128
+ }
129
+
123
130
  img,
124
131
  video {
125
132
  max-width: 100%;
@@ -168,6 +175,18 @@
168
175
  }
169
176
  }
170
177
 
178
+ // The grey stage normally marks the panel's edge. Without it, e.g. for a
179
+ // file without a preview, the panel takes a border instead.
180
+ .media-editor__body:has(.media-editor-preview.is-placeholder)
181
+ .media-editor__sidebar {
182
+ @include break-small() {
183
+ // Matches the page header's bottom border in the route.
184
+ border-inline-start:
185
+ var(--wpds-border-width-xs) solid
186
+ var(--wpds-color-stroke-surface-neutral-weak);
187
+ }
188
+ }
189
+
171
190
  // The strip and the open pane, stacked. No inline padding: the pane below
172
191
  // scrolls, and a gutter here would inset its scrollbar from the sidebar
173
192
  // edge. The strip and the pane carry their own instead.
@@ -222,9 +241,10 @@
222
241
  .media-editor__body.has-panel-open .media-editor__content {
223
242
  display: none;
224
243
 
244
+ // Only remove the right margin, so the route's bottom margin still applies.
225
245
  @include break-small() {
226
246
  display: flex;
227
- margin: 0 0 0 $grid-unit-30;
247
+ margin-right: 0;
228
248
  }
229
249
  }
230
250
 
@@ -6,20 +6,27 @@ import {
6
6
  shortcutAriaLabel,
7
7
  type WPKeycodeModifier,
8
8
  } from '@wordpress/keycodes';
9
+ import { VisuallyHidden } from '@wordpress/ui';
9
10
 
10
- interface KeyCombination {
11
- /** Modifier for cross-platform display (e.g. 'primary', 'primaryShift', 'shift'). */
12
- modifier?: WPKeycodeModifier;
13
- /** The key character(s) to display. Pass an array to render each as its own key. */
14
- character: string | string[];
15
- /** Optional aria-label override (used when character is a symbol). */
16
- ariaLabel?: string;
17
- }
11
+ type KeyCombinationDisplayProps =
12
+ | {
13
+ keyCombination: {
14
+ character: string;
15
+ modifier?: WPKeycodeModifier | null;
16
+ };
17
+ label?: string;
18
+ }
19
+ | {
20
+ keyCombination: {
21
+ character: string[];
22
+ modifier?: WPKeycodeModifier | null;
23
+ };
24
+ label: string;
25
+ };
18
26
 
19
- interface ShortcutEntry {
27
+ type ShortcutEntry = KeyCombinationDisplayProps & {
20
28
  description: string;
21
- keyCombination: KeyCombination;
22
- }
29
+ };
23
30
 
24
31
  const SHORTCUTS: ShortcutEntry[] = [
25
32
  {
@@ -34,8 +41,8 @@ const SHORTCUTS: ShortcutEntry[] = [
34
41
  description: __( 'Pan' ),
35
42
  keyCombination: {
36
43
  character: [ '↑', '↓', '←', '→' ],
37
- ariaLabel: __( 'Arrow keys' ),
38
44
  },
45
+ label: __( 'Arrow keys' ),
39
46
  },
40
47
  {
41
48
  description: __( 'Zoom in' ),
@@ -66,64 +73,51 @@ const SHORTCUTS: ShortcutEntry[] = [
66
73
  keyCombination: {
67
74
  modifier: 'shift',
68
75
  character: [ '↑', '↓', '←', '→' ],
69
- ariaLabel: __( 'Shift + Arrow keys' ),
70
76
  },
77
+ label: __( 'Shift + Arrow keys' ),
71
78
  },
72
79
  ];
73
80
 
74
81
  function KeyCombinationDisplay( {
75
82
  keyCombination,
76
- }: {
77
- keyCombination: KeyCombination;
78
- } ) {
79
- const { modifier, character, ariaLabel } = keyCombination;
80
-
81
- let keys: string[];
82
- if ( Array.isArray( character ) ) {
83
- if ( modifier ) {
84
- // Get the modifier prefix (e.g. ['⇧', '+']) from the first char,
85
- // then replace the last element with all the individual chars.
86
- const sample = displayShortcutList[ modifier ]( character[ 0 ] );
87
- keys = [ ...sample.slice( 0, -1 ), ...character ];
88
- } else {
89
- keys = character;
90
- }
91
- } else {
92
- keys = modifier
93
- ? displayShortcutList[ modifier ]( character )
94
- : [ character ];
95
- }
96
-
97
- const charString = Array.isArray( character )
98
- ? character.join( '' )
99
- : character;
100
- let label: string;
101
- if ( ariaLabel ) {
102
- label = ariaLabel;
103
- } else if ( modifier ) {
104
- label = shortcutAriaLabel[ modifier ]( charString );
105
- } else {
106
- label = charString;
107
- }
83
+ label,
84
+ }: KeyCombinationDisplayProps ) {
85
+ const { character, modifier } = keyCombination;
86
+ const normalizedModifier = modifier ?? 'undefined';
87
+ const characters = Array.isArray( character ) ? character : [ character ];
88
+ const formatted = displayShortcutList[ normalizedModifier ](
89
+ characters[ 0 ] ?? ''
90
+ );
91
+ const prefix = formatted.slice( 0, -1 );
92
+ const keys = Array.isArray( character )
93
+ ? [ ...prefix, ...character ]
94
+ : formatted;
108
95
 
109
96
  return (
110
97
  <kbd
111
98
  className="media-editor-keyboard-shortcuts-modal__shortcut-term"
112
99
  dir="ltr"
113
- aria-label={ label }
114
100
  >
115
- { keys.map( ( key, index ) =>
116
- key === '+' && modifier ? (
117
- <Fragment key={ index }>{ key }</Fragment>
118
- ) : (
119
- <kbd
120
- key={ index }
121
- className="media-editor-keyboard-shortcuts-modal__shortcut-key"
122
- >
123
- { key }
124
- </kbd>
125
- )
126
- ) }
101
+ <VisuallyHidden render={ <span /> }>
102
+ { label ||
103
+ shortcutAriaLabel[ normalizedModifier ](
104
+ characters.join( '' )
105
+ ) }
106
+ </VisuallyHidden>
107
+ <span aria-hidden="true">
108
+ { keys.map( ( key, keyIndex ) =>
109
+ key === '+' && keyIndex < prefix.length ? (
110
+ <Fragment key={ keyIndex }>{ key }</Fragment>
111
+ ) : (
112
+ <kbd
113
+ key={ keyIndex }
114
+ className="media-editor-keyboard-shortcuts-modal__shortcut-key"
115
+ >
116
+ { key }
117
+ </kbd>
118
+ )
119
+ ) }
120
+ </span>
127
121
  </kbd>
128
122
  );
129
123
  }
@@ -155,17 +149,15 @@ export default function MediaEditorKeyboardShortcutsModal( {
155
149
  className="media-editor-keyboard-shortcuts-modal__shortcut-list"
156
150
  role="list"
157
151
  >
158
- { SHORTCUTS.map( ( { description, keyCombination }, index ) => (
152
+ { SHORTCUTS.map( ( shortcut, index ) => (
159
153
  <li
160
154
  key={ index }
161
155
  className="media-editor-keyboard-shortcuts-modal__shortcut"
162
156
  >
163
157
  <span className="media-editor-keyboard-shortcuts-modal__shortcut-description">
164
- { description }
158
+ { shortcut.description }
165
159
  </span>
166
- <KeyCombinationDisplay
167
- keyCombination={ keyCombination }
168
- />
160
+ <KeyCombinationDisplay { ...shortcut } />
169
161
  </li>
170
162
  ) ) }
171
163
  </ul>
@@ -104,6 +104,9 @@ export function MediaEditorModal( {
104
104
  shouldCloseOnEsc
105
105
  noticesClassName="media-editor-modal__snackbar"
106
106
  noticesPortalElement={ portalElement }
107
+ // Rotate, flip and aspect ratio are only in the Crop panel for now.
108
+ // This can go once they move under the canvas (#87282).
109
+ defaultPanel="core/crop"
107
110
  onClose={ handleClose }
108
111
  onSaved={ ( { id: savedId, url, previous } ) => {
109
112
  if ( savedId && onUpdate ) {