How to unRegister shortcut registered by RichTextShortcut ? #46964
Replies: 4 comments 2 replies
-
I'm not sure, but I don't think the rich-text shortcuts can be managed externally via an API. However, you could use a workaround: Option 1) wp.data.dispatch('core/rich-text').removeFormatTypes('core/link') Option 2) const selectedBlock = docutment.querySelector('SELECTOR');
const blockEvents = window.getEventListener(selectedBlock);
const linkEvent = blockEvents['keydown'][INDEX_FROM_USE_KEYDOWN]?.listener;
selectedBlock.removeEventListener('keydown', linkEvent); To get the current selected block you can use: wp.data.select('core/block-editor').getSelectedBlock(); in combination with useSelect und useEffect. Option 2 only removes the addLink for me. All other shortcuts such as bold, italic etc. still seem to work. I am not sure if there is a better solution. You would have to get to the context of the rich-text component. useShortcuts: https://github.com/WordPress/gutenberg/blob/trunk/packages/block-editor/src/components/rich-text/use-shortcuts.js#L14 ShortcutRegisterComponent: https://github.com/WordPress/gutenberg/blob/trunk/packages/block-editor/src/components/rich-text/shortcut.js#L12 |
Beta Was this translation helpful? Give feedback.
-
@devfle Thanks for your reply! I choose Option 2. However, let me check your reply as I do not understand it exactly.
<TagName
...
ref={ useMergeRefs( [
...
useShortcuts( keyboardShortcuts ),
...
] ) }
...
className={ classnames(
'block-editor-rich-text__editable',
props.className,
'rich-text'
) }
...
/>
export function useShortcuts( keyboardShortcuts ) {
return useRefEffect( ( element ) => {
function onKeyDown( event ) {
for ( const keyboardShortcut of keyboardShortcuts.current ) {
keyboardShortcut( event );
/* here is equivalent like...
if (e.ctrlKey && e.which == 66)) {
code for formating bold
}
*/
}
}
element.addEventListener( 'keydown', onKeyDown );
return () => {
element.removeEventListener( 'keydown', onKeyDown );
};
}, [] );
} So, the solution may be 1 or 2 below
right ? From your comment
How to detect |
Beta Was this translation helpful? Give feedback.
-
Also add issue link [Allow users to customize keyboard shortcuts] #3218 for others future reference. |
Beta Was this translation helpful? Give feedback.
-
Old PR may solve this issue #34810 |
Beta Was this translation helpful? Give feedback.
-
I want to unregister
cmd+k
shortcut ofcore/link
which is registered as follows.I tried the code like below, but it doesn't work.
How to unregister this shortcut key?
PS: More specifically, I need to assign a shortcut to
cmd + k
to open a Search modal, and I want to change a shortcut for link block to another key.Beta Was this translation helpful? Give feedback.
All reactions