All files / webview-app/src/hooks useDragInteraction.ts

84.21% Statements 32/38
71.42% Branches 20/28
100% Functions 4/4
84.21% Lines 32/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174                                                                                                          13x 13x 13x   13x 13x   13x   2x       2x 2x   2x 2x   2x                         2x   2x                         13x   4x       4x               4x 4x   4x 2x         2x     2x       2x                 2x         13x   2x       2x               2x 2x 2x         13x                
import { useCallback, useState } from "react";
 
import { calculateResize, type Rect, type ResizeHandle, snapToGrid } from "@/lib/geometry";
import type { CanvasComponent as CanvasComponentModel } from "@/types/canvas";
 
interface DragStartOptions {
  mode?: "move" | "resize";
  handle?: ResizeHandle;
}
 
interface DragSession {
  mode: "move" | "resize";
  pointerId: number | null;
  handle?: ResizeHandle;
  startClientX: number;
  startClientY: number;
  startRect: Rect;
}
 
type DragPointerEvent = Pick<
  React.PointerEvent<HTMLElement>,
  "button" | "clientX" | "clientY" | "stopPropagation"
> &
  Partial<Pick<React.PointerEvent<HTMLElement>, "pointerId">>;
 
interface UseDragInteractionOptions {
  component: Pick<CanvasComponentModel, "id" | "x" | "y" | "width" | "height">;
  zoom: number;
  onSelect: (id: string) => void;
  onMove: (id: string, nextX: number, nextY: number) => void;
  onResize: (
    id: string,
    updates: Pick<CanvasComponentModel, "x" | "y" | "width" | "height">,
  ) => void;
  minWidth?: number;
  minHeight?: number;
  gridSize?: number;
  onInteractionStart?: (id: string, mode: "move" | "resize") => void;
  onInteractionEnd?: (id: string, mode: "move" | "resize") => void;
}
 
export function useDragInteraction({
  component,
  zoom,
  onSelect,
  onMove,
  onResize,
  minWidth = 48,
  minHeight = 28,
  gridSize = 1,
  onInteractionStart,
  onInteractionEnd,
}: UseDragInteractionOptions) {
  const [interaction, setInteraction] = useState<DragSession | null>(null);
  const safeMinWidth = Math.max(1, Math.round(minWidth));
  const safeMinHeight = Math.max(1, Math.round(minHeight));
 
  const isDragging = interaction !== null;
  const isResizing = interaction?.mode === "resize";
 
  const handleMouseDown = useCallback(
    (event: DragPointerEvent, options: DragStartOptions = { mode: "move" }) => {
      Iif (event.button !== 0) {
        return false;
      }
 
      event.stopPropagation();
      onSelect(component.id);
 
      const pointerId = typeof event.pointerId === "number" ? event.pointerId : null;
      const mode = options.mode ?? "move";
 
      setInteraction({
        mode,
        pointerId,
        handle: options.handle,
        startClientX: event.clientX,
        startClientY: event.clientY,
        startRect: {
          x: component.x,
          y: component.y,
          width: component.width,
          height: component.height,
        },
      });
      onInteractionStart?.(component.id, mode);
 
      return true;
    },
    [
      component.height,
      component.id,
      component.width,
      component.x,
      component.y,
      onInteractionStart,
      onSelect,
    ],
  );
 
  const handleMouseMove = useCallback(
    (event: DragPointerEvent) => {
      Iif (!interaction) {
        return;
      }
 
      Iif (
        interaction.pointerId !== null &&
        typeof event.pointerId === "number" &&
        event.pointerId !== interaction.pointerId
      ) {
        return;
      }
 
      const deltaX = (event.clientX - interaction.startClientX) / zoom;
      const deltaY = (event.clientY - interaction.startClientY) / zoom;
 
      if (interaction.mode === "move") {
        onMove(
          component.id,
          snapToGrid(interaction.startRect.x + deltaX, gridSize),
          snapToGrid(interaction.startRect.y + deltaY, gridSize),
        );
        return;
      }
 
      Iif (!interaction.handle) {
        return;
      }
 
      const nextRect = calculateResize(interaction.startRect, {
        handle: interaction.handle,
        deltaX,
        deltaY,
        minWidth: safeMinWidth,
        minHeight: safeMinHeight,
        gridSize,
      });
 
      onResize(component.id, nextRect);
    },
    [component.id, gridSize, interaction, onMove, onResize, safeMinHeight, safeMinWidth, zoom],
  );
 
  const handleMouseUp = useCallback(
    (event: DragPointerEvent) => {
      Iif (!interaction) {
        return false;
      }
 
      Iif (
        interaction.pointerId !== null &&
        typeof event.pointerId === "number" &&
        event.pointerId !== interaction.pointerId
      ) {
        return false;
      }
 
      onInteractionEnd?.(component.id, interaction.mode);
      setInteraction(null);
      return true;
    },
    [component.id, interaction, onInteractionEnd],
  );
 
  return {
    isDragging,
    isResizing,
    handleMouseDown,
    handleMouseMove,
    handleMouseUp,
  };
}