import type { CSSProperties, KeyboardEvent, MouseEvent } from 'react'; import { useRef, useState } from 'react'; type CursorOrientation = 'horizontal' | 'vertical'; const CUR_MOVE = `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='22' height='12' viewBox='0 0 22 12'%3E%3Cpath d='M1 6h20M1 6l4-4M1 6l4 4M21 6l-4-4M21 6l-4 4' fill='none' stroke='black' stroke-width='1.2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") 11 6, ew-resize`; const CUR_MOVE_VERTICAL = `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='22' viewBox='0 0 12 22'%3E%3Cpath d='M6 1v20M6 1l-4 4M6 1l4 4M6 21l-4-4M6 21l4-4' fill='none' stroke='black' stroke-width='1.2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") 6 11, ns-resize`; type AnchorShape = 'diamond' | 'circle' | 'square' | 'triangle'; interface AnchorStyle { size?: number; hoverSize?: number; dragSize?: number; fill?: string; stroke?: string; hoverFill?: string; hoverStroke?: string; dragFill?: string; dragStroke?: string; strokeWidth?: number; guideColor?: string; guideDragColor?: string; guideStyle?: 'solid' | 'dashed' | 'dotted'; } interface AnchorPosition { x: number; y: number; } interface AnchorProps { position: AnchorPosition; shape?: AnchorShape; guideLength: number; anchorStyle?: AnchorStyle; restoreFocusOnLeave?: boolean; onDragMove?: (newPosition: AnchorPosition) => void; onDragEnd?: (lastPosition: AnchorPosition) => void; onDragStart?: (position: AnchorPosition) => void; onDelete?: () => void; cursorOrientation?: CursorOrientation; } interface DragState { startX: number; startY: number; startAnchorX: number; startAnchorY: number; } type InteractionState = 'idle' | 'hovered' | 'dragging'; interface VisualTokens { fill: string; stroke: string; cursor: string; } function resolveVisuals( state: InteractionState, idleFill: string, idleStroke: string, hoverFill: string, hoverStroke: string, dragFill: string, dragStroke: string, cursorOrientation: CursorOrientation, ): VisualTokens { const cursor = cursorOrientation === 'horizontal' ? CUR_MOVE : CUR_MOVE_VERTICAL; if (state === 'dragging') { return { fill: dragFill, stroke: dragStroke, cursor, }; } if (state === 'hovered') { return { fill: hoverFill, stroke: hoverStroke, cursor, }; } return { fill: idleFill, stroke: idleStroke, cursor: 'default', }; } function clientToSvg( event: MouseEvent, svg: SVGSVGElement | null, ): { x: number; y: number } { const { clientX, clientY } = event; if (!svg) return { x: clientX, y: clientY }; const pt = svg.createSVGPoint(); pt.x = clientX; pt.y = clientY; const ctm = svg.getScreenCTM(); if (!ctm) return { x: clientX, y: clientY }; return pt.matrixTransform(ctm.inverse()); } interface ShapeRendererProps { shape?: AnchorShape; fill: string; stroke: string; strokeWidth: number; size: number; } function ShapeRenderer({ shape, fill, stroke, strokeWidth, size, }: ShapeRendererProps) { const half = size / 2; const inset = strokeWidth / 2; const common: CSSProperties = { filter: 'drop-shadow(0 2px 4px rgba(0,0,0,.14))', pointerEvents: 'all', }; if (shape === 'circle') { return ( ); } if (shape === 'diamond') { return ( ); } if (shape === 'triangle') { return ( ); } return ( ); } export function Anchor(props: AnchorProps) { const { position, shape, guideLength, anchorStyle = {}, restoreFocusOnLeave = false, onDragStart, onDragMove, onDragEnd, onDelete, cursorOrientation = 'horizontal', } = props; const { size = 16, hoverSize = size, dragSize = size, fill: idleFill = '#ffffff', stroke: idleStroke = '#d1d5db', hoverFill = '#f3f4f6', hoverStroke = '#9ca3af', dragFill = '#f3f4f6', dragStroke = '#6b7280', strokeWidth = 1, guideColor = 'rgba(156,163,175,0.6)', guideDragColor = 'rgba(107,114,128,0.8)', guideStyle = 'solid', } = anchorStyle; const [hovered, setHovered] = useState(false); const [dragging, setDragging] = useState(false); const dragState = useRef(null); const prevFocus = useRef(null); const interactionState: InteractionState = dragging ? 'dragging' : hovered ? 'hovered' : 'idle'; const { fill, stroke, cursor } = resolveVisuals( interactionState, idleFill, idleStroke, hoverFill, hoverStroke, dragFill, dragStroke, cursorOrientation, ); function handleMouseDown(e: MouseEvent) { e.preventDefault(); setDragging(true); const svg = e.currentTarget.ownerSVGElement; const { x, y } = clientToSvg(e, svg); dragState.current = { startX: x, startY: y, startAnchorX: position.x, startAnchorY: position.y, }; onDragStart?.(position); const onMove = (e: globalThis.MouseEvent) => { const current = dragState.current; if (!current) return; const { x, y } = clientToSvg( e as unknown as MouseEvent, svg, ); const newX = current.startAnchorX + x - current.startX; const newY = current.startAnchorY + y - current.startY; onDragMove?.({ x: newX, y: newY }); }; const onUp = () => { setDragging(false); dragState.current = null; onDragEnd?.({ x: position.x, y: position.y }); window.removeEventListener('mousemove', onMove); window.removeEventListener('mouseup', onUp); }; window.addEventListener('mousemove', onMove); window.addEventListener('mouseup', onUp); } function handleKeyDown(e: KeyboardEvent) { if (e.key === 'Delete' || e.key === 'Backspace') { e.preventDefault(); onDelete?.(); } } function handleMouseEnter(e: MouseEvent) { setHovered(true); prevFocus.current = document.activeElement; e.currentTarget.focus(); } function handleMouseLeave(e: MouseEvent) { setHovered(false); if (restoreFocusOnLeave && prevFocus.current instanceof HTMLElement) { prevFocus.current.focus(); } else { e.currentTarget.blur(); } prevFocus.current = null; } const isVerticalOrientation = cursorOrientation === 'vertical'; return ( <> {(hovered || dragging) && ( )} {(hovered || dragging) && ( )} ); }