contenido
Version:
Contenido is a library with a set of tools to help you create your own rich text editor on top of draft-js without thinking about how to handle things.
47 lines (39 loc) • 1.35 kB
text/typescript
// Core
import { getDefaultKeyBinding, KeyBindingUtil } from '../../core';
// Custom Utilities
import initialShortcuts from './initial';
// Custom Types
import type { ShortcutMap, SyntheticKeyboardEvent } from '../../types';
const getDefaultKeyBindingFn = (
event: SyntheticKeyboardEvent,
customShortcuts?: ShortcutMap[]
): string | null => {
if (event.key === 'Alt') event.preventDefault();
let shortcuts = customShortcuts || initialShortcuts;
for (let shortcutMap of shortcuts) {
if (Array.isArray(shortcutMap.shortcut)) {
let matches = shortcutMap.shortcut.every((key, index) => {
if (
['ctrl', 'control', 'cmd', 'command'].includes(
key.toLocaleLowerCase()
) &&
KeyBindingUtil.hasCommandModifier(event)
)
return true;
if (
(key.toLocaleLowerCase() === 'alt' ||
key.toLocaleLowerCase() === 'option') &&
KeyBindingUtil.isOptionKeyCommand(event)
)
return true;
if (key.toLocaleLowerCase() === 'shift') return event.shiftKey;
return key === event.key;
});
if (matches) return shortcutMap.style;
} else if (shortcutMap.shortcut(event)) {
return shortcutMap.shortcut(event);
}
}
return getDefaultKeyBinding(event);
};
export default getDefaultKeyBindingFn;