Files
RuneStone/src/lib/completion/input.ts
T
Knotty Wyvern 5bd616d7fd Give themes named color roles, theme data files, and file format 3
Phase 5a-1 and 5a-2.

- roles.ts names 51 color roles. One derivation rule fills the roles
  that a theme does not give, for all themes. tokens.css keeps only the
  root defaults, without theme blocks.
- Each built-in theme is one data file in src/lib/theme/themes/. No code
  outside the theme data names a theme ID. characterThemes.ts is gone.
- New roles: link, focus, on-accent, highlight, scrim, the syntax roles,
  and 16 terminal (ANSI) roles. Stray colors in components use the roles.
- contrast.ts checks each theme against the readability rules of
  docs/ui-color.md. The tests mark the known palette failures as todo,
  and a second test fails when a known failure starts to pass.
- Theme files use schema 3 with a mode and an optional built-in base.
  A short file gets the missing colors and fonts from the base. Schema 1
  and 2 files still import. Old syntax names convert on import.
- Docs: ui-color.md, plugin-development.md (theme tokens), ui-design.md,
  guides/themes.md, and UI-041 and UI-042 in design-decisions.md.

The applied colors stay the same for every built-in theme, except three
small changes: the conflict line uses the warn color, the Write
highlight uses the highlight role, and two dialog backdrops use the
scrim role.

pnpm check passes. pnpm test: 447 pass, 31 todo, 0 fail. Full smoke
suite: 93 passed, 2 known. appearance/palettes read the old syntax
names and passed 3 of 3 times after the fix. workspace/manageTemplates
failed one time and passed on retry.
2026-10-28 15:50:45 -07:00

48 lines
3.5 KiB
TypeScript

import { completionHub } from "./hub.ts";
import type { CompletionApi, CompletionHandle } from "./contracts.ts";
export const registerCompletionInput: CompletionApi["registerInput"] = (element, options) => {
let revision = 0, composing = false;
const hint = document.createElement("div");
hint.className = "rs-completion-hint";
hint.style.cssText = "position:fixed;z-index:10000;pointer-events:none;max-width:40em;padding:6px 10px;border-radius:var(--rs-control-radius);background:var(--rs-bg);color:var(--rs-text);border:1px solid currentColor;font:inherit;white-space:pre-wrap";
hint.hidden = true;
hint.setAttribute("aria-hidden", "true");
document.body.append(hint);
const handle = completionHub.registerSurface({
id: options.id,
snapshot: () => ({ documentId: options.id, revision, text: element.value, caret: element.selectionStart ?? 0, selectionEnd: element.selectionEnd ?? 0,
focused: document.activeElement === element, composing, readOnly: element.readOnly || element.disabled,
sensitive: options.sensitive || element instanceof HTMLInputElement && !["text", "search"].includes(element.type), kind: options.kind ?? "prose" }),
preview: text => { hint.hidden = !text; hint.textContent = text ? `${text}\nTab to accept · Escape to dismiss` : ""; const r = element.getBoundingClientRect(); hint.style.left = `${Math.max(0, Math.min(r.left, innerWidth - 350))}px`; hint.style.top = `${Math.min(r.bottom + 4, innerHeight - 100)}px`; },
insert: (text, expected) => {
if (element.value !== expected.text || element.selectionStart !== expected.caret || element.selectionEnd !== expected.caret) return false;
// The native edit command keeps input and textarea undo history intact.
element.focus();
return document.execCommand("insertText", false, text);
},
});
const changed = () => { revision++; handle.changed(); };
const select = () => handle.dismiss();
const start = () => { composing = true; handle.dismiss(); };
const end = () => { composing = false; changed(); };
const key: EventListener = raw => {
const event = raw as KeyboardEvent;
if (event.isComposing || composing) return;
if (event.key === "Tab" && !event.shiftKey && !event.altKey && !event.ctrlKey && handle.accept()) { event.preventDefault(); event.stopPropagation(); }
else if (event.key === "Escape" && handle.dismiss()) { event.preventDefault(); event.stopPropagation(); }
else if ((event.ctrlKey || event.metaKey) && event.altKey && event.key === " ") { event.preventDefault(); handle.request(); }
else if ((event.ctrlKey || event.metaKey) && event.altKey && event.key === "ArrowRight" && handle.accept(true)) event.preventDefault();
};
element.addEventListener("input", changed); element.addEventListener("keydown", key); element.addEventListener("blur", select);
element.addEventListener("pointerdown", select); element.addEventListener("select", select);
element.addEventListener("compositionstart", start); element.addEventListener("compositionend", end);
const dispose = () => {
handle.dispose(); hint.remove();
element.removeEventListener("input", changed); element.removeEventListener("keydown", key); element.removeEventListener("blur", select);
element.removeEventListener("pointerdown", select); element.removeEventListener("select", select);
element.removeEventListener("compositionstart", start); element.removeEventListener("compositionend", end);
};
return { ...handle, dispose } satisfies CompletionHandle;
};