Skip to content

Commit

Permalink
Register global shortcut for moving focus back to the last focused bl…
Browse files Browse the repository at this point in the history
…ock.

Still more todo. This shouldn't go to the last focused block but the last _selection_ which will be a bit more work. Probably best to refactor how it works within use-tab-nav. Maybe turn it into a hook or something that can have centralized logic.
  • Loading branch information
jeryj committed Sep 6, 2023
1 parent fd31f65 commit c3c76b9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/editor/src/components/global-keyboard-shortcuts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { useShortcut } from '@wordpress/keyboard-shortcuts';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';

/**
* Internal dependencies
Expand All @@ -12,6 +13,8 @@ import { store as editorStore } from '../../store';
export default function EditorKeyboardShortcuts() {
const { redo, undo, savePost } = useDispatch( editorStore );
const { isEditedPostDirty, isPostSavingLocked } = useSelect( editorStore );
const { getLastFocus, getSelectedBlockClientId } =
useSelect( blockEditorStore );

useShortcut( 'core/editor/undo', ( event ) => {
undo();
Expand Down Expand Up @@ -45,5 +48,18 @@ export default function EditorKeyboardShortcuts() {
savePost();
} );

useShortcut( 'core/block-editor/focus-editor', ( event ) => {
event.preventDefault();
const lastFocus = getLastFocus();
// Only move focus if the selected block is a match with the last focused block
if (
getSelectedBlockClientId() &&
lastFocus?.current &&
lastFocus?.current.id.includes( getSelectedBlockClientId() )
) {
lastFocus.current.focus();
}
} );

return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ function EditorKeyboardShortcutsRegister() {
},
],
} );

registerShortcut( {
name: 'core/block-editor/focus-editor',
category: 'global',
description: __(
'Navigate to the last focused element in the editor.'
),
keyCombination: {
modifier: 'alt',
character: 'F9',
},
} );
}, [ registerShortcut ] );

return <BlockEditorKeyboardShortcuts.Register />;
Expand Down

0 comments on commit c3c76b9

Please sign in to comment.