Repository navigation
Media Editor Route: Add actions so that a media item can be deleted #86349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
beb9260
b6e2614
96cf455
51f7cbc
eacec33
4b643e7
50450d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,14 +4,15 @@ import { | |
| DropdownMenu, | ||
| MenuGroup, | ||
| MenuItem, | ||
| Modal, | ||
| Spinner as WCSpinner, | ||
| __experimentalConfirmDialog as ConfirmDialog, | ||
| __unstableAnimatePresence as AnimatePresence, | ||
| __unstableMotion as motion, | ||
| } from '@wordpress/components'; | ||
| import { Stack, Tabs, VisuallyHidden } from '@wordpress/ui'; | ||
| import { useReducedMotion, useViewportMatch } from '@wordpress/compose'; | ||
| import { useDispatch, useSelect } from '@wordpress/data'; | ||
| import { useDispatch, useRegistry, useSelect } from '@wordpress/data'; | ||
| import { store as coreStore } from '@wordpress/core-data'; | ||
| import { NavigableRegion } from '@wordpress/admin-ui'; | ||
| import { | ||
|
|
@@ -20,10 +21,11 @@ import { | |
| useCallback, | ||
| useContext, | ||
| useEffect, | ||
| useMemo, | ||
| useRef, | ||
| useState, | ||
| } from '@wordpress/element'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { __, _x, sprintf } from '@wordpress/i18n'; | ||
| import { | ||
| backup, | ||
| close, | ||
|
|
@@ -39,7 +41,7 @@ import { | |
| isKeyboardEvent, | ||
| } from '@wordpress/keycodes'; | ||
| import { SnackbarNotices, store as noticesStore } from '@wordpress/notices'; | ||
| import type { Field, Form } from '@wordpress/dataviews'; | ||
| import type { Action, ActionModal, Field, Form } from '@wordpress/dataviews'; | ||
| import type { | ||
| KeyboardEvent as ReactKeyboardEvent, | ||
| ReactElement, | ||
|
|
@@ -314,10 +316,75 @@ function PanelToggle() { | |
| ); | ||
| } | ||
|
|
||
| function MoreMenu() { | ||
| const { isImage } = useMediaEditorFrameContext(); | ||
| function getActionLabel( action: Action< Media >, items: Media[] ) { | ||
| return typeof action.label === 'string' | ||
| ? action.label | ||
| : action.label( items ); | ||
| } | ||
|
|
||
| interface MediaActionModalProps { | ||
| action: ActionModal< Media >; | ||
| items: Media[]; | ||
| closeModal: () => void; | ||
| } | ||
|
|
||
| // A copy of the `ActionModal` in `@wordpress/dataviews`, which isn't | ||
| // exported. It could be replaced if dataviews exports it. | ||
| function MediaActionModal( { | ||
| action, | ||
| items, | ||
| closeModal, | ||
| }: MediaActionModalProps ) { | ||
| const header = | ||
| typeof action.modalHeader === 'function' | ||
| ? action.modalHeader( items ) | ||
| : action.modalHeader; | ||
| return ( | ||
| <Modal | ||
| title={ header || getActionLabel( action, items ) } | ||
| __experimentalHideHeader={ !! action.hideModalHeader } | ||
| onRequestClose={ closeModal } | ||
| focusOnMount={ action.modalFocusOnMount ?? true } | ||
| size={ action.modalSize || 'medium' } | ||
| > | ||
| <action.RenderModal items={ items } closeModal={ closeModal } /> | ||
| </Modal> | ||
| ); | ||
| } | ||
|
|
||
| export interface MoreMenuProps { | ||
| /** | ||
| * Actions for the attachment, such as those from the entity actions API. | ||
| * Only the ones whose `isEligible` passes for `item` are shown. | ||
| */ | ||
| actions?: Action< Media >[]; | ||
| /** | ||
| * The attachment the actions apply to, with anything their `isEligible` | ||
| * checks need, such as `permissions`. Without it no actions are shown. | ||
| */ | ||
| item?: Media; | ||
| } | ||
|
|
||
| function MoreMenu( { actions = [], item }: MoreMenuProps ) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Happy for feedback on this: the idea of the API change here is that the MoreMenu should optionally accept actions (and the media item). It could be that the media editor might wind up placing these elsewhere at some point. Does this seem a reasonable home for now? |
||
| const { isImage, isSaving } = useMediaEditorFrameContext(); | ||
| const registry = useRegistry(); | ||
| const [ isShortcutsModalOpen, setIsShortcutsModalOpen ] = useState( false ); | ||
| if ( ! isImage ) { | ||
| const [ activeModalAction, setActiveModalAction ] = | ||
| useState< ActionModal< Media > | null >( null ); | ||
|
|
||
| const items = item ? [ item ] : []; | ||
| const eligibleActions = useMemo( | ||
| () => | ||
| item | ||
| ? actions.filter( | ||
| ( action ) => | ||
| ! action.isEligible || action.isEligible( item ) | ||
| ) | ||
| : [], | ||
| [ actions, item ] | ||
| ); | ||
|
|
||
| if ( ! isImage && ! eligibleActions.length ) { | ||
| return null; | ||
| } | ||
| return ( | ||
|
|
@@ -328,25 +395,77 @@ function MoreMenu() { | |
| toggleProps={ { size: 'compact' } } | ||
| > | ||
| { ( { onClose: closeMenu } ) => ( | ||
| <MenuGroup> | ||
| <MenuItem | ||
| icon={ keyboard } | ||
| iconPosition="left" | ||
| onClick={ () => { | ||
| setIsShortcutsModalOpen( true ); | ||
| closeMenu(); | ||
| } } | ||
| > | ||
| { __( 'Keyboard shortcuts' ) } | ||
| </MenuItem> | ||
| </MenuGroup> | ||
| <> | ||
| { isImage && ( | ||
| <MenuGroup> | ||
| <MenuItem | ||
| icon={ keyboard } | ||
| iconPosition="left" | ||
| onClick={ () => { | ||
| setIsShortcutsModalOpen( true ); | ||
| closeMenu(); | ||
| } } | ||
| > | ||
| { __( 'Keyboard shortcuts' ) } | ||
| </MenuItem> | ||
| </MenuGroup> | ||
| ) } | ||
| { !! eligibleActions.length && ( | ||
| <MenuGroup> | ||
| { eligibleActions.map( ( action ) => { | ||
| const label = getActionLabel( | ||
| action, | ||
| items | ||
| ); | ||
| return ( | ||
| <MenuItem | ||
| key={ action.id } | ||
| disabled={ | ||
| isSaving || action.disabled | ||
| } | ||
| onClick={ () => { | ||
| closeMenu(); | ||
| if ( 'RenderModal' in action ) { | ||
| setActiveModalAction( | ||
| action | ||
| ); | ||
| return; | ||
| } | ||
| action.callback( items, { | ||
| registry, | ||
| } ); | ||
| } } | ||
| > | ||
| { 'RenderModal' in action | ||
| ? sprintf( | ||
| /* translators: %s: Action label. Use the ellipsis and spacing appropriate for your language. */ | ||
| _x( | ||
| '%s…', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I see that #81994 added ellipses for actions that trigger a modal. There was probably a good reason or it's following loose reference to https://www.w3.org/WAI/ARIA/apg/patterns/menubar/, but I'll play keyboard hero right here and say that, on the surface, it looks weird and appears superfluous to requirements. Nothing to do here though. Bye!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I don't love it either. A bit further down the track (from the media editor perspective), I think we could do a pass on all these. I also don't like that "View" is just that single word. It makes me think "View what"? But yes, I won't let these questions hold up this PR, just stuff for us to think through when we review the overall editor, its actions and behaviours. |
||
| 'menu item requiring further input' | ||
| ), | ||
| label | ||
| ) | ||
| : label } | ||
| </MenuItem> | ||
| ); | ||
| } ) } | ||
| </MenuGroup> | ||
| ) } | ||
| </> | ||
| ) } | ||
| </DropdownMenu> | ||
| { isShortcutsModalOpen && ( | ||
| <MediaEditorKeyboardShortcutsModal | ||
| onClose={ () => setIsShortcutsModalOpen( false ) } | ||
| /> | ||
| ) } | ||
| { activeModalAction && ( | ||
| <MediaActionModal | ||
| action={ activeModalAction } | ||
| items={ items } | ||
| closeModal={ () => setActiveModalAction( null ) } | ||
| /> | ||
| ) } | ||
| </> | ||
| ); | ||
| } | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've basically copied how DataViews handles modal actions. Given we're behind an experiment I figured copy/pasting is probably a good way to go until we're settled on an overall design / UX for the dedicated media editor route