spelling-suggestions.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import {
  2. FC,
  3. MouseEventHandler,
  4. useEffect,
  5. useMemo,
  6. useRef,
  7. useState,
  8. } from 'react'
  9. import { SpellChecker, Word } from './spellchecker'
  10. import { useTranslation } from 'react-i18next'
  11. import getMeta from '@/utils/meta'
  12. import classnames from 'classnames'
  13. import { sendMB } from '@/infrastructure/event-tracking'
  14. import { SpellingSuggestionsLanguage } from './spelling-suggestions-language'
  15. import { captureException } from '@/infrastructure/error-reporter'
  16. import { debugConsole } from '@/utils/debugging'
  17. import { SpellCheckLanguage } from '../../../../../../types/project-settings'
  18. import {
  19. Dropdown,
  20. DropdownDivider,
  21. DropdownItem,
  22. DropdownMenu,
  23. } from '@/shared/components/dropdown/dropdown-menu'
  24. import DropdownListItem from '@/shared/components/dropdown/dropdown-list-item'
  25. const ITEMS_TO_SHOW = 8
  26. type SpellingSuggestionsProps = {
  27. word: Word
  28. spellCheckLanguage?: string
  29. spellChecker?: SpellChecker | null
  30. handleClose: () => void
  31. handleLearnWord: () => void
  32. handleCorrectWord: (text: string) => void
  33. }
  34. export const SpellingSuggestions: FC<SpellingSuggestionsProps> = ({
  35. word,
  36. spellCheckLanguage,
  37. spellChecker,
  38. handleClose,
  39. handleLearnWord,
  40. handleCorrectWord,
  41. }) => {
  42. const [suggestions, setSuggestions] = useState<string[]>([])
  43. const [waiting, setWaiting] = useState(true)
  44. useEffect(() => {
  45. spellChecker
  46. ?.suggest(word.text)
  47. .then(result => {
  48. setSuggestions(result.suggestions.slice(0, ITEMS_TO_SHOW))
  49. setWaiting(false)
  50. sendMB('spelling-suggestion-shown', {
  51. language: spellCheckLanguage,
  52. count: result.suggestions.length,
  53. // word: transaction.state.sliceDoc(mark.from, mark.to),
  54. })
  55. })
  56. .catch(error => {
  57. captureException(error, {
  58. tags: { ol_spell_check_language: spellCheckLanguage },
  59. })
  60. debugConsole.error(error)
  61. })
  62. }, [word, spellChecker, spellCheckLanguage])
  63. const language = useMemo(() => {
  64. if (spellCheckLanguage) {
  65. return (getMeta('ol-languages') ?? []).find(
  66. item => item.code === spellCheckLanguage
  67. )
  68. }
  69. }, [spellCheckLanguage])
  70. if (!language) {
  71. return null
  72. }
  73. const innerProps = {
  74. suggestions,
  75. waiting,
  76. handleClose,
  77. handleCorrectWord,
  78. handleLearnWord,
  79. language,
  80. }
  81. return <B5SpellingSuggestions {...innerProps} />
  82. }
  83. type SpellingSuggestionsInnerProps = {
  84. suggestions: string[]
  85. waiting: boolean
  86. handleClose: () => void
  87. handleCorrectWord: (text: string) => void
  88. handleLearnWord: () => void
  89. language: SpellCheckLanguage
  90. }
  91. const B5SpellingSuggestions: FC<SpellingSuggestionsInnerProps> = ({
  92. suggestions,
  93. waiting,
  94. language,
  95. handleClose,
  96. handleCorrectWord,
  97. handleLearnWord,
  98. }) => {
  99. const { t } = useTranslation()
  100. const menuRef = useRef<any>(null)
  101. // Handle closing the menu when it loses focus, e.g. click outside the editor
  102. const onToggle = (show: boolean) => {
  103. if (!show) {
  104. handleClose()
  105. }
  106. }
  107. useEffect(() => {
  108. if (!waiting) {
  109. menuRef.current?.focus()
  110. }
  111. }, [waiting])
  112. return (
  113. <Dropdown onToggle={onToggle} show={!waiting}>
  114. <DropdownMenu
  115. className={classnames('dropdown-menu-unpositioned', {
  116. hidden: waiting,
  117. })}
  118. ref={menuRef}
  119. show={!waiting}
  120. tabIndex={0}
  121. onKeyDown={event => {
  122. switch (event.code) {
  123. case 'Escape':
  124. case 'Tab':
  125. event.preventDefault()
  126. handleClose()
  127. break
  128. }
  129. }}
  130. >
  131. {Array.isArray(suggestions) &&
  132. suggestions.map(suggestion => (
  133. <SpellingListItem
  134. key={suggestion}
  135. content={suggestion}
  136. handleClick={event => {
  137. event.preventDefault()
  138. handleCorrectWord(suggestion)
  139. }}
  140. />
  141. ))}
  142. {suggestions?.length > 0 && <DropdownDivider />}
  143. <SpellingListItem
  144. content={t('add_to_dictionary')}
  145. handleClick={event => {
  146. event.preventDefault()
  147. handleLearnWord()
  148. }}
  149. />
  150. <DropdownDivider />
  151. <SpellingSuggestionsLanguage
  152. language={language}
  153. handleClose={handleClose}
  154. />
  155. </DropdownMenu>
  156. </Dropdown>
  157. )
  158. }
  159. const SpellingListItem: FC<{
  160. content: string
  161. handleClick: MouseEventHandler<HTMLButtonElement>
  162. }> = ({ content, handleClick }) => (
  163. <DropdownListItem>
  164. <DropdownItem onClick={handleClick}>{content}</DropdownItem>
  165. </DropdownListItem>
  166. )