All files index.tsx

94.44% Statements 85/90
65.67% Branches 44/67
100% Functions 22/22
97.59% Lines 81/83

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225                                            1x 41x 41x 41x     1x                             1x 20x     3x                           1x   16x         1x 18x 18x 2x 2x     1x 16x 16x 18x 2x         1x 16x 16x 20x 20x       1x                   43x   43x 43x   43x 43x     43x 16x 16x 16x   16x 16x     16x     16x     16x 16x     16x 16x     16x       43x 486x 486x 16x       486x 16x 16x   16x   9x         9x           486x 486x       43x   10x     3x     6x       43x   36x 36x       36x 33x 33x     36x 10x 10x   10x 10x         43x 35x   512x     35x 35x     35x 9x 9x         43x     1x      
import React, { useCallback, useEffect, useImperativeHandle, useRef } from 'react';
import Hotkeys, { HotkeysEvent } from 'hotkeys-js';
 
export type OnKeyFun = (shortcut: string, evn: KeyboardEvent, handle: HotkeysEvent) => void;
 
export interface IReactHotkeysProps {
  keyName?: string;
  filter?: (event: KeyboardEvent) => boolean;
  onKeyUp?: OnKeyFun;
  onKeyDown?: OnKeyFun;
  allowRepeat?: boolean;
  disabled?: boolean;
  splitKey?: string;
  children?: React.ReactNode;
}
 
export interface ReactHotkeysRef {
  readonly isKeyDown: boolean;
  readonly handle: HotkeysEvent;
  readonly props: IReactHotkeysProps;
}
 
const defaultFilter = (event: KeyboardEvent) => {
  const target = (event.target as HTMLElement) || event.srcElement;
  const tagName = target.tagName;
  return !(target.isContentEditable || tagName === 'INPUT' || tagName === 'SELECT' || tagName === 'TEXTAREA');
};
 
const modifierAliases: Record<string, string[]> = {
  shift: ['shift', '⇧'],
  '⇧': ['shift', '⇧'],
  alt: ['alt', 'option', '⌥'],
  option: ['alt', 'option', '⌥'],
  '⌥': ['alt', 'option', '⌥'],
  ctrl: ['control', 'ctrl', '⌃'],
  control: ['control', 'ctrl', '⌃'],
  '⌃': ['control', 'ctrl', '⌃'],
  meta: ['meta', 'os', 'cmd', 'command', '⌘'],
  cmd: ['meta', 'os', 'cmd', 'command', '⌘'],
  command: ['meta', 'os', 'cmd', 'command', '⌘'],
  '⌘': ['meta', 'os', 'cmd', 'command', '⌘'],
};
 
const modifierEventFlag = (part: string): 'shiftKey' | 'altKey' | 'ctrlKey' | 'metaKey' | undefined => {
  switch (part.toLowerCase()) {
    case 'shift':
    case '⇧':
      return 'shiftKey';
    case 'alt':
    case 'option':
    case '⌥':
      return 'altKey';
    case 'ctrl':
    case 'control':
    case '⌃':
      return 'ctrlKey';
    case 'meta':
    case 'os':
    case 'cmd':
    case 'command':
    case '⌘':
      return 'metaKey';
    default:
      return undefined;
  }
};
 
// 这一次 keyup 松开的键,即使 hotkeys-js 还把它记在按下列表里,也算已经松开。
const eventReleasesPart = (event: KeyboardEvent, part: string) => {
  const eventKey = (event.key || '').toLowerCase();
  if (eventKey === part.toLowerCase()) return true;
  const aliases = modifierAliases[part] || modifierAliases[part.toLowerCase()];
  return !!aliases && aliases.includes(eventKey);
};
 
const isShortcutHeld = (shortcut: string, splitKey: string | undefined, event: KeyboardEvent) => {
  const delimiter = splitKey || '+';
  return shortcut.split(delimiter).every((part) => {
    if (eventReleasesPart(event, part)) return false;
    return Hotkeys.isPressed(part);
  });
};
 
// macOS 松开 Shift 后会再发一次字母 keydown。修饰键以这次事件上的状态为准,避免快捷键被重新点亮。
const shortcutModifiersHeld = (shortcut: string, splitKey: string | undefined, event: KeyboardEvent) => {
  const delimiter = splitKey || '+';
  return shortcut.split(delimiter).every((part) => {
    const flag = modifierEventFlag(part);
    return !flag || event[flag] === true;
  });
};
 
