@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
@@ -1 +1 @@
1
- {"version":3,"file":"use-media-editor-state.cjs","names":["ORIGINAL_ASPECT_RATIO","useReducer","mediaEditorReducer","buildInitialMediaEditorState","createImageEditorState","useState","useRef","useCallback","areMediaEditorStatesEqual","useImageEditor","useMemo"],"sources":["../../src/state/use-media-editor-state.ts"],"sourcesContent":["import {\n\tuseCallback,\n\tuseMemo,\n\tuseReducer,\n\tuseRef,\n\tuseState,\n} from '@wordpress/element';\nimport {\n\tcreateImageEditorState,\n\tuseImageEditor,\n\ttype ImageEditorController,\n\ttype ImageEditorState,\n} from '@wordpress/image-editor';\nimport { ORIGINAL_ASPECT_RATIO } from './aspect-ratio-presets';\nimport {\n\tmediaEditorReducer,\n\tareMediaEditorStatesEqual,\n\tbuildInitialMediaEditorState,\n} from './composite-reducer';\nimport type {\n\tCropOptionsSlice,\n\tEditsSlice,\n\tEditValue,\n\tMediaEditorAction,\n\tMediaEditorState,\n} from './types';\n\n/**\n * Resolve an aspect-ratio preset value (the string the dropdown\n * round-trips) into a numeric width/height ratio for the image editor.\n *\n * - `'0'` → `undefined` (Free; no lock)\n * - `'-1'` (Original) → image's natural width / height, or `undefined`\n * if no image is loaded.\n * - Any positive decimal → the parsed number.\n *\n * @param presetKey Preset value from the dropdown.\n * @param image The image editor's loaded image, or `null`.\n * @return The resolved ratio, or `undefined` for Free / unresolvable.\n */\nfunction resolveAspectRatio(\n\tpresetKey: string,\n\timage: ImageEditorState[ 'image' ] | null\n): number | undefined {\n\tconst num = parseFloat( presetKey );\n\tif ( num === 0 || Number.isNaN( num ) ) {\n\t\treturn undefined;\n\t}\n\tif ( num === ORIGINAL_ASPECT_RATIO ) {\n\t\tif ( image && image.naturalWidth > 0 && image.naturalHeight > 0 ) {\n\t\t\treturn image.naturalWidth / image.naturalHeight;\n\t\t}\n\t\treturn undefined;\n\t}\n\tif ( num > 0 ) {\n\t\treturn num;\n\t}\n\treturn undefined;\n}\n\nfunction areImagesEqual(\n\ta: ImageEditorState[ 'image' ],\n\tb: ImageEditorState[ 'image' ]\n): boolean {\n\treturn (\n\t\ta?.src === b?.src &&\n\t\ta?.naturalWidth === b?.naturalWidth &&\n\t\ta?.naturalHeight === b?.naturalHeight\n\t);\n}\n\n/**\n * The media editor session exposed by `useMediaEditorState`.\n *\n * The session owns what is being edited and how: the source image,\n * every editing slice, the shared undo/redo history, gesture\n * boundaries, and whether the edits change the saved image. The\n * image editor is one participant in the session — `imageEditor` is the\n * controller an `<ImageEditorCanvas>` takes via its `controller` prop — rather\n * than the session itself.\n */\nexport interface MediaEditorSession {\n\t/**\n\t * Controller for the image editor. Its setters record history in the\n\t * session; its `isDirty` covers the crop only. Its `setImage` is\n\t * `setSourceImage`, so the `<ImageEditorCanvas>` reporting a newly loaded\n\t * image starts a fresh session.\n\t */\n\timageEditor: ImageEditorController;\n\t/** The cropOptions slice. */\n\tcropOptions: CropOptionsSlice;\n\t/** Set the aspect-ratio preset and reshape the crop in one undo entry. */\n\tsetAspectRatioValue: ( presetKey: string ) => void;\n\t/**\n\t * Put every slice back to its starting state in one undo entry:\n\t * default crop and crop options, no edits. Keeps the image.\n\t */\n\treset: () => void;\n\t/** The named edits other than crop, keyed by edit name. */\n\tedits: EditsSlice;\n\t/**\n\t * Set a named edit, or remove it by passing `undefined`. Recorded\n\t * in the session history like any crop change. Setting a value\n\t * equal in content to the current one is a no-op.\n\t */\n\tsetEdit: ( name: string, value: EditValue | undefined ) => void;\n\t/**\n\t * Replace the image being edited. A different image starts a\n\t * fresh session: default crop and crop options, a clean baseline,\n\t * and no history. Reporting the same image again is a no-op.\n\t */\n\tsetSourceImage: ( image: ImageEditorState[ 'image' ] ) => void;\n\t/**\n\t * Whether anything in the session differs from its baseline,\n\t * including editor-only state such as the aspect-ratio preset.\n\t */\n\tisDirty: boolean;\n\t/**\n\t * Whether the edits change the saved image, so saving has to\n\t * produce new image output rather than only metadata.\n\t */\n\thasOutputEdits: boolean;\n\t/** Whether there's an undoable change in history. */\n\thasUndo: boolean;\n\t/** Whether there's a redoable change in history. */\n\thasRedo: boolean;\n\t/** Undo the last committed change across all slices. */\n\tundo: () => void;\n\t/** Redo the last undone change across all slices. */\n\tredo: () => void;\n\t/**\n\t * Open a gesture boundary. During a gesture all changes coalesce\n\t * into one undo entry — the snapshot taken at `beginGesture`. Pair\n\t * with `endGesture` on pointer-up / key-up.\n\t */\n\tbeginGesture: () => void;\n\t/** Close a gesture boundary and commit the pre-gesture snapshot. */\n\tendGesture: () => void;\n}\n\n/**\n * The media editor session.\n *\n * Owns the only undo/redo history for the editor. The image editor's state,\n * the sidebar crop options and named edits are slices of one state, so\n * one history stack covers them all. The image editor works out its own\n * changes and reports each next state through `onChange`.\n *\n * History semantics:\n * - Each change pushes its pre-state to the undo stack, unless a\n * gesture is open or the change is a no-op (post-state value-equal\n * to pre-state).\n * - During a gesture (`beginGesture` → `endGesture`) changes coalesce:\n * only the pre-gesture snapshot is pushed, at gesture end. Undo and\n * redo close an open gesture first.\n * - Changes the image editor marks as not edits (an image loading, a refit\n * after a window resize) are never recorded.\n *\n * `isDirty` tracks the full session, including undoable UI state. Use\n * `hasOutputEdits` when deciding whether the image output needs saving.\n *\n * The session starts empty. Call `setSourceImage` to load an image.\n *\n * @return The media editor session.\n */\nexport function useMediaEditorState(): MediaEditorSession {\n\tconst [ state, dispatch ] = useReducer( mediaEditorReducer, null, () =>\n\t\tbuildInitialMediaEditorState( createImageEditorState() )\n\t);\n\n\t// The \"clean\" snapshot the current state is compared against for\n\t// `isDirty`. `setSourceImage` refreshes it when a new image starts a\n\t// fresh session.\n\tconst [ initialBaseline, setInitialBaseline ] =\n\t\tuseState< MediaEditorState >( () => state );\n\n\t// Latest state, so several actions in one event see each other's\n\t// output before React commits. Every mutation path writes it\n\t// synchronously.\n\tconst stateRef = useRef( state );\n\n\t// History stacks: full snapshots. Refs avoid re-renders on every\n\t// push; a boolean state pair drives the undo/redo indicators.\n\tconst historyRef = useRef< MediaEditorState[] >( [] );\n\tconst redoStackRef = useRef< MediaEditorState[] >( [] );\n\tconst [ hasUndo, setHasUndo ] = useState( false );\n\tconst [ hasRedo, setHasRedo ] = useState( false );\n\n\t// Gesture state. While open, the first change captures the\n\t// pre-gesture snapshot; later changes don't push.\n\tconst isGestureOpenRef = useRef( false );\n\tconst gestureSnapshotRef = useRef< MediaEditorState | null >( null );\n\n\tconst pushSnapshot = useCallback( ( snapshot: MediaEditorState ) => {\n\t\tconst last = historyRef.current[ historyRef.current.length - 1 ];\n\t\tif ( last && areMediaEditorStatesEqual( last, snapshot ) ) {\n\t\t\treturn;\n\t\t}\n\t\thistoryRef.current = [ ...historyRef.current, snapshot ];\n\t\tredoStackRef.current = [];\n\t\tsetHasUndo( true );\n\t\tsetHasRedo( false );\n\t}, [] );\n\n\tconst dispatchWithHistory = useCallback(\n\t\t( action: MediaEditorAction, recordHistory = true ) => {\n\t\t\tconst preState = stateRef.current;\n\t\t\tconst postState = mediaEditorReducer( preState, action );\n\t\t\tif (\n\t\t\t\trecordHistory &&\n\t\t\t\t! areMediaEditorStatesEqual( preState, postState )\n\t\t\t) {\n\t\t\t\tif ( isGestureOpenRef.current ) {\n\t\t\t\t\tif ( gestureSnapshotRef.current === null ) {\n\t\t\t\t\t\tgestureSnapshotRef.current = preState;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tpushSnapshot( preState );\n\t\t\t\t}\n\t\t\t}\n\t\t\tstateRef.current = postState;\n\t\t\tdispatch( action );\n\t\t},\n\t\t[ pushSnapshot ]\n\t);\n\n\tconst beginGesture = useCallback( () => {\n\t\tif ( isGestureOpenRef.current ) {\n\t\t\treturn;\n\t\t}\n\t\tisGestureOpenRef.current = true;\n\t\tgestureSnapshotRef.current = null;\n\t}, [] );\n\n\t// Close a gesture (or flush a pending one before undo/redo). Pushes\n\t// the pre-gesture snapshot only if it differs from the current\n\t// state. Safe to call when no gesture is open.\n\tconst endGesture = useCallback( () => {\n\t\tconst snapshot = gestureSnapshotRef.current;\n\t\tif (\n\t\t\tsnapshot &&\n\t\t\t! areMediaEditorStatesEqual( snapshot, stateRef.current )\n\t\t) {\n\t\t\tpushSnapshot( snapshot );\n\t\t}\n\t\tgestureSnapshotRef.current = null;\n\t\tisGestureOpenRef.current = false;\n\t}, [ pushSnapshot ] );\n\n\t// Run several changes as one undo entry, unless a gesture is already\n\t// open, which groups them anyway.\n\tconst asOneUndoEntry = useCallback(\n\t\t( run: () => void ) => {\n\t\t\tif ( isGestureOpenRef.current ) {\n\t\t\t\trun();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tbeginGesture();\n\t\t\trun();\n\t\t\tendGesture();\n\t\t},\n\t\t[ beginGesture, endGesture ]\n\t);\n\n\tconst setSourceImage = useCallback(\n\t\t( image: ImageEditorState[ 'image' ] ) => {\n\t\t\tif ( areImagesEqual( stateRef.current.imageEditor.image, image ) ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\t// New image = fresh session: default crop and crop options, a\n\t\t\t// clean baseline, and no history. Edits made against one\n\t\t\t// image must not survive onto another.\n\t\t\tconst next = buildInitialMediaEditorState(\n\t\t\t\tcreateImageEditorState( { image } )\n\t\t\t);\n\t\t\tstateRef.current = next;\n\t\t\tdispatch( { type: 'RESTORE_SNAPSHOT', payload: next } );\n\t\t\tsetInitialBaseline( next );\n\t\t\tisGestureOpenRef.current = false;\n\t\t\tgestureSnapshotRef.current = null;\n\t\t\thistoryRef.current = [];\n\t\t\tredoStackRef.current = [];\n\t\t\tsetHasUndo( false );\n\t\t\tsetHasRedo( false );\n\t\t},\n\t\t[]\n\t);\n\n\tconst handleImageEditorChange = useCallback(\n\t\t( nextState: ImageEditorState, { isEdit }: { isEdit: boolean } ) => {\n\t\t\tif (\n\t\t\t\t! areImagesEqual(\n\t\t\t\t\tstateRef.current.imageEditor.image,\n\t\t\t\t\tnextState.image\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tsetSourceImage( nextState.image );\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tdispatchWithHistory(\n\t\t\t\t{ type: 'SET_IMAGE_EDITOR', payload: nextState },\n\t\t\t\tisEdit\n\t\t\t);\n\t\t},\n\t\t[ dispatchWithHistory, setSourceImage ]\n\t);\n\n\tconst imageEditorController = useImageEditor( {\n\t\tstate: state.imageEditor,\n\t\tonChange: handleImageEditorChange,\n\t} );\n\n\t// The locked ratio has two owners: the preset the sidebar shows, and\n\t// the number the image editor holds. This is the only path that sets\n\t// both, so call it rather than `imageEditor.setAspectRatio`, which\n\t// would leave the sidebar showing a ratio the crop no longer has.\n\tconst setAspectRatioValue = useCallback(\n\t\t( presetKey: string ) => {\n\t\t\tasOneUndoEntry( () => {\n\t\t\t\tdispatchWithHistory( {\n\t\t\t\t\ttype: 'SET_ASPECT_RATIO_VALUE',\n\t\t\t\t\tpayload: presetKey,\n\t\t\t\t} );\n\t\t\t\timageEditorController.setAspectRatio(\n\t\t\t\t\tresolveAspectRatio(\n\t\t\t\t\t\tpresetKey,\n\t\t\t\t\t\tstateRef.current.imageEditor.image\n\t\t\t\t\t)\n\t\t\t\t);\n\t\t\t} );\n\t\t},\n\t\t[ asOneUndoEntry, dispatchWithHistory, imageEditorController ]\n\t);\n\n\tconst reset = useCallback( () => {\n\t\tdispatchWithHistory( { type: 'RESET' } );\n\t}, [ dispatchWithHistory ] );\n\n\tconst setEdit = useCallback(\n\t\t( name: string, value: EditValue | undefined ) => {\n\t\t\tdispatchWithHistory( {\n\t\t\t\ttype: 'SET_EDIT',\n\t\t\t\tpayload: { name, value },\n\t\t\t} );\n\t\t},\n\t\t[ dispatchWithHistory ]\n\t);\n\n\tconst undo = useCallback( () => {\n\t\tendGesture();\n\t\tconst prev = historyRef.current[ historyRef.current.length - 1 ];\n\t\tif ( ! prev ) {\n\t\t\treturn;\n\t\t}\n\t\tredoStackRef.current = [ stateRef.current, ...redoStackRef.current ];\n\t\thistoryRef.current = historyRef.current.slice( 0, -1 );\n\t\tsetHasUndo( historyRef.current.length > 0 );\n\t\tsetHasRedo( true );\n\t\tstateRef.current = prev;\n\t\tdispatch( { type: 'RESTORE_SNAPSHOT', payload: prev } );\n\t}, [ endGesture ] );\n\n\tconst redo = useCallback( () => {\n\t\tendGesture();\n\t\tconst next = redoStackRef.current[ 0 ];\n\t\tif ( ! next ) {\n\t\t\treturn;\n\t\t}\n\t\thistoryRef.current = [ ...historyRef.current, stateRef.current ];\n\t\tredoStackRef.current = redoStackRef.current.slice( 1 );\n\t\tsetHasUndo( true );\n\t\tsetHasRedo( redoStackRef.current.length > 0 );\n\t\tstateRef.current = next;\n\t\tdispatch( { type: 'RESTORE_SNAPSHOT', payload: next } );\n\t}, [ endGesture ] );\n\n\tconst imageEditor = useMemo< ImageEditorController >(\n\t\t() => ( { ...imageEditorController, setImage: setSourceImage } ),\n\t\t[ imageEditorController, setSourceImage ]\n\t);\n\n\tconst isDirty = ! areMediaEditorStatesEqual( state, initialBaseline );\n\n\treturn useMemo(\n\t\t() => ( {\n\t\t\timageEditor,\n\t\t\tcropOptions: state.cropOptions,\n\t\t\tsetAspectRatioValue,\n\t\t\tedits: state.edits,\n\t\t\tsetEdit,\n\t\t\tsetSourceImage,\n\t\t\treset,\n\t\t\tisDirty,\n\t\t\t// Only the crop changes the saved image so far.\n\t\t\t//\n\t\t\t// This reads the image editor's own clean state, not the\n\t\t\t// session's. The two agree because the session only ever\n\t\t\t// replaces the editor's slice with a state the editor handed\n\t\t\t// out, and because a fresh state for an image matches the\n\t\t\t// default one on every field `isDirty` compares. Change\n\t\t\t// either and this has to compare against the session's\n\t\t\t// baseline instead.\n\t\t\thasOutputEdits: imageEditor.isDirty,\n\t\t\thasUndo,\n\t\t\thasRedo,\n\t\t\tundo,\n\t\t\tredo,\n\t\t\tbeginGesture,\n\t\t\tendGesture,\n\t\t} ),\n\t\t[\n\t\t\timageEditor,\n\t\t\tstate.cropOptions,\n\t\t\tsetAspectRatioValue,\n\t\t\tstate.edits,\n\t\t\tsetEdit,\n\t\t\tsetSourceImage,\n\t\t\treset,\n\t\t\tisDirty,\n\t\t\thasUndo,\n\t\t\thasRedo,\n\t\t\tundo,\n\t\t\tredo,\n\t\t\tbeginGesture,\n\t\t\tendGesture,\n\t\t]\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAwCA,SAAS,mBACR,WACA,OACqB;CACrB,MAAM,MAAM,WAAY,SAAU;CAClC,IAAK,QAAQ,KAAK,OAAO,MAAO,GAAI,GACnC;CAED,IAAK,QAAQA,0BAAAA,uBAAwB;EACpC,IAAK,SAAS,MAAM,eAAe,KAAK,MAAM,gBAAgB,GAC7D,OAAO,MAAM,eAAe,MAAM;EAEnC;CACD;CACA,IAAK,MAAM,GACV,OAAO;AAGT;AAEA,SAAS,eACR,GACA,GACU;CACV,OACC,GAAG,QAAQ,GAAG,OACd,GAAG,iBAAiB,GAAG,gBACvB,GAAG,kBAAkB,GAAG;AAE1B;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,mBAAAA,YAAAA,OAAmB;EAC/B,WAAW;EACX,MAAM,OAAO,aAAa,QAAS;EACnC,IAAK,CAAE,MACN;EAED,WAAW,UAAU,CAAE,GAAG,WAAW,SAAS,SAAS,OAAQ;EAC/D,aAAa,UAAU,aAAa,QAAQ,MAAO,CAAE;EACrD,WAAY,IAAK;EACjB,WAAY,aAAa,QAAQ,SAAS,CAAE;EAC5C,SAAS,UAAU;EACnB,SAAU;GAAE,MAAM;GAAoB,SAAS;EAAK,CAAE;CACvD,GAAG,CAAE,UAAW,CAAE;CAElB,MAAM,eAAA,GAAcG,mBAAAA,QAAAA,QACX;EAAE,GAAG;EAAuB,UAAU;CAAe,IAC7D,CAAE,uBAAuB,cAAe,CACzC;CAEA,MAAM,UAAU,EAAA,GAAEF,uBAAAA,0BAAAA,CAA2B,OAAO,eAAgB;CAEpE,QAAA,GAAOE,mBAAAA,QAAAA,QACE;EACP;EACA,aAAa,MAAM;EACnB;EACA,OAAO,MAAM;EACb;EACA;EACA;EACA;EAUA,gBAAgB,YAAY;EAC5B;EACA;EACA;EACA;EACA;EACA;CACD,IACA;EACC;EACA,MAAM;EACN;EACA,MAAM;EACN;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACD,CACD;AACD"}
1
+ {"version":3,"file":"use-media-editor-state.cjs","names":["ORIGINAL_ASPECT_RATIO","useReducer","mediaEditorReducer","buildInitialMediaEditorState","createImageEditorState","useState","useRef","useCallback","areMediaEditorStatesEqual","useImageEditor","useMemo"],"sources":["../../src/state/use-media-editor-state.ts"],"sourcesContent":["import {\n\tuseCallback,\n\tuseMemo,\n\tuseReducer,\n\tuseRef,\n\tuseState,\n} from '@wordpress/element';\nimport {\n\tcreateImageEditorState,\n\tuseImageEditor,\n\ttype ImageEditorController,\n\ttype ImageEditorState,\n} from '@wordpress/image-editor';\nimport { ORIGINAL_ASPECT_RATIO } from './aspect-ratio-presets';\nimport {\n\tmediaEditorReducer,\n\tareMediaEditorStatesEqual,\n\tbuildInitialMediaEditorState,\n} from './composite-reducer';\nimport type {\n\tCropOptionsSlice,\n\tEditsSlice,\n\tEditValue,\n\tMediaEditorAction,\n\tMediaEditorState,\n} from './types';\n\n/**\n * Resolve an aspect-ratio preset value (the string the dropdown\n * round-trips) into a numeric width/height ratio for the image editor.\n *\n * - `'0'` → `undefined` (Free; no lock)\n * - `'-1'` (Original) → image's natural width / height, or `undefined`\n * if no image is loaded.\n * - Any positive decimal → the parsed number.\n *\n * @param presetKey Preset value from the dropdown.\n * @param image The image editor's loaded image, or `null`.\n * @return The resolved ratio, or `undefined` for Free / unresolvable.\n */\nfunction resolveAspectRatio(\n\tpresetKey: string,\n\timage: ImageEditorState[ 'image' ] | null\n): number | undefined {\n\tconst num = parseFloat( presetKey );\n\tif ( num === 0 || Number.isNaN( num ) ) {\n\t\treturn undefined;\n\t}\n\tif ( num === ORIGINAL_ASPECT_RATIO ) {\n\t\tif ( image && image.naturalWidth > 0 && image.naturalHeight > 0 ) {\n\t\t\treturn image.naturalWidth / image.naturalHeight;\n\t\t}\n\t\treturn undefined;\n\t}\n\tif ( num > 0 ) {\n\t\treturn num;\n\t}\n\treturn undefined;\n}\n\nfunction areImagesEqual(\n\ta: ImageEditorState[ 'image' ],\n\tb: ImageEditorState[ 'image' ]\n): boolean {\n\treturn (\n\t\ta?.src === b?.src &&\n\t\ta?.naturalWidth === b?.naturalWidth &&\n\t\ta?.naturalHeight === b?.naturalHeight\n\t);\n}\n\n/**\n * The media editor session exposed by `useMediaEditorState`.\n *\n * The session owns what is being edited and how: the source image,\n * every editing slice, the shared undo/redo history, gesture\n * boundaries, and whether the edits change the saved image. The\n * image editor is one participant in the session — `imageEditor` is the\n * controller an `<ImageEditorCanvas>` takes via its `controller` prop — rather\n * than the session itself.\n */\nexport interface MediaEditorSession {\n\t/**\n\t * Controller for the image editor. Its setters record history in the\n\t * session; its `isDirty` covers the crop only. Its `setImage` is\n\t * `setSourceImage`, so the `<ImageEditorCanvas>` reporting a newly loaded\n\t * image starts a fresh session.\n\t */\n\timageEditor: ImageEditorController;\n\t/** The cropOptions slice. */\n\tcropOptions: CropOptionsSlice;\n\t/** Set the aspect-ratio preset and reshape the crop in one undo entry. */\n\tsetAspectRatioValue: ( presetKey: string ) => void;\n\t/**\n\t * Put every slice back to its starting state in one undo entry:\n\t * default crop and crop options, no edits. Keeps the image.\n\t */\n\treset: () => void;\n\t/**\n\t * The named edits other than crop, keyed by edit name. Not an\n\t * extension API: undoable and cleared by `reset`, but not counted by\n\t * `hasOutputEdits` and not saved.\n\t */\n\tedits: EditsSlice;\n\t/**\n\t * Set a named edit, or remove it by passing `undefined`. Recorded\n\t * in the session history like any crop change. Setting a value\n\t * equal in content to the current one is a no-op.\n\t */\n\tsetEdit: ( name: string, value: EditValue | undefined ) => void;\n\t/**\n\t * Replace the image being edited. A different image starts a\n\t * fresh session: default crop and crop options, a clean baseline,\n\t * and no history. Reporting the same image again is a no-op.\n\t */\n\tsetSourceImage: ( image: ImageEditorState[ 'image' ] ) => void;\n\t/**\n\t * Whether anything in the session differs from its baseline,\n\t * including editor-only state such as the aspect-ratio preset.\n\t */\n\tisDirty: boolean;\n\t/**\n\t * Whether the edits change the saved image, so saving has to\n\t * produce new image output rather than only metadata. Geometry\n\t * counts; named edits do not.\n\t */\n\thasOutputEdits: boolean;\n\t/** Whether there's an undoable change in history. */\n\thasUndo: boolean;\n\t/** Whether there's a redoable change in history. */\n\thasRedo: boolean;\n\t/** Undo the last committed change across all slices. */\n\tundo: () => void;\n\t/** Redo the last undone change across all slices. */\n\tredo: () => void;\n\t/**\n\t * Open a gesture boundary. During a gesture all changes coalesce\n\t * into one undo entry — the snapshot taken at `beginGesture`. Pair\n\t * with `endGesture` on pointer-up / key-up.\n\t */\n\tbeginGesture: () => void;\n\t/** Close a gesture boundary and commit the pre-gesture snapshot. */\n\tendGesture: () => void;\n}\n\n/**\n * The media editor session.\n *\n * Owns the only undo/redo history for the editor. The image editor's state,\n * the sidebar crop options and named edits are slices of one state, so\n * one history stack covers them all. The image editor works out its own\n * changes and reports each next state through `onChange`.\n *\n * History semantics:\n * - Each change pushes its pre-state to the undo stack, unless a\n * gesture is open or the change is a no-op (post-state value-equal\n * to pre-state).\n * - During a gesture (`beginGesture` → `endGesture`) changes coalesce:\n * only the pre-gesture snapshot is pushed, at gesture end. Undo and\n * redo close an open gesture first.\n * - Changes the image editor marks as not edits (an image loading, a refit\n * after a window resize) are never recorded.\n *\n * `isDirty` tracks the full session, including undoable UI state. Use\n * `hasOutputEdits` when deciding whether the image output needs saving.\n *\n * The session starts empty. Call `setSourceImage` to load an image.\n *\n * @return The media editor session.\n */\nexport function useMediaEditorState(): MediaEditorSession {\n\tconst [ state, dispatch ] = useReducer( mediaEditorReducer, null, () =>\n\t\tbuildInitialMediaEditorState( createImageEditorState() )\n\t);\n\n\t// The \"clean\" snapshot the current state is compared against for\n\t// `isDirty`. `setSourceImage` refreshes it when a new image starts a\n\t// fresh session.\n\tconst [ initialBaseline, setInitialBaseline ] =\n\t\tuseState< MediaEditorState >( () => state );\n\n\t// Latest state, so several actions in one event see each other's\n\t// output before React commits. Every mutation path writes it\n\t// synchronously.\n\tconst stateRef = useRef( state );\n\n\t// History stacks: full snapshots. Refs avoid re-renders on every\n\t// push; a boolean state pair drives the undo/redo indicators.\n\tconst historyRef = useRef< MediaEditorState[] >( [] );\n\tconst redoStackRef = useRef< MediaEditorState[] >( [] );\n\tconst [ hasUndo, setHasUndo ] = useState( false );\n\tconst [ hasRedo, setHasRedo ] = useState( false );\n\n\t// Gesture state. While open, the first change captures the\n\t// pre-gesture snapshot; later changes don't push.\n\tconst isGestureOpenRef = useRef( false );\n\tconst gestureSnapshotRef = useRef< MediaEditorState | null >( null );\n\n\tconst pushSnapshot = useCallback( ( snapshot: MediaEditorState ) => {\n\t\tconst last = historyRef.current[ historyRef.current.length - 1 ];\n\t\tif ( last && areMediaEditorStatesEqual( last, snapshot ) ) {\n\t\t\treturn;\n\t\t}\n\t\thistoryRef.current = [ ...historyRef.current, snapshot ];\n\t\tredoStackRef.current = [];\n\t\tsetHasUndo( true );\n\t\tsetHasRedo( false );\n\t}, [] );\n\n\tconst dispatchWithHistory = useCallback(\n\t\t( action: MediaEditorAction, recordHistory = true ) => {\n\t\t\tconst preState = stateRef.current;\n\t\t\tconst postState = mediaEditorReducer( preState, action );\n\t\t\tif (\n\t\t\t\trecordHistory &&\n\t\t\t\t! areMediaEditorStatesEqual( preState, postState )\n\t\t\t) {\n\t\t\t\tif ( isGestureOpenRef.current ) {\n\t\t\t\t\tif ( gestureSnapshotRef.current === null ) {\n\t\t\t\t\t\tgestureSnapshotRef.current = preState;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tpushSnapshot( preState );\n\t\t\t\t}\n\t\t\t}\n\t\t\tstateRef.current = postState;\n\t\t\tdispatch( action );\n\t\t},\n\t\t[ pushSnapshot ]\n\t);\n\n\tconst beginGesture = useCallback( () => {\n\t\tif ( isGestureOpenRef.current ) {\n\t\t\treturn;\n\t\t}\n\t\tisGestureOpenRef.current = true;\n\t\tgestureSnapshotRef.current = null;\n\t}, [] );\n\n\t// Close a gesture (or flush a pending one before undo/redo). Pushes\n\t// the pre-gesture snapshot only if it differs from the current\n\t// state. Safe to call when no gesture is open.\n\tconst endGesture = useCallback( () => {\n\t\tconst snapshot = gestureSnapshotRef.current;\n\t\tif (\n\t\t\tsnapshot &&\n\t\t\t! areMediaEditorStatesEqual( snapshot, stateRef.current )\n\t\t) {\n\t\t\tpushSnapshot( snapshot );\n\t\t}\n\t\tgestureSnapshotRef.current = null;\n\t\tisGestureOpenRef.current = false;\n\t}, [ pushSnapshot ] );\n\n\t// Run several changes as one undo entry, unless a gesture is already\n\t// open, which groups them anyway.\n\tconst asOneUndoEntry = useCallback(\n\t\t( run: () => void ) => {\n\t\t\tif ( isGestureOpenRef.current ) {\n\t\t\t\trun();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tbeginGesture();\n\t\t\trun();\n\t\t\tendGesture();\n\t\t},\n\t\t[ beginGesture, endGesture ]\n\t);\n\n\tconst setSourceImage = useCallback(\n\t\t( image: ImageEditorState[ 'image' ] ) => {\n\t\t\tif ( areImagesEqual( stateRef.current.imageEditor.image, image ) ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\t// New image = fresh session: default crop and crop options, a\n\t\t\t// clean baseline, and no history. Edits made against one\n\t\t\t// image must not survive onto another.\n\t\t\tconst next = buildInitialMediaEditorState(\n\t\t\t\tcreateImageEditorState( { image } )\n\t\t\t);\n\t\t\tstateRef.current = next;\n\t\t\tdispatch( { type: 'RESTORE_SNAPSHOT', payload: next } );\n\t\t\tsetInitialBaseline( next );\n\t\t\tisGestureOpenRef.current = false;\n\t\t\tgestureSnapshotRef.current = null;\n\t\t\thistoryRef.current = [];\n\t\t\tredoStackRef.current = [];\n\t\t\tsetHasUndo( false );\n\t\t\tsetHasRedo( false );\n\t\t},\n\t\t[]\n\t);\n\n\tconst handleImageEditorChange = useCallback(\n\t\t( nextState: ImageEditorState, { isEdit }: { isEdit: boolean } ) => {\n\t\t\tif (\n\t\t\t\t! areImagesEqual(\n\t\t\t\t\tstateRef.current.imageEditor.image,\n\t\t\t\t\tnextState.image\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tsetSourceImage( nextState.image );\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tdispatchWithHistory(\n\t\t\t\t{ type: 'SET_IMAGE_EDITOR', payload: nextState },\n\t\t\t\tisEdit\n\t\t\t);\n\t\t},\n\t\t[ dispatchWithHistory, setSourceImage ]\n\t);\n\n\tconst imageEditorController = useImageEditor( {\n\t\tstate: state.imageEditor,\n\t\tonChange: handleImageEditorChange,\n\t} );\n\n\t// The locked ratio has two owners: the preset the sidebar shows, and\n\t// the number the image editor holds. This is the only path that sets\n\t// both, so call it rather than `imageEditor.setAspectRatio`, which\n\t// would leave the sidebar showing a ratio the crop no longer has.\n\tconst setAspectRatioValue = useCallback(\n\t\t( presetKey: string ) => {\n\t\t\tasOneUndoEntry( () => {\n\t\t\t\tdispatchWithHistory( {\n\t\t\t\t\ttype: 'SET_ASPECT_RATIO_VALUE',\n\t\t\t\t\tpayload: presetKey,\n\t\t\t\t} );\n\t\t\t\timageEditorController.setAspectRatio(\n\t\t\t\t\tresolveAspectRatio(\n\t\t\t\t\t\tpresetKey,\n\t\t\t\t\t\tstateRef.current.imageEditor.image\n\t\t\t\t\t)\n\t\t\t\t);\n\t\t\t} );\n\t\t},\n\t\t[ asOneUndoEntry, dispatchWithHistory, imageEditorController ]\n\t);\n\n\tconst reset = useCallback( () => {\n\t\tdispatchWithHistory( { type: 'RESET' } );\n\t}, [ dispatchWithHistory ] );\n\n\tconst setEdit = useCallback(\n\t\t( name: string, value: EditValue | undefined ) => {\n\t\t\tdispatchWithHistory( {\n\t\t\t\ttype: 'SET_EDIT',\n\t\t\t\tpayload: { name, value },\n\t\t\t} );\n\t\t},\n\t\t[ dispatchWithHistory ]\n\t);\n\n\tconst undo = useCallback( () => {\n\t\tendGesture();\n\t\tconst prev = historyRef.current[ historyRef.current.length - 1 ];\n\t\tif ( ! prev ) {\n\t\t\treturn;\n\t\t}\n\t\tredoStackRef.current = [ stateRef.current, ...redoStackRef.current ];\n\t\thistoryRef.current = historyRef.current.slice( 0, -1 );\n\t\tsetHasUndo( historyRef.current.length > 0 );\n\t\tsetHasRedo( true );\n\t\tstateRef.current = prev;\n\t\tdispatch( { type: 'RESTORE_SNAPSHOT', payload: prev } );\n\t}, [ endGesture ] );\n\n\tconst redo = useCallback( () => {\n\t\tendGesture();\n\t\tconst next = redoStackRef.current[ 0 ];\n\t\tif ( ! next ) {\n\t\t\treturn;\n\t\t}\n\t\thistoryRef.current = [ ...historyRef.current, stateRef.current ];\n\t\tredoStackRef.current = redoStackRef.current.slice( 1 );\n\t\tsetHasUndo( true );\n\t\tsetHasRedo( redoStackRef.current.length > 0 );\n\t\tstateRef.current = next;\n\t\tdispatch( { type: 'RESTORE_SNAPSHOT', payload: next } );\n\t}, [ endGesture ] );\n\n\tconst imageEditor = useMemo< ImageEditorController >(\n\t\t() => ( { ...imageEditorController, setImage: setSourceImage } ),\n\t\t[ imageEditorController, setSourceImage ]\n\t);\n\n\tconst isDirty = ! areMediaEditorStatesEqual( state, initialBaseline );\n\n\treturn useMemo(\n\t\t() => ( {\n\t\t\timageEditor,\n\t\t\tcropOptions: state.cropOptions,\n\t\t\tsetAspectRatioValue,\n\t\t\tedits: state.edits,\n\t\t\tsetEdit,\n\t\t\tsetSourceImage,\n\t\t\treset,\n\t\t\tisDirty,\n\t\t\t// Only the crop changes the saved image so far.\n\t\t\t//\n\t\t\t// This reads the image editor's own clean state, not the\n\t\t\t// session's. The two agree because the session only ever\n\t\t\t// replaces the editor's slice with a state the editor handed\n\t\t\t// out, and because a fresh state for an image matches the\n\t\t\t// default one on every field `isDirty` compares. Change\n\t\t\t// either and this has to compare against the session's\n\t\t\t// baseline instead.\n\t\t\thasOutputEdits: imageEditor.isDirty,\n\t\t\thasUndo,\n\t\t\thasRedo,\n\t\t\tundo,\n\t\t\tredo,\n\t\t\tbeginGesture,\n\t\t\tendGesture,\n\t\t} ),\n\t\t[\n\t\t\timageEditor,\n\t\t\tstate.cropOptions,\n\t\t\tsetAspectRatioValue,\n\t\t\tstate.edits,\n\t\t\tsetEdit,\n\t\t\tsetSourceImage,\n\t\t\treset,\n\t\t\tisDirty,\n\t\t\thasUndo,\n\t\t\thasRedo,\n\t\t\tundo,\n\t\t\tredo,\n\t\t\tbeginGesture,\n\t\t\tendGesture,\n\t\t]\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAwCA,SAAS,mBACR,WACA,OACqB;CACrB,MAAM,MAAM,WAAY,SAAU;CAClC,IAAK,QAAQ,KAAK,OAAO,MAAO,GAAI,GACnC;CAED,IAAK,QAAQA,0BAAAA,uBAAwB;EACpC,IAAK,SAAS,MAAM,eAAe,KAAK,MAAM,gBAAgB,GAC7D,OAAO,MAAM,eAAe,MAAM;EAEnC;CACD;CACA,I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E;EAC1C,WAAY,IAAK;EACjB,SAAS,UAAU;EACnB,SAAU;GAAE,MAAM;GAAoB,SAAS;EAAK,CAAE;CACvD,GAAG,CAAE,UAAW,CAAE;CAElB,MAAM,QAAA,GAAOA,mBAAAA,YAAAA,OAAmB;EAC/B,WAAW;EACX,MAAM,OAAO,aAAa,QAAS;EACnC,IAAK,CAAE,MACN;EAED,WAAW,UAAU,CAAE,GAAG,WAAW,SAAS,SAAS,OAAQ;EAC/D,aAAa,UAAU,aAAa,QAAQ,MAAO,CAAE;EACrD,WAAY,IAAK;EACjB,WAAY,aAAa,QAAQ,SAAS,CAAE;EAC5C,SAAS,UAAU;EACnB,SAAU;GAAE,MAAM;GAAoB,SAAS;EAAK,CAAE;CACvD,GAAG,CAAE,UAAW,CAAE;CAElB,MAAM,eAAA,GAAcG,mBAAAA,QAAAA,QACX;EAAE,GAAG;EAAuB,UAAU;CAAe,IAC7D,CAAE,uBAAuB,cAAe,CACzC;CAEA,MAAM,UAAU,EAAA,GAAEF,uBAAAA,0BAAAA,CAA2B,OAAO,eAAgB;CAEpE,QAAA,GAAOE,mBAAAA,QAAAA,QACE;EACP;EACA,aAAa,MAAM;EACnB;EACA,OAAO,MAAM;EACb;EACA;EACA;EACA;EAUA,gBAAgB,YAAY;EAC5B;EACA;EACA;EACA;EACA;EACA;CACD,IACA;EACC;EACA,MAAM;EACN;EACA,MAAM;EACN;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACD,CACD;AACD"}
@@ -11,12 +11,14 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
11
11
  var __getProtoOf = Object.getPrototypeOf;
12
12
  var __hasOwnProp = Object.prototype.hasOwnProperty;
13
13
  var __copyProps = (to, from, except, desc) => {
14
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
15
- key = keys[i];
16
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
17
- get: ((k) => from[k]).bind(null, key),
18
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
- });
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) {
16
+ desc = __getOwnPropDesc(from, key);
17
+ __defProp(to, key, {
18
+ get: desc && desc.get && !desc.configurable ? desc.get : () => from[key],
19
+ enumerable: !desc || desc.enumerable
20
+ });
21
+ }
20
22
  }
