Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

3.4 KB
Raw Blame
type Selection = { start: number | null; end: number | null; direction: "forward" | "backward" | "none" | null };

function isTextControl(
  view: (Window & typeof globalThis) | null,
  element: Element | null,
): element is HTMLInputElement {
  return Boolean(
    view && element && (element instanceof view.HTMLInputElement || element instanceof view.HTMLTextAreaElement),
  );
}

function readSelection(view: (Window & typeof globalThis) | null, element: Element | null): Selection | null {
  if (!isTextControl(view, element)) return null;
  return { start: element.selectionStart, end: element.selectionEnd, direction: element.selectionDirection };
}

function applySelection(
  view: (Window & typeof globalThis) | null,
  element: HTMLElement,
  selection: Selection | null,
): void {
  if (!selection || selection.start === null || selection.end === null) return;
  if (!isTextControl(view, element)) return;
  element.setSelectionRange(selection.start, selection.end, selection.direction ?? undefined);
}

export function replaceChildrenPreservingFocus(container: HTMLElement, host: HTMLElement): void {
  const view = container.ownerDocument.defaultView;
  const active = container.contains(container.ownerDocument.activeElement)
    ? (container.ownerDocument.activeElement as HTMLElement)
    : null;
  const key = active?.dataset.focusKey;
  const selection = readSelection(view, active);
  container.replaceChildren(host);
  if (!key) return;
  const next = [...container.querySelectorAll<HTMLElement>("[data-focus-key]")].find(
    (element) => element.dataset.focusKey === key,
  );
  next?.focus();
  if (next) applySelection(view, next, selection);
}

export function preservingFocus(doc: Document, mutate: () => void): void {
  const view = doc.defaultView;
  const active = doc.activeElement as HTMLElement | null;
  const selection = readSelection(view, active);
  mutate();
  if (!active || doc.activeElement === active || !active.isConnected) return;
  active.focus();
  applySelection(view, active, selection);
}

export function focusComposerOnPaneClick(pane: HTMLElement, isActive: () => boolean): void {
  let switching = false;
  pane.tabIndex = -1;
  pane.addEventListener(
    "pointerdown",
    (event) => {
      switching =
        !isActive() && event.button === 0 && !event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey;
    },
    true,
  );
  const cancel = (): void => {
    switching = false;
  };
  pane.addEventListener("pointercancel", cancel);
  pane.addEventListener("dragstart", cancel);
  pane.addEventListener("click", (event) => {
    const focus = switching;
    cancel();
    const doc = pane.ownerDocument;
    if (!focus || event.defaultPrevented || doc.activeElement !== pane || doc.getSelection()?.isCollapsed === false)
      return;
    pane.querySelector<HTMLTextAreaElement>(".composer-input:not(:disabled)")?.focus({ preventScroll: true });
  });
}

export function focusPaneComposer(pane: HTMLElement | undefined): void {
  if (!pane) return;
  const doc = pane.ownerDocument;
  const inside = (): boolean => {
    const now = doc.activeElement;
    return Boolean(now && now !== pane && pane.contains(now));
  };
  if (inside() || (doc.activeElement as HTMLElement | null)?.closest?.("dialog, [role='dialog'], [role='menu']"))
    return;
  setTimeout(() => {
    if (inside()) return;
    pane.querySelector<HTMLTextAreaElement>(".composer-input:not(:disabled)")?.focus({ preventScroll: true });
  }, 0);
}