@atlaskit/editor-plugin-block-menu
Version:
BlockMenu plugin for @atlaskit/editor-core
31 lines (28 loc) • 1.49 kB
JavaScript
/**
* Determines whether keyboard events should be suppressed when the block menu is open.
*
* When the block menu is open, we want to block most keyboard events to prevent
* unintended interactions. However, certain actions should still be allowed:
* - Backspace/Delete: Allow deleting selected content
* - Copy/Cut/Paste: Allow clipboard operations (Cmd/Ctrl+C, Cmd/Ctrl+X, Cmd/Ctrl+V)
* - Undo/Redo: Allow undo/redo operations (Cmd/Ctrl+Z, Cmd/Ctrl+Y)
* - Copy Link to Selection: Allow the keyboard shortcut (Cmd/Ctrl+Alt+A)
*
* @param event - The keyboard event to check
* @returns true if the event should be suppressed, false if it should be allowed
*/
export var shouldSuppressKeyboardEvent = function shouldSuppressKeyboardEvent(event) {
var key = event.key.toLowerCase();
var code = event.code.toLowerCase();
var isMetaCtrl = event.metaKey || event.ctrlKey;
var isAlt = event.altKey;
// Check for allowed keyboard shortcuts
var isBackspaceDelete = ['backspace', 'delete'].includes(key);
var isCopyCutPaste = isMetaCtrl && ['c', 'x', 'v'].includes(key);
var isUndoRedo = isMetaCtrl && ['z', 'y'].includes(key);
// Use event.code to detect physical key 'A' because on macOS Option+A produces 'å'
var isCopyLinkToBlock = isMetaCtrl && isAlt && code === 'keya';
// Suppress all events except the allowed ones
var suppressNativeHandling = !isCopyCutPaste && !isBackspaceDelete && !isUndoRedo && !isCopyLinkToBlock;
return suppressNativeHandling;
};