@skalar-os/canvas 0.0.0-stage → 0.1.22

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/index.mjs ADDED
@@ -0,0 +1,1726 @@
1
+ "use client";
2
+ import { jsxs as O, jsx as g, Fragment as Xt } from "react/jsx-runtime";
3
+ import Ht, { useState as V, useRef as U, useMemo as yt, useCallback as y, useEffect as $, createContext as Te, useContext as Ye, forwardRef as se, useImperativeHandle as Re } from "react";
4
+ import { useDraggable as Xe, DragOverlay as Yt, useDroppable as He, DndContext as Oe } from "@dnd-kit/core";
5
+ const _ = {
6
+ cellSize: 400,
7
+ gap: 20,
8
+ workspaceSize: {
9
+ width: 10,
10
+ height: 10
11
+ },
12
+ padding: {
13
+ top: 40,
14
+ right: 40,
15
+ bottom: 40,
16
+ left: 40
17
+ },
18
+ zoomLimits: {
19
+ min: 0.25,
20
+ max: 2,
21
+ step: 0.25
22
+ }
23
+ };
24
+ function re(e) {
25
+ var t, o, n = "";
26
+ if (typeof e == "string" || typeof e == "number") n += e;
27
+ else if (typeof e == "object") if (Array.isArray(e)) {
28
+ var i = e.length;
29
+ for (t = 0; t < i; t++) e[t] && (o = re(e[t])) && (n && (n += " "), n += o);
30
+ } else for (o in e) e[o] && (n && (n += " "), n += o);
31
+ return n;
32
+ }
33
+ function ce() {
34
+ for (var e, t, o = 0, n = "", i = arguments.length; o < i; o++) (e = arguments[o]) && (t = re(e)) && (n && (n += " "), n += t);
35
+ return n;
36
+ }
37
+ const We = ({
38
+ enabled: e = !0,
39
+ onGridPositionChange: t,
40
+ workspaceSize: o,
41
+ panZoom: n = { x: 0, y: 0, zoom: 1 },
42
+ dragState: i,
43
+ containerRef: l
44
+ }) => {
45
+ const [s, c] = V(null), [a, h] = V(null), d = U(null), w = U(null), { cellSize: k, gap: p } = _, x = yt(() => {
46
+ const D = k + p, E = o.width * k + o.width * p, m = o.height * k + o.height * p;
47
+ return {
48
+ totalCellSize: D,
49
+ workspaceWidth: E,
50
+ workspaceHeight: m,
51
+ cellSize: k,
52
+ gap: p
53
+ };
54
+ }, [k, p, o]), T = U(void 0), tt = y((D) => {
55
+ T.current || (T.current = requestAnimationFrame(() => {
56
+ D(), T.current = void 0;
57
+ }));
58
+ }, []), z = y(
59
+ (D, E, m = !1) => {
60
+ let B = (D - n.x) / n.zoom, X = (E - n.y) / n.zoom;
61
+ return m && (i != null && i.isDragging) && i.initialOffset && (B -= i.initialOffset.x, X -= i.initialOffset.y), { mouseX: B, mouseY: X };
62
+ },
63
+ [n, i]
64
+ ), et = y(
65
+ (D, E) => {
66
+ const { totalCellSize: m, cellSize: B, gap: X } = x;
67
+ if (D < 0 || E < 0 || D >= x.workspaceWidth || E >= x.workspaceHeight)
68
+ return null;
69
+ const W = D - X, q = E - X;
70
+ if (W < 0 || q < 0)
71
+ return null;
72
+ const M = Math.floor(W / m), A = Math.floor(q / m), K = M * m, b = A * m;
73
+ return W >= K && W < K + B && q >= b && q < b + B ? { x: M, y: A } : null;
74
+ },
75
+ [x]
76
+ ), nt = y(
77
+ (D) => {
78
+ if (!e) return;
79
+ const E = l.current;
80
+ if (!E) return;
81
+ const m = !!(i != null && i.isDragging && i.initialOffset);
82
+ let B;
83
+ m && w.current === E && d.current ? B = d.current : (B = E.getBoundingClientRect(), w.current = E, d.current = B);
84
+ const X = D.clientX - B.left, W = D.clientY - B.top, { mouseX: q, mouseY: M } = z(
85
+ X,
86
+ W,
87
+ m
88
+ ), { mouseX: A, mouseY: K } = z(
89
+ X,
90
+ W,
91
+ !1
92
+ );
93
+ if (q < 0 || M < 0 || q > x.workspaceWidth || M > x.workspaceHeight) {
94
+ (s !== null || a !== null) && (c(null), h(null), t == null || t(null));
95
+ return;
96
+ }
97
+ const b = et(q, M), G = m ? et(A, K) : b;
98
+ b !== null && (b.x !== (s == null ? void 0 : s.x) || b.y !== (s == null ? void 0 : s.y)) && (c(b), t == null || t(b)), G !== null && (G.x !== (a == null ? void 0 : a.x) || G.y !== (a == null ? void 0 : a.y)) && h(G);
99
+ },
100
+ [
101
+ e,
102
+ et,
103
+ t,
104
+ s,
105
+ a,
106
+ z,
107
+ x,
108
+ i,
109
+ l
110
+ ]
111
+ ), lt = y(
112
+ (D) => {
113
+ (i == null ? void 0 : i.isDragging) && i.draggedWidget ? tt(() => nt(D)) : nt(D);
114
+ },
115
+ [tt, nt, i]
116
+ );
117
+ $(() => {
118
+ i != null && i.isDragging ? (c(null), h(null)) : (d.current = null, w.current = null);
119
+ }, [i == null ? void 0 : i.isDragging]), $(() => {
120
+ d.current = null;
121
+ }, [n.x, n.y, n.zoom]);
122
+ const at = y(() => {
123
+ e && (c(null), h(null), t == null || t(null));
124
+ }, [e, t]);
125
+ return {
126
+ currentGridPosition: s,
127
+ visualGridPosition: a,
128
+ handlers: {
129
+ onMouseMove: lt,
130
+ onMouseLeave: at
131
+ },
132
+ getGridPositionFromMouse: et
133
+ };
134
+ }, ht = {
135
+ BACKGROUND: 0,
136
+ GRID_LINES: 1,
137
+ CELL_NUMBERS: 2,
138
+ CELL_HIGHLIGHT: 3,
139
+ WIDGET_PLACEHOLDER: 4,
140
+ WIDGETS: 5,
141
+ DRAG_OVERLAY: 6
142
+ }, ct = (e) => {
143
+ const { cellSize: t, gap: o, workspaceSize: n } = e;
144
+ return {
145
+ width: n.width * t + (n.width + 1) * o,
146
+ height: n.height * t + (n.height + 1) * o
147
+ };
148
+ }, Rt = (e, t, o, n = _.workspaceSize) => e.x < 0 || e.y < 0 || e.x + t.width > n.width || e.y + t.height > n.height ? !1 : wt(e, t, o).length === 0, wt = (e, t, o) => {
149
+ const n = [];
150
+ for (const i of o)
151
+ Fe(e, t, i.position, i.size) && n.push(i.id);
152
+ return n;
153
+ }, Fe = (e, t, o, n) => {
154
+ const i = e.x, l = e.x + t.width - 1, s = e.y, c = e.y + t.height - 1, a = o.x, h = o.x + n.width - 1, d = o.y, w = o.y + n.height - 1;
155
+ return !(l < a || i > h || c < d || s > w);
156
+ }, xt = (e, t) => {
157
+ const { cellSize: o, gap: n } = _;
158
+ return {
159
+ left: n + e.x * (o + n),
160
+ top: n + e.y * (o + n),
161
+ width: t.width * o + (t.width - 1) * n,
162
+ height: t.height * o + (t.height - 1) * n
163
+ };
164
+ }, Pe = ({
165
+ config: e,
166
+ lineColor: t = "var(--canvas-grid-line, #E5E7EB)",
167
+ centerLineColor: o = "var(--canvas-grid-center-line, #F3F4F6)"
168
+ }) => {
169
+ const { cellSize: n, gap: i, workspaceSize: l } = e, { width: s, height: c } = ct(e);
170
+ return /* @__PURE__ */ O(
171
+ "div",
172
+ {
173
+ "data-layer": "grid-lines",
174
+ style: {
175
+ position: "absolute",
176
+ inset: 0,
177
+ pointerEvents: "none",
178
+ zIndex: ht.GRID_LINES
179
+ },
180
+ children: [
181
+ /* @__PURE__ */ g(
182
+ "div",
183
+ {
184
+ style: {
185
+ position: "absolute",
186
+ left: 0,
187
+ top: 0,
188
+ width: 1,
189
+ height: c,
190
+ backgroundColor: t
191
+ }
192
+ }
193
+ ),
194
+ /* @__PURE__ */ g(
195
+ "div",
196
+ {
197
+ style: {
198
+ position: "absolute",
199
+ left: i - 1,
200
+ top: 0,
201
+ width: 1,
202
+ height: c,
203
+ backgroundColor: t
204
+ }
205
+ }
206
+ ),
207
+ Array.from({ length: l.width - 1 }, (a, h) => {
208
+ const d = i + (h + 1) * n + h * i;
209
+ return /* @__PURE__ */ O(Ht.Fragment, { children: [
210
+ /* @__PURE__ */ g(
211
+ "div",
212
+ {
213
+ style: {
214
+ position: "absolute",
215
+ left: d,
216
+ top: 0,
217
+ width: 1,
218
+ height: c,
219
+ backgroundColor: t
220
+ }
221
+ }
222
+ ),
223
+ /* @__PURE__ */ g(
224
+ "div",
225
+ {
226
+ style: {
227
+ position: "absolute",
228
+ left: d + i - 1,
229
+ top: 0,
230
+ width: 1,
231
+ height: c,
232
+ backgroundColor: t
233
+ }
234
+ }
235
+ )
236
+ ] }, `v-double-${h}`);
237
+ }),
238
+ /* @__PURE__ */ g(
239
+ "div",
240
+ {
241
+ style: {
242
+ position: "absolute",
243
+ left: s - i + 1,
244
+ top: 0,
245
+ width: 1,
246
+ height: c,
247
+ backgroundColor: t
248
+ }
249
+ }
250
+ ),
251
+ /* @__PURE__ */ g(
252
+ "div",
253
+ {
254
+ style: {
255
+ position: "absolute",
256
+ left: s - 2,
257
+ top: 0,
258
+ width: 1,
259
+ height: c,
260
+ backgroundColor: t
261
+ }
262
+ }
263
+ ),
264
+ /* @__PURE__ */ g(
265
+ "div",
266
+ {
267
+ style: {
268
+ position: "absolute",
269
+ left: 0,
270
+ top: 0,
271
+ width: s,
272
+ height: 1,
273
+ backgroundColor: t
274
+ }
275
+ }
276
+ ),
277
+ /* @__PURE__ */ g(
278
+ "div",
279
+ {
280
+ style: {
281
+ position: "absolute",
282
+ left: 0,
283
+ top: i - 1,
284
+ width: s,
285
+ height: 1,
286
+ backgroundColor: t
287
+ }
288
+ }
289
+ ),
290
+ Array.from({ length: l.height - 1 }, (a, h) => {
291
+ const d = i + (h + 1) * n + h * i;
292
+ return /* @__PURE__ */ O(Ht.Fragment, { children: [
293
+ /* @__PURE__ */ g(
294
+ "div",
295
+ {
296
+ style: {
297
+ position: "absolute",
298
+ left: 0,
299
+ top: d,
300
+ width: s,
301
+ height: 1,
302
+ backgroundColor: t
303
+ }
304
+ }
305
+ ),
306
+ /* @__PURE__ */ g(
307
+ "div",
308
+ {
309
+ style: {
310
+ position: "absolute",
311
+ left: 0,
312
+ top: d + i - 1,
313
+ width: s,
314
+ height: 1,
315
+ backgroundColor: t
316
+ }
317
+ }
318
+ )
319
+ ] }, `h-double-${h}`);
320
+ }),
321
+ /* @__PURE__ */ g(
322
+ "div",
323
+ {
324
+ style: {
325
+ position: "absolute",
326
+ left: 0,
327
+ top: c - i + 1,
328
+ width: s,
329
+ height: 1,
330
+ backgroundColor: t
331
+ }
332
+ }
333
+ ),
334
+ /* @__PURE__ */ g(
335
+ "div",
336
+ {
337
+ style: {
338
+ position: "absolute",
339
+ left: 0,
340
+ top: c - 2,
341
+ width: s,
342
+ height: 1,
343
+ backgroundColor: t
344
+ }
345
+ }
346
+ ),
347
+ Array.from({ length: l.width }, (a, h) => {
348
+ const d = i + h * (n + i) + n / 2;
349
+ return /* @__PURE__ */ g(
350
+ "div",
351
+ {
352
+ style: {
353
+ position: "absolute",
354
+ left: d,
355
+ top: 0,
356
+ width: 1,
357
+ height: c,
358
+ backgroundColor: o
359
+ }
360
+ },
361
+ `v-center-${h}`
362
+ );
363
+ }),
364
+ Array.from({ length: l.height }, (a, h) => {
365
+ const d = i + h * (n + i) + n / 2;
366
+ return /* @__PURE__ */ g(
367
+ "div",
368
+ {
369
+ style: {
370
+ position: "absolute",
371
+ left: 0,
372
+ top: d,
373
+ width: s,
374
+ height: 1,
375
+ backgroundColor: o
376
+ }
377
+ },
378
+ `h-center-${h}`
379
+ );
380
+ })
381
+ ]
382
+ }
383
+ );
384
+ }, Be = ({
385
+ config: e,
386
+ color: t = "var(--canvas-grid-number, #9CA3AF)",
387
+ fontSize: o = 48,
388
+ opacity: n = 0.4
389
+ }) => {
390
+ const { cellSize: i, gap: l, workspaceSize: s } = e;
391
+ return /* @__PURE__ */ g(
392
+ "div",
393
+ {
394
+ "data-layer": "cell-numbers",
395
+ style: {
396
+ position: "absolute",
397
+ inset: 0,
398
+ pointerEvents: "none",
399
+ zIndex: ht.CELL_NUMBERS
400
+ },
401
+ children: Array.from(
402
+ { length: s.height },
403
+ (c, a) => Array.from({ length: s.width }, (h, d) => {
404
+ const w = a * s.width + d + 1;
405
+ return /* @__PURE__ */ g(
406
+ "div",
407
+ {
408
+ style: {
409
+ position: "absolute",
410
+ display: "flex",
411
+ alignItems: "flex-start",
412
+ justifyContent: "flex-start",
413
+ fontSize: `${o}px`,
414
+ fontWeight: 300,
415
+ color: t,
416
+ left: l + d * (i + l) + 8,
417
+ top: l + a * (i + l) + 8,
418
+ width: i,
419
+ height: i,
420
+ opacity: n,
421
+ userSelect: "none"
422
+ },
423
+ children: w
424
+ },
425
+ `cell-number-${a}-${d}`
426
+ );
427
+ })
428
+ )
429
+ }
430
+ );
431
+ }, Se = {
432
+ hover: {
433
+ borderColor: "rgba(96, 165, 250, 0.4)",
434
+ backgroundColor: "rgba(96, 165, 250, 0.05)"
435
+ },
436
+ selected: {
437
+ borderColor: "rgba(37, 99, 235, 1)",
438
+ backgroundColor: "rgba(37, 99, 235, 0.2)"
439
+ },
440
+ preview: {
441
+ borderColor: "rgba(74, 222, 128, 1)",
442
+ backgroundColor: "rgba(74, 222, 128, 0.2)"
443
+ }
444
+ }, Ne = ({
445
+ position: e,
446
+ type: t = "hover",
447
+ size: o = { width: 1, height: 1 }
448
+ }) => {
449
+ const { cellSize: n, gap: i } = _, l = o.width * n + (o.width - 1) * i, s = o.height * n + (o.height - 1) * i, c = Se[t];
450
+ return /* @__PURE__ */ g(
451
+ "div",
452
+ {
453
+ "data-layer": "cell-highlight",
454
+ "data-highlight-type": t,
455
+ style: {
456
+ position: "absolute",
457
+ left: i + e.x * (n + i),
458
+ top: i + e.y * (n + i),
459
+ width: l,
460
+ height: s,
461
+ border: `2px solid ${c.borderColor}`,
462
+ backgroundColor: c.backgroundColor,
463
+ borderRadius: "8px",
464
+ pointerEvents: "none",
465
+ zIndex: ht.CELL_HIGHLIGHT,
466
+ transition: "all 150ms ease"
467
+ }
468
+ }
469
+ );
470
+ }, Ve = ({
471
+ position: e,
472
+ size: t,
473
+ mode: o,
474
+ isValidPosition: n,
475
+ conflictingWidgets: i = [],
476
+ label: l = ""
477
+ }) => {
478
+ const { cellSize: s, gap: c } = _, a = t.width * s + (t.width - 1) * c, h = t.height * s + (t.height - 1) * c, d = n ? "#93C5FD" : "#FCA5A5", w = n ? "rgba(96, 165, 250, 0.15)" : "#FEE2E2", p = n ? o === "placement" ? { backgroundColor: "#3B82F6", color: "white" } : { backgroundColor: "#10B981", color: "white" } : { backgroundColor: "#EF4444", color: "white" }, x = () => `${n ? "✓" : "✗"} ${o === "placement" ? "Place" : "Moving"}: ${l}`;
479
+ return /* @__PURE__ */ O(
480
+ "div",
481
+ {
482
+ "data-layer": "widget-placeholder",
483
+ "data-mode": o,
484
+ "data-valid": n,
485
+ style: {
486
+ position: "absolute",
487
+ left: c + e.x * (s + c),
488
+ top: c + e.y * (s + c),
489
+ pointerEvents: "none",
490
+ zIndex: ht.WIDGET_PLACEHOLDER,
491
+ transition: "left 150ms ease, top 150ms ease"
492
+ },
493
+ children: [
494
+ /* @__PURE__ */ O(
495
+ "div",
496
+ {
497
+ style: {
498
+ width: a,
499
+ height: h,
500
+ border: `2px dashed ${d}`,
501
+ backgroundColor: w,
502
+ borderRadius: "12px",
503
+ transition: "background-color 150ms, border-color 150ms",
504
+ position: "relative",
505
+ // Padding so content doesn't touch the dashed border
506
+ padding: "8px",
507
+ boxSizing: "border-box"
508
+ },
509
+ children: [
510
+ /* @__PURE__ */ g(
511
+ "div",
512
+ {
513
+ style: {
514
+ position: "absolute",
515
+ inset: 0,
516
+ display: "flex",
517
+ alignItems: "center",
518
+ justifyContent: "center"
519
+ },
520
+ children: /* @__PURE__ */ g(
521
+ "div",
522
+ {
523
+ style: {
524
+ padding: "4px 8px",
525
+ borderRadius: "4px",
526
+ fontSize: "14px",
527
+ fontWeight: 500,
528
+ ...p
529
+ },
530
+ children: x()
531
+ }
532
+ )
533
+ }
534
+ ),
535
+ (t.width > 1 || t.height > 1) && /* @__PURE__ */ g("div", { style: { position: "absolute", top: "4px", right: "4px" }, children: /* @__PURE__ */ O(
536
+ "div",
537
+ {
538
+ style: {
539
+ backgroundColor: "rgba(0, 0, 0, 0.6)",
540
+ color: "white",
541
+ fontSize: "12px",
542
+ padding: "2px 4px",
543
+ borderRadius: "4px"
544
+ },
545
+ children: [
546
+ t.width,
547
+ "×",
548
+ t.height
549
+ ]
550
+ }
551
+ ) })
552
+ ]
553
+ }
554
+ ),
555
+ !n && i.length > 0 && /* @__PURE__ */ g(
556
+ "div",
557
+ {
558
+ style: {
559
+ position: "absolute",
560
+ bottom: "-32px",
561
+ left: "50%",
562
+ transform: "translateX(-50%)"
563
+ },
564
+ children: /* @__PURE__ */ O(
565
+ "div",
566
+ {
567
+ style: {
568
+ backgroundColor: "#FEE2E2",
569
+ border: "1px solid #FCA5A5",
570
+ borderRadius: "4px",
571
+ padding: "4px 8px",
572
+ fontSize: "12px",
573
+ color: "#B91C1C",
574
+ whiteSpace: "nowrap"
575
+ },
576
+ children: [
577
+ "Conflicts with ",
578
+ i.length,
579
+ " widget",
580
+ i.length !== 1 ? "s" : ""
581
+ ]
582
+ }
583
+ )
584
+ }
585
+ )
586
+ ]
587
+ }
588
+ );
589
+ }, $e = Ht.memo(
590
+ function({ widget: t, isSelected: o, isHidden: n, isDraggable: i, editable: l, onWidgetClick: s, renderWidget: c }) {
591
+ const a = xt(t.position, t.size), { attributes: h, listeners: d, setNodeRef: w, isDragging: k } = Xe({
592
+ id: t.id,
593
+ disabled: !i
594
+ }), p = {
595
+ position: "absolute",
596
+ left: a.left,
597
+ top: a.top,
598
+ width: a.width,
599
+ height: a.height,
600
+ backfaceVisibility: "hidden",
601
+ opacity: n ? 0 : 1,
602
+ // Live mode (no drag, no click handler): let the widget content own the cursor.
603
+ cursor: i ? "move" : s ? "pointer" : "default"
604
+ }, x = (T) => {
605
+ T.stopPropagation(), s == null || s(t.id, T);
606
+ };
607
+ return c ? /* @__PURE__ */ g(
608
+ "div",
609
+ {
610
+ ref: w,
611
+ "data-widget-id": t.id,
612
+ style: p,
613
+ onClick: x,
614
+ ...i ? { ...d, ...h } : {},
615
+ children: /* @__PURE__ */ g("div", { style: { width: "100%", height: "100%", pointerEvents: l ? "none" : "auto" }, children: c(t, o) })
616
+ }
617
+ ) : /* @__PURE__ */ O(
618
+ "div",
619
+ {
620
+ ref: w,
621
+ "data-widget-id": t.id,
622
+ style: {
623
+ ...p,
624
+ display: "flex",
625
+ alignItems: "center",
626
+ justifyContent: "center",
627
+ borderRadius: "8px",
628
+ backgroundColor: o ? "#2563EB" : "#3B82F6",
629
+ borderWidth: "2px",
630
+ borderStyle: k ? "dashed" : "solid",
631
+ borderColor: o ? "#93C5FD" : "#2563EB",
632
+ color: "white",
633
+ fontWeight: 500,
634
+ fontSize: "14px",
635
+ boxShadow: o ? "0 10px 25px rgba(0,0,0,0.2)" : "0 4px 6px rgba(0,0,0,0.1)",
636
+ transition: "background-color 150ms ease, border-color 150ms ease, box-shadow 150ms ease",
637
+ ...k && { zIndex: 50 }
638
+ },
639
+ onClick: x,
640
+ ...i ? { ...d, ...h } : {},
641
+ children: [
642
+ t.title,
643
+ o && /* @__PURE__ */ g(
644
+ "div",
645
+ {
646
+ style: {
647
+ position: "absolute",
648
+ top: "-8px",
649
+ right: "-8px",
650
+ width: "16px",
651
+ height: "16px",
652
+ backgroundColor: "#60A5FA",
653
+ borderRadius: "50%",
654
+ border: "2px solid white"
655
+ }
656
+ }
657
+ )
658
+ ]
659
+ }
660
+ );
661
+ },
662
+ (e, t) => e.widget.id === t.widget.id && e.widget.position.x === t.widget.position.x && e.widget.position.y === t.widget.position.y && e.widget.size.width === t.widget.size.width && e.widget.size.height === t.widget.size.height && e.isSelected === t.isSelected && e.isHidden === t.isHidden && e.isDraggable === t.isDraggable && e.editable === t.editable
663
+ ), _e = ({
664
+ widgets: e,
665
+ selectedWidgetId: t,
666
+ draggingWidgetId: o,
667
+ currentTool: n,
668
+ editable: i = !0,
669
+ onWidgetClick: l,
670
+ renderWidget: s
671
+ }) => {
672
+ const c = i && n === "select";
673
+ return /* @__PURE__ */ g(
674
+ "div",
675
+ {
676
+ "data-layer": "widgets",
677
+ style: {
678
+ position: "absolute",
679
+ inset: 0,
680
+ zIndex: ht.WIDGETS
681
+ },
682
+ children: e.map((a) => /* @__PURE__ */ g(
683
+ $e,
684
+ {
685
+ widget: a,
686
+ isSelected: t === a.id,
687
+ isHidden: o === a.id,
688
+ isDraggable: c,
689
+ editable: i,
690
+ onWidgetClick: l,
691
+ renderWidget: s
692
+ },
693
+ a.id
694
+ ))
695
+ }
696
+ );
697
+ }, Ke = ({
698
+ draggedWidget: e,
699
+ zoomLevel: t,
700
+ renderDraggedWidget: o
701
+ }) => {
702
+ if (!e)
703
+ return /* @__PURE__ */ g(Yt, { dropAnimation: null });
704
+ const { cellSize: n, gap: i } = _, l = (e.size.width * (n + i) - i) * t, s = (e.size.height * (n + i) - i) * t;
705
+ return o ? /* @__PURE__ */ g(Yt, { dropAnimation: null, children: /* @__PURE__ */ g("div", { style: { width: l, height: s, opacity: 0.7 }, children: o(e) }) }) : /* @__PURE__ */ g(Yt, { dropAnimation: null, children: /* @__PURE__ */ g(
706
+ "div",
707
+ {
708
+ "data-layer": "drag-overlay",
709
+ style: {
710
+ display: "flex",
711
+ alignItems: "center",
712
+ justifyContent: "center",
713
+ borderRadius: "12px",
714
+ backgroundColor: "#3B82F6",
715
+ border: "2px dashed #93C5FD",
716
+ color: "white",
717
+ fontWeight: 500,
718
+ boxShadow: "0 25px 50px rgba(0,0,0,0.25)",
719
+ width: l,
720
+ height: s,
721
+ cursor: "move",
722
+ fontSize: `${14 * t}px`,
723
+ // Padding so content doesn't touch the dashed border
724
+ padding: "8px",
725
+ boxSizing: "border-box"
726
+ },
727
+ children: e.title
728
+ }
729
+ ) });
730
+ }, Ge = ({
731
+ children: e,
732
+ className: t,
733
+ onCanvasClick: o,
734
+ onGridPositionChange: n,
735
+ showGridHighlight: i = !0,
736
+ showGrid: l = !0,
737
+ style: s,
738
+ previewState: c,
739
+ panZoom: a,
740
+ selectedTool: h = "select",
741
+ onMouseDown: d,
742
+ isDragging: w = !1,
743
+ dragState: k,
744
+ config: p,
745
+ containerRef: x
746
+ }) => {
747
+ const { workspaceSize: T } = p, { width: tt, height: z } = ct(p), { setNodeRef: et } = He({
748
+ id: "canvas-drop-zone"
749
+ }), { currentGridPosition: nt, handlers: lt } = We({
750
+ enabled: i && h === "select" && !w,
751
+ onGridPositionChange: n,
752
+ workspaceSize: T,
753
+ panZoom: a,
754
+ dragState: k,
755
+ containerRef: x
756
+ });
757
+ return /* @__PURE__ */ O(
758
+ "div",
759
+ {
760
+ ref: et,
761
+ "data-canvas": !0,
762
+ className: ce(t),
763
+ style: {
764
+ ...s,
765
+ position: "relative",
766
+ backgroundColor: l ? "var(--canvas-grid-bg, #FAFAFA)" : "transparent",
767
+ border: l ? "1px solid var(--canvas-grid-line, #E5E7EB)" : "none",
768
+ width: tt,
769
+ height: z
770
+ },
771
+ onClick: o,
772
+ onMouseDown: d,
773
+ ...h === "select" || c != null && c.isActive ? lt : {},
774
+ children: [
775
+ l && /* @__PURE__ */ O(Xt, { children: [
776
+ /* @__PURE__ */ g(Pe, { config: p }),
777
+ /* @__PURE__ */ g(Be, { config: p })
778
+ ] }),
779
+ i && nt && /* @__PURE__ */ g(Ne, { position: nt, type: "hover" }),
780
+ (c == null ? void 0 : c.isActive) && c.position && c.size && /* @__PURE__ */ g(
781
+ Ve,
782
+ {
783
+ position: c.position,
784
+ size: c.size,
785
+ mode: c.mode || "placement",
786
+ isValidPosition: c.isValidPosition,
787
+ conflictingWidgets: c.conflictingWidgets,
788
+ label: c.label
789
+ }
790
+ ),
791
+ /* @__PURE__ */ g(
792
+ "div",
793
+ {
794
+ "data-layer": "widgets-container",
795
+ style: {
796
+ position: "absolute",
797
+ inset: 0,
798
+ zIndex: ht.WIDGETS
799
+ },
800
+ children: e
801
+ }
802
+ )
803
+ ]
804
+ }
805
+ );
806
+ }, le = Te(void 0), je = ({ children: e, defaultTool: t = "select" }) => {
807
+ const [o, n] = V(t), i = y((c) => {
808
+ n(c);
809
+ }, []), l = y(() => {
810
+ n((c) => c === "hand" ? "select" : "hand");
811
+ }, []), s = {
812
+ selectedTool: o,
813
+ setSelectedTool: i,
814
+ toggleHandTool: l
815
+ };
816
+ return /* @__PURE__ */ g(le.Provider, { value: s, children: e });
817
+ }, Ue = () => {
818
+ const e = Ye(le);
819
+ if (e === void 0)
820
+ throw new Error("useTool must be used within a ToolProvider");
821
+ return e;
822
+ }, qe = [
823
+ {
824
+ key: "v",
825
+ action: { type: "SET_TOOL", tool: "select" },
826
+ description: "Switch to Select tool"
827
+ },
828
+ {
829
+ key: "h",
830
+ action: { type: "SET_TOOL", tool: "hand" },
831
+ description: "Switch to Hand tool"
832
+ },
833
+ {
834
+ key: "escape",
835
+ action: { type: "CANCEL_PLACEMENT" },
836
+ description: "Cancel widget placement"
837
+ },
838
+ {
839
+ key: " ",
840
+ // Spacebar
841
+ action: { type: "TEMPORARY_TOOL_START", tool: "hand" },
842
+ onRelease: { type: "TEMPORARY_TOOL_END" },
843
+ description: "Hold to temporarily use Hand tool"
844
+ },
845
+ {
846
+ key: "z",
847
+ modifiers: { ctrl: !0 },
848
+ action: { type: "UNDO" },
849
+ description: "Undo last action"
850
+ },
851
+ {
852
+ key: "z",
853
+ modifiers: { ctrl: !0, shift: !0 },
854
+ action: { type: "REDO" },
855
+ description: "Redo last action"
856
+ }
857
+ ], Je = (e, t) => {
858
+ const o = e.ctrlKey || e.metaKey;
859
+ if (!t)
860
+ return !o && !e.shiftKey && !e.altKey;
861
+ const n = t.ctrl ? o : !o, i = t.shift ? e.shiftKey : !e.shiftKey, l = t.alt ? e.altKey : !e.altKey;
862
+ return n && i && l;
863
+ }, oe = (e) => {
864
+ const t = e.key.toLowerCase();
865
+ return qe.find(
866
+ (o) => o.key === t && Je(e, o.modifiers)
867
+ );
868
+ }, Qe = ({
869
+ currentTool: e,
870
+ onToolChange: t,
871
+ isPlacementActive: o,
872
+ onCancelPlacement: n,
873
+ onUndo: i,
874
+ onRedo: l,
875
+ enabled: s = !0
876
+ }) => {
877
+ const c = U(null), a = U(!1), h = y(
878
+ (p) => {
879
+ switch (p.type) {
880
+ case "SET_TOOL":
881
+ a.current || t(p.tool);
882
+ break;
883
+ case "CANCEL_PLACEMENT":
884
+ o && (n == null || n());
885
+ break;
886
+ case "TEMPORARY_TOOL_START":
887
+ a.current || (a.current = !0, c.current = e, t(p.tool));
888
+ break;
889
+ case "TEMPORARY_TOOL_END":
890
+ a.current && c.current && (t(c.current), c.current = null, a.current = !1);
891
+ break;
892
+ case "UNDO":
893
+ i == null || i();
894
+ break;
895
+ case "REDO":
896
+ l == null || l();
897
+ break;
898
+ }
899
+ },
900
+ [e, o, n, t, i, l]
901
+ ), d = y(
902
+ (p) => {
903
+ var T;
904
+ if (!s || p.target instanceof HTMLInputElement || p.target instanceof HTMLTextAreaElement || (T = p.target) != null && T.isContentEditable)
905
+ return;
906
+ const x = oe(p);
907
+ x && ((p.key === " " || x.action.type === "UNDO" || x.action.type === "REDO") && p.preventDefault(), !p.repeat && h(x.action));
908
+ },
909
+ [s, h]
910
+ ), w = y(
911
+ (p) => {
912
+ if (!s) return;
913
+ const x = oe(p);
914
+ x != null && x.onRelease && h(x.onRelease);
915
+ },
916
+ [s, h]
917
+ ), k = y(() => {
918
+ a.current && c.current && (t(c.current), c.current = null, a.current = !1);
919
+ }, [t]);
920
+ return $(() => {
921
+ if (s)
922
+ return document.addEventListener("keydown", d), document.addEventListener("keyup", w), window.addEventListener("blur", k), () => {
923
+ document.removeEventListener("keydown", d), document.removeEventListener("keyup", w), window.removeEventListener("blur", k);
924
+ };
925
+ }, [s, d, w, k]), {
926
+ isSpaceHeld: a.current
927
+ };
928
+ }, ae = (e, t) => {
929
+ const { cellSize: o, gap: n } = _, i = o + n, l = (0 - t.x) / t.zoom, s = (0 - t.y) / t.zoom, c = (e.width - t.x) / t.zoom, a = (e.height - t.y) / t.zoom, h = Math.floor((l - n) / i), d = Math.floor((s - n) / i), w = Math.ceil((c - n) / i), k = Math.ceil((a - n) / i);
930
+ return { startX: h, startY: d, endX: w, endY: k };
931
+ }, Ot = (e, t) => {
932
+ const o = e.position.x + e.size.width, n = e.position.y + e.size.height;
933
+ return e.position.x < t.endX && o > t.startX && e.position.y < t.endY && n > t.startY;
934
+ }, de = (e, t) => ({
935
+ startX: e.startX - t,
936
+ startY: e.startY - t,
937
+ endX: e.endX + t,
938
+ endY: e.endY + t
939
+ }), Ze = (e, t, o = 1) => {
940
+ if (Ot(e, t))
941
+ return "visible";
942
+ const n = de(t, o);
943
+ return Ot(e, n) ? "buffer" : "hidden";
944
+ }, hi = (e, t, o, n = 1) => {
945
+ const i = ae(t, o);
946
+ return e.map((l) => ({
947
+ id: l.id,
948
+ state: Ze(l, i, n)
949
+ }));
950
+ }, ti = (e, t, o, n = 1) => {
951
+ const i = ae(t, o), l = de(i, n);
952
+ return e.filter((s) => Ot(s, l)).map((s) => s.id);
953
+ }, ei = (e, t) => {
954
+ if (e.length !== t.length) return !1;
955
+ const o = new Set(e);
956
+ return t.every((n) => o.has(n));
957
+ }, ii = ({
958
+ widgets: e,
959
+ panZoom: t,
960
+ getViewport: o,
961
+ bufferZone: n = 1,
962
+ throttleMs: i = 100,
963
+ onVisibilityChange: l,
964
+ enabled: s = !0
965
+ }) => {
966
+ const c = U([]), a = U(null), h = U(!1), d = y(() => {
967
+ if (!s) return [];
968
+ const z = o();
969
+ return z.width === 0 || z.height === 0 ? [] : ti(e, z, t, n);
970
+ }, [e, t, n, o, s]), w = y(() => {
971
+ if (!l || !s) return;
972
+ const z = d();
973
+ ei(z, c.current) || (c.current = z, l(z));
974
+ }, [d, l, s]), k = U(w);
975
+ k.current = w;
976
+ const p = y(() => {
977
+ if (s) {
978
+ if (a.current) {
979
+ h.current = !0;
980
+ return;
981
+ }
982
+ k.current(), a.current = setTimeout(() => {
983
+ a.current = null, h.current && (h.current = !1, k.current());
984
+ }, i);
985
+ }
986
+ }, [i, s]);
987
+ $(() => {
988
+ p();
989
+ }, [p, t.x, t.y, t.zoom, e.length]), $(() => {
990
+ if (s && l) {
991
+ const z = setTimeout(() => {
992
+ k.current();
993
+ }, 0);
994
+ return () => clearTimeout(z);
995
+ }
996
+ }, [s]), $(() => () => {
997
+ a.current && (clearTimeout(a.current), a.current = null);
998
+ }, []);
999
+ const x = y(() => d(), [d]), T = y(
1000
+ (z) => d().includes(z),
1001
+ [d]
1002
+ ), tt = y(() => {
1003
+ w();
1004
+ }, [w]);
1005
+ return {
1006
+ getVisibleIds: x,
1007
+ isWidgetVisible: T,
1008
+ recalculate: tt
1009
+ };
1010
+ }, ni = (e, t, o) => {
1011
+ const n = xt(e.position, e.size), i = n.left + n.width / 2, l = n.top + n.height / 2, s = t.width / 2 - i * o, c = t.height / 2 - l * o;
1012
+ return { x: s, y: c };
1013
+ }, Wt = (e, t, o) => {
1014
+ const { workspaceSize: n, cellSize: i, gap: l } = _, s = n.width * i + (n.width + 1) * l, c = n.height * i + (n.height + 1) * l, a = s * o, h = c * o, d = Math.min(0, t.width - a), w = 0, k = Math.min(0, t.height - h);
1015
+ return {
1016
+ x: Math.max(d, Math.min(w, e.x)),
1017
+ y: Math.max(k, Math.min(0, e.y))
1018
+ };
1019
+ }, oi = (e) => {
1020
+ if (e.length === 0) return null;
1021
+ let t = 1 / 0, o = 1 / 0, n = -1 / 0, i = -1 / 0;
1022
+ for (const l of e) {
1023
+ const s = xt(l.position, l.size);
1024
+ t = Math.min(t, s.left), o = Math.min(o, s.top), n = Math.max(n, s.left + s.width), i = Math.max(i, s.top + s.height);
1025
+ }
1026
+ return {
1027
+ minX: t,
1028
+ minY: o,
1029
+ maxX: n,
1030
+ maxY: i,
1031
+ width: n - t,
1032
+ height: i - o
1033
+ };
1034
+ }, si = (e, t, o = 50, n = !0) => {
1035
+ const i = oi(e);
1036
+ if (!i) return null;
1037
+ const { zoomLimits: l } = _, s = t.width - o * 2, c = t.height - o * 2, a = s / i.width, h = c / i.height;
1038
+ let d = Math.min(a, h);
1039
+ d = Math.max(l.min, Math.min(l.max, d));
1040
+ const w = i.minX + i.width / 2, k = i.minY + i.height / 2, p = t.width / 2 - w * d, x = t.height / 2 - k * d;
1041
+ if (!n)
1042
+ return { x: p, y: x, zoom: d };
1043
+ const T = Wt({ x: p, y: x }, t, d);
1044
+ return {
1045
+ x: T.x,
1046
+ y: T.y,
1047
+ zoom: d
1048
+ };
1049
+ }, ri = (e, t, o) => {
1050
+ const n = 1 - Math.pow(1 - o, 3);
1051
+ return {
1052
+ x: e.x + (t.x - e.x) * n,
1053
+ y: e.y + (t.y - e.y) * n,
1054
+ zoom: e.zoom + (t.zoom - e.zoom) * n
1055
+ };
1056
+ };
1057
+ function ci(e, t, o, n) {
1058
+ let i = e instanceof Element ? e : null;
1059
+ for (; i && i !== t; ) {
1060
+ const l = getComputedStyle(i);
1061
+ if (n !== 0) {
1062
+ const s = l.overflowY;
1063
+ if ((s === "auto" || s === "scroll") && i.scrollHeight > i.clientHeight) return !0;
1064
+ }
1065
+ if (o !== 0) {
1066
+ const s = l.overflowX;
1067
+ if ((s === "auto" || s === "scroll") && i.scrollWidth > i.clientWidth) return !0;
1068
+ }
1069
+ i = i.parentElement;
1070
+ }
1071
+ return !1;
1072
+ }
1073
+ const ue = se(
1074
+ ({
1075
+ widgets: e,
1076
+ config: t,
1077
+ selectedWidgetId: o,
1078
+ tool: n,
1079
+ mode: i = "edit",
1080
+ widgetPlacementMode: l,
1081
+ onWidgetSelect: s,
1082
+ onWidgetMove: c,
1083
+ onWidgetAdd: a,
1084
+ onWidgetPlacementEnd: h,
1085
+ onToolChange: d,
1086
+ onUndo: w,
1087
+ onRedo: k,
1088
+ renderWidget: p,
1089
+ className: x,
1090
+ // Visibility props
1091
+ onVisibilityChange: T,
1092
+ bufferZone: tt = 1,
1093
+ clampPan: z = !0,
1094
+ debug: et = !1
1095
+ }, nt) => {
1096
+ var jt, Ut;
1097
+ const { selectedTool: lt, setSelectedTool: at } = Ue(), D = i !== "edit", E = i === "page", m = yt(() => ({
1098
+ ..._,
1099
+ ...t,
1100
+ workspaceSize: {
1101
+ ..._.workspaceSize,
1102
+ ...t == null ? void 0 : t.workspaceSize
1103
+ },
1104
+ padding: {
1105
+ ..._.padding,
1106
+ ...t == null ? void 0 : t.padding
1107
+ },
1108
+ zoomLimits: {
1109
+ ..._.zoomLimits,
1110
+ ...t == null ? void 0 : t.zoomLimits
1111
+ }
1112
+ }), [t]), B = yt(() => {
1113
+ if (!E) return m;
1114
+ const r = e.reduce(
1115
+ (u, f) => Math.max(u, f.position.y + f.size.height),
1116
+ 0
1117
+ );
1118
+ return {
1119
+ ...m,
1120
+ workspaceSize: {
1121
+ ...m.workspaceSize,
1122
+ height: Math.max(m.workspaceSize.height, r + 1)
1123
+ }
1124
+ };
1125
+ }, [E, m, e]), X = n ?? lt;
1126
+ $(() => {
1127
+ n && n !== lt && at(n);
1128
+ }, [n, lt, at]);
1129
+ const [W, q] = V({
1130
+ gridPosition: null,
1131
+ visualPosition: null
1132
+ }), M = l ? {
1133
+ isActive: l.isActive,
1134
+ widgetTemplate: l.selectedTemplate,
1135
+ isValidPosition: !0,
1136
+ conflictingWidgets: []
1137
+ } : {
1138
+ isActive: !1,
1139
+ widgetTemplate: null,
1140
+ isValidPosition: !0,
1141
+ conflictingWidgets: []
1142
+ }, [A, K] = V({
1143
+ isActive: !1,
1144
+ mode: null,
1145
+ position: null,
1146
+ size: null,
1147
+ label: "",
1148
+ isValidPosition: !0,
1149
+ conflictingWidgets: [],
1150
+ template: null
1151
+ }), [b, G] = V({ x: 0, y: 0 }), [bt, he] = V(z), [it, Ft] = V(!1), [vt, Pt] = V({ x: 0, y: 0 }), [C, kt] = V(1), [ge, Bt] = V(!1), dt = U(null), [L, St] = V({
1152
+ isDragging: !1,
1153
+ draggedWidget: null,
1154
+ initialWidgetPosition: { x: 0, y: 0 }
1155
+ }), [ot, Nt] = V(null), J = U(null), gt = y(() => {
1156
+ const r = J.current;
1157
+ return r ? {
1158
+ width: r.clientWidth,
1159
+ height: r.clientHeight
1160
+ } : { width: 800, height: 600 };
1161
+ }, []), [zt, fe] = V(1);
1162
+ $(() => {
1163
+ if (!E) return;
1164
+ const r = J.current;
1165
+ if (!r) return;
1166
+ const { width: u } = ct(m), f = () => {
1167
+ r.clientWidth > 0 && fe(r.clientWidth / u);
1168
+ };
1169
+ f();
1170
+ const v = new ResizeObserver(f);
1171
+ return v.observe(r), () => v.disconnect();
1172
+ }, [E, m]), $(() => {
1173
+ if (!z && !bt && J.current) {
1174
+ const { width: r } = ct(m), u = J.current.clientWidth;
1175
+ G({
1176
+ x: (u - r) / 2,
1177
+ y: 0
1178
+ }), he(!0);
1179
+ }
1180
+ }, [z, bt, m]);
1181
+ const { getVisibleIds: Vt, isWidgetVisible: $t } = ii({
1182
+ widgets: e,
1183
+ panZoom: { x: b.x, y: b.y, zoom: C },
1184
+ getViewport: gt,
1185
+ bufferZone: tt,
1186
+ onVisibilityChange: T,
1187
+ enabled: !!T
1188
+ }), ft = y(
1189
+ (r, u = 300) => new Promise((f) => {
1190
+ dt.current && cancelAnimationFrame(dt.current);
1191
+ const v = {
1192
+ x: b.x,
1193
+ y: b.y,
1194
+ zoom: C
1195
+ }, I = performance.now();
1196
+ Bt(!0);
1197
+ const Y = (R) => {
1198
+ const H = R - I, S = Math.min(H / u, 1), F = ri(v, r, S);
1199
+ G({ x: F.x, y: F.y }), kt(F.zoom), S < 1 ? dt.current = requestAnimationFrame(Y) : (Bt(!1), dt.current = null, f());
1200
+ };
1201
+ dt.current = requestAnimationFrame(Y);
1202
+ }),
1203
+ [b, C]
1204
+ ), pt = y((r) => {
1205
+ (r.x !== void 0 || r.y !== void 0) && G((u) => ({
1206
+ x: r.x ?? u.x,
1207
+ y: r.y ?? u.y
1208
+ })), r.zoom !== void 0 && kt(r.zoom);
1209
+ }, []);
1210
+ Re(
1211
+ nt,
1212
+ () => ({
1213
+ navigateToWidget: (r, u = {}) => {
1214
+ const { animate: f = !0, duration: v = 300, select: I = !1, zoom: Y } = u, R = e.find((N) => N.id === r);
1215
+ if (!R)
1216
+ return console.warn(`[Canvas] Widget not found: ${r}`), !1;
1217
+ const H = gt(), S = Y ?? C, F = ni(R, H, S), j = z ? Wt(F, H, S) : F, P = {
1218
+ x: j.x,
1219
+ y: j.y,
1220
+ zoom: S
1221
+ };
1222
+ return f ? ft(P, v) : pt(P), I && s && s(r), !0;
1223
+ },
1224
+ getPanZoom: () => ({
1225
+ x: b.x,
1226
+ y: b.y,
1227
+ zoom: C
1228
+ }),
1229
+ setPanZoom: (r, u = !0) => {
1230
+ const f = gt(), v = r.zoom ?? C, I = {
1231
+ x: r.x ?? b.x,
1232
+ y: r.y ?? b.y
1233
+ }, Y = z ? Wt(I, f, v) : I, R = {
1234
+ x: Y.x,
1235
+ y: Y.y,
1236
+ zoom: v
1237
+ };
1238
+ u ? ft(R) : pt(R);
1239
+ },
1240
+ resetView: (r = !0) => {
1241
+ const u = { x: 0, y: 0, zoom: 1 };
1242
+ r ? ft(u) : pt(u);
1243
+ },
1244
+ fitToContent: (r = !0) => {
1245
+ const u = gt(), f = si(e, u, 50, z);
1246
+ if (!f) {
1247
+ console.warn("[Canvas] No widgets to fit");
1248
+ return;
1249
+ }
1250
+ r ? ft(f) : pt(f);
1251
+ },
1252
+ getVisibleWidgets: () => Vt(),
1253
+ isWidgetVisible: (r) => $t(r)
1254
+ }),
1255
+ [
1256
+ e,
1257
+ b,
1258
+ C,
1259
+ gt,
1260
+ ft,
1261
+ pt,
1262
+ s,
1263
+ Vt,
1264
+ $t
1265
+ ]
1266
+ ), $(() => () => {
1267
+ dt.current && cancelAnimationFrame(dt.current);
1268
+ }, []), $(() => {
1269
+ if (!ot) return;
1270
+ const r = e.find((u) => u.id === ot.widgetId);
1271
+ r && r.position.x === ot.targetPosition.x && r.position.y === ot.targetPosition.y && Nt(null);
1272
+ }, [ot, e]);
1273
+ const pe = y(
1274
+ (r) => {
1275
+ if (q((u) => ({
1276
+ ...u,
1277
+ gridPosition: r
1278
+ })), A.isActive && A.mode === "placement" && r) {
1279
+ const u = A.template;
1280
+ if (u) {
1281
+ const f = Rt(r, u.size, e, m.workspaceSize), v = wt(r, u.size, e);
1282
+ K((I) => ({
1283
+ ...I,
1284
+ position: r,
1285
+ isValidPosition: f,
1286
+ conflictingWidgets: v
1287
+ }));
1288
+ }
1289
+ }
1290
+ },
1291
+ [A.isActive, A.mode, A.template, e]
1292
+ );
1293
+ $(() => {
1294
+ const r = W.gridPosition;
1295
+ if (M.isActive && M.widgetTemplate && r) {
1296
+ const u = Rt(
1297
+ r,
1298
+ M.widgetTemplate.size,
1299
+ e,
1300
+ m.workspaceSize
1301
+ ), f = wt(
1302
+ r,
1303
+ M.widgetTemplate.size,
1304
+ e
1305
+ );
1306
+ K({
1307
+ isActive: !0,
1308
+ mode: "placement",
1309
+ position: r,
1310
+ size: M.widgetTemplate.size,
1311
+ label: M.widgetTemplate.label,
1312
+ isValidPosition: u,
1313
+ conflictingWidgets: f,
1314
+ template: M.widgetTemplate
1315
+ });
1316
+ } else L.isDragging || K((u) => ({
1317
+ ...u,
1318
+ isActive: !1,
1319
+ mode: null,
1320
+ position: null
1321
+ }));
1322
+ }, [
1323
+ W.gridPosition,
1324
+ M.isActive,
1325
+ M.widgetTemplate,
1326
+ e,
1327
+ L.isDragging
1328
+ ]);
1329
+ const me = y(() => {
1330
+ if (M.isActive && M.widgetTemplate && W.gridPosition) {
1331
+ if (A.isValidPosition) {
1332
+ const r = {
1333
+ id: `widget-${Date.now()}`,
1334
+ type: M.widgetTemplate.type,
1335
+ position: W.gridPosition,
1336
+ size: M.widgetTemplate.size,
1337
+ title: M.widgetTemplate.label
1338
+ };
1339
+ a == null || a(r), h == null || h();
1340
+ }
1341
+ return;
1342
+ }
1343
+ s == null || s(null);
1344
+ }, [
1345
+ M,
1346
+ A,
1347
+ W.gridPosition,
1348
+ h,
1349
+ a,
1350
+ s
1351
+ ]), ye = y(
1352
+ (r, u) => {
1353
+ u.stopPropagation(), s == null || s(r);
1354
+ },
1355
+ [s]
1356
+ ), Et = yt(() => !L.isDragging || !L.draggedWidget ? [] : e.filter((r) => r.id !== L.draggedWidget.id), [L.isDragging, L.draggedWidget, e]), we = y(
1357
+ (r) => {
1358
+ const { active: u } = r, f = e.find((v) => v.id === u.id);
1359
+ if (f && r.activatorEvent) {
1360
+ const v = r.activatorEvent, I = { x: v.clientX, y: v.clientY }, Y = J.current;
1361
+ if (!Y) return;
1362
+ const R = Y.getBoundingClientRect(), H = I.x - R.left, S = I.y - R.top, F = (H - b.x) / C, j = (S - b.y) / C, P = xt(
1363
+ f.position,
1364
+ f.size
1365
+ ), N = {
1366
+ x: F - P.left,
1367
+ y: j - P.top
1368
+ }, { cellSize: Q, gap: Z } = m, st = Q + Z, rt = {
1369
+ x: f.position.x * st,
1370
+ y: f.position.y * st
1371
+ };
1372
+ St({
1373
+ isDragging: !0,
1374
+ draggedWidget: f,
1375
+ initialOffset: N,
1376
+ startZoomLevel: C,
1377
+ startPanPosition: { ...b },
1378
+ initialMousePosition: I,
1379
+ initialWidgetPosition: rt
1380
+ }), K({
1381
+ isActive: !0,
1382
+ mode: "drag",
1383
+ position: f.position,
1384
+ size: f.size,
1385
+ label: f.title || `Widget ${f.id}`,
1386
+ isValidPosition: !0,
1387
+ conflictingWidgets: [],
1388
+ template: null
1389
+ });
1390
+ }
1391
+ },
1392
+ [e, b, C, m]
1393
+ ), xe = y(
1394
+ (r) => {
1395
+ var u, f;
1396
+ if (!(!L.isDragging || !L.draggedWidget))
1397
+ try {
1398
+ const v = r.delta.x / C, I = r.delta.y / C, Y = L.initialWidgetPosition.x + v, R = L.initialWidgetPosition.y + I, { cellSize: H, gap: S, workspaceSize: F } = m, j = H + S, P = Math.round(Y / j), N = Math.round(R / j), Q = { x: P, y: N };
1399
+ if (((u = A.position) == null ? void 0 : u.x) === P && ((f = A.position) == null ? void 0 : f.y) === N)
1400
+ return;
1401
+ const Z = P >= 0 && N >= 0 && P + L.draggedWidget.size.width <= F.width && N + L.draggedWidget.size.height <= F.height, st = Z && Rt(Q, L.draggedWidget.size, Et, m.workspaceSize), rt = Z ? wt(
1402
+ Q,
1403
+ L.draggedWidget.size,
1404
+ Et
1405
+ ) : [];
1406
+ K((ut) => ({
1407
+ ...ut,
1408
+ position: Q,
1409
+ isValidPosition: st,
1410
+ conflictingWidgets: rt
1411
+ }));
1412
+ } catch (v) {
1413
+ console.error("Error in handleDragMove:", v);
1414
+ }
1415
+ },
1416
+ [L, Et, A.position, C, m]
1417
+ ), be = y(
1418
+ (r) => {
1419
+ const { active: u, over: f } = r, v = u.id;
1420
+ if (f && f.id === "canvas-drop-zone" && A.isActive && A.position && A.isValidPosition && A.position) {
1421
+ const Y = A.position;
1422
+ Nt({
1423
+ widgetId: v,
1424
+ targetPosition: Y
1425
+ }), c == null || c(v, Y);
1426
+ }
1427
+ St({
1428
+ isDragging: !1,
1429
+ draggedWidget: null,
1430
+ initialOffset: void 0,
1431
+ startZoomLevel: void 0,
1432
+ startPanPosition: void 0,
1433
+ initialMousePosition: void 0,
1434
+ initialWidgetPosition: { x: 0, y: 0 }
1435
+ }), K({
1436
+ isActive: !1,
1437
+ mode: null,
1438
+ position: null,
1439
+ size: null,
1440
+ label: "",
1441
+ isValidPosition: !0,
1442
+ conflictingWidgets: [],
1443
+ template: null
1444
+ });
1445
+ },
1446
+ [A, c]
1447
+ ), ve = y(
1448
+ (r) => {
1449
+ var u, f;
1450
+ if (!E && X === "hand") {
1451
+ if (D && ((f = (u = r.target) == null ? void 0 : u.closest) != null && f.call(u, "[data-widget-id]")))
1452
+ return;
1453
+ Ft(!0), Pt({ x: r.clientX, y: r.clientY });
1454
+ }
1455
+ },
1456
+ [X, D, E]
1457
+ ), Mt = y(
1458
+ (r) => {
1459
+ if (it && X === "hand") {
1460
+ const u = r.clientX - vt.x, f = r.clientY - vt.y;
1461
+ G((v) => {
1462
+ const I = J.current;
1463
+ if (!I) return v;
1464
+ const Y = I.clientWidth, R = I.clientHeight, { width: H, height: S } = ct(m), F = H * C, j = S * C;
1465
+ let P, N, Q, Z;
1466
+ if (z)
1467
+ P = Math.min(0, Y - F), N = 0;
1468
+ else {
1469
+ const ut = (Y - F) / 2;
1470
+ P = ut, N = ut;
1471
+ }
1472
+ Q = Math.min(0, R - j), Z = 0;
1473
+ const st = Math.max(P, Math.min(N, v.x + u)), rt = Math.max(Q, Math.min(Z, v.y + f));
1474
+ return { x: st, y: rt };
1475
+ }), Pt({ x: r.clientX, y: r.clientY });
1476
+ }
1477
+ },
1478
+ [it, vt, X, C, m]
1479
+ ), Ct = y(() => {
1480
+ it && Ft(!1);
1481
+ }, [it]), Dt = y(
1482
+ (r) => {
1483
+ const u = J.current;
1484
+ if (!u) return;
1485
+ if (!r.ctrlKey && !r.metaKey) {
1486
+ if (ci(r.target, u, r.deltaX, r.deltaY))
1487
+ return;
1488
+ r.preventDefault();
1489
+ const mt = 1, Me = r.deltaX * mt, Ce = r.deltaY * mt;
1490
+ G((Qt) => {
1491
+ const { width: De, height: Ae } = ct(m), Zt = De * C, Ie = Ae * C, te = u.clientWidth, Le = u.clientHeight;
1492
+ let Lt, Tt, ee, ie;
1493
+ if (z)
1494
+ Lt = Math.min(0, te - Zt), Tt = 0;
1495
+ else {
1496
+ const ne = (te - Zt) / 2;
1497
+ Lt = ne, Tt = ne;
1498
+ }
1499
+ return ee = Math.min(0, Le - Ie), ie = 0, {
1500
+ x: Math.max(Lt, Math.min(Tt, Qt.x - Me)),
1501
+ y: Math.max(ee, Math.min(ie, Qt.y - Ce))
1502
+ };
1503
+ });
1504
+ return;
1505
+ }
1506
+ r.preventDefault();
1507
+ const f = u.getBoundingClientRect(), v = r.clientX - f.left, I = r.clientY - f.top, Y = -r.deltaY * 1e-3, R = C, H = Math.max(0.7, Math.min(1.5, R + Y * 0.5));
1508
+ if (H === R) return;
1509
+ const S = (v - b.x) / R, F = (I - b.y) / R, j = v - S * H, P = I - F * H, { width: N, height: Q } = ct(m), Z = N * H, st = Q * H, rt = u.clientWidth, ut = u.clientHeight;
1510
+ let At, It, qt, Jt;
1511
+ if (z)
1512
+ At = Math.min(0, rt - Z), It = 0;
1513
+ else {
1514
+ const mt = (rt - Z) / 2;
1515
+ At = mt, It = mt;
1516
+ }
1517
+ qt = Math.min(0, ut - st), Jt = 0, kt(H), G({
1518
+ x: Math.max(At, Math.min(It, j)),
1519
+ y: Math.max(qt, Math.min(Jt, P))
1520
+ });
1521
+ },
1522
+ [C, b, m]
1523
+ );
1524
+ $(() => {
1525
+ const r = J.current;
1526
+ return r && !E && r.addEventListener("wheel", Dt, { passive: !1 }), it && (document.addEventListener("mousemove", Mt), document.addEventListener("mouseup", Ct)), () => {
1527
+ r && r.removeEventListener("wheel", Dt), it && (document.removeEventListener("mousemove", Mt), document.removeEventListener("mouseup", Ct));
1528
+ };
1529
+ }, [it, Mt, Ct, Dt, E]);
1530
+ const ke = y(
1531
+ (r) => {
1532
+ at(r), d == null || d(r);
1533
+ },
1534
+ [at, d]
1535
+ );
1536
+ Qe({
1537
+ currentTool: X,
1538
+ onToolChange: ke,
1539
+ isPlacementActive: M.isActive,
1540
+ onCancelPlacement: h,
1541
+ onUndo: w,
1542
+ onRedo: k
1543
+ });
1544
+ const { workspaceSize: _t } = m, Kt = /* @__PURE__ */ g(
1545
+ Ge,
1546
+ {
1547
+ config: B,
1548
+ showGrid: !D,
1549
+ onCanvasClick: me,
1550
+ onGridPositionChange: pe,
1551
+ showGridHighlight: !D && (X === "select" || M.isActive) && !L.isDragging,
1552
+ previewState: A,
1553
+ selectedTool: X,
1554
+ onMouseDown: ve,
1555
+ panZoom: E ? { x: 0, y: 0, zoom: zt } : { x: b.x, y: b.y, zoom: C },
1556
+ isDragging: L.isDragging,
1557
+ dragState: L,
1558
+ containerRef: J,
1559
+ children: /* @__PURE__ */ g(
1560
+ _e,
1561
+ {
1562
+ widgets: e,
1563
+ selectedWidgetId: o,
1564
+ draggingWidgetId: ((jt = L.draggedWidget) == null ? void 0 : jt.id) ?? (ot == null ? void 0 : ot.widgetId),
1565
+ currentTool: X,
1566
+ editable: !D,
1567
+ onWidgetClick: D ? void 0 : ye,
1568
+ renderWidget: p
1569
+ }
1570
+ )
1571
+ }
1572
+ ), { width: ze, height: Ee } = ct(B), Gt = /* @__PURE__ */ O(
1573
+ "div",
1574
+ {
1575
+ className: ce(x),
1576
+ style: {
1577
+ position: "relative",
1578
+ width: "100%",
1579
+ // Page mode: the canvas sizes itself to its content (the host scrolls it).
1580
+ height: E ? "auto" : "100%",
1581
+ backgroundColor: "var(--canvas-container-bg, #F3F4F6)"
1582
+ },
1583
+ children: [
1584
+ /* @__PURE__ */ g(
1585
+ "div",
1586
+ {
1587
+ ref: J,
1588
+ "data-canvas-container": !0,
1589
+ style: {
1590
+ backgroundColor: "var(--canvas-bg, #F9FAFB)",
1591
+ ...E ? { position: "relative", width: "100%" } : { position: "absolute", inset: 0, overflow: "hidden" },
1592
+ cursor: E ? "default" : X === "hand" ? it ? "grabbing" : "grab" : "default"
1593
+ },
1594
+ children: E ? /* @__PURE__ */ g("div", { style: { width: "100%", height: Ee * zt, overflow: "hidden" }, children: /* @__PURE__ */ g(
1595
+ "div",
1596
+ {
1597
+ style: {
1598
+ width: ze,
1599
+ transform: `scale(${zt})`,
1600
+ transformOrigin: "top left"
1601
+ },
1602
+ children: Kt
1603
+ }
1604
+ ) }) : /* @__PURE__ */ g(
1605
+ "div",
1606
+ {
1607
+ style: {
1608
+ transform: `translate(${b.x}px, ${b.y}px) scale(${C})`,
1609
+ transition: it || ge ? "none" : "transform 0.1s ease-out",
1610
+ transformOrigin: "top left",
1611
+ visibility: bt ? "visible" : "hidden"
1612
+ },
1613
+ children: Kt
1614
+ }
1615
+ )
1616
+ }
1617
+ ),
1618
+ et && /* @__PURE__ */ O(
1619
+ "div",
1620
+ {
1621
+ style: {
1622
+ position: "absolute",
1623
+ bottom: "8px",
1624
+ right: "8px",
1625
+ backgroundColor: "rgba(0, 0, 0, 0.8)",
1626
+ color: "white",
1627
+ fontSize: "12px",
1628
+ padding: "8px",
1629
+ borderRadius: "4px",
1630
+ fontFamily: "monospace"
1631
+ },
1632
+ children: [
1633
+ "Widgets: ",
1634
+ e.length,
1635
+ " |",
1636
+ W.gridPosition && /* @__PURE__ */ O(Xt, { children: [
1637
+ " ",
1638
+ "Grid:",
1639
+ " ",
1640
+ /* @__PURE__ */ O("span", { style: { color: "#86EFAC" }, children: [
1641
+ "(",
1642
+ W.gridPosition.x,
1643
+ ", ",
1644
+ W.gridPosition.y,
1645
+ ")"
1646
+ ] }),
1647
+ " ",
1648
+ "|"
1649
+ ] }),
1650
+ M.isActive && /* @__PURE__ */ O(Xt, { children: [
1651
+ " ",
1652
+ "Placing: ",
1653
+ /* @__PURE__ */ g("span", { style: { color: "#93C5FD" }, children: (Ut = M.widgetTemplate) == null ? void 0 : Ut.label }),
1654
+ " ",
1655
+ "|"
1656
+ ] }),
1657
+ "Zoom: ",
1658
+ /* @__PURE__ */ O("span", { style: { color: "#FDE047" }, children: [
1659
+ Math.round(C * 100),
1660
+ "%"
1661
+ ] }),
1662
+ " | Workspace:",
1663
+ " ",
1664
+ _t.width,
1665
+ "x",
1666
+ _t.height,
1667
+ " cells"
1668
+ ]
1669
+ }
1670
+ )
1671
+ ]
1672
+ }
1673
+ );
1674
+ return D ? Gt : /* @__PURE__ */ O(Oe, { onDragStart: we, onDragMove: xe, onDragEnd: be, children: [
1675
+ Gt,
1676
+ /* @__PURE__ */ g(
1677
+ Ke,
1678
+ {
1679
+ draggedWidget: L.draggedWidget,
1680
+ zoomLevel: C,
1681
+ renderDraggedWidget: p ? (r) => p(r, !1) : void 0
1682
+ }
1683
+ )
1684
+ ] });
1685
+ }
1686
+ );
1687
+ ue.displayName = "CanvasInternal";
1688
+ const li = se((e, t) => /* @__PURE__ */ g(je, { defaultTool: e.tool ?? "select", children: /* @__PURE__ */ g(ue, { ref: t, ...e }) }));
1689
+ li.displayName = "Canvas";
1690
+ export {
1691
+ li as Canvas,
1692
+ Ne as CellHighlight,
1693
+ Be as CellNumbers,
1694
+ Ke as DragOverlayLayer,
1695
+ _ as GRID_CONFIG,
1696
+ Ge as Grid,
1697
+ Pe as GridLines,
1698
+ qe as KEYBOARD_SHORTCUTS,
1699
+ ht as LAYER_Z_INDEX,
1700
+ le as ToolContext,
1701
+ je as ToolProvider,
1702
+ _e as WidgetLayer,
1703
+ Ve as WidgetPlaceholder,
1704
+ ei as areVisibleIdsEqual,
1705
+ hi as calculateAllWidgetsVisibility,
1706
+ ni as calculateCenteredPanPosition,
1707
+ si as calculateFitToContent,
1708
+ ae as calculateViewportGridBounds,
1709
+ Ze as calculateWidgetVisibility,
1710
+ oi as calculateWidgetsBoundingBox,
1711
+ Wt as clampPanPosition,
1712
+ Fe as doWidgetsOverlap,
1713
+ de as expandBoundsWithBuffer,
1714
+ oe as findMatchingShortcut,
1715
+ wt as getConflictingWidgets,
1716
+ ti as getVisibleWidgetIds,
1717
+ xt as getWidgetPlacementPosition,
1718
+ ri as interpolatePanZoom,
1719
+ Rt as isValidGridPosition,
1720
+ Je as matchesModifiers,
1721
+ We as useGridSnap,
1722
+ Qe as useKeyboardShortcuts,
1723
+ Ue as useTool,
1724
+ ii as useWidgetVisibility,
1725
+ Ot as widgetIntersectsBounds
1726
+ };