(shortcuts, eleRef)
| 182 | ] |
| 183 | */ |
| 184 | export function useKeyboardShortcuts(shortcuts, eleRef) { |
| 185 | const shortcutsRef = useRef(shortcuts); |
| 186 | |
| 187 | const matchFound = (shortcut, e)=>{ |
| 188 | if(!shortcut) return false; |
| 189 | let keyCode = getCode(e); |
| 190 | const ctrlKey = (isMac() && shortcut.ctrl_is_meta) ? e.metaKey : e.ctrlKey; |
| 191 | |
| 192 | return Boolean(shortcut.alt) == e.altKey && |
| 193 | Boolean(shortcut.shift) == e.shiftKey && |
| 194 | Boolean(shortcut.control) == ctrlKey && |
| 195 | shortcut.key.key_code == keyCode; |
| 196 | }; |
| 197 | |
| 198 | useEffect(()=>{ |
| 199 | let ele = eleRef.current ?? document; |
| 200 | const dispatch = (e)=>{ |
| 201 | Promise.resolve(0).then(()=>{ |
| 202 | let allListeners = _.filter(shortcutsRef.current, (s)=>matchFound(s.shortcut, e)); |
| 203 | for(const {options} of allListeners) { |
| 204 | Promise.resolve(0).then(()=>{ |
| 205 | if(options.callback && (options.enabled ?? true)) { |
| 206 | e.preventDefault(); |
| 207 | e.stopPropagation(); |
| 208 | options.callback(e); |
| 209 | } |
| 210 | }); |
| 211 | } |
| 212 | }); |
| 213 | }; |
| 214 | ele.addEventListener('keydown', dispatch); |
| 215 | return ()=>{ |
| 216 | ele.removeEventListener('keydown', dispatch); |
| 217 | }; |
| 218 | }, [eleRef.current]); |
| 219 | |
| 220 | useEffect(()=>{ |
| 221 | shortcutsRef.current = shortcuts; |
| 222 | }, [shortcuts]); |
| 223 | } |
| 224 | |
| 225 | export function useWindowSize() { |
| 226 | const [size, setSize] = useState([999999, 999999]); |
no outgoing calls
no test coverage detected