const ReactHotkeys = React.forwardRef<ReactHotkeysRef, IReactHotkeysProps>((props, ref) => {
  const {
    keyName,
    filter = defaultFilter,
    onKeyUp,
    onKeyDown,
    allowRepeat,
    disabled,
    splitKey,
    children,
  } = props;
 
  const activeKeysRef = useRef(new Set<string>());
  const currentHandleRef = useRef({} as HotkeysEvent);
  // 回调每次 render 都可能是新函数。放进 ref,避免重新绑定 hotkeys 时把按下状态清掉。
  const latestRef = useRef({ onKeyDown, onKeyUp, disabled, allowRepeat, splitKey });
  latestRef.current = { onKeyDown, onKeyUp, disabled, allowRepeat, splitKey };
 
  // 处理 keydown 事件
  const handleKeyDown = useCallback((keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent) => {
    const { onKeyDown: keyDown, disabled: isDisabled, allowRepeat: repeat, splitKey: delimiter } = latestRef.current;
    Iif (isDisabled || keyboardEvent.type === 'keyup') return;
    Iif (!shortcutModifiersHeld(hotkeysEvent.shortcut, delimiter, keyboardEvent)) return;
 
    const shortcut = hotkeysEvent.shortcut;
    const isAlreadyPressed = activeKeysRef.current.has(shortcut);
 
    // 命中快捷键就拦住浏览器默认行为,例如 command+f 打开查找。
    keyboardEvent.preventDefault();
 
    // 如果不允许重复且已经按下,则跳过
    Iif (isAlreadyPressed && !repeat) return false;
 
    // 记录当前按下的快捷键
    activeKeysRef.current.add(shortcut);
    currentHandleRef.current = hotkeysEvent;
 
    // 触发 onKeyDown 回调
    Eif (keyDown) {
      keyDown(shortcut, keyboardEvent, hotkeysEvent);
    }
    // hotkeys-js 在回调返回 false 时也会 preventDefault。
    return false;
  }, []);
 
  // 处理 keyup 事件 - 使用原生事件监听。省略 onKeyUp 时也要清掉按下状态。
  const handleKeyUp = useCallback((keyboardEvent: KeyboardEvent) => {
    const { onKeyUp: keyUp, disabled: isDisabled, splitKey: delimiter } = latestRef.current;
    const releasedKeys = Array.from(activeKeysRef.current).filter((shortcut) => {
      return !isShortcutHeld(shortcut, delimiter, keyboardEvent);
    });
 
    // 处理被释放的按键
    releasedKeys.forEach(shortcut => {
      Eif (activeKeysRef.current.has(shortcut)) {
        activeKeysRef.current.delete(shortcut);
 
        if (!isDisabled && keyUp) {
          // 构造 HotkeysEvent(基于最后记录的 handle)
          const hotkeysEvent = {
            ...currentHandleRef.current,
            shortcut
          };
 
          keyUp(shortcut, keyboardEvent, hotkeysEvent);
        }
      }
    });
 
    // 清空 handle 如果没有按键被按下
    Eif (activeKeysRef.current.size === 0) {
      currentHandleRef.current = {} as HotkeysEvent;
    }
  }, []);
 
  useImperativeHandle(ref, () => ({
    get isKeyDown() {
      return activeKeysRef.current.size > 0;
    },
    get handle() {
      return currentHandleRef.current;
    },
    get props() {
      return props;
    },
  }), [props]);
 
  useEffect(() => {
    // 设置过滤器
    Eif (filter) {
      Hotkeys.filter = filter;
    }
 
    // 只绑定 keydown。keyup 交给下面的 document 监听,避免 allowRepeat 时 onKeyDown 在松开时再触发。
    if (keyName) {
      Hotkeys.unbind(keyName);
      Hotkeys(keyName, { splitKey, keydown: true, keyup: false }, handleKeyDown);
    }
 
    return () => {
      Eif (keyName) {
        Hotkeys.unbind(keyName);
      }
      activeKeysRef.current.clear();
      currentHandleRef.current = {} as HotkeysEvent;
    };
  }, [keyName, splitKey, filter, handleKeyDown]);
 
  // 始终监听 keyup,这样省略 onKeyUp 时也能把 isKeyDown 复位。
  useEffect(() => {
    const handleGlobalKeyUp = (e: KeyboardEvent) => {
      // 使用 setTimeout 确保 hotkeys-js 内部状态已更新
      setTimeout(() => handleKeyUp(e), 0);
    };
 
    Eif (typeof document !== 'undefined') {
      document.addEventListener('keyup', handleGlobalKeyUp);
    }
 
    return () => {
      Eif (typeof document !== 'undefined') {
        document.removeEventListener('keyup', handleGlobalKeyUp);
      }
    };
  }, [handleKeyUp]);
 
  return <>{children}</>;
});
 
ReactHotkeys.displayName = 'ReactHotkeys';
 
export default ReactHotkeys;