21
23
  return to;
22
24
  };
@@ -26,12 +28,12 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
28
  }) : target, mod));
27
29
  let _wordpress_data = require("@wordpress/data");
28
30
  let __reducer_ts = require("./reducer.cjs");
29
- __reducer_ts = __toESM(__reducer_ts);
30
31
  let __actions_ts = require("./actions.cjs");
31
- __actions_ts = __toESM(__actions_ts);
32
32
  let __selectors_ts = require("./selectors.cjs");
33
- __selectors_ts = __toESM(__selectors_ts);
34
33
  let __constants_ts = require("./constants.cjs");
34
+ __reducer_ts = __toESM(__reducer_ts);
35
+ __actions_ts = __toESM(__actions_ts);
36
+ __selectors_ts = __toESM(__selectors_ts);
35
37
  // packages/media-editor/src/store/index.ts
36
38
  const store = (0, _wordpress_data.createReduxStore)(__constants_ts.STORE_NAME, {
37
39
  reducer: __reducer_ts.default,
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["createReduxStore","STORE_NAME","register"],"sources":["../../src/store/index.ts"],"sourcesContent":["import { createReduxStore, register } from '@wordpress/data';\nimport reducer from './reducer';\nimport * as actions from './actions';\nimport * as selectors from './selectors';\nimport { STORE_NAME } from './constants';\n\nexport const store = createReduxStore( STORE_NAME, {\n\treducer,\n\tactions,\n\tselectors,\n} );\n\nregister( store );\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,MAAa,SAAA,GAAQA,gBAAAA,iBAAAA,CAAkBC,eAAAA,YAAY;CAClD,SAAA,aAAA;CACA,SAAA;CACA,WAAA;AACD,CAAE;CAEFC,GAAAA,gBAAAA,SAAAA,CAAU,KAAM"}
1
+ {"version":3,"file":"index.cjs","names":["createReduxStore","STORE_NAME","reducer","actions","selectors","register"],"sources":["../../src/store/index.ts"],"sourcesContent":["import { createReduxStore, register } from '@wordpress/data';\nimport reducer from './reducer';\nimport * as actions from './actions';\nimport * as selectors from './selectors';\nimport { STORE_NAME } from './constants';\n\nexport const store = createReduxStore( STORE_NAME, {\n\treducer,\n\tactions,\n\tselectors,\n} );\n\nregister( store );\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,MAAa,SAAA,GAAQA,gBAAAA,iBAAAA,CAAkBC,eAAAA,YAAY;CAClDC,SAAAA,aAAAA;CACAC,SAAAA;CACAC,WAAAA;AACD,CAAE;CAEFC,GAAAA,gBAAAA,SAAAA,CAAU,KAAM"}
@@ -4,6 +4,13 @@ Object.defineProperties(exports, {
4
4
  [Symbol.toStringTag]: { value: "Module" }
5
5
  });
6
6
  // packages/media-editor/src/utils/get-media-type.ts
7
+ /** The media types the editor tells apart, by MIME type prefix. */
8
+ const MEDIA_TYPES = [
9
+ "image",
10
+ "video",
11
+ "audio",
12
+ "application"
13
+ ];
7
14
  /**
8
15
  * Determines the media type from a MIME type string.
9
16
  *
@@ -17,6 +24,7 @@ function getMediaTypeFromMimeType(mimeType) {
17
24
  if (mimeType.startsWith("audio/")) return { type: "audio" };
18
25
  return { type: "application" };
19
26
  }
27
+ exports.MEDIA_TYPES = MEDIA_TYPES;
20
28
  exports.getMediaTypeFromMimeType = getMediaTypeFromMimeType;
21
29
 
22
30
  //# sourceMappingURL=get-media-type.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"get-media-type.cjs","names":[],"sources":["../../src/utils/get-media-type.ts"],"sourcesContent":["/**\n * Media type result.\n */\nexport interface MediaType {\n\ttype: 'image' | 'video' | 'audio' | 'application';\n}\n\n/**\n * Determines the media type from a MIME type string.\n *\n * @param mimeType - The MIME type to check.\n * @return Object with type property ('image', 'video', 'audio', or 'application').\n */\nexport function getMediaTypeFromMimeType( mimeType?: string ): MediaType {\n\tif ( ! mimeType ) {\n\t\treturn { type: 'application' };\n\t}\n\n\tif ( mimeType.startsWith( 'image/' ) ) {\n\t\treturn { type: 'image' };\n\t}\n\tif ( mimeType.startsWith( 'video/' ) ) {\n\t\treturn { type: 'video' };\n\t}\n\tif ( mimeType.startsWith( 'audio/' ) ) {\n\t\treturn { type: 'audio' };\n\t}\n\treturn { type: 'application' };\n}\n"],"mappings":";;;;;;;;;;;;AAaA,SAAgB,yBAA0B,UAA+B;CACxE,IAAK,CAAE,UACN,OAAO,EAAE,MAAM,cAAc;CAG9B,IAAK,SAAS,WAAY,QAAS,GAClC,OAAO,EAAE,MAAM,QAAQ;CAExB,IAAK,SAAS,WAAY,QAAS,GAClC,OAAO,EAAE,MAAM,QAAQ;CAExB,IAAK,SAAS,WAAY,QAAS,GAClC,OAAO,EAAE,MAAM,QAAQ;CAExB,OAAO,EAAE,MAAM,cAAc;AAC9B"}
1
+ {"version":3,"file":"get-media-type.cjs","names":[],"sources":["../../src/utils/get-media-type.ts"],"sourcesContent":["/** The media types the editor tells apart, by MIME type prefix. */\nexport const MEDIA_TYPES = [\n\t'image',\n\t'video',\n\t'audio',\n\t'application',\n] as const;\n\n/**\n * Media type result.\n */\nexport interface MediaType {\n\ttype: ( typeof MEDIA_TYPES )[ number ];\n}\n\n/**\n * Determines the media type from a MIME type string.\n *\n * @param mimeType - The MIME type to check.\n * @return Object with type property ('image', 'video', 'audio', or 'application').\n */\nexport function getMediaTypeFromMimeType( mimeType?: string ): MediaType {\n\tif ( ! mimeType ) {\n\t\treturn { type: 'application' };\n\t}\n\n\tif ( mimeType.startsWith( 'image/' ) ) {\n\t\treturn { type: 'image' };\n\t}\n\tif ( mimeType.startsWith( 'video/' ) ) {\n\t\treturn { type: 'video' };\n\t}\n\tif ( mimeType.startsWith( 'audio/' ) ) {\n\t\treturn { type: 'audio' };\n\t}\n\treturn { type: 'application' };\n}\n"],"mappings":";;;;;;;AACA,MAAa,cAAc;CAC1B;CACA;CACA;CACA;AACD;;;;;;;AAeA,SAAgB,yBAA0B,UAA+B;CACxE,IAAK,CAAE,UACN,OAAO,EAAE,MAAM,cAAc;CAG9B,IAAK,SAAS,WAAY,QAAS,GAClC,OAAO,EAAE,MAAM,QAAQ;CAExB,IAAK,SAAS,WAAY,QAAS,GAClC,OAAO,EAAE,MAAM,QAAQ;CAExB,IAAK,SAAS,WAAY,QAAS,GAClC,OAAO,EAAE,MAAM,QAAQ;CAExB,OAAO,EAAE,MAAM,cAAc;AAC9B"}
@@ -4,6 +4,12 @@ Object.defineProperties(exports, {
4
4
  [Symbol.toStringTag]: { value: "Module" }
5
5
  });
6
6
  let __get_media_type_ts = require("./get-media-type.cjs");
7
+ Object.defineProperty(exports, "MEDIA_TYPES", {
8
+ enumerable: true,
9
+ get: function() {
10
+ return __get_media_type_ts.MEDIA_TYPES;
11
+ }
12
+ });
7
13
  Object.defineProperty(exports, "getMediaTypeFromMimeType", {
8
14
  enumerable: true,
9
15
  get: function() {
@@ -1,6 +1,7 @@
1
1
  import clsx from "clsx";
2
- import { Button, DropdownMenu, MenuGroup, MenuItem, Modal, Spinner, __experimentalConfirmDialog, __unstableAnimatePresence, __unstableMotion } from "@wordpress/components";
2
+ import { Button, DropdownMenu, MenuGroup, MenuItem, Modal, Slot, SlotFillProvider, Spinner, __experimentalConfirmDialog, __unstableAnimatePresence, __unstableMotion } from "@wordpress/components";
3
3
  import { Stack, Tabs, VisuallyHidden } from "@wordpress/ui";
4
+ import { PluginArea } from "@wordpress/plugins";
4
5
  import { useReducedMotion, useViewportMatch } from "@wordpress/compose";
5
6
  import { useDispatch, useRegistry, useSelect } from "@wordpress/data";
6
7
  import { store } from "@wordpress/core-data";
@@ -8,8 +9,9 @@ import { NavigableRegion } from "@wordpress/admin-ui";
8
9
  import { createContext, createPortal, useCallback, useContext, useEffect, useMemo, useRef, useState } from "@wordpress/element";
9
10
  import { __, _x, sprintf } from "@wordpress/i18n";
10
11
  import { backup, close, drawerRight, keyboard, moreVertical, redo, undo } from "@wordpress/icons";
11
- import { displayShortcut, isAppleOS, isKeyboardEvent } from "@wordpress/keycodes";
12
+ import { displayShortcut, isAppleOS, isKeyboardEvent, withIgnoreIMEEvents } from "@wordpress/keycodes";
12
13
  import { SnackbarNotices, store as store$1 } from "@wordpress/notices";
14
+ import { inertValue } from "@wordpress/react-inert-value";
13
15
  import { MediaEditorProvider } from "../media-editor-provider/index.mjs";
14
16
  import MediaPreview from "../media-preview/index.mjs";
15
17
  import MediaEditorCanvas from "../media-editor-canvas/index.mjs";
@@ -17,6 +19,7 @@ import MediaEditorFineRotation from "../media-editor-fine-rotation/index.mjs";
17
19
  import MediaEditorCropPanel from "../media-editor-crop-panel/index.mjs";
18
20
  import MediaEditorImageControls from "../media-editor-image-controls/index.mjs";
19
21
  import MediaForm from "../media-form/index.mjs";
22
+ import MediaEditorPanel, { MEDIA_EDITOR_PLUGIN_SCOPE, MediaEditorPanelsProvider, getPanelSlotName, useMediaEditorPanels } from "../media-editor-panel/index.mjs";
20
23
  import { getMediaTypeFromMimeType } from "../../utils/index.mjs";
21
24
  import { MediaEditorStateProvider, useMediaEditor } from "../../state/index.mjs";
22
25
  import { CROP_CONTROL_ATTR } from "../../hooks/use-crop-gesture-handlers.mjs";
@@ -28,14 +31,16 @@ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
28
31
  const ATTACHMENT_EMBED_QUERY = { _embed: "author,wp:attached-to" };
29
32
  const PLACEMENT_CONTROL_IDLE_MS = 300;
30
33
  /**
31
- * Identifier for the details panel, and the panel the sidebar opens on before
32
- * the user picks another. The sidebar tracks which panel is open rather than
33
- * whether one is, so a further panel is a new id here and a new entry in the
34
- * tab list — not a change to the state's shape.
34
+ * The Details panel's id. Every media type has this panel, so the sidebar
35
+ * shows it when the requested panel doesn't exist.
36
+ *
37
+ * The sidebar stores the open panel's id, so adding a panel only needs a new
38
+ * id and a new tab.
35
39
  */
36
- const DETAILS_PANEL = "details";
40
+ const DETAILS_PANEL = "core/details";
37
41
  /** Identifier for the crop panel. Images only; other media has nothing to crop. */
38
- const CROP_PANEL = "crop";
42
+ const CROP_PANEL = "core/crop";
43
+ const CROP_MEDIA_TYPES = ["image"];
39
44
  const SIDEBAR_ANIMATION_DURATION = .3;
40
45
  const sidebarVariants = {
41
46
  open: { width: "auto" },
@@ -69,6 +74,7 @@ function MediaEditorSidebar({ tabs, activeTab, onSelectTab, disabled = false })
69
74
  value: tab.id,
70
75
  tabIndex: -1,
71
76
  className: "media-editor__panel",
77
+ inert: inertValue(disabled),
72
78
  children: /* @__PURE__ */ jsx(Stack, {
73
79
  direction: "column",
74
80
  gap: "lg",
@@ -91,6 +97,16 @@ function useMediaEditorFrameContext() {
91
97
  if (!context) throw new Error("useMediaEditorFrameContext must be used within MediaEditor");
92
98
  return context;
93
99
  }
100
+ function CropPanelContent({ disabled }) {
101
+ const { aspectRatioPresets } = useMediaEditorFrameContext();
102
+ const { aspectRatioValue, setAspectRatioValue, aspectRatioOptions } = useCropOptions({ aspectRatioPresets });
103
+ return /* @__PURE__ */ jsx(MediaEditorCropPanel, {
104
+ aspectRatioValue,
105
+ onAspectRatioChange: setAspectRatioValue,
106
+ aspectRatioOptions,
107
+ disabled
108
+ });
109
+ }
94
110
  function PanelToggle() {
95
111
  const { isWide, activePanel, onTogglePanel } = useMediaEditorFrameContext();
96
112
  const isPanelOpen = !!activePanel;
@@ -153,7 +169,11 @@ function MoreMenu({ actions = [], item }) {
153
169
  }
154
170
  action.callback(items, { registry });
155
171
  },
156
- children: "RenderModal" in action ? sprintf(_x("%s…", "menu item requiring further input"), label) : label
172
+ children: "RenderModal" in action ? sprintf(
173
+ /** translators: %s: Action label. Use the ellipsis and spacing appropriate for your language. */
174
+ _x("%s…", "menu item requiring further input"),
175
+ label
176
+ ) : label
157
177
  }, action.id);
158
178
  }) })] })
