-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Media editor modal: expand keyboard shortcuts and add interaction helpers #77871
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
58811ce
Implement keyboard shortcuts modal in media editor
ramonjd 7dbd9a0
- Updated the `ShortcutEntry` structure to use a `keyCombination` obj…
ramonjd 74347c0
Add lock aspect ratio hint to cropper
ramonjd 61535db
- Removed the `onHandleActiveChange` prop and its related state manag…
ramonjd 41b19db
Add flip functionality to InteractionController and update keyboard s…
ramonjd 68c74ae
Apply suggestions from code review
ramonjd 0e418d7
Ensure + key is displayed correctly in keyboard shortcuts modal
ramonjd 2e00106
Rename flip to toggleFlip and add tests
ramonjd 81f023d
Update keyboard shortcuts modal to support array of key characters
ramonjd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
178 changes: 178 additions & 0 deletions
178
packages/media-editor/src/components/media-editor-keyboard-shortcuts-modal/index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { Modal } from '@wordpress/components'; | ||
| import { Fragment } from '@wordpress/element'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { | ||
| displayShortcutList, | ||
| shortcutAriaLabel, | ||
| type WPKeycodeModifier, | ||
| } from '@wordpress/keycodes'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import './style.scss'; | ||
|
|
||
| interface KeyCombination { | ||
| /** Modifier for cross-platform display (e.g. 'primary', 'primaryShift', 'shift'). */ | ||
| modifier?: WPKeycodeModifier; | ||
| /** The key character(s) to display. Pass an array to render each as its own key. */ | ||
| character: string | string[]; | ||
| /** Optional aria-label override (used when character is a symbol). */ | ||
| ariaLabel?: string; | ||
| } | ||
|
|
||
| interface ShortcutEntry { | ||
| description: string; | ||
| keyCombination: KeyCombination; | ||
| } | ||
|
|
||
| const SHORTCUTS: ShortcutEntry[] = [ | ||
| { | ||
| description: __( 'Undo' ), | ||
| keyCombination: { modifier: 'primary', character: 'z' }, | ||
| }, | ||
| { | ||
| description: __( 'Redo' ), | ||
| keyCombination: { modifier: 'primaryShift', character: 'z' }, | ||
| }, | ||
| { | ||
| description: __( 'Pan' ), | ||
| keyCombination: { | ||
| character: [ '↑', '↓', '←', '→' ], | ||
| ariaLabel: __( 'Arrow keys' ), | ||
| }, | ||
| }, | ||
| { | ||
| description: __( 'Zoom in' ), | ||
| keyCombination: { character: '+' }, | ||
| }, | ||
| { | ||
| description: __( 'Zoom out' ), | ||
| keyCombination: { character: '-' }, | ||
| }, | ||
| { | ||
| description: __( 'Rotate 90° clockwise' ), | ||
| keyCombination: { character: 'R' }, | ||
| }, | ||
| { | ||
| description: __( 'Flip horizontal' ), | ||
| keyCombination: { character: 'H' }, | ||
| }, | ||
| { | ||
| description: __( 'Flip vertical' ), | ||
| keyCombination: { character: 'V' }, | ||
| }, | ||
| { | ||
| description: __( 'Resize crop (large step)' ), | ||
| keyCombination: { | ||
| modifier: 'shift', | ||
| character: [ '↑', '↓', '←', '→' ], | ||
| ariaLabel: __( 'Shift + Arrow keys' ), | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| function KeyCombinationDisplay( { | ||
| keyCombination, | ||
| }: { | ||
| keyCombination: KeyCombination; | ||
| } ) { | ||
| const { modifier, character, ariaLabel } = keyCombination; | ||
|
|
||
| let keys: string[]; | ||
| if ( Array.isArray( character ) ) { | ||
| if ( modifier ) { | ||
| // Get the modifier prefix (e.g. ['⇧', '+']) from the first char, | ||
| // then replace the last element with all the individual chars. | ||
| const sample = displayShortcutList[ modifier ]( character[ 0 ] ); | ||
| keys = [ ...sample.slice( 0, -1 ), ...character ]; | ||
| } else { | ||
| keys = character; | ||
| } | ||
| } else { | ||
| keys = modifier | ||
| ? displayShortcutList[ modifier ]( character ) | ||
| : [ character ]; | ||
| } | ||
|
|
||
| const charString = Array.isArray( character ) | ||
| ? character.join( '' ) | ||
| : character; | ||
| let label: string; | ||
| if ( ariaLabel ) { | ||
| label = ariaLabel; | ||
| } else if ( modifier ) { | ||
| label = shortcutAriaLabel[ modifier ]( charString ); | ||
| } else { | ||
| label = charString; | ||
| } | ||
|
|
||
| return ( | ||
| <kbd | ||
| className="media-editor-keyboard-shortcuts-modal__shortcut-term" | ||
| aria-label={ label } | ||
| > | ||
| { keys.map( ( key, index ) => | ||
| key === '+' && modifier ? ( | ||
| <Fragment key={ index }>{ key }</Fragment> | ||
| ) : ( | ||
| <kbd | ||
| key={ index } | ||
| className="media-editor-keyboard-shortcuts-modal__shortcut-key" | ||
| > | ||
| { key } | ||
| </kbd> | ||
| ) | ||
| ) } | ||
| </kbd> | ||
| ); | ||
| } | ||
|
|
||
| interface MediaEditorKeyboardShortcutsModalProps { | ||
| onClose: () => void; | ||
| } | ||
|
|
||
| export default function MediaEditorKeyboardShortcutsModal( { | ||
| onClose, | ||
| }: MediaEditorKeyboardShortcutsModalProps ) { | ||
| return ( | ||
| <Modal | ||
| className="media-editor-keyboard-shortcuts-modal" | ||
| title={ __( 'Keyboard shortcuts' ) } | ||
| onRequestClose={ onClose } | ||
| > | ||
| <p className="media-editor-keyboard-shortcuts-modal__note"> | ||
| { __( | ||
| 'These shortcuts work when the image editor has focus.' | ||
| ) } | ||
| </p> | ||
| { /* | ||
| * Disable reason: The `list` ARIA role is redundant but | ||
| * Safari+VoiceOver won't announce the list otherwise. | ||
| */ } | ||
| { /* eslint-disable jsx-a11y/no-redundant-roles */ } | ||
| <ul | ||
| className="media-editor-keyboard-shortcuts-modal__shortcut-list" | ||
| role="list" | ||
| > | ||
| { SHORTCUTS.map( ( { description, keyCombination }, index ) => ( | ||
| <li | ||
| key={ index } | ||
| className="media-editor-keyboard-shortcuts-modal__shortcut" | ||
| > | ||
| <span className="media-editor-keyboard-shortcuts-modal__shortcut-description"> | ||
| { description } | ||
| </span> | ||
| <KeyCombinationDisplay | ||
| keyCombination={ keyCombination } | ||
| /> | ||
| </li> | ||
| ) ) } | ||
| </ul> | ||
| { /* eslint-enable jsx-a11y/no-redundant-roles */ } | ||
| </Modal> | ||
| ); | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
packages/media-editor/src/components/media-editor-keyboard-shortcuts-modal/style.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| @use "@wordpress/base-styles/colors" as *; | ||
| @use "@wordpress/base-styles/variables" as *; | ||
|
|
||
| .media-editor-keyboard-shortcuts-modal { | ||
| min-width: 280px; | ||
|
|
||
| .media-editor-keyboard-shortcuts-modal__note { | ||
| margin: 0 0 $grid-unit-20; | ||
| color: $gray-700; | ||
| font-size: 13px; | ||
| } | ||
|
|
||
| .media-editor-keyboard-shortcuts-modal__shortcut-list { | ||
| list-style: none; | ||
| margin: 0; | ||
| padding: 0; | ||
| } | ||
|
|
||
| .media-editor-keyboard-shortcuts-modal__shortcut { | ||
| display: flex; | ||
| align-items: baseline; | ||
| padding: 0.6rem 0; | ||
| border-top: 1px solid $gray-300; | ||
| margin-bottom: 0; | ||
|
|
||
| &:last-child { | ||
| border-bottom: 1px solid $gray-300; | ||
| } | ||
| } | ||
|
|
||
| .media-editor-keyboard-shortcuts-modal__shortcut-description { | ||
| flex: 1; | ||
| margin: 0; | ||
| } | ||
|
|
||
| .media-editor-keyboard-shortcuts-modal__shortcut-term { | ||
| background: none; | ||
| font-weight: 600; | ||
| margin: 0 0 0 1rem; | ||
| padding: 0; | ||
| text-align: right; | ||
| } | ||
|
|
||
| .media-editor-keyboard-shortcuts-modal__shortcut-key { | ||
| padding: 0.25rem 0.5rem; | ||
| border-radius: 8%; | ||
| margin: 0 0.2rem 0 0.2rem; | ||
|
|
||
| &:last-child { | ||
| margin: 0 0 0 0.2rem; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.