Update editor, workspace, Git services, appearance, and project guides
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
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";
|
||||
|
||||
/** WebView2 owns spelling suggestions and applies corrections to the original word. */
|
||||
export function isSpellingTarget(target: EventTarget | null): boolean {
|
||||
if (!(target instanceof HTMLElement)) return false;
|
||||
if (target instanceof HTMLTextAreaElement || (target instanceof HTMLInputElement && ["text", "search"].includes(target.type))) {
|
||||
return target.spellcheck && !target.disabled && !target.readOnly;
|
||||
}
|
||||
const editable = target.closest<HTMLElement>('[contenteditable="true"][spellcheck="true"]');
|
||||
return !!editable && target.isContentEditable
|
||||
&& !target.closest('a, code, pre, button, [data-href], [spellcheck="false"], .writer-inline-code, .writer-code-source, .writer-code-line');
|
||||
}
|
||||
|
||||
export function useSpellingMenu(event: MouseEvent, selectionEmpty: boolean): boolean {
|
||||
return selectionEmpty && isSpellingTarget(event.target);
|
||||
}
|
||||
|
||||
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[] };
|
||||
const icons: Record<string, ActionIcon> = {
|
||||
undo: "undo", redo: "redo",
|
||||
cut: "cut", copy: "copy", paste: "paste", pasteAndMatchStyle: "paste-text",
|
||||
selectAll: "select", addToDictionary: "book", spellCheck: "book", spellingAndGrammar: "book",
|
||||
};
|
||||
|
||||
function nativeLabel(label: string): string {
|
||||
// Windows uses ampersands for access keys and doubles literal ampersands.
|
||||
return label.replace(/&&|&/g, value => value === "&&" ? "&" : "");
|
||||
}
|
||||
|
||||
export function spellingItems(items: NativeSpellingItem[], select: (id: number) => void): ActionMenuItem[] {
|
||||
return items.map(item => item.kind === 3 ? { separator: true } : {
|
||||
label: nativeLabel(item.label), icon: icons[item.name], shortcut: item.shortcut || undefined,
|
||||
disabled: !item.enabled, checked: item.kind === 1 || item.kind === 2 ? item.checked : undefined,
|
||||
radio: item.kind === 2,
|
||||
children: item.kind === 4 ? spellingItems(item.children, select) : undefined,
|
||||
run: () => select(item.id),
|
||||
});
|
||||
}
|
||||
|
||||
/** Mount once per app window. Other platforms keep their native spelling menu. */
|
||||
export function mountSpellingMenu(): () => void {
|
||||
if (!isTauri()) return () => {};
|
||||
let disposed = false, unlisten: (() => void) | undefined;
|
||||
let context: { target: HTMLElement; x: number; y: number } | undefined;
|
||||
let active: { request: string; root: HTMLElement; target: HTMLElement } | undefined;
|
||||
const finish = (command: number | null = null, restoreFocus = false) => {
|
||||
if (!active) return;
|
||||
const { request, root, target } = active;
|
||||
active = undefined;
|
||||
root.remove();
|
||||
if (restoreFocus && target.isConnected) target.focus({ preventScroll: true });
|
||||
void invoke("spelling_menu_select", { request, command }).catch(error => console.error("Cannot close the spelling menu", error));
|
||||
};
|
||||
const remember = (event: MouseEvent) => {
|
||||
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 };
|
||||
};
|
||||
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();
|
||||
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 }) => {
|
||||
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 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);
|
||||
active = { request: payload.request, root, target };
|
||||
}).then(async stop => {
|
||||
if (disposed) { stop(); return; }
|
||||
unlisten = stop;
|
||||
await invoke("spelling_menu_install");
|
||||
if (disposed) await invoke("spelling_menu_uninstall");
|
||||
}).catch(error => console.warn("Native spelling menu remains active", error));
|
||||
return () => {
|
||||
disposed = true;
|
||||
finish(); unlisten?.();
|
||||
void invoke("spelling_menu_uninstall").catch(() => {});
|
||||
window.removeEventListener("contextmenu", remember);
|
||||
window.removeEventListener("pointerdown", outside);
|
||||
window.removeEventListener("resize", cancel);
|
||||
window.removeEventListener("blur", cancel);
|
||||
window.removeEventListener("pagehide", cancel);
|
||||
document.removeEventListener("scroll", scroll, true);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user