Add interface languages and improve themes and workspace controls for 0.1.36
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { tr } from "../i18n/index.ts";
|
||||
import { invoke, isTauri } from "@tauri-apps/api/core";
|
||||
import { listen } from "@tauri-apps/api/event";
|
||||
import { actionMenu, type ActionMenuItem } from "../ui/actionMenu";
|
||||
@@ -87,32 +88,40 @@ export function mountSpellingMenu(): () => void {
|
||||
const { target, x, y, word } = context;
|
||||
const requestRevision = ++revision;
|
||||
let items = spellingItems(payload.items, id => finish(id, true));
|
||||
const parent = target.closest<HTMLDialogElement>("dialog[open]") ?? document.body;
|
||||
const show = () => {
|
||||
const previous = active?.request === payload.request ? active.root : undefined;
|
||||
const focusedLabel = previous?.contains(document.activeElement) ? document.activeElement?.querySelector(".action-label")?.textContent : undefined;
|
||||
previous?.remove();
|
||||
const root = actionMenu(x, y, items, () => finish(null, true), tr("Spelling actions"), parent);
|
||||
active = { request: payload.request, root, target };
|
||||
if (focusedLabel) [...root.querySelectorAll<HTMLButtonElement>(":scope > button")].find(button => button.querySelector(".action-label")?.textContent === focusedLabel)?.focus();
|
||||
};
|
||||
if (payload.fallback_spelling) {
|
||||
const readable = (entries: NativeSpellingItem[]): NativeSpellingItem[] => entries.filter(item => item.kind === 3 || item.label.trim()).map(item => ({ ...item, children: readable(item.children) }));
|
||||
items = spellingItems(readable(payload.items), id => finish(id, true));
|
||||
show();
|
||||
if (word) {
|
||||
const languages = [...new Set([target.lang, document.documentElement.lang, ...navigator.languages].filter(Boolean))];
|
||||
try {
|
||||
const result = await invoke<{ language: string; words: string[]; misspelled: boolean }>("spelling_suggestions", { word: word.word, languages });
|
||||
const suggestions: ActionMenuItem[] = result.words.map(value => ({ label: value, run: () => { finish(); word.replace(value); } }));
|
||||
if (result.misspelled && !suggestions.length) suggestions.push({ label: "No spelling suggestions", disabled: true });
|
||||
if (result.misspelled) suggestions.push({ label: "Add to dictionary", icon: "book", run: () => {
|
||||
if (result.misspelled && !suggestions.length) suggestions.push({ label: tr("No spelling suggestions"), disabled: true });
|
||||
if (result.misspelled) suggestions.push({ label: tr("Add to dictionary"), icon: "book", run: () => {
|
||||
finish(null, true);
|
||||
void invoke("spelling_suggestions", { word: word.word, languages: [result.language], add: true }).catch(error => console.error("Could not add the word", error));
|
||||
} }, { separator: true });
|
||||
items = [...suggestions, ...items];
|
||||
} catch {
|
||||
items = [{ label: "Spelling suggestions unavailable", disabled: true }, { separator: true }, ...items];
|
||||
items = [{ label: tr("Spelling suggestions unavailable"), disabled: true }, { separator: true }, ...items];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (disposed || requestRevision !== revision || !target.isConnected || (payload.fallback_spelling && word && !word.valid())) {
|
||||
if (disposed || requestRevision !== revision || (payload.fallback_spelling && active?.request !== payload.request) || !target.isConnected || (payload.fallback_spelling && word && !word.valid())) {
|
||||
void invoke("spelling_menu_select", { request: payload.request, command: null });
|
||||
return;
|
||||
}
|
||||
const parent = target.closest<HTMLDialogElement>("dialog[open]") ?? document.body;
|
||||
const root = actionMenu(x, y, items, () => finish(null, true), "Spelling actions", parent);
|
||||
active = { request: payload.request, root, target };
|
||||
show();
|
||||
}).then(async stop => {
|
||||
if (disposed) { stop(); return; }
|
||||
unlisten = stop;
|
||||
|
||||
Reference in New Issue
Block a user