@reekon-tools/boldr-utils 1.6.17 → 1.6.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/canvas/AnnotationCanvas.d.ts +11 -0
- package/dist/canvas/AnnotationCanvas.js +10 -0
- package/dist/canvas/AnnotationCanvas.native.d.ts +8 -0
- package/dist/canvas/AnnotationCanvas.native.js +6 -0
- package/dist/canvas/AnnotationCanvasInner.d.ts +39 -0
- package/dist/canvas/AnnotationCanvasInner.js +219 -0
- package/dist/canvas/AnnotationCanvasInner.native.d.ts +35 -0
- package/dist/canvas/AnnotationCanvasInner.native.js +138 -0
- package/dist/canvas/AnnotationCanvasSkia.d.ts +27 -0
- package/dist/canvas/AnnotationCanvasSkia.js +20 -0
- package/dist/canvas/Tool.d.ts +38 -0
- package/dist/canvas/Tool.js +1 -0
- package/dist/canvas/elements/BackgroundImageElement.d.ts +9 -0
- package/dist/canvas/elements/BackgroundImageElement.js +37 -0
- package/dist/canvas/elements/MeasurementStampElement.d.ts +13 -0
- package/dist/canvas/elements/MeasurementStampElement.js +30 -0
- package/dist/canvas/elements/ShapeElement.d.ts +7 -0
- package/dist/canvas/elements/ShapeElement.js +62 -0
- package/dist/canvas/elements/StrokeElement.d.ts +7 -0
- package/dist/canvas/elements/StrokeElement.js +18 -0
- package/dist/canvas/measurementPicker.d.ts +10 -0
- package/dist/canvas/measurementPicker.js +1 -0
- package/dist/canvas/measurementStampOverlay.d.ts +11 -0
- package/dist/canvas/measurementStampOverlay.js +1 -0
- package/dist/canvas/pointerAdapter.d.ts +3 -0
- package/dist/canvas/pointerAdapter.js +19 -0
- package/dist/canvas/stampLayout.d.ts +5 -0
- package/dist/canvas/stampLayout.js +14 -0
- package/dist/canvas/tools/measurementStampTool.d.ts +9 -0
- package/dist/canvas/tools/measurementStampTool.js +37 -0
- package/dist/canvas/tools/panTool.d.ts +5 -0
- package/dist/canvas/tools/panTool.js +25 -0
- package/dist/canvas/tools/penTool.d.ts +13 -0
- package/dist/canvas/tools/penTool.js +68 -0
- package/dist/canvas/tools/selectTool.d.ts +2 -0
- package/dist/canvas/tools/selectTool.js +182 -0
- package/dist/canvas/useAnnotationCanvasState.d.ts +54 -0
- package/dist/canvas/useAnnotationCanvasState.js +210 -0
- package/dist/canvas/viewport.d.ts +16 -0
- package/dist/canvas/viewport.js +54 -0
- package/dist/data/AnnotationDataContext.d.ts +8 -0
- package/dist/data/AnnotationDataContext.js +11 -0
- package/dist/data/AnnotationDataProvider.d.ts +65 -0
- package/dist/data/AnnotationDataProvider.js +4 -0
- package/dist/data/InMemoryAnnotationProvider.d.ts +30 -0
- package/dist/data/InMemoryAnnotationProvider.js +197 -0
- package/dist/data/canvasPersistence.d.ts +3 -0
- package/dist/data/canvasPersistence.js +26 -0
- package/dist/data/hooks/useAnnotationCanvasDoc.d.ts +33 -0
- package/dist/data/hooks/useAnnotationCanvasDoc.js +314 -0
- package/dist/data/hooks/useAnnotationDoc.d.ts +7 -0
- package/dist/data/hooks/useAnnotationDoc.js +33 -0
- package/dist/data/hooks/useAnnotationList.d.ts +7 -0
- package/dist/data/hooks/useAnnotationList.js +26 -0
- package/dist/data/hooks/useAnnotationMutations.d.ts +9 -0
- package/dist/data/hooks/useAnnotationMutations.js +11 -0
- package/dist/hooks/useParseMeasurement.d.ts +4 -0
- package/dist/hooks/useParseMeasurement.js +14 -0
- package/dist/types/firestore.d.ts +1 -0
- package/dist/utils/evaluateFormula.d.ts +20 -0
- package/dist/utils/evaluateFormula.js +31 -0
- package/package.json +1 -1
- package/dist/annotation/canvas/tools/measurementLineTool.d.ts +0 -12
- package/dist/annotation/canvas/tools/measurementLineTool.js +0 -95
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Group, Line, RoundedRect, Text, } from '@shopify/react-native-skia';
|
|
3
|
+
import { DecimalTolerance, FractionalTolerance, } from '../../types/firestore.js';
|
|
4
|
+
import { convertMicrometers } from '../../utils/micrometersToUnit.js';
|
|
5
|
+
import { STAMP_HEIGHT, STAMP_PADDING_X, STAMP_PADDING_Y, STAMP_WIDTH, } from '../stampLayout.js';
|
|
6
|
+
const VALUE_FONT_BASELINE_OFFSET = 14;
|
|
7
|
+
const LABEL_FONT_BASELINE_OFFSET = 24;
|
|
8
|
+
const formatValue = (measurement, unit, fractionalTolerance, decimalTolerance) => {
|
|
9
|
+
if (!measurement)
|
|
10
|
+
return '—';
|
|
11
|
+
const result = convertMicrometers(measurement.value, unit, fractionalTolerance, decimalTolerance);
|
|
12
|
+
return `${result.value}${result.unit ? ` ${result.unit}` : ''}`;
|
|
13
|
+
};
|
|
14
|
+
export const MeasurementStampElement = ({ placed, measurement, fallbackUnit, fractionalTolerance = FractionalTolerance.Sixteenth, decimalTolerance = DecimalTolerance.Hundredth, valueFont, labelFont, }) => {
|
|
15
|
+
const unit = placed.unitOverride ?? measurement?.unit ?? fallbackUnit;
|
|
16
|
+
const valueText = formatValue(measurement, unit, fractionalTolerance, decimalTolerance);
|
|
17
|
+
const label = placed.labelOverride ??
|
|
18
|
+
measurement?.label ??
|
|
19
|
+
(measurement
|
|
20
|
+
? `M${measurement.measurementIndex ?? ''}`
|
|
21
|
+
: '');
|
|
22
|
+
const showLabel = placed.showLabel !== false && !!label;
|
|
23
|
+
const showValue = placed.showValue !== false;
|
|
24
|
+
const scale = placed.scale ?? 1;
|
|
25
|
+
// Fixed-size stamp — no measureText calls. Keeps rendering deterministic
|
|
26
|
+
// even when no font is loaded (browser/native both).
|
|
27
|
+
const baseX = placed.anchor.x - (STAMP_WIDTH * scale) / 2;
|
|
28
|
+
const baseY = placed.anchor.y - (STAMP_HEIGHT * scale) / 2;
|
|
29
|
+
return (_jsxs(Group, { transform: [{ translateX: baseX }, { translateY: baseY }, { scale }], children: [placed.leader && (_jsx(Line, { p1: placed.leader.from, p2: placed.leader.to, color: "#3B82F6", style: "stroke", strokeWidth: 1.5 })), _jsx(RoundedRect, { x: 0, y: 0, width: STAMP_WIDTH, height: STAMP_HEIGHT, r: 6, color: "#FFFFFFEE" }), _jsx(RoundedRect, { x: 0, y: 0, width: STAMP_WIDTH, height: STAMP_HEIGHT, r: 6, color: "#3B82F6", style: "stroke", strokeWidth: 1.25 }), showValue && valueFont && (_jsx(Text, { x: STAMP_PADDING_X, y: STAMP_PADDING_Y + VALUE_FONT_BASELINE_OFFSET, text: valueText, font: valueFont, color: "#111827" })), showLabel && labelFont && (_jsx(Text, { x: STAMP_PADDING_X, y: STAMP_PADDING_Y + LABEL_FONT_BASELINE_OFFSET, text: label, font: labelFont, color: "#6B7280" }))] }));
|
|
30
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type SkFont } from '@shopify/react-native-skia';
|
|
2
|
+
import type { AnnotationShape } from '../../types/annotation.js';
|
|
3
|
+
export interface ShapeElementProps {
|
|
4
|
+
shape: AnnotationShape;
|
|
5
|
+
font?: SkFont | null;
|
|
6
|
+
}
|
|
7
|
+
export declare const ShapeElement: ({ shape, font }: ShapeElementProps) => import("react/jsx-runtime").JSX.Element | null;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Circle, Line, Path, Rect, Skia, Text, } from '@shopify/react-native-skia';
|
|
3
|
+
import { useMemo } from 'react';
|
|
4
|
+
const polygonPath = (points) => {
|
|
5
|
+
const path = Skia.Path.Make();
|
|
6
|
+
if (points.length === 0)
|
|
7
|
+
return path;
|
|
8
|
+
path.moveTo(points[0].x, points[0].y);
|
|
9
|
+
for (let i = 1; i < points.length; i++) {
|
|
10
|
+
path.lineTo(points[i].x, points[i].y);
|
|
11
|
+
}
|
|
12
|
+
path.close();
|
|
13
|
+
return path;
|
|
14
|
+
};
|
|
15
|
+
export const ShapeElement = ({ shape, font }) => {
|
|
16
|
+
const { kind, geometry, style, text } = shape;
|
|
17
|
+
const stroke = style.stroke ?? '#000000';
|
|
18
|
+
const fill = style.fill;
|
|
19
|
+
const strokeWidth = style.strokeWidth ?? 2;
|
|
20
|
+
const polyPath = useMemo(() => (kind === 'polygon' ? polygonPath(geometry.points) : null), [kind, geometry.points]);
|
|
21
|
+
switch (kind) {
|
|
22
|
+
case 'rect': {
|
|
23
|
+
const [a, b] = geometry.points;
|
|
24
|
+
if (!a || !b)
|
|
25
|
+
return null;
|
|
26
|
+
const x = Math.min(a.x, b.x);
|
|
27
|
+
const y = Math.min(a.y, b.y);
|
|
28
|
+
const w = Math.abs(b.x - a.x);
|
|
29
|
+
const h = Math.abs(b.y - a.y);
|
|
30
|
+
return (_jsxs(_Fragment, { children: [fill && _jsx(Rect, { x: x, y: y, width: w, height: h, color: fill }), _jsx(Rect, { x: x, y: y, width: w, height: h, color: stroke, style: "stroke", strokeWidth: strokeWidth })] }));
|
|
31
|
+
}
|
|
32
|
+
case 'ellipse': {
|
|
33
|
+
const [a, b] = geometry.points;
|
|
34
|
+
if (!a || !b)
|
|
35
|
+
return null;
|
|
36
|
+
const cx = (a.x + b.x) / 2;
|
|
37
|
+
const cy = (a.y + b.y) / 2;
|
|
38
|
+
const r = Math.max(Math.abs(b.x - a.x), Math.abs(b.y - a.y)) / 2;
|
|
39
|
+
return (_jsxs(_Fragment, { children: [fill && _jsx(Circle, { cx: cx, cy: cy, r: r, color: fill }), _jsx(Circle, { cx: cx, cy: cy, r: r, color: stroke, style: "stroke", strokeWidth: strokeWidth })] }));
|
|
40
|
+
}
|
|
41
|
+
case 'line':
|
|
42
|
+
case 'arrow': {
|
|
43
|
+
const [a, b] = geometry.points;
|
|
44
|
+
if (!a || !b)
|
|
45
|
+
return null;
|
|
46
|
+
return (_jsx(Line, { p1: a, p2: b, color: stroke, style: "stroke", strokeWidth: strokeWidth, strokeCap: "round" }));
|
|
47
|
+
}
|
|
48
|
+
case 'polygon': {
|
|
49
|
+
if (!polyPath)
|
|
50
|
+
return null;
|
|
51
|
+
return (_jsxs(_Fragment, { children: [fill && _jsx(Path, { path: polyPath, color: fill }), _jsx(Path, { path: polyPath, color: stroke, style: "stroke", strokeWidth: strokeWidth })] }));
|
|
52
|
+
}
|
|
53
|
+
case 'text': {
|
|
54
|
+
const [origin] = geometry.points;
|
|
55
|
+
if (!origin || !text)
|
|
56
|
+
return null;
|
|
57
|
+
if (!font)
|
|
58
|
+
return null;
|
|
59
|
+
return (_jsx(Text, { x: origin.x, y: origin.y, text: text, font: font, color: stroke }));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type SkPath } from '@shopify/react-native-skia';
|
|
2
|
+
import type { AnnotationStroke } from '../../types/annotation.js';
|
|
3
|
+
export declare const pointsToSkPath: (points: number[]) => SkPath;
|
|
4
|
+
export interface StrokeElementProps {
|
|
5
|
+
stroke: AnnotationStroke;
|
|
6
|
+
}
|
|
7
|
+
export declare const StrokeElement: ({ stroke }: StrokeElementProps) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Path, Skia } from '@shopify/react-native-skia';
|
|
3
|
+
import { useMemo } from 'react';
|
|
4
|
+
export const pointsToSkPath = (points) => {
|
|
5
|
+
const path = Skia.Path.Make();
|
|
6
|
+
if (points.length < 2)
|
|
7
|
+
return path;
|
|
8
|
+
path.moveTo(points[0], points[1]);
|
|
9
|
+
for (let i = 2; i < points.length; i += 2) {
|
|
10
|
+
path.lineTo(points[i], points[i + 1]);
|
|
11
|
+
}
|
|
12
|
+
return path;
|
|
13
|
+
};
|
|
14
|
+
export const StrokeElement = ({ stroke }) => {
|
|
15
|
+
const path = useMemo(() => pointsToSkPath(stroke.points), [stroke.points]);
|
|
16
|
+
const opacity = stroke.tool === 'highlighter' ? 0.3 : 1;
|
|
17
|
+
return (_jsx(Path, { path: path, color: stroke.color, style: "stroke", strokeWidth: stroke.width, strokeCap: "round", strokeJoin: "round", opacity: opacity }));
|
|
18
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Units } from '../types/firestore.js';
|
|
2
|
+
export interface MeasurementRef {
|
|
3
|
+
measurementId: string;
|
|
4
|
+
measurementPath: string;
|
|
5
|
+
groupId: string;
|
|
6
|
+
label?: string;
|
|
7
|
+
value?: number;
|
|
8
|
+
unit?: Units;
|
|
9
|
+
}
|
|
10
|
+
export type PickMeasurement = () => Promise<MeasurementRef | null>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { PlacedMeasurementRef } from '../types/annotation.js';
|
|
3
|
+
import type { Measurement } from '../types/firestore.js';
|
|
4
|
+
export interface MeasurementStampRenderArgs {
|
|
5
|
+
placed: PlacedMeasurementRef;
|
|
6
|
+
measurement: Measurement | null;
|
|
7
|
+
selected: boolean;
|
|
8
|
+
size: number;
|
|
9
|
+
zoom: number;
|
|
10
|
+
}
|
|
11
|
+
export type RenderMeasurementStamp = (args: MeasurementStampRenderArgs) => ReactNode;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Web DOM PointerEvent → CanvasPointerEvent. The native variant will live
|
|
2
|
+
// in pointerAdapter.native.ts and will translate gesture-handler events.
|
|
3
|
+
export const domEventToCanvasPointerEvent = (event, canvas, viewport) => {
|
|
4
|
+
const rect = canvas.getBoundingClientRect();
|
|
5
|
+
const screen = {
|
|
6
|
+
x: event.clientX - rect.left,
|
|
7
|
+
y: event.clientY - rect.top,
|
|
8
|
+
};
|
|
9
|
+
return {
|
|
10
|
+
pointerId: event.pointerId,
|
|
11
|
+
screen,
|
|
12
|
+
world: viewport.screenToWorld(screen),
|
|
13
|
+
pressure: event.pressure || undefined,
|
|
14
|
+
shiftKey: event.shiftKey,
|
|
15
|
+
altKey: event.altKey,
|
|
16
|
+
metaKey: event.metaKey,
|
|
17
|
+
ctrlKey: event.ctrlKey,
|
|
18
|
+
};
|
|
19
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Shared layout constants for the placed measurement stamp. Lives in a
|
|
2
|
+
// Skia-free module so both render (MeasurementStampElement) and hit-test
|
|
3
|
+
// (selectTool) can import it without dragging @shopify/react-native-skia
|
|
4
|
+
// into the consumer's static import graph.
|
|
5
|
+
export const STAMP_WIDTH = 120;
|
|
6
|
+
export const STAMP_HEIGHT = 44;
|
|
7
|
+
export const STAMP_PADDING_X = 10;
|
|
8
|
+
export const STAMP_PADDING_Y = 6;
|
|
9
|
+
// Constant SCREEN-space edge length of a placed measurement rendered as a
|
|
10
|
+
// square tile (the overlay path — see measurementStampOverlay.ts). The tile
|
|
11
|
+
// is a fixed-size pin: its on-screen size is this times `placed.scale` and
|
|
12
|
+
// does NOT change with zoom (only its position tracks the canvas). Also drives
|
|
13
|
+
// the select-tool hit box, which converts it back to doc space via the zoom.
|
|
14
|
+
export const STAMP_TILE_SIZE = 120;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { PlacedMeasurementRef } from '../../types/annotation.js';
|
|
2
|
+
import type { Tool } from '../Tool.js';
|
|
3
|
+
export interface MeasurementStampToolOptions {
|
|
4
|
+
autoSwitchToSelect?: boolean;
|
|
5
|
+
onPlaced?: (ref: PlacedMeasurementRef) => void;
|
|
6
|
+
onAutoSwitch?: (toToolId: string) => void;
|
|
7
|
+
selectToolId?: string;
|
|
8
|
+
}
|
|
9
|
+
export declare const createMeasurementStampTool: (options?: MeasurementStampToolOptions) => Tool;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { DEFAULT_LAYER_ID } from '../../types/annotation.js';
|
|
2
|
+
let counter = 0;
|
|
3
|
+
const makeId = () => `measurement-${Date.now().toString(36)}-${(counter++).toString(36)}`;
|
|
4
|
+
const firstLayerId = (doc) => doc.layers[0]?.id ?? DEFAULT_LAYER_ID;
|
|
5
|
+
export const createMeasurementStampTool = (options = {}) => {
|
|
6
|
+
const autoSwitchToSelect = options.autoSwitchToSelect ?? true;
|
|
7
|
+
const selectToolId = options.selectToolId ?? 'select';
|
|
8
|
+
return {
|
|
9
|
+
id: 'measurement',
|
|
10
|
+
label: 'Place measurement',
|
|
11
|
+
cursor: 'copy',
|
|
12
|
+
onPointerUp(event, ctx) {
|
|
13
|
+
// Tap-to-place. Open the consumer's picker and commit on selection.
|
|
14
|
+
void ctx.requestPickMeasurement().then((ref) => {
|
|
15
|
+
if (!ref)
|
|
16
|
+
return;
|
|
17
|
+
const placed = {
|
|
18
|
+
id: makeId(),
|
|
19
|
+
layerId: firstLayerId(ctx.document),
|
|
20
|
+
measurementPath: ref.measurementPath,
|
|
21
|
+
measurementId: ref.measurementId,
|
|
22
|
+
groupId: ref.groupId,
|
|
23
|
+
anchor: event.world,
|
|
24
|
+
labelOverride: ref.label,
|
|
25
|
+
unitOverride: ref.unit,
|
|
26
|
+
showLabel: true,
|
|
27
|
+
showValue: true,
|
|
28
|
+
createdAt: Date.now(),
|
|
29
|
+
};
|
|
30
|
+
ctx.commit({ ops: [{ op: 'addMeasurement', measurement: placed }] });
|
|
31
|
+
options.onPlaced?.(placed);
|
|
32
|
+
if (autoSwitchToSelect)
|
|
33
|
+
options.onAutoSwitch?.(selectToolId);
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Built-in "Hand" tool. When active, any drag pans the viewport. Tools that
|
|
2
|
+
// want one-off pan triggers (middle-mouse, space-drag) get those via the
|
|
3
|
+
// AnnotationCanvas `gestures` prop without needing to switch tools.
|
|
4
|
+
export const createPanTool = (options = {}) => ({
|
|
5
|
+
id: 'pan',
|
|
6
|
+
label: 'Hand',
|
|
7
|
+
cursor: options.cursor ?? 'grab',
|
|
8
|
+
onPointerDown(event, _ctx, _state) {
|
|
9
|
+
return { kind: 'panning', lastScreen: event.screen };
|
|
10
|
+
},
|
|
11
|
+
onPointerMove(event, ctx, state) {
|
|
12
|
+
const s = state;
|
|
13
|
+
if (s?.kind !== 'panning')
|
|
14
|
+
return s;
|
|
15
|
+
const delta = {
|
|
16
|
+
x: event.screen.x - s.lastScreen.x,
|
|
17
|
+
y: event.screen.y - s.lastScreen.y,
|
|
18
|
+
};
|
|
19
|
+
ctx.applyPan(delta);
|
|
20
|
+
return { kind: 'panning', lastScreen: event.screen };
|
|
21
|
+
},
|
|
22
|
+
onPointerUp(_event, _ctx, _state) {
|
|
23
|
+
// No commit — viewport state is canvas-internal, not persisted.
|
|
24
|
+
},
|
|
25
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { AnnotationStroke } from '../../types/annotation.js';
|
|
2
|
+
import type { Tool } from '../Tool.js';
|
|
3
|
+
export interface PenToolOptions {
|
|
4
|
+
color?: string;
|
|
5
|
+
width?: number;
|
|
6
|
+
minSampleDistance?: number;
|
|
7
|
+
variant?: 'pen' | 'marker' | 'highlighter';
|
|
8
|
+
}
|
|
9
|
+
export interface PenDrawingState {
|
|
10
|
+
kind: 'pen-drawing';
|
|
11
|
+
stroke: AnnotationStroke;
|
|
12
|
+
}
|
|
13
|
+
export declare const createPenTool: (options?: PenToolOptions) => Tool;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { DEFAULT_LAYER_ID } from '../../types/annotation.js';
|
|
2
|
+
const distSq = (ax, ay, bx, by) => {
|
|
3
|
+
const dx = ax - bx;
|
|
4
|
+
const dy = ay - by;
|
|
5
|
+
return dx * dx + dy * dy;
|
|
6
|
+
};
|
|
7
|
+
let counter = 0;
|
|
8
|
+
const makeId = (prefix) => `${prefix}-${Date.now().toString(36)}-${(counter++).toString(36)}`;
|
|
9
|
+
export const createPenTool = (options = {}) => {
|
|
10
|
+
const color = options.color ?? '#111827';
|
|
11
|
+
const width = options.width ?? 2;
|
|
12
|
+
const variant = options.variant ?? 'pen';
|
|
13
|
+
const minSampleDistance = options.minSampleDistance ?? 1.5;
|
|
14
|
+
const minSampleDistanceSq = minSampleDistance * minSampleDistance;
|
|
15
|
+
return {
|
|
16
|
+
id: variant,
|
|
17
|
+
label: variant === 'pen' ? 'Pen' : variant === 'marker' ? 'Marker' : 'Highlighter',
|
|
18
|
+
cursor: 'crosshair',
|
|
19
|
+
onPointerDown(event, ctx) {
|
|
20
|
+
const stroke = {
|
|
21
|
+
id: makeId('stroke'),
|
|
22
|
+
layerId: ctx.document.layers[0]?.id ?? DEFAULT_LAYER_ID,
|
|
23
|
+
tool: variant,
|
|
24
|
+
color,
|
|
25
|
+
width,
|
|
26
|
+
points: [event.world.x, event.world.y],
|
|
27
|
+
pressure: event.pressure !== undefined ? [event.pressure] : undefined,
|
|
28
|
+
createdAt: Date.now(),
|
|
29
|
+
};
|
|
30
|
+
return { kind: 'pen-drawing', stroke };
|
|
31
|
+
},
|
|
32
|
+
onPointerMove(event, _ctx, state) {
|
|
33
|
+
const s = state;
|
|
34
|
+
if (s?.kind !== 'pen-drawing')
|
|
35
|
+
return s;
|
|
36
|
+
const len = s.stroke.points.length;
|
|
37
|
+
const lastX = s.stroke.points[len - 2];
|
|
38
|
+
const lastY = s.stroke.points[len - 1];
|
|
39
|
+
if (distSq(lastX, lastY, event.world.x, event.world.y) < minSampleDistanceSq) {
|
|
40
|
+
return s;
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
kind: 'pen-drawing',
|
|
44
|
+
stroke: {
|
|
45
|
+
...s.stroke,
|
|
46
|
+
points: [...s.stroke.points, event.world.x, event.world.y],
|
|
47
|
+
pressure: s.stroke.pressure && event.pressure !== undefined
|
|
48
|
+
? [...s.stroke.pressure, event.pressure]
|
|
49
|
+
: s.stroke.pressure,
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
},
|
|
53
|
+
onPointerUp(_event, ctx, state) {
|
|
54
|
+
const s = state;
|
|
55
|
+
if (s?.kind !== 'pen-drawing')
|
|
56
|
+
return;
|
|
57
|
+
// Need at least two distinct samples to be a stroke.
|
|
58
|
+
if (s.stroke.points.length < 4)
|
|
59
|
+
return;
|
|
60
|
+
ctx.commit({ ops: [{ op: 'addStroke', stroke: s.stroke }] });
|
|
61
|
+
},
|
|
62
|
+
// No renderPreview. The canvas inner detects PenDrawingState in
|
|
63
|
+
// toolState and renders the in-flight stroke directly with its own
|
|
64
|
+
// (Skia-aware) StrokeElement. Keeping tools Skia-free is what lets
|
|
65
|
+
// consumers import them statically without breaking WithSkiaWeb's
|
|
66
|
+
// lazy-load ordering on web.
|
|
67
|
+
};
|
|
68
|
+
};
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { STAMP_TILE_SIZE } from '../stampLayout.js';
|
|
2
|
+
const HIT_PADDING = 6;
|
|
3
|
+
// Hit-test in doc-space. Crude but fast — good enough for v1; tools can
|
|
4
|
+
// override via `hitTest` for more precision later.
|
|
5
|
+
const hitStroke = (stroke, p) => {
|
|
6
|
+
const r = stroke.width / 2 + HIT_PADDING;
|
|
7
|
+
const r2 = r * r;
|
|
8
|
+
for (let i = 0; i < stroke.points.length - 2; i += 2) {
|
|
9
|
+
if (segmentDistanceSq(p.x, p.y, stroke.points[i], stroke.points[i + 1], stroke.points[i + 2], stroke.points[i + 3]) <= r2) {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return false;
|
|
14
|
+
};
|
|
15
|
+
const hitMeasurement = (m, p, zoom = 1) => {
|
|
16
|
+
// The stamp renders as a constant *screen*-size square centered on the
|
|
17
|
+
// anchor, so its doc-space footprint shrinks as you zoom in. Convert the
|
|
18
|
+
// screen-space half-extent (+ padding) back to doc space via the zoom so
|
|
19
|
+
// the hit box always matches what's drawn.
|
|
20
|
+
const scale = m.scale ?? 1;
|
|
21
|
+
const half = ((STAMP_TILE_SIZE * scale) / 2 + HIT_PADDING) / zoom;
|
|
22
|
+
const dx = Math.abs(p.x - m.anchor.x);
|
|
23
|
+
const dy = Math.abs(p.y - m.anchor.y);
|
|
24
|
+
return dx <= half && dy <= half;
|
|
25
|
+
};
|
|
26
|
+
const segmentDistanceSq = (px, py, ax, ay, bx, by) => {
|
|
27
|
+
const abx = bx - ax;
|
|
28
|
+
const aby = by - ay;
|
|
29
|
+
const lenSq = abx * abx + aby * aby;
|
|
30
|
+
let t = lenSq === 0 ? 0 : ((px - ax) * abx + (py - ay) * aby) / lenSq;
|
|
31
|
+
t = Math.max(0, Math.min(1, t));
|
|
32
|
+
const cx = ax + t * abx;
|
|
33
|
+
const cy = ay + t * aby;
|
|
34
|
+
const dx = px - cx;
|
|
35
|
+
const dy = py - cy;
|
|
36
|
+
return dx * dx + dy * dy;
|
|
37
|
+
};
|
|
38
|
+
const findHit = (doc, world, zoom) => {
|
|
39
|
+
// Hit-test in z-order (top first): measurements > shapes > strokes.
|
|
40
|
+
for (let i = doc.placedMeasurements.length - 1; i >= 0; i--) {
|
|
41
|
+
const m = doc.placedMeasurements[i];
|
|
42
|
+
if (hitMeasurement(m, world, zoom))
|
|
43
|
+
return { id: m.id, kind: 'measurement' };
|
|
44
|
+
}
|
|
45
|
+
for (let i = doc.shapes.length - 1; i >= 0; i--) {
|
|
46
|
+
// Default shape hit test: bounding box of the shape's points + padding.
|
|
47
|
+
const s = doc.shapes[i];
|
|
48
|
+
const pts = s.geometry.points;
|
|
49
|
+
if (pts.length === 0)
|
|
50
|
+
continue;
|
|
51
|
+
let minX = pts[0].x;
|
|
52
|
+
let maxX = pts[0].x;
|
|
53
|
+
let minY = pts[0].y;
|
|
54
|
+
let maxY = pts[0].y;
|
|
55
|
+
for (let j = 1; j < pts.length; j++) {
|
|
56
|
+
const p = pts[j];
|
|
57
|
+
if (p.x < minX)
|
|
58
|
+
minX = p.x;
|
|
59
|
+
if (p.x > maxX)
|
|
60
|
+
maxX = p.x;
|
|
61
|
+
if (p.y < minY)
|
|
62
|
+
minY = p.y;
|
|
63
|
+
if (p.y > maxY)
|
|
64
|
+
maxY = p.y;
|
|
65
|
+
}
|
|
66
|
+
if (world.x >= minX - HIT_PADDING &&
|
|
67
|
+
world.x <= maxX + HIT_PADDING &&
|
|
68
|
+
world.y >= minY - HIT_PADDING &&
|
|
69
|
+
world.y <= maxY + HIT_PADDING) {
|
|
70
|
+
return { id: s.id, kind: 'shape' };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
for (let i = doc.strokes.length - 1; i >= 0; i--) {
|
|
74
|
+
const s = doc.strokes[i];
|
|
75
|
+
if (hitStroke(s, world))
|
|
76
|
+
return { id: s.id, kind: 'stroke' };
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
};
|
|
80
|
+
const translatePatch = (elementKind, id, doc, delta) => {
|
|
81
|
+
if (elementKind === 'measurement') {
|
|
82
|
+
const m = doc.placedMeasurements.find((x) => x.id === id);
|
|
83
|
+
if (!m)
|
|
84
|
+
return null;
|
|
85
|
+
return {
|
|
86
|
+
op: 'updateMeasurement',
|
|
87
|
+
id,
|
|
88
|
+
patch: {
|
|
89
|
+
anchor: { x: m.anchor.x + delta.x, y: m.anchor.y + delta.y },
|
|
90
|
+
leader: m.leader
|
|
91
|
+
? {
|
|
92
|
+
from: {
|
|
93
|
+
x: m.leader.from.x + delta.x,
|
|
94
|
+
y: m.leader.from.y + delta.y,
|
|
95
|
+
},
|
|
96
|
+
to: {
|
|
97
|
+
x: m.leader.to.x + delta.x,
|
|
98
|
+
y: m.leader.to.y + delta.y,
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
: undefined,
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
if (elementKind === 'shape') {
|
|
106
|
+
const s = doc.shapes.find((x) => x.id === id);
|
|
107
|
+
if (!s)
|
|
108
|
+
return null;
|
|
109
|
+
return {
|
|
110
|
+
op: 'updateShape',
|
|
111
|
+
id,
|
|
112
|
+
patch: {
|
|
113
|
+
geometry: {
|
|
114
|
+
...s.geometry,
|
|
115
|
+
points: s.geometry.points.map((p) => ({
|
|
116
|
+
x: p.x + delta.x,
|
|
117
|
+
y: p.y + delta.y,
|
|
118
|
+
})),
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
const stroke = doc.strokes.find((x) => x.id === id);
|
|
124
|
+
if (!stroke)
|
|
125
|
+
return null;
|
|
126
|
+
const points = stroke.points.slice();
|
|
127
|
+
for (let i = 0; i < points.length; i += 2) {
|
|
128
|
+
points[i] = points[i] + delta.x;
|
|
129
|
+
points[i + 1] = points[i + 1] + delta.y;
|
|
130
|
+
}
|
|
131
|
+
return { op: 'updateStroke', id, patch: { points } };
|
|
132
|
+
};
|
|
133
|
+
export const createSelectTool = () => ({
|
|
134
|
+
id: 'select',
|
|
135
|
+
label: 'Select',
|
|
136
|
+
cursor: 'default',
|
|
137
|
+
onPointerDown(event, ctx) {
|
|
138
|
+
const hit = findHit(ctx.document, event.world, ctx.viewport.state.zoom);
|
|
139
|
+
if (!hit) {
|
|
140
|
+
ctx.setSelection(null);
|
|
141
|
+
return { kind: 'idle' };
|
|
142
|
+
}
|
|
143
|
+
ctx.setSelection({ ids: [hit.id] });
|
|
144
|
+
return {
|
|
145
|
+
kind: 'dragging',
|
|
146
|
+
id: hit.id,
|
|
147
|
+
elementKind: hit.kind,
|
|
148
|
+
start: event.world,
|
|
149
|
+
delta: { x: 0, y: 0 },
|
|
150
|
+
};
|
|
151
|
+
},
|
|
152
|
+
onPointerMove(event, ctx, state) {
|
|
153
|
+
const s = state;
|
|
154
|
+
if (s?.kind !== 'dragging')
|
|
155
|
+
return s;
|
|
156
|
+
const delta = { x: event.world.x - s.start.x, y: event.world.y - s.start.y };
|
|
157
|
+
const op = translatePatch(s.elementKind, s.id, ctx.document, delta);
|
|
158
|
+
if (op)
|
|
159
|
+
ctx.preview({ ops: [op] });
|
|
160
|
+
return { ...s, delta };
|
|
161
|
+
},
|
|
162
|
+
onPointerUp(_event, ctx, state) {
|
|
163
|
+
const s = state;
|
|
164
|
+
if (s?.kind !== 'dragging')
|
|
165
|
+
return;
|
|
166
|
+
if (s.delta.x === 0 && s.delta.y === 0)
|
|
167
|
+
return;
|
|
168
|
+
const op = translatePatch(s.elementKind, s.id, ctx.document, s.delta);
|
|
169
|
+
if (op)
|
|
170
|
+
ctx.commit({ ops: [op] });
|
|
171
|
+
},
|
|
172
|
+
onCancel(_state, ctx) {
|
|
173
|
+
ctx.preview({ ops: [] });
|
|
174
|
+
},
|
|
175
|
+
hitTest(element, p) {
|
|
176
|
+
if (element.kind === 'measurement')
|
|
177
|
+
return hitMeasurement(element, p);
|
|
178
|
+
if (element.kind === 'stroke')
|
|
179
|
+
return hitStroke(element, p);
|
|
180
|
+
return false;
|
|
181
|
+
},
|
|
182
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Measurement } from '../types/firestore.js';
|
|
2
|
+
import { type AnnotationCanvasState, type AnnotationDocumentPatch, type AnnotationStroke, type Selection, type Vec2 } from '../types/annotation.js';
|
|
3
|
+
import type { MeasurementRef } from './measurementPicker.js';
|
|
4
|
+
import type { CanvasPointerEvent, Tool, ToolContext, ToolState } from './Tool.js';
|
|
5
|
+
import { type ViewportState } from './viewport.js';
|
|
6
|
+
export interface AnnotationCanvasHandle {
|
|
7
|
+
undo(): void;
|
|
8
|
+
redo(): void;
|
|
9
|
+
canUndo(): boolean;
|
|
10
|
+
canRedo(): boolean;
|
|
11
|
+
zoomToFit(): void;
|
|
12
|
+
resetView(): void;
|
|
13
|
+
placeMeasurementAtCenter(ref: MeasurementRef): void;
|
|
14
|
+
}
|
|
15
|
+
export interface UseAnnotationCanvasStateProps {
|
|
16
|
+
canvas: AnnotationCanvasState;
|
|
17
|
+
onCommit(patch: AnnotationDocumentPatch): void;
|
|
18
|
+
tools: Tool[];
|
|
19
|
+
activeToolId: string;
|
|
20
|
+
selection: Selection | null;
|
|
21
|
+
onSelectionChange(selection: Selection | null): void;
|
|
22
|
+
measurements?: Measurement[];
|
|
23
|
+
pickMeasurement?: () => Promise<MeasurementRef | null>;
|
|
24
|
+
width: number;
|
|
25
|
+
height: number;
|
|
26
|
+
initialViewport?: ViewportState;
|
|
27
|
+
imperativeRef?: {
|
|
28
|
+
current: AnnotationCanvasHandle | null;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export interface AnnotationCanvasStateApi {
|
|
32
|
+
effectiveCanvas: AnnotationCanvasState;
|
|
33
|
+
worldTransform: Array<{
|
|
34
|
+
scale: number;
|
|
35
|
+
} | {
|
|
36
|
+
translateX: number;
|
|
37
|
+
} | {
|
|
38
|
+
translateY: number;
|
|
39
|
+
}>;
|
|
40
|
+
viewport: ViewportState;
|
|
41
|
+
measurementsById: Map<string, Measurement>;
|
|
42
|
+
activeTool: Tool | null;
|
|
43
|
+
toolState: ToolState;
|
|
44
|
+
ctx: ToolContext;
|
|
45
|
+
penDrawingStroke: AnnotationStroke | null;
|
|
46
|
+
customPreviewState: ToolState;
|
|
47
|
+
dispatchPointerDown(event: CanvasPointerEvent): void;
|
|
48
|
+
dispatchPointerMove(event: CanvasPointerEvent): void;
|
|
49
|
+
dispatchPointerUp(event: CanvasPointerEvent): void;
|
|
50
|
+
dispatchPointerCancel(): void;
|
|
51
|
+
pan(deltaScreen: Vec2): void;
|
|
52
|
+
zoom(focalScreen: Vec2, nextZoom: number): void;
|
|
53
|
+
}
|
|
54
|
+
export declare const useAnnotationCanvasState: (props: UseAnnotationCanvasStateProps) => AnnotationCanvasStateApi;
|