Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/media-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancements

- `MediaEditor.MoreMenu` takes `actions` and `item` props, and lists the actions eligible for the item, such as "Permanently delete". It now also shows for media other than images when it has actions ([#86349](https://github.com/WordPress/gutenberg/pull/86349)).

### Bug Fixes

- Keep the fine-rotation ruler's ticks and labels visible when the browser forces dark colors ([#86402](https://github.com/WordPress/gutenberg/pull/86402)).
Expand Down
155 changes: 137 additions & 18 deletions packages/media-editor/src/components/media-editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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( {
Comment on lines +331 to +333

Copy link
Copy Markdown
Contributor Author

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

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 ) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 (
Expand All @@ -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…',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image

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!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 ) }
/>
) }
</>
);
}
Expand Down
Loading
Loading