|
| 1 | +import React, { useState } from "react" |
| 2 | +import { View } from "react-native" |
| 3 | +import { GestureDetector, Gesture } from "react-native-gesture-handler" |
| 4 | +import Svg, { Path } from "react-native-svg" |
| 5 | +import type { AnnotationStore } from "./store" |
| 6 | +import type { Shape, Tool, PenPoint } from "@reprojs/sdk-utils" |
| 7 | +import { newShapeId } from "@reprojs/sdk-utils" |
| 8 | + |
| 9 | +interface Props { |
| 10 | + width: number |
| 11 | + height: number |
| 12 | + tool: Tool |
| 13 | + color: string |
| 14 | + strokeWidth: number |
| 15 | + store: AnnotationStore |
| 16 | +} |
| 17 | + |
| 18 | +export function AnnotationCanvas({ width, height, tool, color, strokeWidth, store }: Props) { |
| 19 | + const [draft, setDraft] = useState<PenPoint[]>([]) |
| 20 | + |
| 21 | + const pan = Gesture.Pan() |
| 22 | + .onStart((e) => { |
| 23 | + setDraft([{ x: e.x, y: e.y, p: 1 }]) |
| 24 | + }) |
| 25 | + .onUpdate((e) => { |
| 26 | + setDraft((d) => [...d, { x: e.x, y: e.y, p: 1 }]) |
| 27 | + }) |
| 28 | + .onEnd(() => { |
| 29 | + if (draft.length === 0) { |
| 30 | + setDraft([]) |
| 31 | + return |
| 32 | + } |
| 33 | + const shape = buildShape(tool, draft, color, strokeWidth) |
| 34 | + if (shape) store.addShape(shape) |
| 35 | + setDraft([]) |
| 36 | + }) |
| 37 | + |
| 38 | + return ( |
| 39 | + <GestureDetector gesture={pan}> |
| 40 | + <View style={{ width, height }}> |
| 41 | + <Svg width={width} height={height}> |
| 42 | + {store.snapshot().map((s, i) => renderCommitted(s, i))} |
| 43 | + {draft.length > 0 && renderDraft(tool, draft, color, strokeWidth)} |
| 44 | + </Svg> |
| 45 | + </View> |
| 46 | + </GestureDetector> |
| 47 | + ) |
| 48 | +} |
| 49 | + |
| 50 | +function buildShape( |
| 51 | + tool: Tool, |
| 52 | + points: PenPoint[], |
| 53 | + color: string, |
| 54 | + strokeWidth: number, |
| 55 | +): Shape | null { |
| 56 | + const first = points[0] |
| 57 | + const last = points[points.length - 1] |
| 58 | + if (!first || !last) return null |
| 59 | + const id = newShapeId() |
| 60 | + if (tool === "pen") return { kind: "pen", id, color, strokeWidth, points } |
| 61 | + if (tool === "arrow") |
| 62 | + return { |
| 63 | + kind: "arrow", |
| 64 | + id, |
| 65 | + color, |
| 66 | + strokeWidth, |
| 67 | + x1: first.x, |
| 68 | + y1: first.y, |
| 69 | + x2: last.x, |
| 70 | + y2: last.y, |
| 71 | + } |
| 72 | + if (tool === "rect" || tool === "highlight") { |
| 73 | + return { |
| 74 | + kind: tool, |
| 75 | + id, |
| 76 | + color, |
| 77 | + strokeWidth, |
| 78 | + x: Math.min(first.x, last.x), |
| 79 | + y: Math.min(first.y, last.y), |
| 80 | + w: Math.abs(last.x - first.x), |
| 81 | + h: Math.abs(last.y - first.y), |
| 82 | + } |
| 83 | + } |
| 84 | + if (tool === "text") |
| 85 | + return { |
| 86 | + kind: "text", |
| 87 | + id, |
| 88 | + color, |
| 89 | + strokeWidth, |
| 90 | + x: first.x, |
| 91 | + y: first.y, |
| 92 | + w: 120, |
| 93 | + h: 24, |
| 94 | + content: "Tap to edit", |
| 95 | + fontSize: 16, |
| 96 | + } |
| 97 | + return null |
| 98 | +} |
| 99 | + |
| 100 | +function renderCommitted(s: Shape, key: number): React.ReactNode { |
| 101 | + if (s.kind === "pen") { |
| 102 | + const d = s.points.map((pt, i) => `${i === 0 ? "M" : "L"}${pt.x},${pt.y}`).join(" ") |
| 103 | + return <Path key={key} d={d} stroke={s.color} strokeWidth={s.strokeWidth} fill="none" /> |
| 104 | + } |
| 105 | + // v1: only pen strokes rendered in-canvas; other shapes appear via the flatten view on submit. |
| 106 | + return null |
| 107 | +} |
| 108 | + |
| 109 | +function renderDraft( |
| 110 | + tool: Tool, |
| 111 | + points: PenPoint[], |
| 112 | + color: string, |
| 113 | + strokeWidth: number, |
| 114 | +): React.ReactNode { |
| 115 | + if (tool !== "pen") return null |
| 116 | + const d = points.map((pt, i) => `${i === 0 ? "M" : "L"}${pt.x},${pt.y}`).join(" ") |
| 117 | + return <Path d={d} stroke={color} strokeWidth={strokeWidth} fill="none" /> |
| 118 | +} |
0 commit comments