2015-12-11 12:37:54 +08:00
|
|
|
path = require('path')
|
|
|
|
|
2015-12-10 07:10:55 +08:00
|
|
|
spellchecker = require('spellchecker')
|
|
|
|
|
|
|
|
class NylasSpellchecker
|
|
|
|
constructor: ->
|
|
|
|
@languageAvailable = false
|
|
|
|
|
|
|
|
lang = navigator.language
|
|
|
|
@setLanguage(lang)
|
|
|
|
|
|
|
|
isLanguageAvailable: (lang) =>
|
|
|
|
return false unless lang
|
|
|
|
dicts = @getAvailableDictionaries()
|
|
|
|
return (lang in dicts) or (lang.split(/[-_]/)[0] in dicts)
|
|
|
|
|
|
|
|
isSpelledCorrectly: (args...) => not @isMisspelled(args...)
|
|
|
|
|
2015-12-12 04:35:10 +08:00
|
|
|
setLanguage: (lang="", dict="") ->
|
2015-12-10 07:10:55 +08:00
|
|
|
@languageAvailable = @isLanguageAvailable(lang)
|
2015-12-11 13:26:04 +08:00
|
|
|
if @languageAvailable
|
2015-12-10 07:10:55 +08:00
|
|
|
spellCheck = @isSpelledCorrectly
|
|
|
|
else
|
|
|
|
spellCheck = -> true
|
|
|
|
|
2015-12-10 07:32:54 +08:00
|
|
|
# Need to default to a valid language so web-frame
|
|
|
|
# `setSpellCheckProvder` gets a non empty string
|
|
|
|
if lang.length is 0 then lang = "en-US"
|
|
|
|
|
2015-12-10 07:10:55 +08:00
|
|
|
@_setWebframeSpellchecker(lang, spellCheck)
|
2015-12-12 04:35:10 +08:00
|
|
|
|
|
|
|
if process.platform is "linux"
|
|
|
|
dictionaryPath = @_getHunspellDictionary()
|
|
|
|
else
|
|
|
|
# On Mac we defer to NSSpellChecker
|
|
|
|
# On Windows we use the Windows Spell Check API
|
|
|
|
#
|
|
|
|
# Both of those automatically provide a set of dictionaries based on
|
|
|
|
# the language string.
|
|
|
|
#
|
|
|
|
# On Windows 10 you can see the dictionaries that are available by
|
|
|
|
# looking in: C:\Users\YourName\AppData\Roaming\Microsoft\Spelling
|
|
|
|
#
|
|
|
|
# The `dictionaryPath` parameter is ignored on Mac & Windows by
|
|
|
|
# node-spellchecker
|
|
|
|
dictionaryPath = ""
|
|
|
|
|
|
|
|
if dict.length is 0 then dict = dictionaryPath
|
|
|
|
|
2015-12-11 12:37:54 +08:00
|
|
|
spellchecker.setDictionary(lang, dict)
|
2015-12-10 07:10:55 +08:00
|
|
|
|
|
|
|
# Separate method for testing
|
|
|
|
_setWebframeSpellchecker: (lang, spellCheck) ->
|
|
|
|
require('web-frame').setSpellCheckProvider(lang, false, {spellCheck})
|
|
|
|
|
2015-12-12 04:35:10 +08:00
|
|
|
# node-spellchecker's method for resolving the builtin hunspell
|
|
|
|
# dictionaries for Linux (From
|
|
|
|
# https://github.com/atom/node-spellchecker/blob/master/lib/spellchecker.js#L50-L61)
|
|
|
|
_getHunspellDictionary: ->
|
2015-12-11 12:37:54 +08:00
|
|
|
dict = path.join(require.resolve('spellchecker'), '..', '..', 'vendor', 'hunspell_dictionaries')
|
|
|
|
try
|
|
|
|
# HACK: Special case being in an asar archive
|
2015-12-11 12:51:40 +08:00
|
|
|
unpacked = dict.replace('.asar' + path.sep, '.asar.unpacked' + path.sep)
|
2015-12-11 12:37:54 +08:00
|
|
|
if require('fs').statSyncNoException(unpacked)
|
2015-12-11 12:51:40 +08:00
|
|
|
dict = unpacked
|
2015-12-11 12:37:54 +08:00
|
|
|
catch
|
|
|
|
|
|
|
|
dict
|
|
|
|
|
2015-12-10 07:10:55 +08:00
|
|
|
#### spellchecker methods ####
|
2015-12-11 12:37:54 +08:00
|
|
|
setDictionary: (lang, dict) => @setLanguage(lang, dict)
|
2015-12-10 07:10:55 +08:00
|
|
|
|
|
|
|
add: spellchecker.add
|
|
|
|
|
|
|
|
isMisspelled: (text) =>
|
2015-12-11 13:26:04 +08:00
|
|
|
if @languageAvailable
|
2015-12-11 12:37:54 +08:00
|
|
|
spellchecker.isMisspelled(text)
|
|
|
|
else
|
|
|
|
return false
|
2015-12-10 07:10:55 +08:00
|
|
|
|
|
|
|
getAvailableDictionaries: ->
|
2015-12-11 13:26:04 +08:00
|
|
|
if process.platform is 'linux'
|
|
|
|
arr = spellchecker.getAvailableDictionaries()
|
|
|
|
if not "en_US" in arr
|
|
|
|
arr.push('en_US') # Installed by default in node-spellchecker's vendor directory
|
|
|
|
arr
|
|
|
|
else
|
|
|
|
spellchecker.getAvailableDictionaries() ? []
|
2015-12-10 07:10:55 +08:00
|
|
|
|
|
|
|
getCorrectionsForMisspelling: (args...) =>
|
2015-12-11 13:26:04 +08:00
|
|
|
if @languageAvailable
|
2015-12-10 07:10:55 +08:00
|
|
|
spellchecker.getCorrectionsForMisspelling(args...)
|
|
|
|
else return []
|
|
|
|
|
|
|
|
Spellchecker: => @
|
|
|
|
|
|
|
|
module.exports = new NylasSpellchecker
|