159
179
  }),
@@ -284,7 +304,7 @@ function SaveActions({ size = "default" }) {
284
304
  })]
285
305
  });
286
306
  }
287
- function MediaEditorContent({ fields = [], form, id, aspectRatioPresets, onClose, onSaved, onEditOriginal, renderFrame, noticesClassName = "media-editor__snackbar", noticesPortalElement, shouldCloseOnEsc = false }) {
307
+ function MediaEditorContent({ fields = [], form, id, aspectRatioPresets, onClose, onSaved, onEditOriginal, renderFrame, noticesClassName = "media-editor__snackbar", noticesPortalElement, shouldCloseOnEsc = false, defaultPanel = DETAILS_PANEL }) {
288
308
  const session = useMediaEditor();
289
309
  const isWide = useViewportMatch("small");
290
310
  const sidebarTransition = {
@@ -302,7 +322,17 @@ function MediaEditorContent({ fields = [], form, id, aspectRatioPresets, onClose
302
322
  const layout = isWide ? "wide" : "narrow";
303
323
  const sourceMedia = useSelect((select) => select(store).getEntityRecord("postType", "attachment", id), [id]);
304
324
  const { clearEntityRecordEdits, editEntityRecord, invalidateResolution } = useDispatch(store);
305
- const { removeAllNotices } = useDispatch(store$1);
325
+ const { createErrorNotice, removeAllNotices } = useDispatch(store$1);
326
+ const handlePluginError = useCallback((name) => {
327
+ createErrorNotice(sprintf(
328
+ /** translators: %s: plugin name */
329
+ __("The \"%s\" plugin has encountered an error and cannot be rendered."),
330
+ name
331
+ ), {
332
+ type: "snackbar",
333
+ context: MEDIA_EDITOR_NOTICES_CONTEXT
334
+ });
335
+ }, [createErrorNotice]);
306
336
  const [isDiscardDialogOpen, setIsDiscardDialogOpen] = useState(false);
307
337
  const [isPlacementActive, setIsPlacementActive] = useState(false);
308
338
  const [isCanvasGestureActive, setIsCanvasGestureActive] = useState(false);
@@ -376,7 +406,7 @@ function MediaEditorContent({ fields = [], form, id, aspectRatioPresets, onClose
376
406
  const hasChanges = session.hasOutputEdits || hasEdits || isSourceReplaced;
377
407
  const mediaType = getMediaTypeFromMimeType(media?.mime_type).type;
378
408
  const isImage = !!media && mediaType === "image";
379
- const currentTab = chosenTab ?? (isWide && isImage ? CROP_PANEL : DETAILS_PANEL);
409
+ const currentTab = chosenTab ?? (isWide ? defaultPanel : DETAILS_PANEL);
380
410
  let activePanel = chosenPanel;
381
411
  if (activePanel === void 0) activePanel = isWide ? currentTab : null;
382
412
  const selectPanel = useCallback((panel) => {
@@ -386,7 +416,6 @@ function MediaEditorContent({ fields = [], form, id, aspectRatioPresets, onClose
386
416
  const togglePanel = useCallback(() => {
387
417
  setChosenPanel(activePanel ? null : currentTab);
388
418
  }, [activePanel, currentTab]);
389
- const { aspectRatioValue, setAspectRatioValue, aspectRatioOptions } = useCropOptions({ aspectRatioPresets });
390
419
  const { isSaving, save: saveMediaEditor } = useSaveMediaEditor({
391
420
  session,
392
421
  id,
@@ -413,7 +442,7 @@ function MediaEditorContent({ fields = [], form, id, aspectRatioPresets, onClose
413
442
  }
414
443
  discardAndClose();
415
444
  };
416
- const handleKeyDown = (event) => {
445
+ const handleKeyDown = withIgnoreIMEEvents((event) => {
417
446
  const isUndoShortcut = isKeyboardEvent.primary(event, "z");
418
447
  const isRedoShortcut = isKeyboardEvent.primaryShift(event, "z") || !isAppleOS() && isKeyboardEvent.primary(event, "y");
419
448
  if ((isUndoShortcut || isRedoShortcut) && isImage) {
@@ -436,7 +465,7 @@ function MediaEditorContent({ fields = [], form, id, aspectRatioPresets, onClose
436
465
  setIsDiscardDialogOpen(true);
437
466
  }
438
467
  }
439
- };
468
+ });
440
469
  const snackbar = /* @__PURE__ */ jsx(SnackbarNotices, {
441
470
  className: noticesClassName,
442
471
  context: MEDIA_EDITOR_NOTICES_CONTEXT
@@ -446,91 +475,78 @@ function MediaEditorContent({ fields = [], form, id, aspectRatioPresets, onClose
446
475
  aspectRatioPresets,
447
476
  disabled: isSaving
448
477
  }) : null;
449
- const tabs = [...isImage ? [{
450
- id: CROP_PANEL,
451
- title: __("Crop"),
452
- render: () => /* @__PURE__ */ jsx(MediaEditorCropPanel, {
453
- aspectRatioValue,
454
- onAspectRatioChange: setAspectRatioValue,
455
- aspectRatioOptions,
456
- disabled: isSaving
478
+ const { panels, contextValue: panelsContextValue } = useMediaEditorPanels();
479
+ const availableTabs = panels.filter((panel) => !panel.mediaTypes || panel.mediaTypes.includes(mediaType)).map((panel) => ({
480
+ id: panel.name,
481
+ title: panel.title,
482
+ render: () => /* @__PURE__ */ jsx(Slot, {
483
+ bubblesVirtually: true,
484
+ name: getPanelSlotName(panel.name),
485
+ fillProps: { disabled: isSaving }
457
486
  })
458
- }] : [], {
459
- id: DETAILS_PANEL,
460
- title: __("Details"),
461
- render: () => /* @__PURE__ */ jsx(MediaForm, {})
462
- }];
487
+ }));
488
+ const defaultTab = availableTabs.find((tab) => tab.id === defaultPanel);
489
+ const tabs = defaultTab ? [defaultTab, ...availableTabs.filter((tab) => tab !== defaultTab)] : availableTabs;
463
490
  const ruler = isImage ? /* @__PURE__ */ jsx(MediaEditorFineRotation, {
464
491
  onPlacementControlInteraction: signalPlacementControlInteraction,
465
492
  disabled: isSaving
466
493
  }) : null;
467
- const children = /* @__PURE__ */ jsxs(MediaEditorProvider, {
468
- value: media ?? void 0,
469
- onChange: handleChange,
470
- settings: {
471
- fields: isSaving ? fields.map((field) => ({
472
- ...field,
473
- isDisabled: true
474
- })) : fields,
475
- form
476
- },
477
- children: [
478
- /* @__PURE__ */ jsx("div", {
479
- className: "media-editor",
480
- children: !media ? /* @__PURE__ */ jsx("div", {
481
- className: "media-editor__loading",
482
- children: /* @__PURE__ */ jsx(Spinner, {})
483
- }) : /* @__PURE__ */ jsxs("div", {
484
- className: clsx("media-editor__body", { "has-panel-open": !!activePanel }),
485
- children: [/* @__PURE__ */ jsxs(NavigableRegion, {
486
- className: "media-editor__content",
487
- ariaLabel: isImage ? __("Image editor") : __("Media preview"),
488
- children: [/* @__PURE__ */ jsx("div", {
489
- className: "media-editor__canvas-area",
490
- children: isImage ? /* @__PURE__ */ jsx(MediaEditorCanvas, {
491
- isPlacementActive,
492
- onGestureStart: handleCanvasGestureStart,
493
- onGestureEnd: handleCanvasGestureEnd,
494
- disabled: isSaving
495
- }) : /* @__PURE__ */ jsx(MediaPreview, {})
496
- }), isImage && /* @__PURE__ */ jsxs("div", {
497
- className: "media-editor__canvas-toolbar",
498
- children: [ruler, imageControls]
499
- })]
500
- }), /* @__PURE__ */ jsx(__unstableAnimatePresence, {
501
- initial: false,
502
- custom: sidebarTransition,
503
- children: !!activePanel && /* @__PURE__ */ jsx(__unstableMotion.div, {
504
- className: "media-editor__sidebar-container",
505
- variants: sidebarVariants,
506
- initial: "closed",
507
- animate: "open",
508
- exit: "closed",
509
- transition: sidebarTransition,
510
- children: /* @__PURE__ */ jsx(MediaEditorSidebar, {
511
- tabs,
512
- activeTab: tabs.some((tab) => tab.id === activePanel) ? activePanel : DETAILS_PANEL,
513
- onSelectTab: selectPanel,
514
- disabled: isSaving
515
- })
516
- })
494
+ const children = /* @__PURE__ */ jsxs(Fragment, { children: [
495
+ /* @__PURE__ */ jsx("div", {
496
+ className: "media-editor",
497
+ children: !media ? /* @__PURE__ */ jsx("div", {
498
+ className: "media-editor__loading",
499
+ children: /* @__PURE__ */ jsx(Spinner, {})
500
+ }) : /* @__PURE__ */ jsxs("div", {
501
+ className: clsx("media-editor__body", { "has-panel-open": !!activePanel }),
502
+ children: [/* @__PURE__ */ jsxs(NavigableRegion, {
503
+ className: "media-editor__content",
504
+ ariaLabel: isImage ? __("Image editor") : __("Media preview"),
505
+ children: [/* @__PURE__ */ jsx("div", {
506
+ className: "media-editor__canvas-area",
507
+ children: isImage ? /* @__PURE__ */ jsx(MediaEditorCanvas, {
508
+ isPlacementActive,
509
+ onGestureStart: handleCanvasGestureStart,
510
+ onGestureEnd: handleCanvasGestureEnd,
511
+ disabled: isSaving
512
+ }) : /* @__PURE__ */ jsx(MediaPreview, {})
513
+ }), isImage && /* @__PURE__ */ jsxs("div", {
514
+ className: "media-editor__canvas-toolbar",
515
+ children: [ruler, imageControls]
517
516
  })]
518
- })
519
- }),
520
- /* @__PURE__ */ jsx(__experimentalConfirmDialog, {
521
- isOpen: isDiscardDialogOpen,
522
- confirmButtonText: __("Discard"),
523
- cancelButtonText: __("Keep editing"),
524
- onCancel: () => setIsDiscardDialogOpen(false),
525
- onConfirm: () => {
526
- setIsDiscardDialogOpen(false);
527
- discardAndClose();
528
- },
529
- children: __("Are you sure you want to discard your unsaved changes?")
530
- }),
531
- noticesPortalElement ? createPortal(snackbar, noticesPortalElement) : snackbar
532
- ]
533
- });
517
+ }), /* @__PURE__ */ jsx(__unstableAnimatePresence, {
518
+ initial: false,
519
+ custom: sidebarTransition,
520
+ children: !!activePanel && /* @__PURE__ */ jsx(__unstableMotion.div, {
521
+ className: "media-editor__sidebar-container",
522
+ variants: sidebarVariants,
523
+ initial: "closed",
524
+ animate: "open",
525
+ exit: "closed",
526
+ transition: sidebarTransition,
527
+ children: tabs.length > 0 && /* @__PURE__ */ jsx(MediaEditorSidebar, {
528
+ tabs,
529
+ activeTab: tabs.some((tab) => tab.id === activePanel) ? activePanel : DETAILS_PANEL,
530
+ onSelectTab: selectPanel,
531
+ disabled: isSaving
532
+ })
533
+ })
534
+ })]
535
+ })
536
+ }),
537
+ /* @__PURE__ */ jsx(__experimentalConfirmDialog, {
538
+ isOpen: isDiscardDialogOpen,
539
+ confirmButtonText: __("Discard"),
540
+ cancelButtonText: __("Keep editing"),
541
+ onCancel: () => setIsDiscardDialogOpen(false),
542
+ onConfirm: () => {
543
+ setIsDiscardDialogOpen(false);
544
+ discardAndClose();
545
+ },
546
+ children: __("Are you sure you want to discard your unsaved changes?")
547
+ }),
548
+ noticesPortalElement ? createPortal(snackbar, noticesPortalElement) : snackbar
549
+ ] });
534
550
  const contextValue = {
535
551
  isImage,
536
552
  isSaving,
@@ -551,7 +567,38 @@ function MediaEditorContent({ fields = [], form, id, aspectRatioPresets, onClose
551
567
  return /* @__PURE__ */ jsx(MediaEditorFrameContext.Provider, {
552
568
  value: contextValue,
553
569
  children: renderFrame({
554
- children,
570
+ children: /* @__PURE__ */ jsx(MediaEditorProvider, {
571
+ value: media ?? void 0,
572
+ onChange: handleChange,
573
+ settings: {
574
+ fields: isSaving ? fields.map((field) => ({
575
+ ...field,
576
+ isDisabled: true
577
+ })) : fields,
578
+ form
579
+ },
580
+ children: /* @__PURE__ */ jsx(SlotFillProvider, { children: /* @__PURE__ */ jsxs(MediaEditorPanelsProvider, {
581
+ value: panelsContextValue,
582
+ children: [
583
+ /* @__PURE__ */ jsx(MediaEditorPanel, {
584
+ name: CROP_PANEL,
585
+ title: __("Crop"),
586
+ mediaTypes: CROP_MEDIA_TYPES,
587
+ children: ({ disabled }) => /* @__PURE__ */ jsx(CropPanelContent, { disabled })
588
+ }),
589
+ /* @__PURE__ */ jsx(MediaEditorPanel, {
590
+ name: DETAILS_PANEL,
591
+ title: __("Details"),
592
+ children: /* @__PURE__ */ jsx(MediaForm, {})
593
+ }),
594
+ /* @__PURE__ */ jsx(PluginArea, {
595
+ scope: MEDIA_EDITOR_PLUGIN_SCOPE,
596
+ onError: handlePluginError
597
+ }),
598
+ children
599
+ ]
600
+ }) })
601
+ }),
555
602
  isImage,
556
603
  layout,
557
604
  onRequestClose: handleRequestClose,