PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 4.0.3
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v4.0.3
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / form-builder / blocks / src / components / DisableKeyboardShortcuts.js

DisableKeyboardShortcuts.js in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 4.0.3, at form-builder/blocks/src/components/DisableKeyboardShortcuts.js

70 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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