From cffa9c1a28f5c2c9862876e28e02a6466c8668fe Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Mon, 13 Jan 2025 10:59:35 +0000 Subject: [PATCH] Improve spell check when dictionary is edited (#22635) GitOrigin-RevId: 20d36cb987d014809423240a46c7c577781dfde6 --- .../components/dictionary-modal-content.tsx | 26 ++-- .../js/features/dictionary/ignored-words.ts | 42 +----- .../extensions/spelling/backend.ts | 13 -- .../extensions/spelling/cache.ts | 27 ++-- .../extensions/spelling/context-menu.tsx | 22 ++-- .../extensions/spelling/ignored-words.ts | 31 ----- .../extensions/spelling/index.ts | 75 +++-------- .../extensions/spelling/learned-words.ts | 24 ++++ .../extensions/spelling/misspelled-words.ts | 13 +- .../extensions/spelling/spellchecker.ts | 121 +++++++++--------- .../spelling/spelling-suggestions.tsx | 40 +++--- .../hooks/use-codemirror-scope.ts | 40 +----- .../source-editor/hooks/use-hunspell.ts | 7 +- .../dictionary-modal-content.spec.jsx | 30 +++-- .../extensions/spelling/cache.test.ts | 24 ++-- .../extensions/spelling/spellchecker.test.ts | 58 +++------ 16 files changed, 218 insertions(+), 375 deletions(-) delete mode 100644 services/web/frontend/js/features/source-editor/extensions/spelling/backend.ts delete mode 100644 services/web/frontend/js/features/source-editor/extensions/spelling/ignored-words.ts create mode 100644 services/web/frontend/js/features/source-editor/extensions/spelling/learned-words.ts diff --git a/services/web/frontend/js/features/dictionary/components/dictionary-modal-content.tsx b/services/web/frontend/js/features/dictionary/components/dictionary-modal-content.tsx index 0a0fabf242..90369d2467 100644 --- a/services/web/frontend/js/features/dictionary/components/dictionary-modal-content.tsx +++ b/services/web/frontend/js/features/dictionary/components/dictionary-modal-content.tsx @@ -2,7 +2,6 @@ import { useCallback, useState } from 'react' import { useTranslation } from 'react-i18next' import useAsync from '../../../shared/hooks/use-async' import { postJSON } from '../../../infrastructure/fetch-json' -import ignoredWords from '../ignored-words' import { debugConsole } from '@/utils/debugging' import { OLModalBody, @@ -15,6 +14,7 @@ import OLNotification from '@/features/ui/components/ol/ol-notification' import OLButton from '@/features/ui/components/ol/ol-button' import OLIconButton from '@/features/ui/components/ol/ol-icon-button' import { bsVersion } from '@/features/utils/bootstrap-5' +import { learnedWords as initialLearnedWords } from '@/features/source-editor/extensions/spelling/learned-words' type DictionaryModalContentProps = { handleHide: () => void @@ -26,22 +26,24 @@ export default function DictionaryModalContent({ handleHide, }: DictionaryModalContentProps) { const { t } = useTranslation() - const [learnedWords, setLearnedWords] = useState(ignoredWords.learnedWords) + + const [learnedWords, setLearnedWords] = useState>( + initialLearnedWords.global + ) const { isError, runAsync } = useAsync() const handleRemove = useCallback( word => { - runAsync( - postJSON('/spelling/unlearn', { - body: { - word, - }, - }) - ) + runAsync(postJSON('/spelling/unlearn', { body: { word } })) .then(() => { - ignoredWords.remove(word) - setLearnedWords(new Set(ignoredWords.learnedWords)) + setLearnedWords(value => { + value.delete(word) + return new Set(value) + }) + window.dispatchEvent( + new CustomEvent('editor:remove-learned-word', { detail: word }) + ) }) .catch(debugConsole.error) }, @@ -62,7 +64,7 @@ export default function DictionaryModalContent({ /> ) : null} - {learnedWords?.size > 0 ? ( + {learnedWords.size > 0 ? (