UNPKG

@coreui/react

Version:

UI Components Library for React.js

116 lines (113 loc) 3.77 kB
'use strict'; const MODIFIER_KEYS = new Set(['alt', 'ctrl', 'meta', 'shift']); const KEY_ALIASES = { ' ': 'space', cmd: 'meta', command: 'meta', control: 'ctrl', esc: 'escape', option: 'alt', return: 'enter', spacebar: 'space', }; const KEY_LABELS = { alt: 'Alt', ctrl: 'Ctrl', meta: '⌘', shift: 'Shift', space: 'Space', }; const EDITABLE_TARGET_SELECTOR = 'input, textarea, select, [contenteditable=""], [contenteditable="true"], [contenteditable="plaintext-only"]'; const normalizeKey = (key) => KEY_ALIASES[key.toLowerCase()] || key.toLowerCase(); const parseShortcut = (shortcut) => shortcut .split(',') .map((value) => value.trim()) .filter(Boolean) .map((value) => { const keys = value.split('+').map((part) => normalizeKey(part.trim())); const modifiers = { alt: false, ctrl: false, meta: false, shift: false, }; let key = ''; for (const part of keys) { if (MODIFIER_KEYS.has(part)) { modifiers[part] = true; continue; } key = part; } return { key, modifiers, shortcut: value, }; }); const getPlatform = () => { var _a; if (typeof window === 'undefined') { return ''; } const navigator = window.navigator; return ((_a = navigator.userAgentData) === null || _a === void 0 ? void 0 : _a.platform) || navigator.platform || navigator.userAgent || ''; }; const isMacOS = () => /Mac|iPhone|iPad|iPod|macOS|Macintosh/.test(getPlatform()); const getPreferredShortcut = (shortcuts) => shortcuts.find((shortcut) => (isMacOS() ? shortcut.modifiers.meta : shortcut.modifiers.ctrl)) || shortcuts[0] || null; const getKeyLabel = (key) => KEY_LABELS[key] || (key.length === 1 ? key.toUpperCase() : `${key.charAt(0).toUpperCase()}${key.slice(1)}`); const formatShortcutTokens = (shortcut) => shortcut .split('+') .map((part) => normalizeKey(part.trim())) .map((part) => getKeyLabel(part)) .filter(Boolean); const getPressedKeys = (event) => { const pressedKeys = new Set(); if (event.altKey) { pressedKeys.add(KEY_LABELS.alt); } if (event.ctrlKey) { pressedKeys.add(KEY_LABELS.ctrl); } if (event.metaKey) { pressedKeys.add(KEY_LABELS.meta); } if (event.shiftKey) { pressedKeys.add(KEY_LABELS.shift); } const normalizedKey = normalizeKey(event.key); if (!MODIFIER_KEYS.has(normalizedKey) && event.type === 'keydown') { pressedKeys.add(getKeyLabel(normalizedKey)); } return pressedKeys; }; const isEditableTarget = (target) => { if (!(target instanceof Element)) { return false; } return target.matches(EDITABLE_TARGET_SELECTOR) || !!target.closest(EDITABLE_TARGET_SELECTOR); }; const shouldIgnoreShortcut = (event) => isEditableTarget(event.target) && !event.ctrlKey && !event.metaKey; const matchesShortcut = (shortcut, event) => { if (!shortcut.key || normalizeKey(event.key) !== shortcut.key) { return false; } return (shortcut.modifiers.alt === event.altKey && shortcut.modifiers.ctrl === event.ctrlKey && shortcut.modifiers.meta === event.metaKey && shortcut.modifiers.shift === event.shiftKey); }; exports.formatShortcutTokens = formatShortcutTokens; exports.getKeyLabel = getKeyLabel; exports.getPreferredShortcut = getPreferredShortcut; exports.getPressedKeys = getPressedKeys; exports.isEditableTarget = isEditableTarget; exports.isMacOS = isMacOS; exports.matchesShortcut = matchesShortcut; exports.normalizeKey = normalizeKey; exports.parseShortcut = parseShortcut; exports.shouldIgnoreShortcut = shouldIgnoreShortcut; //# sourceMappingURL=utils.js.map