Update the browser, editor, settings, and installer
This commit is contained in:
@@ -2,6 +2,7 @@ import { invoke, isTauri } from "@tauri-apps/api/core";
|
||||
import { listen } from "@tauri-apps/api/event";
|
||||
import { actionMenu, type ActionMenuItem } from "../ui/actionMenu";
|
||||
import type { ActionIcon } from "../ui/actionIcons";
|
||||
import { spellingWord } from "./spellingWord";
|
||||
|
||||
/** WebView2 owns spelling suggestions and applies corrections to the original word. */
|
||||
export function isSpellingTarget(target: EventTarget | null): boolean {
|
||||
@@ -22,7 +23,7 @@ export type NativeSpellingItem = {
|
||||
id: number; name: string; label: string; shortcut: string; kind: number;
|
||||
enabled: boolean; checked: boolean; children: NativeSpellingItem[];
|
||||
};
|
||||
type NativeMenu = { request: string; items: NativeSpellingItem[] };
|
||||
type NativeMenu = { request: string; items: NativeSpellingItem[]; fallback_spelling?: boolean };
|
||||
const icons: Record<string, ActionIcon> = {
|
||||
undo: "undo", redo: "redo",
|
||||
cut: "cut", copy: "copy", paste: "paste", pasteAndMatchStyle: "paste-text",
|
||||
@@ -48,7 +49,8 @@ export function spellingItems(items: NativeSpellingItem[], select: (id: number)
|
||||
export function mountSpellingMenu(): () => void {
|
||||
if (!isTauri()) return () => {};
|
||||
let disposed = false, unlisten: (() => void) | undefined;
|
||||
let context: { target: HTMLElement; x: number; y: number } | undefined;
|
||||
let context: { target: HTMLElement; x: number; y: number; word: ReturnType<typeof spellingWord> } | undefined;
|
||||
let revision = 0;
|
||||
let active: { request: string; root: HTMLElement; target: HTMLElement } | undefined;
|
||||
const finish = (command: number | null = null, restoreFocus = false) => {
|
||||
if (!active) return;
|
||||
@@ -59,30 +61,57 @@ export function mountSpellingMenu(): () => void {
|
||||
void invoke("spelling_menu_select", { request, command }).catch(error => console.error("Cannot close the spelling menu", error));
|
||||
};
|
||||
const remember = (event: MouseEvent) => {
|
||||
revision++;
|
||||
finish();
|
||||
if (event.defaultPrevented || !isSpellingTarget(event.target)) { context = undefined; return; }
|
||||
const target = (event.target as HTMLElement).closest<HTMLElement>('[contenteditable="true"], textarea, input')!;
|
||||
const bounds = document.getSelection()?.rangeCount ? document.getSelection()!.getRangeAt(0).getBoundingClientRect() : target.getBoundingClientRect();
|
||||
context = { target, x: event.clientX || bounds.left, y: event.clientY || bounds.bottom };
|
||||
context = { target, x: event.clientX || bounds.left, y: event.clientY || bounds.bottom, word: spellingWord(target, event.clientX, event.clientY) };
|
||||
};
|
||||
const outside = (event: PointerEvent) => { if (active && !active.root.contains(event.target as Node)) finish(); };
|
||||
const scroll = (event: Event) => { if (active && !active.root.contains(event.target as Node)) finish(); };
|
||||
const cancel = () => finish();
|
||||
const outside = (event: PointerEvent) => { if (!active?.root.contains(event.target as Node)) { revision++; finish(); } };
|
||||
const scroll = (event: Event) => { if (!active?.root.contains(event.target as Node)) { revision++; finish(); } };
|
||||
const cancel = () => { revision++; finish(); };
|
||||
window.addEventListener("contextmenu", remember);
|
||||
window.addEventListener("pointerdown", outside);
|
||||
window.addEventListener("resize", cancel);
|
||||
window.addEventListener("blur", cancel);
|
||||
window.addEventListener("pagehide", cancel);
|
||||
document.addEventListener("scroll", scroll, true);
|
||||
void listen<NativeMenu>("spelling-menu", ({ payload }) => {
|
||||
document.addEventListener("input", cancel, true);
|
||||
void listen<NativeMenu>("spelling-menu", async ({ payload }) => {
|
||||
finish();
|
||||
if (disposed || !context?.target.isConnected) {
|
||||
void invoke("spelling_menu_select", { request: payload.request, command: null, fallback: true });
|
||||
return;
|
||||
}
|
||||
const { target, x, y } = context;
|
||||
const { target, x, y, word } = context;
|
||||
const requestRevision = ++revision;
|
||||
let items = spellingItems(payload.items, id => finish(id, true));
|
||||
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));
|
||||
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: () => {
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (disposed || requestRevision !== revision || !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, spellingItems(payload.items, id => finish(id, true)), () => finish(null, true), "Spelling actions", parent);
|
||||
const root = actionMenu(x, y, items, () => finish(null, true), "Spelling actions", parent);
|
||||
active = { request: payload.request, root, target };
|
||||
}).then(async stop => {
|
||||
if (disposed) { stop(); return; }
|
||||
@@ -100,5 +129,6 @@ export function mountSpellingMenu(): () => void {
|
||||
window.removeEventListener("blur", cancel);
|
||||
window.removeEventListener("pagehide", cancel);
|
||||
document.removeEventListener("scroll", scroll, true);
|
||||
document.removeEventListener("input", cancel, true);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user