Skip to content

Commit

Permalink
fix: fix automatic spellcheck language detection
Browse files Browse the repository at this point in the history
  • Loading branch information
adlk committed Aug 13, 2023
1 parent dfa9471 commit 099f46d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 32 deletions.
14 changes: 8 additions & 6 deletions 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);
Expand Down
1 change: 1 addition & 0 deletions src/i18n/languages.js
Expand Up @@ -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)',
Expand Down
14 changes: 8 additions & 6 deletions src/models/ServiceBrowserView.ts
Expand Up @@ -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');

Expand Down Expand Up @@ -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,
},
});
}
Expand Down Expand Up @@ -301,6 +302,7 @@ export class ServiceBrowserView {
};

this.webContents.session.setSpellCheckerEnabled(this.state.isSpellcheckerEnabled);
this.webContents.session.setSpellCheckerLanguages([this.state.spellcheckerLanguage]);
}

remove() {
Expand Down
31 changes: 19 additions & 12 deletions 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');

Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 10 additions & 8 deletions 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 = {};
Expand Down Expand Up @@ -40,8 +43,6 @@ class RecipeController {
this.initialize();
}

cldIdentifier = null;

async initialize() {
Object.keys(this.ipcEvents).forEach((channel) => {
ipcRenderer.on(channel, (...args) => {
Expand All @@ -54,6 +55,8 @@ class RecipeController {
setTimeout(() => ipcRenderer.send('hello'), 100);

autorun(() => this.update());

this.automaticLanguageDetection();
}

loadRecipeModule(event, config, recipe) {
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 099f46d

Please sign in to comment.