98 lines
No EOL
2.7 KiB
JavaScript
98 lines
No EOL
2.7 KiB
JavaScript
'use strict';
|
|
|
|
const electron = require('electron');
|
|
const EventEmitter = require('events');
|
|
const { getElectronMajorVersion } = require('./_common');
|
|
const ipcRenderer = require('./ipc');
|
|
|
|
if (getElectronMajorVersion() < 8) {
|
|
function setSpellCheckProvider(locale, autoCorrectWord, provider) {
|
|
const asyncProvider = {
|
|
spellCheck: (words, callback) => callback(words.filter(word => !provider.spellCheck(word)))
|
|
};
|
|
electron.webFrame.setSpellCheckProvider(locale, asyncProvider);
|
|
}
|
|
|
|
function replaceMisspelling(word) {
|
|
electron.remote.getCurrentWebContents().replaceMisspelling(word);
|
|
}
|
|
|
|
module.exports = {
|
|
setSpellCheckProvider,
|
|
replaceMisspelling
|
|
};
|
|
} else {
|
|
class Spellchecker extends EventEmitter {
|
|
|
|
constructor() {
|
|
super();
|
|
this._learnedWords = new Set();
|
|
|
|
this._handleSpellcheckData = (_, misspelledWord, dictionarySuggestions) => {
|
|
this.emit('spellcheck-result', misspelledWord, dictionarySuggestions);
|
|
};
|
|
|
|
ipcRenderer.on('SPELLCHECK_RESULT', this._handleSpellcheckData);
|
|
}
|
|
|
|
removeAllListeners() {
|
|
ipcRenderer.removeListener('SPELLCHECK_RESULT', this._handleSpellcheckData);
|
|
super.removeAllListeners();
|
|
}
|
|
|
|
_loadLearnedWords() {
|
|
if (!this._loadInitialLearnedWordsPromise) {
|
|
this._loadInitialLearnedWordsPromise = electron.remote.session.defaultSession.listWordsInSpellCheckerDictionary().then(dictionaryContents => {
|
|
this._learnedWords = new Set(dictionaryContents);
|
|
return Promise.resolve();
|
|
});
|
|
}
|
|
|
|
return this._loadInitialLearnedWordsPromise;
|
|
}
|
|
|
|
getAvailableDictionaries() {
|
|
const session = electron.remote.session.defaultSession;
|
|
return session.availableSpellCheckerLanguages;
|
|
}
|
|
|
|
setLocale(locale) {
|
|
let succeeded = true;
|
|
try {
|
|
electron.remote.session.defaultSession.setSpellCheckerLanguages([locale]);
|
|
} catch (_) {
|
|
succeeded = false;
|
|
}
|
|
|
|
return succeeded;
|
|
}
|
|
|
|
setLearnedWords(learnedWords) {
|
|
const session = electron.remote.session.defaultSession;
|
|
|
|
this._loadLearnedWords().then(() => {
|
|
this._learnedWords.forEach(word => {
|
|
if (!learnedWords.has(word)) {
|
|
session.removeWordFromSpellCheckerDictionary(word);
|
|
}
|
|
});
|
|
|
|
learnedWords.forEach(word => {
|
|
if (!this._learnedWords.has(word)) {
|
|
session.addWordToSpellCheckerDictionary(word);
|
|
}
|
|
});
|
|
|
|
this._learnedWords = new Set(learnedWords);
|
|
});
|
|
}
|
|
|
|
replaceMisspelling(correction) {
|
|
electron.remote.getCurrentWebContents().replaceMisspelling(correction);
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
Spellchecker: new Spellchecker()
|
|
};
|
|
} |