| 9 | import { getPointerPositionRelativeToStage } from "./getPointerPosition"; |
| 10 | |
| 11 | export function useDrawArrowTool() { |
| 12 | const tool = useStateStore((state) => state.selectedTool); |
| 13 | const properties = useToolPropertiesStore( |
| 14 | (s) => s.properties.arrow ?? defaultArrowProperties |
| 15 | ); |
| 16 | |
| 17 | const arrows = useHistoryStore((state) => state.current.arrows); |
| 18 | const addArrow = useHistoryStore((state) => state.addArrow); |
| 19 | const updateArrowInStore = useHistoryStore((state) => state.updateArrow); |
| 20 | const saveToHistory = useHistoryStore((state) => state.saveToHistory); |
| 21 | |
| 22 | const isDrawing = useRef(false); |
| 23 | const currentArrowId = useRef<string | null>(null); |
| 24 | |
| 25 | const handlePointerDown = (e: KonvaEventObject<MouseEvent | TouchEvent>) => { |
| 26 | if (tool !== "arrow") return; |
| 27 | |
| 28 | // Save current state before starting new drawing |
| 29 | saveToHistory(); |
| 30 | |
| 31 | isDrawing.current = true; |
| 32 | |
| 33 | const stage = e.target.getStage(); |
| 34 | const pos = stage ? getPointerPositionRelativeToStage(stage) : null; |
| 35 | if (!pos) return; |
| 36 | |
| 37 | const id = createShapeId(); |
| 38 | currentArrowId.current = id; |
| 39 | |
| 40 | addArrow({ |
| 41 | id, |
| 42 | x: pos.x, |
| 43 | y: pos.y, |
| 44 | points: [0, 0, 0, 0], |
| 45 | stroke: properties.stroke, |
| 46 | strokeWidth: properties.strokeWidth, |
| 47 | opacity: properties.opacity, |
| 48 | }); |
| 49 | }; |
| 50 | |
| 51 | const handlePointerMove = (e: KonvaEventObject<MouseEvent | TouchEvent>) => { |
| 52 | if (!isDrawing.current || tool !== "arrow") return; |
| 53 | |
| 54 | const stage = e.target.getStage(); |
| 55 | const pos = stage ? getPointerPositionRelativeToStage(stage) : null; |
| 56 | if (!pos) return; |
| 57 | |
| 58 | if (currentArrowId.current) { |
| 59 | const arrow = arrows.find((a) => a.id === currentArrowId.current); |
| 60 | if (arrow) { |
| 61 | updateArrowInStore(currentArrowId.current, { |
| 62 | points: [0, 0, pos.x - arrow.x, pos.y - arrow.y], |
| 63 | }); |
| 64 | } |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | const handlePointerUp = () => { |