-
Notifications
You must be signed in to change notification settings - Fork 29
Register undo/redo keyboard shortcuts for block editor #78
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
base: trunk
Are you sure you want to change the base?
Changes from 1 commit
412f745
54e29bc
0b979e7
6040f6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ import { | |
| useEffect, | ||
| useRef, | ||
| } from '@wordpress/element'; | ||
| import { useSelect } from '@wordpress/data'; | ||
| import { useSelect, useDispatch } from '@wordpress/data'; | ||
| import { parse } from '@wordpress/blocks'; | ||
| import { | ||
| BlockEditorProvider, | ||
|
|
@@ -276,6 +276,52 @@ export default function PressThisEditor( { | |
| const [ isLoadingTags, setIsLoadingTags ] = useState( false ); | ||
| const tagSearchTimeout = useRef( null ); | ||
|
|
||
| // Undo/Redo keyboard shortcuts. | ||
| // BlockEditorKeyboardShortcuts handles block-level shortcuts but not undo/redo. | ||
| // In full Gutenberg, EditorKeyboardShortcuts from @wordpress/editor registers these, | ||
| // but Press This uses BlockEditorProvider directly. | ||
| const { undo, redo } = useDispatch( blockEditorStore ); | ||
|
|
||
| useEffect( () => { | ||
| function handleKeyDown( event ) { | ||
| // Don't override native undo in regular form fields (title, URL input, etc.). | ||
| const tagName = event.target.tagName.toLowerCase(); | ||
| if ( | ||
| tagName === 'input' || | ||
| tagName === 'textarea' || | ||
| tagName === 'select' | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const isModKey = event.ctrlKey || event.metaKey; | ||
| if ( ! isModKey ) { | ||
| return; | ||
| } | ||
|
|
||
| const key = event.key.toLowerCase(); | ||
|
|
||
| // Ctrl+Z / Cmd+Z = Undo, Ctrl+Shift+Z / Cmd+Shift+Z = Redo. | ||
| if ( key === 'z' ) { | ||
| event.preventDefault(); | ||
| if ( event.shiftKey ) { | ||
| redo(); | ||
| } else { | ||
| undo(); | ||
| } | ||
| } | ||
|
|
||
| // Ctrl+Y / Cmd+Y = Redo (Windows/Linux convention). | ||
| if ( key === 'y' && ! event.shiftKey ) { | ||
| event.preventDefault(); | ||
| redo(); | ||
| } | ||
| } | ||
|
|
||
| document.addEventListener( 'keydown', handleKeyDown ); | ||
| return () => document.removeEventListener( 'keydown', handleKeyDown ); | ||
| }, [ undo, redo ] ); | ||
|
|
||
| // Parse initial content. | ||
| useEffect( () => { | ||
| if ( post.content ) { | ||
|
Comment on lines
379
to
381
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.