diff --git a/assets/js/dashboard/datepicker.js b/assets/js/dashboard/datepicker.js index 9c9ad9182..32a81ed8f 100644 --- a/assets/js/dashboard/datepicker.js +++ b/assets/js/dashboard/datepicker.js @@ -22,6 +22,7 @@ import { isAfter, } from "./util/date"; import { navigateToQuery, QueryLink, QueryButton } from "./query"; +import { shouldIgnoreKeypress } from "./keybinding.js" function renderArrow(query, site, period, prevDate, nextDate) { const insertionDate = parseUTCDate(site.statsBegin); @@ -133,8 +134,7 @@ class DatePicker extends React.Component { handleKeydown(e) { const { query, history } = this.props; - if (e.target.tagName === 'INPUT') return true; - if (e.ctrlKey || e.metaKey || e.altKey || e.isComposing || e.keyCode === 229) return true; + if (shouldIgnoreKeypress(e)) return true const newSearch = { period: false, diff --git a/assets/js/dashboard/keybinding.js b/assets/js/dashboard/keybinding.js new file mode 100644 index 000000000..5654a170f --- /dev/null +++ b/assets/js/dashboard/keybinding.js @@ -0,0 +1,35 @@ +/** + * Returns whether a keydown or keyup event should be ignored or not. + * + * Keybindings are ignored when a modifier key is pressed, for example, if the + * keybinding is , but the user pressed or , the event + * should be discarded. + * + * Another case for ignoring a keybinding, is when the user is typing into a + * form, and presses the keybinding. For example, if the keybinding is

and + * the user types , the event should also be discarded. + * + * @param {*} event - Captured HTML DOM event + * @return {boolean} Whether the event should be ignored or not. + * + */ +export function shouldIgnoreKeypress(event) { + const modifierPressed = event.ctrlKey || event.metaKey || event.altKey || event.keyCode == 229 + const isTyping = event.isComposing || event.target.tagName == "INPUT" || event.target.tagName == "TEXTAREA" + + return modifierPressed || isTyping +} + + +/** + * Returns whether the given keybinding has been pressed and should be + * processed. Events can be ignored based on `shouldIgnoreKeypress(event)`. + * + * @param {string} keybinding - The target key to checked, e.g. `"i"`. + * @return {boolean} Whether the event should be processed or not. + * + */ +export function isKeyPressed(event, keybinding) { + const keyPressed = event.key.toLowerCase() == keybinding.toLowerCase() + return keyPressed && !shouldIgnoreKeypress(event) +} diff --git a/assets/js/dashboard/stats/graph/interval-picker.js b/assets/js/dashboard/stats/graph/interval-picker.js index 890ce2586..1286b59af 100644 --- a/assets/js/dashboard/stats/graph/interval-picker.js +++ b/assets/js/dashboard/stats/graph/interval-picker.js @@ -1,8 +1,9 @@ import { Menu, Transition } from '@headlessui/react'; import { ChevronDownIcon } from '@heroicons/react/20/solid' -import React, { Fragment } from 'react'; +import React, { Fragment, useCallback, useEffect } from 'react'; import classNames from 'classnames' import * as storage from '../../util/storage' +import { isKeyPressed } from '../../keybinding.js' export const INTERVAL_LABELS = { 'minute': 'Minutes', @@ -20,6 +21,17 @@ export const storeInterval = function(period, domain, interval) { storage.setItem(`interval__${period}__${domain}`, interval) } +function subscribeKeybinding(element) { + const handleKeyPress = useCallback((event) => { + if (isKeyPressed(event, "i")) element.current?.click() + }, []) + + useEffect(() => { + document.addEventListener('keydown', handleKeyPress) + return () => document.removeEventListener('keydown', handleKeyPress) + }, [handleKeyPress]) +} + function DropdownItem({ option, currentInterval, updateInterval }) { return ( updateInterval(option)} key={option} disabled={option == currentInterval}> @@ -39,29 +51,35 @@ function DropdownItem({ option, currentInterval, updateInterval }) { export function IntervalPicker({ graphData, query, site, updateInterval }) { if (query.period == 'realtime') return null + const menuElement = React.useRef(null) + subscribeKeybinding(menuElement) + const currentInterval = graphData?.interval const options = site.validIntervalsByPeriod[query.period] return (

- - { INTERVAL_LABELS[currentInterval] } - + {({ open }) => ( + <> + + { INTERVAL_LABELS[currentInterval] } + - - - { options.map((option) => DropdownItem({ option, currentInterval, updateInterval })) } - - + + + { options.map((option) => DropdownItem({ option, currentInterval, updateInterval })) } + + + + )} ) }