Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
1 line
8.8 KiB
Plaintext
1 line
8.8 KiB
Plaintext
{"version":3,"file":"useHotkey.js","names":["c","_c","useModal","setsAreEqual","useCallback","useEffect","pressedKeys","Map","map","altleft","altright","controlleft","controlright","ctrlleft","ctrlright","escape","metaleft","metaright","osleft","osright","shiftleft","shiftright","stripKey","key","toLowerCase","trim","replace","pushToKeys","code","forEach","time","pressedKey","delete","set","Date","now","removeFromKeys","clear","useHotkey","options","func","$","cmdCtrlKey","editDepth","keyCodes","modalState","t0","event","e","detail","undefined","_temp","hasCmd","window","navigator","userAgent","includes","pressedWithoutModifier","keys","filter","_temp2","Set","has","ctrlKey","maxEditDepth","Object","key_1","isOpen","length","keydown","keyup","_temp3","t1","t2","document","addEventListener","removeEventListener","key_0","e_0"],"sources":["../../src/hooks/useHotkey.ts"],"sourcesContent":["'use client'\n\nimport { useModal } from '@faceless-ui/modal'\nimport { setsAreEqual } from 'payload/shared'\nimport { useCallback, useEffect } from 'react'\n\n// Required to be outside of hook, else debounce would be necessary\n// and then one could not prevent the default behaviour.\n\n// It maps the pressed keys with the time they were pressed, in order to implement a maximum time\n// for the user to press the next key in the sequence\n\n// This is necessary to prevent a bug where the keyup event, which unsets the key as pressed\n// is not fired when the window is not focused.\n// When the user then comes back to the window, the key is still registered as pressed, even though it's not.\nconst pressedKeys = new Map<string, number>([])\n\nconst map = {\n altleft: 'alt',\n altright: 'alt',\n controlleft: 'ctrl',\n controlright: 'ctrl',\n ctrlleft: 'ctrl',\n ctrlright: 'ctrl',\n escape: 'esc',\n metaleft: 'meta',\n metaright: 'meta',\n osleft: 'meta',\n osright: 'meta',\n shiftleft: 'shift',\n shiftright: 'shift',\n}\n\nconst stripKey = (key: string) => {\n return (map[key.toLowerCase()] || key).trim().toLowerCase().replace('key', '')\n}\n\nconst pushToKeys = (code: string) => {\n const key = stripKey(code)\n\n // There is a weird bug with macos that if the keys are not cleared they remain in the\n // pressed keys set.\n if (key === 'meta') {\n pressedKeys.forEach(\n (time, pressedKey) => pressedKey !== 'meta' && pressedKeys.delete(pressedKey),\n )\n }\n\n pressedKeys.set(key, Date.now())\n}\n\nconst removeFromKeys = (code: string) => {\n const key = stripKey(code)\n // There is a weird bug with macos that if the keys are not cleared they remain in the\n // pressed keys set.\n if (key === 'meta') {\n pressedKeys.clear()\n }\n pressedKeys.delete(key)\n}\n\n/**\n * Hook function to work with hotkeys.\n * @param param0.keyCode {string[]} The keys to listen for (`Event.code` without `'Key'` and lowercased)\n * @param param0.cmdCtrlKey {boolean} Whether Ctrl on windows or Cmd on mac must be pressed\n * @param param0.editDepth {boolean} This ensures that the hotkey is only triggered for the most top-level drawer in case there are nested drawers\n * @param func The callback function\n */\nexport const useHotkey = (\n options: {\n cmdCtrlKey: boolean\n editDepth: number\n keyCodes: string[]\n },\n func: (e: KeyboardEvent) => void,\n): void => {\n const { cmdCtrlKey, editDepth, keyCodes } = options\n\n const { modalState } = useModal()\n\n const keydown = useCallback(\n (event: CustomEvent | KeyboardEvent) => {\n const e: KeyboardEvent = event.detail?.key ? event.detail : event\n if (e.key === undefined) {\n // Autofill events, or other synthetic events, can be ignored\n return\n }\n\n // Filter out pressed keys which have been pressed > 3 seconds ago\n pressedKeys.forEach((time, key) => {\n if (Date.now() - time > 3000) {\n pressedKeys.delete(key)\n }\n })\n\n if (e.code) {\n pushToKeys(e.code)\n }\n\n // Check for Mac and iPad\n const hasCmd = window.navigator.userAgent.includes('Mac OS X')\n const pressedWithoutModifier = [...pressedKeys.keys()].filter(\n (key) => !['alt', 'ctrl', 'meta', 'shift'].includes(key),\n )\n\n // Check whether arrays contain the same values (regardless of number of occurrences)\n if (\n setsAreEqual(new Set(pressedWithoutModifier), new Set(keyCodes)) &&\n (!cmdCtrlKey || (hasCmd && pressedKeys.has('meta')) || (!hasCmd && e.ctrlKey))\n ) {\n // get the maximum edit depth by counting the number of open drawers. modalState is and object which contains the state of all drawers.\n const maxEditDepth =\n Object.keys(modalState).filter((key) => modalState[key]?.isOpen)?.length + 1 || 1\n\n if (maxEditDepth !== editDepth) {\n // We only want to execute the hotkey from the most top-level drawer / edit depth.\n return\n }\n // execute the function associated with the maximum edit depth\n func(e)\n }\n },\n [keyCodes, cmdCtrlKey, editDepth, modalState, func],\n )\n\n const keyup = useCallback((e: KeyboardEvent) => {\n if (e.code) {\n removeFromKeys(e.code)\n }\n }, [])\n\n useEffect(() => {\n document.addEventListener('keydown', keydown, false)\n document.addEventListener('bypassKeyDown', keydown, false) // this is called if the keydown event's propagation is stopped by react-select\n document.addEventListener('keyup', keyup, false)\n\n return () => {\n document.removeEventListener('keydown', keydown)\n document.removeEventListener('bypassKeyDown', keydown)\n document.removeEventListener('keyup', keyup)\n }\n }, [keydown, keyup])\n}\n"],"mappings":"AAAA;;AAAA,SAAAA,CAAA,IAAAC,EAAA;AAEA,SAASC,QAAQ,QAAQ;AACzB,SAASC,YAAY,QAAQ;AAC7B,SAASC,WAAW,EAAEC,SAAS,QAAQ;AAEvC;AACA;AAEA;AACA;AAEA;AACA;AACA;AACA,MAAMC,WAAA,GAAc,IAAIC,GAAA,CAAoB,EAAE;AAE9C,MAAMC,GAAA,GAAM;EACVC,OAAA,EAAS;EACTC,QAAA,EAAU;EACVC,WAAA,EAAa;EACbC,YAAA,EAAc;EACdC,QAAA,EAAU;EACVC,SAAA,EAAW;EACXC,MAAA,EAAQ;EACRC,QAAA,EAAU;EACVC,SAAA,EAAW;EACXC,MAAA,EAAQ;EACRC,OAAA,EAAS;EACTC,SAAA,EAAW;EACXC,UAAA,EAAY;AACd;AAEA,MAAMC,QAAA,GAAYC,GAAA;EAChB,OAAO,CAACf,GAAG,CA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