Repository navigation
feat(desktop): choose spellcheck languages for the composer #32311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HugoPlante
wants to merge
6
commits into
pingdotgg:main
Choose a base branch
from
HugoPlante:feat/spellcheck-languages
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+430
−2
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8497ee9
feat(desktop): choose spellcheck languages for the composer
HugoPlante 11fce03
fix(desktop): clearing spellcheck languages restores the locale defau…
HugoPlante 776a52a
fix(web): spellcheck picker shows active languages and respects the l…
HugoPlante 5f5ecf9
Merge remote-tracking branch 'github/main' into feat/spellcheck-langu…
maria-rcks b27316e
fix(desktop): merge main into spellcheck language settings
maria-rcks 67336d0
fix(desktop): preserve system spellcheck defaults and picker access
maria-rcks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts"; | ||
| import * as HostProcess from "@t3tools/shared/HostProcess"; | ||
| import { it } from "@effect/vitest"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Option from "effect/Option"; | ||
| import { expect, vi } from "vite-plus/test"; | ||
|
|
||
| const session = vi.hoisted(() => ({ | ||
| languages: ["en-US"] as string[], | ||
| availableSpellCheckerLanguages: ["en-US", "en-GB", "fr-FR", "de-DE"], | ||
| getSpellCheckerLanguages: () => [...session.languages], | ||
| setSpellCheckerLanguages: (languages: string[]) => { | ||
| session.languages = [...languages]; | ||
| }, | ||
| })); | ||
| vi.mock("electron", () => ({ | ||
| app: { getLocale: () => "en-US", getSystemLocale: () => "en-GB" }, | ||
| session: { defaultSession: session }, | ||
| })); | ||
|
|
||
| import * as DesktopClientSettings from "./DesktopClientSettings.ts"; | ||
| import * as DesktopSpellcheck from "./DesktopSpellcheck.ts"; | ||
|
|
||
| const withLanguages = (spellcheckLanguages: ReadonlyArray<string>) => ({ | ||
| ...DEFAULT_CLIENT_SETTINGS, | ||
| spellcheckLanguages, | ||
| }); | ||
|
|
||
| // `persisted` is the list Electron restored from its Preferences file at launch. | ||
| const run = ( | ||
| platform: NodeJS.Platform, | ||
| stored: ReadonlyArray<string>, | ||
| persisted: ReadonlyArray<string> = ["en-GB"], | ||
| ) => | ||
| Effect.gen(function* () { | ||
| session.languages = [...persisted]; | ||
| return yield* DesktopSpellcheck.DesktopSpellcheck; | ||
| }).pipe( | ||
| Effect.provide( | ||
| DesktopSpellcheck.layer.pipe( | ||
| Layer.provide(DesktopClientSettings.layerTest(Option.some(withLanguages(stored)))), | ||
| ), | ||
| ), | ||
| Effect.provideService(HostProcess.Platform, platform), | ||
| ); | ||
|
|
||
| it.effect("applies stored languages, drops unknown codes, and restores the locale default", () => | ||
| Effect.gen(function* () { | ||
| const spellcheck = yield* run("linux", ["fr-FR", "xx-XX", "en-US", "fr-FR"]); | ||
| yield* spellcheck.initialize; | ||
| expect(session.languages).toEqual(["fr-FR", "en-US"]); | ||
|
|
||
| yield* spellcheck.configure(withLanguages(["de-DE"])); | ||
| expect(session.languages).toEqual(["de-DE"]); | ||
|
|
||
| yield* spellcheck.configure(withLanguages(["xx-XX"])); | ||
| expect(session.languages).toEqual(["en-GB"]); | ||
|
|
||
| yield* spellcheck.configure(withLanguages([])); | ||
| expect(session.languages).toEqual(["en-GB"]); | ||
| expect(yield* spellcheck.availableLanguages).toEqual(session.availableSpellCheckerLanguages); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("clearing an override after a restart restores the locale default", () => | ||
| Effect.gen(function* () { | ||
| const spellcheck = yield* run("linux", ["fr-FR"], ["fr-FR"]); | ||
| yield* spellcheck.initialize; | ||
| yield* spellcheck.configure(withLanguages([])); | ||
| expect(session.languages).toEqual(["en-GB"]); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("leaves a list it never overrode alone", () => | ||
| Effect.gen(function* () { | ||
| const spellcheck = yield* run("linux", [], ["en-US", "fr-FR"]); | ||
| yield* spellcheck.initialize; | ||
| yield* spellcheck.configure(withLanguages([])); | ||
| expect(session.languages).toEqual(["en-US", "fr-FR"]); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("leaves the native macOS spellchecker alone", () => | ||
| Effect.gen(function* () { | ||
| const spellcheck = yield* run("darwin", ["fr-FR"]); | ||
| yield* spellcheck.initialize; | ||
| expect(session.languages).toEqual(["en-GB"]); | ||
| expect(yield* spellcheck.availableLanguages).toEqual([]); | ||
| }), | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { DEFAULT_CLIENT_SETTINGS, type ClientSettings } from "@t3tools/contracts"; | ||
| import * as HostProcess from "@t3tools/shared/HostProcess"; | ||
| import * as Context from "effect/Context"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Option from "effect/Option"; | ||
| import * as Ref from "effect/Ref"; | ||
| import * as Electron from "electron"; | ||
|
|
||
| import * as DesktopClientSettings from "./DesktopClientSettings.ts"; | ||
|
|
||
| export class DesktopSpellcheck extends Context.Service< | ||
| DesktopSpellcheck, | ||
| { | ||
| readonly initialize: Effect.Effect<void>; | ||
| readonly configure: (settings: ClientSettings) => Effect.Effect<void>; | ||
| /** Empty where the OS spellchecker picks languages itself (macOS). */ | ||
| readonly availableLanguages: Effect.Effect<ReadonlyArray<string>>; | ||
| } | ||
| >()("@t3tools/desktop/settings/DesktopSpellcheck") {} | ||
|
|
||
| /** Electron seeds its list from the system locale; Chromium falls back to the base language. */ | ||
| function localeDefaultLanguages( | ||
| locale: string, | ||
| available: ReadonlyArray<string>, | ||
| ): ReadonlyArray<string> { | ||
| const base = locale.split("-")[0] ?? locale; | ||
| const match = | ||
| available.find((language) => language === locale) ?? | ||
| available.find((language) => language === base) ?? | ||
| available.find((language) => language.startsWith(`${base}-`)); | ||
| return [match ?? "en-US"]; | ||
| } | ||
|
|
||
| /** @public */ | ||
| export const make = Effect.gen(function* () { | ||
| const clientSettings = yield* DesktopClientSettings.DesktopClientSettings; | ||
| const platform = yield* HostProcess.Platform; | ||
| // Electron persists the list across restarts, so only undo an override this app applied; | ||
| // a list the user set in Chromium's Preferences by hand is left alone. | ||
| const overriddenRef = yield* Ref.make(false); | ||
|
|
||
| const configure = Effect.fn("desktop.spellcheck.configure")(function* (settings: ClientSettings) { | ||
| // macOS always uses the native spellchecker, which ignores Electron's language list. | ||
| if (platform === "darwin") return; | ||
| const session = Electron.session.defaultSession; | ||
| const available = session.availableSpellCheckerLanguages; | ||
| const supported = new Set(available); | ||
| const requested = [...new Set(settings.spellcheckLanguages)].filter((language) => | ||
| supported.has(language), | ||
| ); | ||
| const overridden = yield* Ref.getAndSet(overriddenRef, requested.length > 0); | ||
| if (requested.length === 0 && !overridden) return; | ||
| const languages = | ||
| requested.length > 0 | ||
| ? requested | ||
| : localeDefaultLanguages(Electron.app.getSystemLocale(), available); | ||
| yield* Effect.annotateCurrentSpan({ languages: languages.join(",") }); | ||
| // Only throws for codes outside availableSpellCheckerLanguages, which are filtered out above. | ||
| yield* Effect.sync(() => session.setSpellCheckerLanguages([...languages])); | ||
| }); | ||
|
|
||
| return DesktopSpellcheck.of({ | ||
| initialize: clientSettings.get.pipe( | ||
| Effect.flatMap((stored) => | ||
| configure(Option.getOrElse(stored, () => DEFAULT_CLIENT_SETTINGS)), | ||
| ), | ||
| Effect.ignore, | ||
| ), | ||
| configure, | ||
| availableLanguages: Effect.sync(() => | ||
| platform === "darwin" ? [] : Electron.session.defaultSession.availableSpellCheckerLanguages, | ||
| ), | ||
| }); | ||
| }); | ||
|
|
||
| export const layer = Layer.effect(DesktopSpellcheck, make); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.