Skip to content

Free open-source image editor for the browser

5.0 KB
Raw Blame
import { type PointerEvent, useEffect, useRef } from "react";
import { useDocumentMapping } from "@/components/editor/mapping";
import { useDocument } from "@/components/editor/session";
import { useViewport } from "@/components/editor/viewport";
import type { Point } from "@/core/image/frame";

type Drag = {
  pointer: number;
  box: DOMRect;
  start: Point;
  /** The value when the drag began; a live edit changes the rendered value under the pointer. */
  from: Point;
  next: Point;
  unsubscribe: () => void;
};

/** A dragged value: the destination's first point or the donor offset, changed as one history edit. */
export type AnchorDrag = {
  from: Point;
  editOnRelease?: boolean;
  /** Follows the pointer; no value ends the drag. */
  onDrag: (next?: Point) => void;
  onDrop: (next: Point) => void;
};

const marker = { r: 7, fill: "#3b82f6", stroke: "white", strokeWidth: 2 };

/** Viewport pixels from an anchor's center that grab or select it. */
export const anchorReach = 14;

/** The first-point anchor of a destination or source contour, draggable while its patch is selected. */
export function HealAnchor({
  kind,
  center,
  drag,
}: {
  kind: "destination" | "source";
  center: Point;
  drag?: AnchorDrag;
}) {
  if (drag) return <DraggedAnchor kind={kind} center={center} drag={drag} />;
  return (
    <circle
      {...{ [`data-heal-${kind}-anchor`]: "true" }}
      cx={center[0]}
      cy={center[1]}
      {...marker}
    />
  );
}

function DraggedAnchor({
  kind,
  center,
  drag,
}: {
  kind: "destination" | "source";
  center: Point;
  drag: AnchorDrag;
}) {
  const document = useDocument();
  const mapping = useDocumentMapping();
  const camera = useViewport();
  const current = useRef<Drag | undefined>(undefined);
  const opened = useRef(false);
  /** A drag inside an open group, such as a finishing stroke, joins it and leaves the group to its opener. */
  function end(commit: boolean) {
    if (!opened.current) return;
    opened.current = false;
    if (commit) document.history.commit();
    else document.history.cancel();
  }
  function point(event: PointerEvent<SVGCircleElement>, box: DOMRect) {
    return mapping.toDocument([
      event.clientX - box.left,
      event.clientY - box.top,
    ]);
  }
  function cancel() {
    const active = current.current;
    if (!active) return;
    current.current = undefined;
    active.unsubscribe();
    drag.onDrag();
    end(false);
  }
  useEffect(() => () => cancel(), []);
  function start(event: PointerEvent<SVGCircleElement>) {
    const box = camera.ref.current?.getBoundingClientRect();
    const at = box && point(event, box);
    if (
      current.current ||
      event.button !== 0 ||
      !event.isPrimary ||
      camera.panMode ||
      event.shiftKey ||
      event.altKey ||
      !box ||
      !at
    ) {
      return;
    }
    opened.current = !drag.editOnRelease && document.history.begin();
    current.current = {
      pointer: event.pointerId,
      box,
      start: at,
      from: drag.from,
      next: drag.from,
      unsubscribe: document.history.status.subscribe((state, previous) => {
        if (
          !state.editing &&
          (previous.editing ||
            state.undoCount !== previous.undoCount ||
            state.redoCount !== previous.redoCount)
        )
          cancel();
      }),
    };
    event.preventDefault();
    event.stopPropagation();
    event.currentTarget.setPointerCapture(event.pointerId);
  }
  function move(event: PointerEvent<SVGCircleElement>) {
    const active = current.current;
    if (!active || active.pointer !== event.pointerId) return;
    const at = point(event, active.box);
    // Past where input stops the anchor waits for the pointer to return.
    if (!at) return;
    active.next = [
      active.from[0] + at[0] - active.start[0],
      active.from[1] + at[1] - active.start[1],
    ];
    drag.onDrag(active.next);
    event.preventDefault();
    event.stopPropagation();
  }
  function finish(event: PointerEvent<SVGCircleElement>) {
    const active = current.current;
    if (!active || active.pointer !== event.pointerId) return;
    current.current = undefined;
    active.unsubscribe();
    event.stopPropagation();
    event.currentTarget.releasePointerCapture(event.pointerId);
    const moved =
      active.next[0] !== active.from[0] || active.next[1] !== active.from[1];
    if (!moved) {
      drag.onDrag();
      end(false);
      return;
    }
    drag.onDrop(active.next);
    drag.onDrag();
    end(true);
  }
  return (
    <g>
      <circle
        cx={center[0]}
        cy={center[1]}
        {...marker}
        className="pointer-events-none"
      />
      <circle
        {...{ [`data-heal-${kind}-handle`]: "true" }}
        data-hide-brush-cursor="true"
        cx={center[0]}
        cy={center[1]}
        r={anchorReach}
        fill="transparent"
        className="pointer-events-auto cursor-grab active:cursor-grabbing"
        onPointerDown={start}
        onPointerMove={move}
        onPointerUp={finish}
        onPointerCancel={cancel}
        onLostPointerCapture={cancel}
      />
    </g>
  );
}