Skip to content

Commit 099f46d

Browse files
committed
fix: fix automatic spellcheck language detection
1 parent dfa9471 commit 099f46d

File tree

5 files changed

+46
-32
lines changed

5 files changed

+46
-32
lines changed

src/electron/ipc-api/cld.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
import { loadModule } from 'cld3-asm';
12
import { ipcMain } from 'electron';
2-
import cld from 'cld';
33

44
const debug = require('debug')('Franz:ipcApi:cld');
55

66
export default async () => {
7+
const cldFactory = await loadModule();
8+
const cld = cldFactory.create(0, 1000);
79
ipcMain.handle('detect-language', async (event, { sample }) => {
810
try {
9-
const result = await cld.detect(sample);
10-
debug('Checking language', 'probability', result.languages);
11-
if (result.reliable) {
12-
debug('Language detected reliably, setting spellchecker language to', result.languages[0].code);
11+
const result = cld.findLanguage(sample);
12+
debug('Checking language', result.language);
13+
if (result.is_reliable) {
14+
debug('Language detected reliably, setting spellchecker language to', result.language);
1315

14-
return result.languages[0].code;
16+
return result.language;
1517
}
1618
} catch (e) {
1719
console.error(e);

src/i18n/languages.js

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export const SPELLCHECKER_LOCALES = {
7575
da: 'Dansk',
7676
de: 'Deutsch',
7777
el: 'ελληνικά (Greek)',
78+
en: 'English',
7879
'en-AU': 'English (Australia)',
7980
'en-CA': 'English (Canada)',
8081
'en-GB': 'English (Great Britain)',

src/models/ServiceBrowserView.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import {
22
BrowserView, BrowserWindow, BrowserWindowConstructorOptions, ipcMain, Menu, Rectangle, shell,
33
} from 'electron';
44
import ms from 'ms';
5-
import {
6-
REQUEST_SERVICE_SPELLCHECKING_LANGUAGE, SERVICE_SPELLCHECKING_LANGUAGE, UPDATE_SERVICE_STATE, UPDATE_SPELLCHECKING_LANGUAGE,
7-
} from '../ipcChannels';
8-
import Settings from '../electron/Settings';
95
import { TAB_BAR_WIDTH, TODOS_RECIPE_ID } from '../config';
10-
import RecipeModel from './Recipe';
116
import { buildMenuTpl } from '../electron/serviceContextMenuTemplate';
7+
import Settings from '../electron/Settings';
8+
import { isMac } from '../environment';
129
import { IPC } from '../features/todos/constants';
1310
import { getRecipeDirectory, loadRecipeConfig } from '../helpers/recipe-helpers';
14-
import { isMac } from '../environment';
1511
import { isValidExternalURL } from '../helpers/url-helpers';
1612
import userAgent from '../helpers/userAgent-helpers';
13+
import {
14+
REQUEST_SERVICE_SPELLCHECKING_LANGUAGE, SERVICE_SPELLCHECKING_LANGUAGE, UPDATE_SERVICE_STATE, UPDATE_SPELLCHECKING_LANGUAGE,
15+
} from '../ipcChannels';
16+
import RecipeModel from './Recipe';
1717

1818
const debug = require('debug')('Franz:Models:ServiceBrowserView');
1919

@@ -104,6 +104,7 @@ export class ServiceBrowserView {
104104
preload: recipeId !== TODOS_RECIPE_ID ? `${__dirname}/../webview/recipe.js` : `${__dirname}/../features/todos/preload.js`,
105105
contextIsolation: false,
106106
spellcheck: this.state.isSpellcheckerEnabled,
107+
sandbox: false,
107108
},
108109
});
109110
}
@@ -301,6 +302,7 @@ export class ServiceBrowserView {
301302
};
302303

303304
this.webContents.session.setSpellCheckerEnabled(this.state.isSpellcheckerEnabled);
305+
this.webContents.session.setSpellCheckerLanguages([this.state.spellcheckerLanguage]);
304306
}
305307

306308
remove() {

src/stores/ServicesStore.js

+19-12
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
1+
import { app, webContents } from '@electron/remote';
2+
import { debounce, remove } from 'lodash';
13
import {
24
action,
3-
reaction,
45
computed,
56
observable,
7+
reaction,
68
toJS,
79
} from 'mobx';
8-
import { debounce, remove } from 'lodash';
910
import ms from 'ms';
10-
import { app, webContents } from '@electron/remote';
1111

1212
import { ipcRenderer } from 'electron';
13-
import Store from './lib/Store';
14-
import Request from './lib/Request';
15-
import CachedRequest from './lib/CachedRequest';
16-
import { matchRoute } from '../helpers/routing-helpers';
17-
import { gaEvent } from '../lib/analytics';
18-
import { workspaceStore } from '../features/workspaces';
1913
import { serviceLimitStore } from '../features/serviceLimit';
20-
import { RESTRICTION_TYPES } from '../models/Service';
2114
import { TODOS_RECIPE_ID } from '../features/todos';
15+
import { workspaceStore } from '../features/workspaces';
16+
import { matchRoute } from '../helpers/routing-helpers';
2217
import { SPELLCHECKER_LOCALES } from '../i18n/languages';
2318
import {
24-
OPEN_SERVICE_DEV_TOOLS, REQUEST_SERVICE_SPELLCHECKING_LANGUAGE, SERVICE_SPELLCHECKING_LANGUAGE, UPDATE_SPELLCHECKING_LANGUAGE, NAVIGATE_SERVICE_TO, RELOAD_SERVICE, HIDE_ALL_SERVICES, ACTIVATE_NEXT_SERVICE, ACTIVATE_PREVIOUS_SERVICE, ACTIVATE_SERVICE, UPDATE_SERVICE_STATE,
19+
ACTIVATE_NEXT_SERVICE, ACTIVATE_PREVIOUS_SERVICE, ACTIVATE_SERVICE,
20+
HIDE_ALL_SERVICES,
21+
NAVIGATE_SERVICE_TO,
22+
OPEN_SERVICE_DEV_TOOLS,
23+
RELOAD_SERVICE,
24+
REQUEST_SERVICE_SPELLCHECKING_LANGUAGE, SERVICE_SPELLCHECKING_LANGUAGE,
25+
UPDATE_SERVICE_STATE,
26+
UPDATE_SPELLCHECKING_LANGUAGE,
2527
} from '../ipcChannels';
28+
import { gaEvent } from '../lib/analytics';
29+
import { RESTRICTION_TYPES } from '../models/Service';
30+
import CachedRequest from './lib/CachedRequest';
31+
import Request from './lib/Request';
32+
import Store from './lib/Store';
2633

2734
const debug = require('debug')('Franz:ServiceStore');
2835

@@ -895,7 +902,7 @@ export default class ServicesStore extends Store {
895902
}
896903

897904
_handleSpellcheckerLocale() {
898-
ipcRenderer.on(UPDATE_SPELLCHECKING_LANGUAGE, (event, { serviceId, locale }) => {
905+
ipcRenderer.on(UPDATE_SPELLCHECKING_LANGUAGE, (event, serviceId, { locale }) => {
899906
if (!serviceId) {
900907
console.warn('Did not receive service');
901908
} else {

src/webview/recipe.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import { ipcRenderer } from 'electron';
2-
import path from 'path';
3-
import { autorun, observable } from 'mobx';
42
import { debounce } from 'lodash';
3+
import { autorun, observable } from 'mobx';
4+
import path from 'path';
55

66
import RecipeWebview from './lib/RecipeWebview';
77

8-
import { getSpellcheckerLocaleByFuzzyIdentifier } from './spellchecker';
98
import { injectDarkModeStyle, isDarkModeStyleInjected, removeDarkModeStyle } from './darkmode';
10-
import './notifications';
119
import './desktopCapturer';
10+
import './notifications';
11+
import { getSpellcheckerLocaleByFuzzyIdentifier } from './spellchecker';
1212

13+
// import { DEFAULT_WEB_CONTENTS_ID } from '../config';
1314
import { DEFAULT_APP_SETTINGS_VANILLA } from '../configVanilla';
1415
import { UPDATE_SPELLCHECKING_LANGUAGE } from '../ipcChannels';
1516

17+
// const DEFAULT_WEB_CONTENTS_ID = 1;
18+
1619
const debug = require('debug')('Franz:Plugin');
1720

1821
window.FranzAPI = {};
@@ -40,8 +43,6 @@ class RecipeController {
4043
this.initialize();
4144
}
4245

43-
cldIdentifier = null;
44-
4546
async initialize() {
4647
Object.keys(this.ipcEvents).forEach((channel) => {
4748
ipcRenderer.on(channel, (...args) => {
@@ -54,6 +55,8 @@ class RecipeController {
5455
setTimeout(() => ipcRenderer.send('hello'), 100);
5556

5657
autorun(() => this.update());
58+
59+
this.automaticLanguageDetection();
5760
}
5861

5962
loadRecipeModule(event, config, recipe) {
@@ -111,11 +114,10 @@ class RecipeController {
111114

112115
debug('Detecting language for', value);
113116
const locale = await ipcRenderer.invoke('detect-language', { sample: value });
114-
115117
const spellcheckerLocale = getSpellcheckerLocaleByFuzzyIdentifier(locale);
116118
debug('Language detected reliably, setting spellchecker language to', spellcheckerLocale);
117119
if (spellcheckerLocale) {
118-
ipcRenderer.invoke(UPDATE_SPELLCHECKING_LANGUAGE, { locale: spellcheckerLocale });
120+
ipcRenderer.send(UPDATE_SPELLCHECKING_LANGUAGE, { locale: spellcheckerLocale });
119121
}
120122
}, 225));
121123
}

0 commit comments

Comments
 (0)