From 099f46deb383566edce261d622b7f308e7ed8e71 Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Sun, 13 Aug 2023 15:02:04 +0200 Subject: [PATCH] fix: fix automatic spellcheck language detection --- src/electron/ipc-api/cld.js | 14 ++++++++------ src/i18n/languages.js | 1 + src/models/ServiceBrowserView.ts | 14 ++++++++------ src/stores/ServicesStore.js | 31 +++++++++++++++++++------------ src/webview/recipe.js | 18 ++++++++++-------- 5 files changed, 46 insertions(+), 32 deletions(-) diff --git a/src/electron/ipc-api/cld.js b/src/electron/ipc-api/cld.js index 23f18aa94..74e4ad9f8 100644 --- a/src/electron/ipc-api/cld.js +++ b/src/electron/ipc-api/cld.js @@ -1,17 +1,19 @@ +import { loadModule } from 'cld3-asm'; import { ipcMain } from 'electron'; -import cld from 'cld'; const debug = require('debug')('Franz:ipcApi:cld'); export default async () => { + const cldFactory = await loadModule(); + const cld = cldFactory.create(0, 1000); ipcMain.handle('detect-language', async (event, { sample }) => { try { - const result = await cld.detect(sample); - debug('Checking language', 'probability', result.languages); - if (result.reliable) { - debug('Language detected reliably, setting spellchecker language to', result.languages[0].code); + const result = cld.findLanguage(sample); + debug('Checking language', result.language); + if (result.is_reliable) { + debug('Language detected reliably, setting spellchecker language to', result.language); - return result.languages[0].code; + return result.language; } } catch (e) { console.error(e); diff --git a/src/i18n/languages.js b/src/i18n/languages.js index d3e94d93c..a5ef99d8f 100644 --- a/src/i18n/languages.js +++ b/src/i18n/languages.js @@ -75,6 +75,7 @@ export const SPELLCHECKER_LOCALES = { da: 'Dansk', de: 'Deutsch', el: 'ελληνικά (Greek)', + en: 'English', 'en-AU': 'English (Australia)', 'en-CA': 'English (Canada)', 'en-GB': 'English (Great Britain)', diff --git a/src/models/ServiceBrowserView.ts b/src/models/ServiceBrowserView.ts index be3591841..57dab1465 100644 --- a/src/models/ServiceBrowserView.ts +++ b/src/models/ServiceBrowserView.ts @@ -2,18 +2,18 @@ import { BrowserView, BrowserWindow, BrowserWindowConstructorOptions, ipcMain, Menu, Rectangle, shell, } from 'electron'; import ms from 'ms'; -import { - REQUEST_SERVICE_SPELLCHECKING_LANGUAGE, SERVICE_SPELLCHECKING_LANGUAGE, UPDATE_SERVICE_STATE, UPDATE_SPELLCHECKING_LANGUAGE, -} from '../ipcChannels'; -import Settings from '../electron/Settings'; import { TAB_BAR_WIDTH, TODOS_RECIPE_ID } from '../config'; -import RecipeModel from './Recipe'; import { buildMenuTpl } from '../electron/serviceContextMenuTemplate'; +import Settings from '../electron/Settings'; +import { isMac } from '../environment'; import { IPC } from '../features/todos/constants'; import { getRecipeDirectory, loadRecipeConfig } from '../helpers/recipe-helpers'; -import { isMac } from '../environment'; import { isValidExternalURL } from '../helpers/url-helpers'; import userAgent from '../helpers/userAgent-helpers'; +import { + REQUEST_SERVICE_SPELLCHECKING_LANGUAGE, SERVICE_SPELLCHECKING_LANGUAGE, UPDATE_SERVICE_STATE, UPDATE_SPELLCHECKING_LANGUAGE, +} from '../ipcChannels'; +import RecipeModel from './Recipe'; const debug = require('debug')('Franz:Models:ServiceBrowserView'); @@ -104,6 +104,7 @@ export class ServiceBrowserView { preload: recipeId !== TODOS_RECIPE_ID ? `${__dirname}/../webview/recipe.js` : `${__dirname}/../features/todos/preload.js`, contextIsolation: false, spellcheck: this.state.isSpellcheckerEnabled, + sandbox: false, }, }); } @@ -301,6 +302,7 @@ export class ServiceBrowserView { }; this.webContents.session.setSpellCheckerEnabled(this.state.isSpellcheckerEnabled); + this.webContents.session.setSpellCheckerLanguages([this.state.spellcheckerLanguage]); } remove() { diff --git a/src/stores/ServicesStore.js b/src/stores/ServicesStore.js index 579440214..cf77bdc01 100644 --- a/src/stores/ServicesStore.js +++ b/src/stores/ServicesStore.js @@ -1,28 +1,35 @@ +import { app, webContents } from '@electron/remote'; +import { debounce, remove } from 'lodash'; import { action, - reaction, computed, observable, + reaction, toJS, } from 'mobx'; -import { debounce, remove } from 'lodash'; import ms from 'ms'; -import { app, webContents } from '@electron/remote'; import { ipcRenderer } from 'electron'; -import Store from './lib/Store'; -import Request from './lib/Request'; -import CachedRequest from './lib/CachedRequest'; -import { matchRoute } from '../helpers/routing-helpers'; -import { gaEvent } from '../lib/analytics'; -import { workspaceStore } from '../features/workspaces'; import { serviceLimitStore } from '../features/serviceLimit'; -import { RESTRICTION_TYPES } from '../models/Service'; import { TODOS_RECIPE_ID } from '../features/todos'; +import { workspaceStore } from '../features/workspaces'; +import { matchRoute } from '../helpers/routing-helpers'; import { SPELLCHECKER_LOCALES } from '../i18n/languages'; import { - 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, + ACTIVATE_NEXT_SERVICE, ACTIVATE_PREVIOUS_SERVICE, ACTIVATE_SERVICE, + HIDE_ALL_SERVICES, + NAVIGATE_SERVICE_TO, + OPEN_SERVICE_DEV_TOOLS, + RELOAD_SERVICE, + REQUEST_SERVICE_SPELLCHECKING_LANGUAGE, SERVICE_SPELLCHECKING_LANGUAGE, + UPDATE_SERVICE_STATE, + UPDATE_SPELLCHECKING_LANGUAGE, } from '../ipcChannels'; +import { gaEvent } from '../lib/analytics'; +import { RESTRICTION_TYPES } from '../models/Service'; +import CachedRequest from './lib/CachedRequest'; +import Request from './lib/Request'; +import Store from './lib/Store'; const debug = require('debug')('Franz:ServiceStore'); @@ -895,7 +902,7 @@ export default class ServicesStore extends Store { } _handleSpellcheckerLocale() { - ipcRenderer.on(UPDATE_SPELLCHECKING_LANGUAGE, (event, { serviceId, locale }) => { + ipcRenderer.on(UPDATE_SPELLCHECKING_LANGUAGE, (event, serviceId, { locale }) => { if (!serviceId) { console.warn('Did not receive service'); } else { diff --git a/src/webview/recipe.js b/src/webview/recipe.js index 28e38baa8..4aeedb96e 100644 --- a/src/webview/recipe.js +++ b/src/webview/recipe.js @@ -1,18 +1,21 @@ import { ipcRenderer } from 'electron'; -import path from 'path'; -import { autorun, observable } from 'mobx'; import { debounce } from 'lodash'; +import { autorun, observable } from 'mobx'; +import path from 'path'; import RecipeWebview from './lib/RecipeWebview'; -import { getSpellcheckerLocaleByFuzzyIdentifier } from './spellchecker'; import { injectDarkModeStyle, isDarkModeStyleInjected, removeDarkModeStyle } from './darkmode'; -import './notifications'; import './desktopCapturer'; +import './notifications'; +import { getSpellcheckerLocaleByFuzzyIdentifier } from './spellchecker'; +// import { DEFAULT_WEB_CONTENTS_ID } from '../config'; import { DEFAULT_APP_SETTINGS_VANILLA } from '../configVanilla'; import { UPDATE_SPELLCHECKING_LANGUAGE } from '../ipcChannels'; +// const DEFAULT_WEB_CONTENTS_ID = 1; + const debug = require('debug')('Franz:Plugin'); window.FranzAPI = {}; @@ -40,8 +43,6 @@ class RecipeController { this.initialize(); } - cldIdentifier = null; - async initialize() { Object.keys(this.ipcEvents).forEach((channel) => { ipcRenderer.on(channel, (...args) => { @@ -54,6 +55,8 @@ class RecipeController { setTimeout(() => ipcRenderer.send('hello'), 100); autorun(() => this.update()); + + this.automaticLanguageDetection(); } loadRecipeModule(event, config, recipe) { @@ -111,11 +114,10 @@ class RecipeController { debug('Detecting language for', value); const locale = await ipcRenderer.invoke('detect-language', { sample: value }); - const spellcheckerLocale = getSpellcheckerLocaleByFuzzyIdentifier(locale); debug('Language detected reliably, setting spellchecker language to', spellcheckerLocale); if (spellcheckerLocale) { - ipcRenderer.invoke(UPDATE_SPELLCHECKING_LANGUAGE, { locale: spellcheckerLocale }); + ipcRenderer.send(UPDATE_SPELLCHECKING_LANGUAGE, { locale: spellcheckerLocale }); } }, 225)); }