Skip to content

Commit

Permalink
fix(electron): respect locale for spellchecker
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 committed Nov 15, 2024
1 parent 54c5122 commit 57ccd48
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
11 changes: 11 additions & 0 deletions packages/frontend/apps/electron/src/main/ui/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { app, nativeTheme, shell } from 'electron';
import { getLinkPreview } from 'link-preview-js';

import { isMacOS } from '../../shared/utils';
import { persistentConfig } from '../config-storage/persist';
import { logger } from '../logger';
import type { WorkbenchViewMeta } from '../shared-state-schema';
Expand Down Expand Up @@ -220,4 +221,14 @@ export const uiHandlers = {
app.relaunch();
app.quit();
},
onLanguageChange: async (e, language: string) => {
// only works for win/linux
if (isMacOS()) {
return;
}

if (e.sender.session.availableSpellCheckerLanguages.includes(language)) {
e.sender.session.setSpellCheckerLanguages([language, 'en-US']);
}
},
} satisfies NamespaceHandlers;
8 changes: 7 additions & 1 deletion packages/frontend/core/src/modules/editor-setting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
GlobalStateService,
} from '@toeverything/infra';

import { DesktopApiService } from '../desktop-api';
import { I18n } from '../i18n';
import { UserDBService } from '../userspace';
import { EditorSetting } from './entities/editor-setting';
import { CurrentUserDBEditorSettingProvider } from './impls/user-db';
Expand All @@ -25,5 +27,9 @@ export function configureEditorSettingModule(framework: Framework) {
}

export function configureSpellCheckSettingModule(framework: Framework) {
framework.service(SpellCheckSettingService, [GlobalStateService]);
framework.service(SpellCheckSettingService, [
GlobalStateService,
I18n,
DesktopApiService,
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,31 @@ import type {
SpellCheckStateKey,
SpellCheckStateSchema,
} from '@affine/electron/main/shared-state-schema';
import type { Language } from '@affine/i18n';
import type { GlobalStateService } from '@toeverything/infra';
import { LiveData, Service } from '@toeverything/infra';

import type { DesktopApiService } from '../../desktop-api';
import type { I18n } from '../../i18n';

const SPELL_CHECK_SETTING_KEY: SpellCheckStateKey = 'spellCheckState';

export class SpellCheckSettingService extends Service {
constructor(private readonly globalStateService: GlobalStateService) {
constructor(
private readonly globalStateService: GlobalStateService,
private readonly i18n: I18n,
private readonly desktopApiService: DesktopApiService
) {
super();

// this will be called even during initialization
this.i18n.i18next.on('languageChanged', (language: Language) => {
this.desktopApiService.handler.ui
.onLanguageChange(language)
.catch(err => {
console.error(err);
});
});
}

enabled$ = LiveData.from(
Expand Down

0 comments on commit 57ccd48

Please sign in to comment.