Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

on-blur-or-focus-block.tsx 1.7KB

6 месяцев назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import type { FC } from 'react'
  2. import { useEffect, useRef } from 'react'
  3. import {
  4. BLUR_COMMAND,
  5. COMMAND_PRIORITY_EDITOR,
  6. FOCUS_COMMAND,
  7. KEY_ESCAPE_COMMAND,
  8. } from 'lexical'
  9. import { mergeRegister } from '@lexical/utils'
  10. import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
  11. import { CLEAR_HIDE_MENU_TIMEOUT } from './workflow-variable-block'
  12. type OnBlurBlockProps = {
  13. onBlur?: () => void
  14. onFocus?: () => void
  15. }
  16. const OnBlurBlock: FC<OnBlurBlockProps> = ({
  17. onBlur,
  18. onFocus,
  19. }) => {
  20. const [editor] = useLexicalComposerContext()
  21. const ref = useRef<any>(null)
  22. useEffect(() => {
  23. return mergeRegister(
  24. editor.registerCommand(
  25. CLEAR_HIDE_MENU_TIMEOUT,
  26. () => {
  27. if (ref.current) {
  28. clearTimeout(ref.current)
  29. ref.current = null
  30. }
  31. return true
  32. },
  33. COMMAND_PRIORITY_EDITOR,
  34. ),
  35. editor.registerCommand(
  36. BLUR_COMMAND,
  37. (event) => {
  38. // Check if the clicked target element is var-search-input
  39. const target = event?.relatedTarget as HTMLElement
  40. if (!target?.classList?.contains('var-search-input')) {
  41. ref.current = setTimeout(() => {
  42. editor.dispatchCommand(KEY_ESCAPE_COMMAND, new KeyboardEvent('keydown', { key: 'Escape' }))
  43. }, 200)
  44. if (onBlur)
  45. onBlur()
  46. }
  47. return true
  48. },
  49. COMMAND_PRIORITY_EDITOR,
  50. ),
  51. editor.registerCommand(
  52. FOCUS_COMMAND,
  53. () => {
  54. if (onFocus)
  55. onFocus()
  56. return true
  57. },
  58. COMMAND_PRIORITY_EDITOR,
  59. ),
  60. )
  61. }, [editor, onBlur, onFocus])
  62. return null
  63. }
  64. export default OnBlurBlock