@particle-academy/fancy-3d 0.6.0 → 0.7.1

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.
@@ -1,758 +1,21 @@
1
1
  'use strict';
2
2
 
3
- var react = require('react');
4
- var jsxRuntime = require('react/jsx-runtime');
3
+ var chunkJY26QMCY_cjs = require('../chunk-JY26QMCY.cjs');
4
+ require('../chunk-7D3SIGGX.cjs');
5
5
 
6
- // src/canvas/Canvas.tsx
7
6
 
8
- // src/utils/cn.ts
9
- function flatten(value) {
10
- if (!value) return "";
11
- if (typeof value === "string" || typeof value === "number") return String(value);
12
- if (Array.isArray(value)) return value.map(flatten).filter(Boolean).join(" ");
13
- if (typeof value === "object") {
14
- return Object.entries(value).filter(([, v]) => Boolean(v)).map(([k]) => k).join(" ");
15
- }
16
- return "";
17
- }
18
- function cn(...inputs) {
19
- return inputs.map(flatten).filter(Boolean).join(" ");
20
- }
21
- function useControllableState(controlledValue, defaultValue, onChange) {
22
- const [uncontrolledValue, setUncontrolledValue] = react.useState(defaultValue);
23
- const isControlled = controlledValue !== void 0;
24
- const value = isControlled ? controlledValue : uncontrolledValue;
25
- const onChangeRef = react.useRef(onChange);
26
- onChangeRef.current = onChange;
27
- const setValue = react.useCallback(
28
- (next) => {
29
- const nextValue = typeof next === "function" ? next(value) : next;
30
- if (!isControlled) {
31
- setUncontrolledValue(nextValue);
32
- }
33
- onChangeRef.current?.(nextValue);
34
- },
35
- [isControlled, value]
36
- );
37
- return [value, setValue];
38
- }
39
- function usePanZoom({
40
- viewport,
41
- setViewport,
42
- minZoom,
43
- maxZoom,
44
- pannable,
45
- zoomable,
46
- containerRef
47
- }) {
48
- const isPanningRef = react.useRef(false);
49
- const startRef = react.useRef({ x: 0, y: 0, panX: 0, panY: 0 });
50
- const viewportRef = react.useRef(viewport);
51
- viewportRef.current = viewport;
52
- const setViewportRef = react.useRef(setViewport);
53
- setViewportRef.current = setViewport;
54
- const zoomableRef = react.useRef(zoomable);
55
- zoomableRef.current = zoomable;
56
- const minZoomRef = react.useRef(minZoom);
57
- minZoomRef.current = minZoom;
58
- const maxZoomRef = react.useRef(maxZoom);
59
- maxZoomRef.current = maxZoom;
60
- react.useEffect(() => {
61
- const container = containerRef.current;
62
- if (!container) return;
63
- function handleWheel(e) {
64
- if (!zoomableRef.current) return;
65
- if (!e.ctrlKey && !e.metaKey) return;
66
- e.preventDefault();
67
- const rect = container.getBoundingClientRect();
68
- const mouseX = e.clientX - rect.left;
69
- const mouseY = e.clientY - rect.top;
70
- setViewportRef.current((prev) => {
71
- const factor = 1 - e.deltaY * 1e-3;
72
- const newZoom = Math.min(maxZoomRef.current, Math.max(minZoomRef.current, prev.zoom * factor));
73
- const ratio = newZoom / prev.zoom;
74
- return {
75
- zoom: newZoom,
76
- panX: mouseX - (mouseX - prev.panX) * ratio,
77
- panY: mouseY - (mouseY - prev.panY) * ratio
78
- };
79
- });
80
- }
81
- container.addEventListener("wheel", handleWheel, { passive: false });
82
- return () => {
83
- container.removeEventListener("wheel", handleWheel);
84
- };
85
- }, [containerRef]);
86
- const onPointerDown = react.useCallback(
87
- (e) => {
88
- if (!pannable) return;
89
- if (e.button !== 0) return;
90
- const target = e.target;
91
- if (target !== containerRef.current && !target.hasAttribute("data-canvas-bg")) return;
92
- isPanningRef.current = true;
93
- startRef.current = { x: e.clientX, y: e.clientY, panX: viewport.panX, panY: viewport.panY };
94
- e.target.setPointerCapture(e.pointerId);
95
- e.preventDefault();
96
- },
97
- [pannable, viewport.panX, viewport.panY, containerRef]
98
- );
99
- const onPointerMove = react.useCallback(
100
- (e) => {
101
- if (!isPanningRef.current) return;
102
- const dx = e.clientX - startRef.current.x;
103
- const dy = e.clientY - startRef.current.y;
104
- setViewport((prev) => ({
105
- ...prev,
106
- panX: startRef.current.panX + dx,
107
- panY: startRef.current.panY + dy
108
- }));
109
- },
110
- [setViewport]
111
- );
112
- const onPointerUp = react.useCallback(() => {
113
- isPanningRef.current = false;
114
- }, []);
115
- return {
116
- containerProps: { onPointerDown, onPointerMove, onPointerUp },
117
- isPanning: isPanningRef.current
118
- };
119
- }
120
- function useNodeRegistry() {
121
- const rectsRef = react.useRef(/* @__PURE__ */ new Map());
122
- const [version, setVersion] = react.useState(0);
123
- const registerNode = react.useCallback((id, rect) => {
124
- rectsRef.current.set(id, rect);
125
- setVersion((v) => v + 1);
126
- }, []);
127
- const unregisterNode = react.useCallback((id) => {
128
- rectsRef.current.delete(id);
129
- setVersion((v) => v + 1);
130
- }, []);
131
- return { registerNode, unregisterNode, nodeRects: rectsRef.current, version };
132
- }
133
- var CanvasContext = react.createContext(null);
134
- function useCanvas() {
135
- const ctx = react.useContext(CanvasContext);
136
- if (!ctx) throw new Error("useCanvas must be used within a Canvas component");
137
- return ctx;
138
- }
139
- function CanvasNode({ children, id, x, y, draggable, onPositionChange, className, style }) {
140
- const { registerNode, unregisterNode, viewport, gridSize, snapToGrid } = useCanvas();
141
- const nodeRef = react.useRef(null);
142
- const isDragging = react.useRef(false);
143
- const dragStart = react.useRef({ mouseX: 0, mouseY: 0, nodeX: 0, nodeY: 0 });
144
- react.useEffect(() => {
145
- const el = nodeRef.current;
146
- if (!el) return;
147
- const updateRect = () => {
148
- registerNode(id, { x, y, width: el.offsetWidth, height: el.offsetHeight });
149
- };
150
- updateRect();
151
- const observer = new ResizeObserver(updateRect);
152
- observer.observe(el);
153
- return () => {
154
- observer.disconnect();
155
- unregisterNode(id);
156
- };
157
- }, [id, x, y, registerNode, unregisterNode]);
158
- const handlePointerDown = react.useCallback(
159
- (e) => {
160
- if (!draggable || e.button !== 0) return;
161
- e.stopPropagation();
162
- isDragging.current = true;
163
- dragStart.current = { mouseX: e.clientX, mouseY: e.clientY, nodeX: x, nodeY: y };
164
- e.target.setPointerCapture(e.pointerId);
165
- },
166
- [draggable, x, y]
167
- );
168
- const handlePointerMove = react.useCallback(
169
- (e) => {
170
- if (!isDragging.current) return;
171
- const dx = (e.clientX - dragStart.current.mouseX) / viewport.zoom;
172
- const dy = (e.clientY - dragStart.current.mouseY) / viewport.zoom;
173
- let nx = dragStart.current.nodeX + dx;
174
- let ny = dragStart.current.nodeY + dy;
175
- if (snapToGrid && gridSize > 0) {
176
- nx = Math.round(nx / gridSize) * gridSize;
177
- ny = Math.round(ny / gridSize) * gridSize;
178
- }
179
- onPositionChange?.(nx, ny);
180
- },
181
- [viewport.zoom, onPositionChange, snapToGrid, gridSize]
182
- );
183
- const handlePointerUp = react.useCallback(() => {
184
- isDragging.current = false;
185
- }, []);
186
- return /* @__PURE__ */ jsxRuntime.jsx(
187
- "div",
188
- {
189
- ref: nodeRef,
190
- "data-react-fancy-canvas-node": "",
191
- "data-node-id": id,
192
- className: cn("absolute", draggable && "cursor-grab active:cursor-grabbing", className),
193
- style: { left: x, top: y, ...style },
194
- onPointerDown: handlePointerDown,
195
- onPointerMove: handlePointerMove,
196
- onPointerUp: handlePointerUp,
197
- children
198
- }
199
- );
200
- }
201
- CanvasNode.displayName = "CanvasNode";
202
7
 
