From 5a1ff8149ddea925c60ef98bd2c45e37c31f9db3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Alby?= Date: Tue, 24 May 2022 09:47:22 +0200 Subject: [PATCH] Merge pull request #8001 from overleaf/ta-td-refactor-learned-words Refactor Learned Words Class GitOrigin-RevId: eae6558b5d4eb03b95df511d1cd9ba35b22bb344 --- .../js/features/dictionary/ignored-words.ts | 31 +++++++++++++++++++ .../spell-check/SpellCheckManager.js | 15 ++++----- 2 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 services/web/frontend/js/features/dictionary/ignored-words.ts diff --git a/services/web/frontend/js/features/dictionary/ignored-words.ts b/services/web/frontend/js/features/dictionary/ignored-words.ts new file mode 100644 index 0000000000..3f3a846f28 --- /dev/null +++ b/services/web/frontend/js/features/dictionary/ignored-words.ts @@ -0,0 +1,31 @@ +import getMeta from '../../utils/meta' +import { IGNORED_MISSPELLINGS } from '../../ide/editor/directives/aceEditor/spell-check/IgnoredMisspellings' + +export class IgnoredWords { + public learnedWords: Set + private ignoredMisspellings: Set + + constructor() { + this.reset() + this.ignoredMisspellings = new Set(IGNORED_MISSPELLINGS) + } + + reset() { + this.learnedWords = new Set(getMeta('ol-learnedWords')) + } + + add(wordText) { + this.learnedWords.add(wordText) + window.dispatchEvent( + new CustomEvent('learnedWords:add', { detail: wordText }) + ) + } + + has(wordText) { + return ( + this.ignoredMisspellings.has(wordText) || this.learnedWords.has(wordText) + ) + } +} + +export default new IgnoredWords() diff --git a/services/web/frontend/js/ide/editor/directives/aceEditor/spell-check/SpellCheckManager.js b/services/web/frontend/js/ide/editor/directives/aceEditor/spell-check/SpellCheckManager.js index f989b3395b..bba602de62 100644 --- a/services/web/frontend/js/ide/editor/directives/aceEditor/spell-check/SpellCheckManager.js +++ b/services/web/frontend/js/ide/editor/directives/aceEditor/spell-check/SpellCheckManager.js @@ -1,5 +1,4 @@ -import getMeta from '../../../../../utils/meta' -import { IGNORED_MISSPELLINGS } from './IgnoredMisspellings' +import ignoredWords from '../../../../../features/dictionary/ignored-words' // eslint-disable-next-line prefer-regex-literals const BLACKLISTED_COMMAND_REGEX = new RegExp( @@ -62,10 +61,6 @@ class SpellCheckManager { this.selectedHighlightContents = null - this.ignoredMisspellings = new Set( - IGNORED_MISSPELLINGS.concat(getMeta('ol-learnedWords')) - ) - $(document).on('click', e => { // There is a bug (?) in Safari when ctrl-clicking an element, and the // the contextmenu event is preventDefault-ed. In this case, the @@ -198,7 +193,7 @@ class SpellCheckManager { this.adapter.highlightedWordManager.removeWord(highlight.word) const language = this.$scope.spellCheckLanguage this.cache.put(`${language}:${highlight.word}`, true) - this.ignoredMisspellings.add(highlight.word) + ignoredWords.add(highlight.word) } markLinesAsUpdated(change) { @@ -341,7 +336,9 @@ class SpellCheckManager { apiRequest(endpoint, data, callback) { if (callback == null) { callback = function (error, result) { - console.error(error) + if (error) { + console.error(error) + } } } data.token = window.user.id @@ -384,7 +381,7 @@ class SpellCheckManager { if (word[word.length - 1] === "'") { word = word.slice(0, -1) } - if (!this.ignoredMisspellings.has(word)) { + if (!ignoredWords.has(word)) { positions.push({ row: rowIdx, column: result.index }) words.push(word) }