UNPKG

@orca-fe/hooks

Version:

React Hooks Collections

149 lines (147 loc) 4.05 kB
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper"; import { detect } from 'detect-browser'; /** * 是否为mac系统(包含iphone手机) * */ export function isMac() { var info = detect(); return (info === null || info === void 0 ? void 0 : info.os) === 'Mac OS' || (info === null || info === void 0 ? void 0 : info.os) === 'darwin'; } var fnKeysOrder = { Command: 1, Ctrl: 2, Alt: 3, Shift: 4 }; /** * 格式化快捷键字符串,按顺序排列功能键 * @param hotkeyStr */ export function formatHotKeyStr(hotkeyStr = '') { var hotkeyArr = hotkeyStr.split('+'); var fnKeys = []; var normalKeys = []; var _iterator = _createForOfIteratorHelper(hotkeyArr), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var key = _step.value; if (fnKeysOrder[key]) { fnKeys.push(key); } else { normalKeys.push(key || '+'); } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return [...fnKeys.sort((a, b) => fnKeysOrder[a] - fnKeysOrder[b]), ...normalKeys].join('+'); } export function toHotkeyStr(event) { var metaKey = event.metaKey, ctrlKey = event.ctrlKey, altKey = event.altKey, shiftKey = event.shiftKey, _key = event.key, keyCode = event.keyCode; var key = _key; // 如果是數字或字母,使用 fromCharCode 的方式 if (keyCode >= 0x30 && keyCode <= 0x39 || keyCode >= 0x41 && keyCode <= 0x5a) { key = String.fromCharCode(keyCode); } if (key === 'Command' || key === 'Control' || key === 'Meta' || key === 'Shift' || key === 'Alt') { return ''; } var keyArr = []; if (isMac()) { if (metaKey) { keyArr.push('Command'); } if (ctrlKey) { keyArr.push('Ctrl'); } if (shiftKey) { keyArr.push('Shift'); } if (/^[a-z]$/.test(key)) { keyArr.push(key.toUpperCase()); } else { keyArr.push(key); } } else { if (ctrlKey) { keyArr.push('Ctrl'); } if (shiftKey) { keyArr.push('Shift'); } if (altKey) { keyArr.push('Alt'); } if (/^[a-z]$/.test(key)) { keyArr.push(key.toUpperCase()); } else { keyArr.push(key); } } return formatHotKeyStr(keyArr.join('+')); } export function divOnlyFilter(e) { var _e$target; if (((_e$target = e.target) === null || _e$target === void 0 ? void 0 : _e$target['tagName']) === 'DIV') { return true; } return false; } export function isInput(element) { return element.tagName === 'INPUT' || element.tagName === 'TEXTAREA'; } function smartForEach(arr, callback) { if (Array.isArray(arr)) { arr.forEach(callback); } else { callback(arr, 0); } } /** * 生成热键缓存 */ export function createHotkeyCache(hotkey) { var hotkeyMapping = new Map(); var macHotkeyMapping = new Map(); Object.entries(hotkey).forEach(([hotkeyName, commands]) => { var _commands = _slicedToArray(commands, 2), winKeys = _commands[0], _commands$ = _commands[1], macKeys = _commands$ === void 0 ? winKeys : _commands$; smartForEach(winKeys, hotKey => { if (hotkeyMapping.has(hotKey)) { console.warn(`Duplicate hot-key '${hotKey}' for command [${hotkeyName}]`); } else { hotkeyMapping.set(hotKey, hotkeyName); } if (!macKeys) { if (macHotkeyMapping.has(hotKey)) { console.warn(`Duplicate hot-key '${hotKey}' for command(Mac) [${hotkeyName}]`); } else { macHotkeyMapping.set(hotKey, hotkeyName); } } }); // MacMode if (macKeys) { smartForEach(macKeys, hotKey => { if (macHotkeyMapping.has(hotKey)) { console.warn(`Duplicate hot-key '${hotKey}' for command(Mac) [${hotkeyName}]`); } else { macHotkeyMapping.set(hotKey, hotkeyName); } }); } }); return { hotkeyMapping, macHotkeyMapping }; }