203
- // src/canvas/canvas.utils.ts
204
- function getAnchorPoint(rect, anchor, otherRect) {
205
- const cx = rect.x + rect.width / 2;
206
- const cy = rect.y + rect.height / 2;
207
- if (anchor === "auto" && otherRect) {
208
- const ocx = otherRect.x + otherRect.width / 2;
209
- const ocy = otherRect.y + otherRect.height / 2;
210
- const dx = ocx - cx;
211
- const dy = ocy - cy;
212
- if (Math.abs(dx) > Math.abs(dy)) {
213
- return dx > 0 ? { x: rect.x + rect.width, y: cy } : { x: rect.x, y: cy };
214
- }
215
- return dy > 0 ? { x: cx, y: rect.y + rect.height } : { x: cx, y: rect.y };
216
- }
217
- switch (anchor) {
218
- case "top":
219
- return { x: cx, y: rect.y };
220
- case "bottom":
221
- return { x: cx, y: rect.y + rect.height };
222
- case "left":
223
- return { x: rect.x, y: cy };
224
- case "right":
225
- return { x: rect.x + rect.width, y: cy };
226
- case "center":
227
- return { x: cx, y: cy };
228
- default:
229
- return { x: cx, y: cy };
230
- }
231
- }
232
- function bezierPath(from, to) {
233
- const dx = Math.abs(to.x - from.x);
234
- const dy = Math.abs(to.y - from.y);
235
- if (dx > dy) {
236
- const offset2 = dx * 0.5;
237
- const cp1x = from.x + (to.x > from.x ? offset2 : -offset2);
238
- const cp2x = to.x + (to.x > from.x ? -offset2 : offset2);
239
- return `M${from.x},${from.y} C${cp1x},${from.y} ${cp2x},${to.y} ${to.x},${to.y}`;
240
- }
241
- const offset = Math.max(dy * 0.5, 30);
242
- const cp1y = from.y + (to.y > from.y ? offset : -offset);
243
- const cp2y = to.y + (to.y > from.y ? -offset : offset);
244
- return `M${from.x},${from.y} C${from.x},${cp1y} ${to.x},${cp2y} ${to.x},${to.y}`;
245
- }
246
- function stepPath(from, to) {
247
- const midX = (from.x + to.x) / 2;
248
- return `M${from.x},${from.y} H${midX} V${to.y} H${to.x}`;
249
- }
250
- function straightPath(from, to) {
251
- return `M${from.x},${from.y} L${to.x},${to.y}`;
252
- }
253
- function getEdgePath(from, to, curve = "bezier") {
254
- switch (curve) {
255
- case "bezier":
256
- return bezierPath(from, to);
257
- case "step":
258
- return stepPath(from, to);
259
- case "straight":
260
- return straightPath(from, to);
261
- }
262
- }
263
- function CanvasEdge({
264
- from,
265
- to,
266
- fromAnchor = "auto",
267
- toAnchor = "auto",
268
- curve = "bezier",
269
- color = "currentColor",
270
- strokeWidth = 2,
271
- dashed = false,
272
- animated = false,
273
- label,
274
- className,
275
- markerStart,
276
- markerEnd
277
- }) {
278
- const { nodeRects, registryVersion } = useCanvas();
279
- const path = react.useMemo(() => {
280
- const fromRect = nodeRects.get(from);
281
- const toRect = nodeRects.get(to);
282
- if (!fromRect || !toRect) return null;
283
- const fromPt = getAnchorPoint(fromRect, fromAnchor, toRect);
284
- const toPt = getAnchorPoint(toRect, toAnchor, fromRect);
285
- return {
286
- d: getEdgePath(fromPt, toPt, curve),
287
- midX: (fromPt.x + toPt.x) / 2,
288
- midY: (fromPt.y + toPt.y) / 2
289
- };
290
- }, [from, to, fromAnchor, toAnchor, curve, nodeRects, registryVersion]);
291
- if (!path) return null;
292
- return /* @__PURE__ */ jsxRuntime.jsxs("g", { "data-react-fancy-canvas-edge": "", className: cn("text-zinc-300 dark:text-zinc-600", className), children: [
293
- /* @__PURE__ */ jsxRuntime.jsx(
294
- "path",
295
- {
296
- d: path.d,
297
- fill: "none",
298
- stroke: color,
299
- strokeWidth,
300
- strokeDasharray: dashed ? "6 4" : void 0,
301
- markerStart: markerStart ? `url(#${markerStart})` : void 0,
302
- markerEnd: markerEnd ? `url(#${markerEnd})` : void 0,
303
- className: animated ? "animate-[dash_1s_linear_infinite]" : "",
304
- style: animated ? { strokeDasharray: "8 4" } : void 0
305
- }
306
- ),
307
- label && /* @__PURE__ */ jsxRuntime.jsx("foreignObject", { x: path.midX - 40, y: path.midY - 12, width: 80, height: 24, children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex items-center justify-center text-xs text-zinc-500", children: label }) })
308
- ] });
309
- }
310
- CanvasEdge.displayName = "CanvasEdge";
311
- function CanvasMinimap({ width = 150, height = 100, className }) {
312
- const { nodeRects, registryVersion, viewport } = useCanvas();
313
- const bounds = react.useMemo(() => {
314
- if (nodeRects.size === 0) return { minX: 0, minY: 0, maxX: 500, maxY: 300 };
315
- let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
316
- nodeRects.forEach((r) => {
317
- minX = Math.min(minX, r.x);
318
- minY = Math.min(minY, r.y);
319
- maxX = Math.max(maxX, r.x + r.width);
320
- maxY = Math.max(maxY, r.y + r.height);
321
- });
322
- const padding = 50;
323
- return { minX: minX - padding, minY: minY - padding, maxX: maxX + padding, maxY: maxY + padding };
324
- }, [nodeRects, registryVersion]);
325
- const scaleX = width / (bounds.maxX - bounds.minX || 1);
326
- const scaleY = height / (bounds.maxY - bounds.minY || 1);
327
- const scale = Math.min(scaleX, scaleY);
328
- return /* @__PURE__ */ jsxRuntime.jsx(
329
- "div",
330
- {
331
- "data-react-fancy-canvas-minimap": "",
332
- className: cn(
333
- "absolute right-3 bottom-3 overflow-hidden rounded-lg border border-zinc-200 bg-white/90 dark:border-zinc-700 dark:bg-zinc-900/90",
334
- className
335
- ),
336
- style: { width, height },
337
- children: /* @__PURE__ */ jsxRuntime.jsxs("svg", { width, height, children: [
338
- Array.from(nodeRects.entries()).map(([id, rect]) => /* @__PURE__ */ jsxRuntime.jsx(
339
- "rect",
340
- {
341
- x: (rect.x - bounds.minX) * scale,
342
- y: (rect.y - bounds.minY) * scale,
343
- width: Math.max(rect.width * scale, 4),
344
- height: Math.max(rect.height * scale, 3),
345
- rx: 1,
346
- className: "fill-blue-400/60"
347
- },
348
- id
349
- )),
350
- /* @__PURE__ */ jsxRuntime.jsx(
351
- "rect",
352
- {
353
- x: (-viewport.panX / viewport.zoom - bounds.minX) * scale,
354
- y: (-viewport.panY / viewport.zoom - bounds.minY) * scale,
355
- width: (width / viewport.zoom / scale > 0 ? width / viewport.zoom : width) * scale / (bounds.maxX - bounds.minX || 1) * (bounds.maxX - bounds.minX),
356
- height: (height / viewport.zoom / scale > 0 ? height / viewport.zoom : height) * scale / (bounds.maxY - bounds.minY || 1) * (bounds.maxY - bounds.minY),
357
- fill: "none",
358
- stroke: "currentColor",
359
- strokeWidth: 1,
360
- className: "text-blue-500"
361
- }
362
- )
363
- ] })
364
- }
365
- );
366
- }
367
- CanvasMinimap.displayName = "CanvasMinimap";
368
-
369
- // node_modules/lucide-react/dist/esm/shared/src/utils.js
370
- var toKebabCase = (string) => string.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
371
- var toCamelCase = (string) => string.replace(
372
- /^([A-Z])|[\s-_]+(\w)/g,
373
- (match, p1, p2) => p2 ? p2.toUpperCase() : p1.toLowerCase()
374
- );
375
- var toPascalCase = (string) => {
376
- const camelCase = toCamelCase(string);
377
- return camelCase.charAt(0).toUpperCase() + camelCase.slice(1);
378
- };
379
- var mergeClasses = (...classes) => classes.filter((className, index, array) => {
380
- return Boolean(className) && className.trim() !== "" && array.indexOf(className) === index;
381
- }).join(" ").trim();
382
- var hasA11yProp = (props) => {
383
- for (const prop in props) {
384
- if (prop.startsWith("aria-") || prop === "role" || prop === "title") {
385
- return true;
386
- }
387
- }
388
- };
389
-
390
- // node_modules/lucide-react/dist/esm/defaultAttributes.js
391
- var defaultAttributes = {
392
- xmlns: "http://www.w3.org/2000/svg",
393
- width: 24,
394
- height: 24,
395
- viewBox: "0 0 24 24",
396
- fill: "none",
397
- stroke: "currentColor",
398
- strokeWidth: 2,
399
- strokeLinecap: "round",
400
- strokeLinejoin: "round"
401
- };
402
-
403
- // node_modules/lucide-react/dist/esm/Icon.js
404
- var Icon = react.forwardRef(
405
- ({
406
- color = "currentColor",
407
- size = 24,
408
- strokeWidth = 2,
409
- absoluteStrokeWidth,
410
- className = "",
411
- children,
412
- iconNode,
413
- ...rest
414
- }, ref) => react.createElement(
415
- "svg",
416
- {
417
- ref,
418
- ...defaultAttributes,
419
- width: size,
420
- height: size,
421
- stroke: color,
422
- strokeWidth: absoluteStrokeWidth ? Number(strokeWidth) * 24 / Number(size) : strokeWidth,
423
- className: mergeClasses("lucide", className),
424
- ...!children && !hasA11yProp(rest) && { "aria-hidden": "true" },
425
- ...rest
426
- },
427
- [
428
- ...iconNode.map(([tag, attrs]) => react.createElement(tag, attrs)),
429
- ...Array.isArray(children) ? children : [children]
430
- ]
431
- )
432
- );
433
-
434
- // node_modules/lucide-react/dist/esm/createLucideIcon.js
435
- var createLucideIcon = (iconName, iconNode) => {
436
- const Component = react.forwardRef(
437
- ({ className, ...props }, ref) => react.createElement(Icon, {
438
- ref,
439
- iconNode,
440
- className: mergeClasses(
441
- `lucide-${toKebabCase(toPascalCase(iconName))}`,
442
- `lucide-${iconName}`,
443
- className
444
- ),
445
- ...props
446
- })
447
- );
448
- Component.displayName = toPascalCase(iconName);
449
- return Component;
450
- };
451
-
452
- // node_modules/lucide-react/dist/esm/icons/maximize.js
453
- var __iconNode = [
454
- ["path", { d: "M8 3H5a2 2 0 0 0-2 2v3", key: "1dcmit" }],
455
- ["path", { d: "M21 8V5a2 2 0 0 0-2-2h-3", key: "1e4gt3" }],
456
- ["path", { d: "M3 16v3a2 2 0 0 0 2 2h3", key: "wsl5sc" }],
457
- ["path", { d: "M16 21h3a2 2 0 0 0 2-2v-3", key: "18trek" }]
458
- ];
459
- var Maximize = createLucideIcon("maximize", __iconNode);
460
-
461
- // node_modules/lucide-react/dist/esm/icons/rotate-ccw.js
462
- var __iconNode2 = [
463
- ["path", { d: "M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8", key: "1357e3" }],
464
- ["path", { d: "M3 3v5h5", key: "1xhq8a" }]
465
- ];
466
- var RotateCcw = createLucideIcon("rotate-ccw", __iconNode2);
467
-
468
- // node_modules/lucide-react/dist/esm/icons/zoom-in.js
469
- var __iconNode3 = [
470
- ["circle", { cx: "11", cy: "11", r: "8", key: "4ej97u" }],
471
- ["line", { x1: "21", x2: "16.65", y1: "21", y2: "16.65", key: "13gj7c" }],
472
- ["line", { x1: "11", x2: "11", y1: "8", y2: "14", key: "1vmskp" }],
473
- ["line", { x1: "8", x2: "14", y1: "11", y2: "11", key: "durymu" }]
474
- ];
475
- var ZoomIn = createLucideIcon("zoom-in", __iconNode3);
476
-
477
- // node_modules/lucide-react/dist/esm/icons/zoom-out.js
478
- var __iconNode4 = [
479
- ["circle", { cx: "11", cy: "11", r: "8", key: "4ej97u" }],
480
- ["line", { x1: "21", x2: "16.65", y1: "21", y2: "16.65", key: "13gj7c" }],
481
- ["line", { x1: "8", x2: "14", y1: "11", y2: "11", key: "durymu" }]
482
- ];
483
- var ZoomOut = createLucideIcon("zoom-out", __iconNode4);
484
- function CanvasControls({
485
- className,
486
- showZoomIn = true,
487
- showZoomOut = true,
488
- showReset = true,
489
- showFitAll = true
490
- }) {
491
- const { setViewport, nodeRects, containerRef } = useCanvas();
492
- const zoomIn = () => setViewport((v) => ({ ...v, zoom: Math.min(3, v.zoom * 1.25) }));
493
- const zoomOut = () => setViewport((v) => ({ ...v, zoom: Math.max(0.1, v.zoom / 1.25) }));
494
- const reset = () => setViewport({ panX: 0, panY: 0, zoom: 1 });
495
- const fitAll = () => {
496
- const container = containerRef.current;
497
- if (!container || nodeRects.size === 0) return reset();
498
- let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
499
- nodeRects.forEach((r) => {
500
- minX = Math.min(minX, r.x);
501
- minY = Math.min(minY, r.y);
502
- maxX = Math.max(maxX, r.x + r.width);
503
- maxY = Math.max(maxY, r.y + r.height);
504
- });
505
- const padding = 40;
506
- const contentW = maxX - minX + padding * 2;
507
- const contentH = maxY - minY + padding * 2;
508
- const cw = container.clientWidth;
509
- const ch = container.clientHeight;
510
- const zoom = Math.min(cw / contentW, ch / contentH, 1.5);
511
- const panX = (cw - contentW * zoom) / 2 - minX * zoom + padding * zoom;
512
- const panY = (ch - contentH * zoom) / 2 - minY * zoom + padding * zoom;
513
- setViewport({ panX, panY, zoom });
514
- };
515
- const btnClass = "flex h-8 w-8 items-center justify-center rounded-md text-zinc-500 hover:bg-zinc-100 hover:text-zinc-700 dark:hover:bg-zinc-800 dark:hover:text-zinc-300 transition-colors";
516
- return /* @__PURE__ */ jsxRuntime.jsxs(
517
- "div",
518
- {
519
- "data-react-fancy-canvas-controls": "",
520
- className: cn(
521
- "absolute bottom-3 left-3 flex gap-1 rounded-lg border border-zinc-200 bg-white/90 p-1 shadow-sm dark:border-zinc-700 dark:bg-zinc-900/90",
522
- className
523
- ),
524
- children: [
525
- showZoomIn && /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", onClick: zoomIn, className: btnClass, "aria-label": "Zoom in", children: /* @__PURE__ */ jsxRuntime.jsx(ZoomIn, { size: 16 }) }),
526
- showZoomOut && /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", onClick: zoomOut, className: btnClass, "aria-label": "Zoom out", children: /* @__PURE__ */ jsxRuntime.jsx(ZoomOut, { size: 16 }) }),
527
- showReset && /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", onClick: reset, className: btnClass, "aria-label": "Reset view", children: /* @__PURE__ */ jsxRuntime.jsx(RotateCcw, { size: 16 }) }),
528
- showFitAll && /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", onClick: fitAll, className: btnClass, "aria-label": "Fit all", children: /* @__PURE__ */ jsxRuntime.jsx(Maximize, { size: 16 }) })
529
- ]
530
- }
531
- );
532
- }
533
- CanvasControls.displayName = "CanvasControls";
534
-
535
- // src/canvas/engines/dom.ts
536
- var domEngine = {
537
- name: "dom",
538
- mount(host, _viewport) {
539
- return {
540
- name: "dom",
541
- root: host,
542
- updateViewport: () => {
543
- },
544
- dispose: () => {
545
- }
546
- };
547
- }
548
- };
549
-
550
- // src/frame.ts
551
- var FIT_PADDING = 40;
552
- var FIT_MAX_ZOOM = 1.5;
553
- function fitZoom(bounds, width, height) {
554
- const contentW = bounds.maxX - bounds.minX + FIT_PADDING * 2;
555
- const contentH = bounds.maxY - bounds.minY + FIT_PADDING * 2;
556
- if (width <= 0 || height <= 0 || contentW <= 0 || contentH <= 0) return 1;
557
- return Math.min(width / contentW, height / contentH, FIT_MAX_ZOOM);
558
- }
559
- function resolveEngine(spec) {
560
- if (!spec || spec === "dom") return domEngine;
561
- return spec;
562
- }
563
- var DEFAULT_VIEWPORT = { panX: 0, panY: 0, zoom: 1 };
564
- function CanvasRoot({
565
- children,
566
- viewport: controlledViewport,
567
- defaultViewport = DEFAULT_VIEWPORT,
568
- onViewportChange,
569
- minZoom = 0.1,
570
- maxZoom = 3,
571
- pannable = true,
572
- zoomable = true,
573
- showGrid = false,
574
- gridStyle = "dots",
575
- gridSize = 20,
576
- gridColor = "rgb(161 161 170 / 0.3)",
577
- snapToGrid = false,
578
- fitOnMount = false,
579
- engine: engineSpec,
580
- className,
581
- style
582
- }) {
583
- const containerRef = react.useRef(null);
584
- const [viewport, setViewport] = useControllableState(controlledViewport, defaultViewport, onViewportChange);
585
- const { registerNode, unregisterNode, nodeRects, version: registryVersion } = useNodeRegistry();
586
- const [engineHandle, setEngineHandle] = react.useState(null);
587
- react.useEffect(() => {
588
- const host = containerRef.current;
589
- if (!host) return;
590
- const resolved = resolveEngine(engineSpec);
591
- let handle = null;
592
- try {
593
- handle = resolved.mount(host, viewport);
594
- setEngineHandle(handle);
595
- } catch (err) {
596
- console.error(`[fancy-3d] Canvas engine "${resolved.name}" failed to mount:`, err);
597
- setEngineHandle(null);
598
- }
599
- return () => {
600
- try {
601
- handle?.dispose();
602
- } catch (err) {
603
- console.error(`[fancy-3d] Canvas engine "${handle?.name}" failed to dispose:`, err);
604
- }
605
- setEngineHandle(null);
606
- };
607
- }, [engineSpec]);
608
- react.useEffect(() => {
609
- engineHandle?.updateViewport(viewport);
610
- }, [engineHandle, viewport]);
611
- const { containerProps } = usePanZoom({
612
- viewport,
613
- setViewport,
614
- minZoom,
615
- maxZoom,
616
- pannable,
617
- zoomable,
618
- containerRef
619
- });
620
- const ctx = react.useMemo(
621
- () => ({ viewport, setViewport, registerNode, unregisterNode, nodeRects, registryVersion, containerRef, gridSize, snapToGrid, engine: engineHandle }),
622
- [viewport, setViewport, registerNode, unregisterNode, nodeRects, registryVersion, gridSize, snapToGrid, engineHandle]
623
- );
624
- const hasFitted = react.useRef(false);
625
- react.useEffect(() => {
626
- if (!fitOnMount || hasFitted.current || nodeRects.size === 0) return;
627
- const container = containerRef.current;
628
- if (!container || container.clientWidth === 0) return;
629
- hasFitted.current = true;
630
- requestAnimationFrame(() => {
631
- let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
632
- nodeRects.forEach((r) => {
633
- minX = Math.min(minX, r.x);
634
- minY = Math.min(minY, r.y);
635
- maxX = Math.max(maxX, r.x + r.width);
636
- maxY = Math.max(maxY, r.y + r.height);
637
- });
638
- const padding = FIT_PADDING;
639
- const contentW = maxX - minX + padding * 2;
640
- const contentH = maxY - minY + padding * 2;
641
- const cw = container.clientWidth;
642
- const ch = container.clientHeight;
643
- const zoom = fitZoom({ minX, minY, maxX, maxY }, cw, ch);
644
- const panX = (cw - contentW * zoom) / 2 - minX * zoom + padding * zoom;
645
- const panY = (ch - contentH * zoom) / 2 - minY * zoom + padding * zoom;
646
- setViewport({ panX, panY, zoom });
647
- });
648
- }, [fitOnMount, nodeRects, registryVersion, setViewport]);
649
- const edges = [];
650
- const others = [];
651
- const overlays = [];
652
- react.Children.forEach(children, (child) => {
653
- const el = child;
654
- if (!el || !el.type) return;
655
- const elType = el.type;
656
- if (elType === CanvasEdge || elType?._isCanvasEdge) {
657
- edges.push(el);
658
- } else if (elType === CanvasMinimap || elType === CanvasControls) {
659
- overlays.push(el);
660
- } else {
661
- others.push(el);
662
- }
663
- });
664
- return /* @__PURE__ */ jsxRuntime.jsx(CanvasContext.Provider, { value: ctx, children: /* @__PURE__ */ jsxRuntime.jsxs(
665
- "div",
666
- {
667
- ref: containerRef,
668
- "data-react-fancy-canvas": "",
669
- className: cn("relative overflow-hidden", className),
670
- style: { touchAction: "none", ...style },
671
- ...containerProps,
672
- children: [
673
- /* @__PURE__ */ jsxRuntime.jsx(
674
- "div",
675
- {
676
- "data-canvas-bg": "",
677
- className: "absolute inset-0",
678
- style: showGrid && gridStyle !== "none" ? gridStyle === "lines" ? {
679
- backgroundImage: `linear-gradient(to right, ${gridColor} 1px, transparent 1px), linear-gradient(to bottom, ${gridColor} 1px, transparent 1px)`,
680
- backgroundSize: `${gridSize * viewport.zoom}px ${gridSize * viewport.zoom}px`,
681
- backgroundPosition: `${viewport.panX}px ${viewport.panY}px`
682
- } : {
683
- backgroundImage: `radial-gradient(circle, ${gridColor} 1px, transparent 1px)`,
684
- backgroundSize: `${gridSize * viewport.zoom}px ${gridSize * viewport.zoom}px`,
685
- backgroundPosition: `${viewport.panX}px ${viewport.panY}px`
686
- } : void 0
687
- }
688
- ),
689
- /* @__PURE__ */ jsxRuntime.jsx(
690
- "div",
691
- {
692
- className: "absolute origin-top-left",
693
- style: {
694
- transform: `translate(${viewport.panX}px, ${viewport.panY}px) scale(${viewport.zoom})`
695
- },
696
- children: others
697
- }
698
- ),
699
- /* @__PURE__ */ jsxRuntime.jsxs(
700
- "svg",
701
- {
702
- className: "pointer-events-none absolute inset-0 h-full w-full",
703
- style: {
704
- transform: `translate(${viewport.panX}px, ${viewport.panY}px) scale(${viewport.zoom})`,
705
- transformOrigin: "0 0"
706
- },
707
- children: [
708
- /* @__PURE__ */ jsxRuntime.jsxs("defs", { children: [
709
- /* @__PURE__ */ jsxRuntime.jsx("marker", { id: "canvas-arrow", viewBox: "0 0 10 10", refX: "10", refY: "5", markerWidth: "8", markerHeight: "8", orient: "auto", children: /* @__PURE__ */ jsxRuntime.jsx("path", { d: "M0,0 L10,5 L0,10 Z", fill: "#71717a" }) }),
710
- /* @__PURE__ */ jsxRuntime.jsx("marker", { id: "canvas-circle", viewBox: "0 0 10 10", refX: "5", refY: "5", markerWidth: "8", markerHeight: "8", orient: "auto", children: /* @__PURE__ */ jsxRuntime.jsx("circle", { cx: "5", cy: "5", r: "3.5", fill: "#71717a" }) }),
711
- /* @__PURE__ */ jsxRuntime.jsx("marker", { id: "canvas-diamond", viewBox: "0 0 12 12", refX: "6", refY: "6", markerWidth: "10", markerHeight: "10", orient: "auto", children: /* @__PURE__ */ jsxRuntime.jsx("polygon", { points: "6,0 12,6 6,12 0,6", fill: "none", stroke: "#71717a", strokeWidth: "1.5" }) }),
712
- /* @__PURE__ */ jsxRuntime.jsx("marker", { id: "canvas-one", viewBox: "0 0 2 16", refX: "1", refY: "8", markerWidth: "2", markerHeight: "14", orient: "auto", children: /* @__PURE__ */ jsxRuntime.jsx("line", { x1: "1", y1: "0", x2: "1", y2: "16", stroke: "#71717a", strokeWidth: "2" }) }),
713
- /* @__PURE__ */ jsxRuntime.jsxs("marker", { id: "canvas-crow-foot", viewBox: "0 0 16 16", refX: "16", refY: "8", markerWidth: "14", markerHeight: "14", orient: "auto", children: [
714
- /* @__PURE__ */ jsxRuntime.jsx("line", { x1: "16", y1: "8", x2: "0", y2: "0", stroke: "#71717a", strokeWidth: "2", strokeLinecap: "round" }),
715
- /* @__PURE__ */ jsxRuntime.jsx("line", { x1: "16", y1: "8", x2: "0", y2: "8", stroke: "#71717a", strokeWidth: "2", strokeLinecap: "round" }),
716
- /* @__PURE__ */ jsxRuntime.jsx("line", { x1: "16", y1: "8", x2: "0", y2: "16", stroke: "#71717a", strokeWidth: "2", strokeLinecap: "round" }),
717
- /* @__PURE__ */ jsxRuntime.jsx("line", { x1: "16", y1: "0", x2: "16", y2: "16", stroke: "#71717a", strokeWidth: "2", strokeLinecap: "round" })
718
- ] })
719
- ] }),
720
- edges
721
- ]
722
- }
723
- ),
724
- overlays
725
- ]
726
- }
727
- ) });
728
- }
729
- var Canvas = Object.assign(CanvasRoot, {
730
- Node: CanvasNode,
731
- Edge: CanvasEdge,
732
- Minimap: CanvasMinimap,
733
- Controls: CanvasControls
8
+ Object.defineProperty(exports, "Canvas", {
9
+ enumerable: true,
10
+ get: function () { return chunkJY26QMCY_cjs.Canvas; }
11
+ });
12
+ Object.defineProperty(exports, "domEngine", {
13
+ enumerable: true,
14
+ get: function () { return chunkJY26QMCY_cjs.domEngine; }
15
+ });
16
+ Object.defineProperty(exports, "useCanvas", {
17
+ enumerable: true,
18
+ get: function () { return chunkJY26QMCY_cjs.useCanvas; }
734
19
  });
735
- /*! Bundled license information:
736
-
737
- lucide-react/dist/esm/shared/src/utils.js:
738
- lucide-react/dist/esm/defaultAttributes.js:
739
- lucide-react/dist/esm/Icon.js:
740
- lucide-react/dist/esm/createLucideIcon.js:
741
- lucide-react/dist/esm/icons/maximize.js:
742
- lucide-react/dist/esm/icons/rotate-ccw.js:
743
- lucide-react/dist/esm/icons/zoom-in.js:
744
- lucide-react/dist/esm/icons/zoom-out.js:
745
- lucide-react/dist/esm/lucide-react.js:
746
- (**
747
- * @license lucide-react v0.511.0 - ISC
748
- *
749
- * This source code is licensed under the ISC license.
750
- * See the LICENSE file in the root directory of this source tree.
751
- *)
752
- */
753
-
754
- exports.Canvas = Canvas;
755
- exports.domEngine = domEngine;
756
- exports.useCanvas = useCanvas;
757
20
  //# sourceMappingURL=index.cjs.map
758
21
  //# sourceMappingURL=index.cjs.map