|
| 1 | +import type { RefObject } from "react"; |
| 2 | +import { type DomEditSelection } from "./domEditing"; |
| 3 | +import type { GroupOverlayItem, OverlayRect } from "./domEditOverlayGeometry"; |
| 4 | +import type { BlockedMoveState, ResizeHandle } from "./domEditOverlayGestures"; |
| 5 | +import type { createDomEditOverlayGestureHandlers } from "./useDomEditOverlayGestures"; |
| 6 | +import { DomEditCropHandles } from "./DomEditCropHandles"; |
| 7 | +import { DomEditRotateHandle } from "./DomEditRotateHandle"; |
| 8 | +import { resolveRotatedResizeCursor } from "./domEditResizeLocal"; |
| 9 | + |
| 10 | +// Corner resize handles, Canva-style: one per corner, diagonal cursors. |
| 11 | +// Corners scale about the element center; the translate keeps the center |
| 12 | +// planted, so they need the manual-offset capability in addition to manual-size. |
| 13 | +const RESIZE_HANDLE_DEFS: Array<{ |
| 14 | + handle: ResizeHandle; |
| 15 | + cursor: string; |
| 16 | + x: "left" | "right"; |
| 17 | + y: "top" | "bottom"; |
| 18 | +}> = [ |
| 19 | + { handle: "nw", cursor: "nwse-resize", x: "left", y: "top" }, |
| 20 | + { handle: "ne", cursor: "nesw-resize", x: "right", y: "top" }, |
| 21 | + { handle: "sw", cursor: "nesw-resize", x: "left", y: "bottom" }, |
| 22 | + { handle: "se", cursor: "nwse-resize", x: "right", y: "bottom" }, |
| 23 | +]; |
| 24 | + |
| 25 | +// Visible dot is 9px; the pointer target is a 16px invisible square centered |
| 26 | +// on the corner so click targets don't shrink with the smaller dot. |
| 27 | +const RESIZE_HANDLE_HIT_PX = 16; |
| 28 | + |
| 29 | +type CropInset = { top: number; right: number; bottom: number; left: number }; |
| 30 | +const NO_CROP_INSET: CropInset = { top: 0, right: 0, bottom: 0, left: 0 }; |
| 31 | + |
| 32 | +function resizeHandleStyle( |
| 33 | + def: (typeof RESIZE_HANDLE_DEFS)[number], |
| 34 | + overlayRect: { left: number; top: number; width: number; height: number }, |
| 35 | + cropInset?: CropInset, |
| 36 | +): React.CSSProperties { |
| 37 | + const half = RESIZE_HANDLE_HIT_PX / 2; |
| 38 | + const inset = cropInset ?? NO_CROP_INSET; |
| 39 | + const style: React.CSSProperties = { cursor: def.cursor, touchAction: "none" }; |
| 40 | + // Position relative to the overlay container (not the selection box). |
| 41 | + // This ensures the dots render as siblings of the box border div — strictly |
| 42 | + // above it — rather than as children where the parent border can visually |
| 43 | + // overlap the dot circle at the corner. |
| 44 | + style.left = |
| 45 | + def.x === "left" |
| 46 | + ? overlayRect.left + inset.left - half |
| 47 | + : overlayRect.left + overlayRect.width - inset.right - half; |
| 48 | + style.top = |
| 49 | + def.y === "top" |
| 50 | + ? overlayRect.top + inset.top - half |
| 51 | + : overlayRect.top + overlayRect.height - inset.bottom - half; |
| 52 | + return style; |
| 53 | +} |
| 54 | + |
| 55 | +type GestureHandlers = ReturnType<typeof createDomEditOverlayGestureHandlers>; |
| 56 | + |
| 57 | +interface DomEditGroupChromeProps { |
| 58 | + groupOverlayItems: GroupOverlayItem[]; |
| 59 | + groupBounds: OverlayRect; |
| 60 | + allowCanvasMovement: boolean; |
| 61 | + groupCanMove: boolean; |
| 62 | + gestures: GestureHandlers; |
| 63 | + onBoxMouseDown: (e: React.MouseEvent) => void; |
| 64 | + onBoxClick: (event: React.MouseEvent<HTMLDivElement>) => void; |
| 65 | +} |
| 66 | + |
| 67 | +// Multi-selection chrome: per-member outlines plus a single draggable bounding |
| 68 | +// box spanning the union of the members. |
| 69 | +export function DomEditGroupChrome({ |
| 70 | + groupOverlayItems, |
| 71 | + groupBounds, |
| 72 | + allowCanvasMovement, |
| 73 | + groupCanMove, |
| 74 | + gestures, |
| 75 | + onBoxMouseDown, |
| 76 | + onBoxClick, |
| 77 | +}: DomEditGroupChromeProps) { |
| 78 | + return ( |
| 79 | + <> |
| 80 | + {groupOverlayItems.map((item) => ( |
| 81 | + <div |
| 82 | + key={item.key} |
| 83 | + aria-hidden="true" |
| 84 | + className="pointer-events-none absolute rounded-xl border border-studio-accent/70" |
| 85 | + style={{ |
| 86 | + left: item.rect.left, |
| 87 | + top: item.rect.top, |
| 88 | + width: item.rect.width, |
| 89 | + height: item.rect.height, |
| 90 | + }} |
| 91 | + /> |
| 92 | + ))} |
| 93 | + <div |
| 94 | + data-dom-edit-selection-box="true" |
| 95 | + className="pointer-events-auto absolute rounded-xl border border-studio-accent shadow-[0_0_0_1px_rgba(60,230,172,0.3)]" |
| 96 | + style={{ |
| 97 | + left: groupBounds.left, |
| 98 | + top: groupBounds.top, |
| 99 | + width: groupBounds.width, |
| 100 | + height: groupBounds.height, |
| 101 | + cursor: allowCanvasMovement && groupCanMove ? "move" : "default", |
| 102 | + }} |
| 103 | + onPointerDown={(e) => { |
| 104 | + if (!allowCanvasMovement || !groupCanMove || e.shiftKey) return; |
| 105 | + gestures.startGroupDrag(e); |
| 106 | + }} |
| 107 | + onMouseDown={onBoxMouseDown} |
| 108 | + onClick={onBoxClick} |
| 109 | + /> |
| 110 | + </> |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +interface DomEditSelectionChromeProps { |
| 115 | + selection: DomEditSelection; |
| 116 | + overlayRect: OverlayRect; |
| 117 | + allowCanvasMovement: boolean; |
| 118 | + cropOutlineInsetPx?: { top: number; right: number; bottom: number; left: number }; |
| 119 | + boxRef: RefObject<HTMLDivElement | null>; |
| 120 | + boxChromeClass: string; |
| 121 | + boxClipPath: string | undefined; |
| 122 | + selectionKey: string; |
| 123 | + groupSelectionCount: number; |
| 124 | + blockedMoveRef: RefObject<BlockedMoveState | null>; |
| 125 | + gestures: GestureHandlers; |
| 126 | + onStyleCommit?: (property: string, value: string) => Promise<void> | void; |
| 127 | + onBoxMouseDown: (e: React.MouseEvent) => void; |
| 128 | + onBoxClick: (event: React.MouseEvent<HTMLDivElement>) => void; |
| 129 | +} |
| 130 | + |
| 131 | +// Oriented selection chrome: a rotation wrapper spanning the overlay, rotated by |
| 132 | +// the element's live angle about the selection box CENTER. Its children (border |
| 133 | +// box, corner dots, rotate handle, crop pills) keep their existing |
| 134 | +// overlay-absolute positions — rotating the whole plane about the box center |
| 135 | +// lands them on the element's real transformed corners for free. At angle 0 the |
| 136 | +// transform is a no-op, so the chrome is pixel-identical. |
| 137 | +export function DomEditSelectionChrome({ |
| 138 | + selection, |
| 139 | + overlayRect, |
| 140 | + allowCanvasMovement, |
| 141 | + cropOutlineInsetPx, |
| 142 | + boxRef, |
| 143 | + boxChromeClass, |
| 144 | + boxClipPath, |
| 145 | + selectionKey, |
| 146 | + groupSelectionCount, |
| 147 | + blockedMoveRef, |
| 148 | + gestures, |
| 149 | + onStyleCommit, |
| 150 | + onBoxMouseDown, |
| 151 | + onBoxClick, |
| 152 | +}: DomEditSelectionChromeProps) { |
| 153 | + return ( |
| 154 | + <div |
| 155 | + className="pointer-events-none absolute inset-0" |
| 156 | + style={{ |
| 157 | + transformOrigin: `${overlayRect.left + overlayRect.width / 2}px ${overlayRect.top + overlayRect.height / 2}px`, |
| 158 | + transform: overlayRect.angle ? `rotate(${overlayRect.angle}deg)` : undefined, |
| 159 | + }} |
| 160 | + > |
| 161 | + {allowCanvasMovement && selection.capabilities.canApplyManualRotation && ( |
| 162 | + <DomEditRotateHandle |
| 163 | + overlayRect={overlayRect} |
| 164 | + cropOutlineInsetPx={cropOutlineInsetPx} |
| 165 | + onStartRotate={(e) => { |
| 166 | + e.stopPropagation(); |
| 167 | + gestures.startGesture("rotate", e); |
| 168 | + }} |
| 169 | + /> |
| 170 | + )} |
| 171 | + <div |
| 172 | + key={selectionKey} |
| 173 | + ref={boxRef} |
| 174 | + data-dom-edit-selection-box="true" |
| 175 | + className={`pointer-events-auto absolute rounded-md ${boxChromeClass}`} |
| 176 | + style={{ |
| 177 | + left: overlayRect.left, |
| 178 | + top: overlayRect.top, |
| 179 | + width: overlayRect.width, |
| 180 | + height: overlayRect.height, |
| 181 | + clipPath: boxClipPath, |
| 182 | + cursor: |
| 183 | + allowCanvasMovement && selection.capabilities.canApplyManualOffset ? "move" : "default", |
| 184 | + }} |
| 185 | + onPointerDown={(e) => { |
| 186 | + if (!allowCanvasMovement || e.shiftKey) return; |
| 187 | + if (selection.capabilities.canApplyManualOffset) { |
| 188 | + gestures.startGesture("drag", e); |
| 189 | + return; |
| 190 | + } |
| 191 | + e.preventDefault(); |
| 192 | + e.stopPropagation(); |
| 193 | + e.currentTarget.setPointerCapture(e.pointerId); |
| 194 | + blockedMoveRef.current = { |
| 195 | + pointerId: e.pointerId, |
| 196 | + startX: e.clientX, |
| 197 | + startY: e.clientY, |
| 198 | + notified: false, |
| 199 | + }; |
| 200 | + }} |
| 201 | + onMouseDown={onBoxMouseDown} |
| 202 | + onClick={onBoxClick} |
| 203 | + > |
| 204 | + {cropOutlineInsetPx && ( |
| 205 | + <div |
| 206 | + className="pointer-events-none absolute rounded-md border border-studio-accent/80 shadow-[0_0_0_1px_rgba(60,230,172,0.25)]" |
| 207 | + style={{ |
| 208 | + left: cropOutlineInsetPx.left, |
| 209 | + top: cropOutlineInsetPx.top, |
| 210 | + right: cropOutlineInsetPx.right, |
| 211 | + bottom: cropOutlineInsetPx.bottom, |
| 212 | + }} |
| 213 | + /> |
| 214 | + )} |
| 215 | + </div> |
| 216 | + {/* Resize-handle dots rendered as siblings of the selection box, not |
| 217 | + children, so they paint strictly above the box border. Each handle |
| 218 | + is positioned relative to the overlay container using the |
| 219 | + overlayRect origin, matching the old child-relative offsets. */} |
| 220 | + {allowCanvasMovement && |
| 221 | + selection.capabilities.canApplyManualSize && |
| 222 | + RESIZE_HANDLE_DEFS.map((def) => |
| 223 | + def.handle !== "se" && !selection.capabilities.canApplyManualOffset ? null : ( |
| 224 | + <div |
| 225 | + key={def.handle} |
| 226 | + className="pointer-events-auto absolute flex h-4 w-4 items-center justify-center" |
| 227 | + style={{ |
| 228 | + ...resizeHandleStyle(def, overlayRect, cropOutlineInsetPx ?? undefined), |
| 229 | + // Cursor rotates with the object: bucket the corner's base |
| 230 | + // diagonal + element rotation into the 8 CSS resize cursors. |
| 231 | + cursor: resolveRotatedResizeCursor(def.handle, overlayRect.angle ?? 0), |
| 232 | + }} |
| 233 | + onPointerDown={(e) => { |
| 234 | + e.stopPropagation(); |
| 235 | + gestures.startGesture("resize", e, { resizeHandle: def.handle }); |
| 236 | + }} |
| 237 | + > |
| 238 | + <div className="pointer-events-none h-[12px] w-[12px] rounded-full border-[1.5px] border-studio-accent bg-white shadow-[0_0_3px_rgba(0,0,0,0.45)]" /> |
| 239 | + </div> |
| 240 | + ), |
| 241 | + )} |
| 242 | + {selection.capabilities.canCrop && groupSelectionCount <= 1 && ( |
| 243 | + <DomEditCropHandles |
| 244 | + selection={selection} |
| 245 | + overlayRect={overlayRect} |
| 246 | + onStyleCommit={onStyleCommit} |
| 247 | + /> |
| 248 | + )} |
| 249 | + </div> |
| 250 | + ); |
| 251 | +} |
0 commit comments