shortcuts.js 958 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { useCallback } from 'react'
  2. import { useDetachCompileContext } from '../../../shared/context/detach-compile-context'
  3. import PropTypes from 'prop-types'
  4. export const Shortcuts = ({ children }) => {
  5. const { startCompile } = useDetachCompileContext()
  6. const handleKeyDown = useCallback(
  7. event => {
  8. if (event.metaKey) {
  9. switch (event.key) {
  10. case 's':
  11. case 'Enter':
  12. event.preventDefault()
  13. startCompile()
  14. break
  15. }
  16. } else if (event.ctrlKey) {
  17. switch (event.key) {
  18. case '.':
  19. event.preventDefault()
  20. startCompile()
  21. break
  22. }
  23. }
  24. },
  25. [startCompile]
  26. )
  27. return (
  28. // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
  29. <div onKeyDown={handleKeyDown} role="tabpanel" tabIndex="0">
  30. {children}
  31. </div>
  32. )
  33. }
  34. Shortcuts.propTypes = {
  35. children: PropTypes.node,
  36. }