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

Refactor Learned Words Class

GitOrigin-RevId: eae6558b5d4eb03b95df511d1cd9ba35b22bb344
This commit is contained in:
Timothée Alby 2022-05-24 09:47:22 +02:00 committed by Copybot
parent 3580ec6db3
commit 5a1ff8149d
2 changed files with 37 additions and 9 deletions

View file

@ -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()

View file

@ -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,9 +336,11 @@ class SpellCheckManager {
apiRequest(endpoint, data, callback) {
if (callback == null) {
callback = function (error, result) {
if (error) {
console.error(error)
}
}
}
data.token = window.user.id
data._csrf = window.csrfToken
// use angular timeout option to cancel request if doc is changed
@ -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)
}