Skip to content

Free open-source image editor for the browser

2.3 KB
Raw Blame
import {
  type KeyboardEvent,
  type PointerEvent,
  useEffect,
  useRef,
} from "react";

type Edit = { begin: () => boolean; commit: () => void; cancel: () => void };
type Gesture = "pointerup" | "keyup";

/** Groups pointer and repeated keyboard changes; text fields commit their own draft. A gesture inside an open group joins it and leaves the group to its opener. */
export function useEditGesture(edit: Edit) {
  const active = useRef<Gesture | null>(null);
  const opened = useRef(false);
  function begin(end: Gesture) {
    active.current = end;
    opened.current = edit.begin() || opened.current;
  }
  useEffect(() => {
    function finish(event: Event) {
      if (active.current !== event.type) {
        return;
      }
      active.current = null;
      if (opened.current) {
        opened.current = false;
        edit.commit();
      }
    }
    function cancel() {
      if (!active.current) {
        return;
      }
      active.current = null;
      if (opened.current) {
        opened.current = false;
        edit.cancel();
      }
    }
    const controller = new AbortController();
    const { signal } = controller;
    window.addEventListener("pointerup", finish, { signal });
    window.addEventListener("keyup", finish, { signal });
    window.addEventListener("pointercancel", cancel, { signal });
    window.addEventListener("blur", cancel, { signal });
    return () => {
      controller.abort();
      cancel();
    };
  }, [edit]);
  return {
    onPointerDownCapture(event: PointerEvent) {
      if (event.button === 0) {
        const focused = document.activeElement;
        if (
          focused instanceof HTMLInputElement &&
          focused.type === "text" &&
          focused !== event.target
        ) {
          focused.blur();
        }
        begin("pointerup");
      }
    },
    onPointerMoveCapture() {
      if (active.current === "pointerup") {
        opened.current = edit.begin() || opened.current;
      }
    },
    onKeyDownCapture(event: KeyboardEvent) {
      if (
        event.metaKey ||
        event.ctrlKey ||
        event.altKey ||
        active.current === "pointerup" ||
        (event.target instanceof HTMLInputElement &&
          event.target.type !== "range")
      ) {
        return;
      }
      // Unused keys create no history entry; shortcuts keep their own transactions.
      begin("keyup");
    },
  };
}