Skip to content

The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.

1.2 KB
Raw Blame
/** Soft-wrap action targets outside the regular editor, retained while its menus are open. */
import { getContext, setContext } from "@/platform/context";

interface SoftWrapSurface {
  enabled(): boolean;
  toggle(): void;
}

let focused: SoftWrapSurface | null = null;

export function focusedSoftWrapSurface(): SoftWrapSurface | null {
  return focused;
}

export function registerSoftWrapSurface(host: HTMLElement, surface: SoftWrapSurface): () => void {
  const focus = (target: Element | null) => {
    if (target && host.contains(target)) {
      focused = surface;
      setContext("softWrapSurfaceFocus", true);
    } else if (
      focused === surface &&
      !getContext().popupOpen &&
      !target?.closest('[role="menu"], [role="menubar"], [aria-haspopup="menu"]')
    ) {
      focused = null;
      setContext("softWrapSurfaceFocus", false);
    }
  };
  const onFocus = (event: FocusEvent) => focus(event.target instanceof Element ? event.target : null);
  document.addEventListener("focusin", onFocus);
  focus(document.activeElement);
  return () => {
    document.removeEventListener("focusin", onFocus);
    if (focused === surface) {
      focused = null;
      setContext("softWrapSurfaceFocus", false);
    }
  };
}