| 1 |
/** |
| 2 |
* Unregister all Gutenberg keyboard shortcuts on PB form CPTs. |
| 3 |
* On-screen controls keep working; only key bindings go. |
| 4 |
* |
| 5 |
* Scope: `core/keyboard-shortcuts` only. RichText formatting is internal and |
| 6 |
* stays. Sweep categories via `getCategoryShortcuts` (no "all names" selector); |
| 7 |
* re-unsubscribe on store changes until empty. |
| 8 |
*/ |
| 9 |
|
| 10 |
import { useEffect } from '@wordpress/element'; |
| 11 |
import { useSelect, select as dataSelect, dispatch as dataDispatch, subscribe } from '@wordpress/data'; |
| 12 |
import { registerPlugin } from '@wordpress/plugins'; |
| 13 |
|
| 14 |
const PB_CPTS = [ 'wppb-rf-cpt', 'wppb-epf-cpt' ]; |
| 15 |
const STORE = 'core/keyboard-shortcuts'; |
| 16 |
|
| 17 |
// Core categories plus headroom; unknown categories return []. |
| 18 |
const SWEEP_CATEGORIES = [ |
| 19 |
'global', 'block', 'main', 'list-view', |
| 20 |
'text', 'canvas', 'editor', 'register', 'selection', 'navigation', |
| 21 |
]; |
| 22 |
|
| 23 |
const collectShortcutNames = () => { |
| 24 |
const sel = dataSelect( STORE ); |
| 25 |
if ( ! sel || typeof sel.getCategoryShortcuts !== 'function' ) return []; |
| 26 |
const names = new Set(); |
| 27 |
for ( const category of SWEEP_CATEGORIES ) { |
| 28 |
let list; |
| 29 |
try { list = sel.getCategoryShortcuts( category ); } catch ( e ) { continue; } |
| 30 |
if ( Array.isArray( list ) ) { |
| 31 |
list.forEach( ( name ) => { if ( name ) names.add( name ); } ); |
| 32 |
} |
| 33 |
} |
| 34 |
return Array.from( names ); |
| 35 |
}; |
| 36 |
|
| 37 |
// Only dispatches for names that exist — empty store → no loop. |
| 38 |
const unregisterAllShortcuts = () => { |
| 39 |
const dis = dataDispatch( STORE ); |
| 40 |
if ( ! dis || typeof dis.unregisterShortcut !== 'function' ) return 0; |
| 41 |
const names = collectShortcutNames(); |
| 42 |
names.forEach( ( name ) => dis.unregisterShortcut( name ) ); |
| 43 |
return names.length; |
| 44 |
}; |
| 45 |
|
| 46 |
const DisableKeyboardShortcuts = () => { |
| 47 |
const postType = useSelect( |
| 48 |
( select ) => select( 'core/editor' ).getCurrentPostType(), |
| 49 |
[] |
| 50 |
); |
| 51 |
const isOurPostType = PB_CPTS.includes( postType ); |
| 52 |
|
| 53 |
useEffect( () => { |
| 54 |
if ( ! isOurPostType ) return undefined; |
| 55 |
|
| 56 |
unregisterAllShortcuts(); |
| 57 |
|
| 58 |
// Core may register after us or on remount; re-sweep until empty. |
| 59 |
const unsubscribe = subscribe( () => { |
| 60 |
unregisterAllShortcuts(); |
| 61 |
}, STORE ); |
| 62 |
|
| 63 |
return unsubscribe; |
| 64 |
}, [ isOurPostType ] ); |
| 65 |
|
| 66 |
return null; |
| 67 |
}; |
| 68 |
|
| 69 |
registerPlugin( 'wppb-fb-disable-keyboard-shortcuts', { render: DisableKeyboardShortcuts } ); |
| 70 |
|