Просмотр исходного кода

Merge pull request #8001 from overleaf/ta-td-refactor-learned-words

Refactor Learned Words Class

GitOrigin-RevId: eae6558b5d4eb03b95df511d1cd9ba35b22bb344
Timothée Alby 4 лет назад
Родитель
Сommit
5a1ff8149d

+ 31 - 0
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<string>
+  private ignoredMisspellings: Set<string>
+
+  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()

+ 6 - 9
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)
         }