@almadar/ui 5.120.0 → 5.121.0

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.
@@ -43,6 +43,7 @@ import { DndContext, pointerWithin, rectIntersection, closestCorners, useSensors
43
43
  import { useSortable, arrayMove, sortableKeyboardCoordinates, SortableContext, rectSortingStrategy, verticalListSortingStrategy } from '@dnd-kit/sortable';
44
44
  import { CSS } from '@dnd-kit/utilities';
45
45
  import { useNodeId, ReactFlowProvider, Handle, Position } from '@xyflow/react';
46
+ import { forceSimulation, forceLink, forceManyBody, forceCollide, forceX, forceY } from 'd3-force';
46
47
  import { isDrawHostPattern, getPatternDefinition, getComponentForPattern as getComponentForPattern$1 } from '@almadar/core/patterns';
47
48
 
48
49
  var __defProp = Object.defineProperty;
@@ -35360,7 +35361,10 @@ function resolveColor3(color, el) {
35360
35361
  const resolved = getComputedStyle(el).getPropertyValue(m[1]).trim();
35361
35362
  return resolved || (m[2]?.trim() ?? "#888888");
35362
35363
  }
35363
- var GROUP_COLORS2, labelMeasureCtx, GraphCanvas;
35364
+ function truncateLabel(label) {
35365
+ return label.length > MAX_LABEL_CHARS ? label.slice(0, MAX_LABEL_CHARS - 1) + "\u2026" : label;
35366
+ }
35367
+ var GROUP_COLORS2, labelMeasureCtx, MAX_LABEL_CHARS, GraphCanvas;
35364
35368
  var init_GraphCanvas = __esm({
35365
35369
  "components/core/molecules/GraphCanvas.tsx"() {
35366
35370
  "use client";
@@ -35381,6 +35385,7 @@ var init_GraphCanvas = __esm({
35381
35385
  "var(--color-accent)"
35382
35386
  ];
35383
35387
  labelMeasureCtx = null;
35388
+ MAX_LABEL_CHARS = 22;
35384
35389
  GraphCanvas = ({
35385
35390
  title,
35386
35391
  nodes: propNodes = [],
@@ -35491,6 +35496,7 @@ var init_GraphCanvas = __esm({
35491
35496
  const w = viewW * layoutScale;
35492
35497
  const h = viewH * layoutScale;
35493
35498
  const labelFont = resolveColor3("var(--font-family)", canvas) || "system-ui";
35499
+ const clamp01 = (v) => Math.min(1, Math.max(0, v));
35494
35500
  const prevPos = /* @__PURE__ */ new Map();
35495
35501
  for (const pn of nodesRef.current) if (pn.x != null && pn.y != null) prevPos.set(pn.id, { x: pn.x, y: pn.y });
35496
35502
  const simNodes = propNodes.map((n, idx) => {
@@ -35515,7 +35521,7 @@ var init_GraphCanvas = __esm({
35515
35521
  y = h * 0.2 + rand() * h * 0.6;
35516
35522
  }
35517
35523
  }
35518
- return { ...n, x, y, vx: 0, vy: 0, fx: 0, fy: 0 };
35524
+ return { ...n, x, y, vx: 0, vy: 0 };
35519
35525
  });
35520
35526
  nodesRef.current = simNodes;
35521
35527
  const prevIds = /* @__PURE__ */ new Set([...prevPos.keys()]);
@@ -35524,17 +35530,15 @@ var init_GraphCanvas = __esm({
35524
35530
  const overlap = prevIds.size === 0 ? 0 : kept / Math.max(prevIds.size, newIdList.length);
35525
35531
  const fullRelayout = !laidOutRef.current || overlap < 0.5;
35526
35532
  if (layout === "force") {
35527
- const maxIterations = 300;
35528
- const COOL = 0.12;
35529
- const COLLIDE_PASSES = 6;
35530
- const LABEL_GAP = 12;
35531
- const LABEL_H = 16;
35532
- const SIMILARITY_NEIGHBORS = 5;
35533
- const SIMILARITY_MIN_WEIGHT = 0.25;
35534
- const drawnPairs = /* @__PURE__ */ new Set();
35535
- for (const edge of propEdges) drawnPairs.add(edgeKeyOf(edge.source, edge.target));
35536
- const similarityNeighbors = /* @__PURE__ */ new Map();
35537
- {
35533
+ if (fullRelayout) {
35534
+ const LABEL_GAP = 12;
35535
+ const LABEL_H = 16;
35536
+ for (const n of simNodes) n.labelW = showLabels && n.label ? measureLabelWidth(truncateLabel(n.label), labelFont) : 0;
35537
+ const SIMILARITY_NEIGHBORS = 5;
35538
+ const SIMILARITY_MIN_WEIGHT = 0.25;
35539
+ const drawnPairs = /* @__PURE__ */ new Set();
35540
+ for (const edge of propEdges) drawnPairs.add(edgeKeyOf(edge.source, edge.target));
35541
+ const similarityNeighbors = /* @__PURE__ */ new Map();
35538
35542
  const bySource = /* @__PURE__ */ new Map();
35539
35543
  for (const pair of propSimilarity) {
35540
35544
  if (pair.weight < SIMILARITY_MIN_WEIGHT) continue;
@@ -35545,113 +35549,33 @@ var init_GraphCanvas = __esm({
35545
35549
  }
35546
35550
  for (const [id, arr] of bySource) {
35547
35551
  arr.sort((a, b) => b.weight - a.weight);
35548
- const keep = new Set(arr.slice(0, SIMILARITY_NEIGHBORS).map((p) => p.target));
35549
- similarityNeighbors.set(id, keep);
35550
- }
35551
- }
35552
- const activeSimilarity = propSimilarity.filter((pair) => {
35553
- if (drawnPairs.has(edgeKeyOf(pair.source, pair.target))) return false;
35554
- const a = similarityNeighbors.get(pair.source);
35555
- const b = similarityNeighbors.get(pair.target);
35556
- return (a?.has(pair.target) ?? false) || (b?.has(pair.source) ?? false);
35557
- });
35558
- const tick = (iter, options) => {
35559
- const skipForces = options?.skipForces ?? false;
35560
- const nodes = nodesRef.current;
35561
- const centerX = w / 2;
35562
- const centerY = h / 2;
35563
- const temp = skipForces ? 0 : Math.max(COOL, 1 - iter / maxIterations);
35564
- for (const node of nodes) {
35565
- node.fx = 0;
35566
- node.fy = 0;
35567
- }
35568
- if (!skipForces) {
35569
- for (let i = 0; i < nodes.length; i++) {
35570
- for (let j = i + 1; j < nodes.length; j++) {
35571
- const dx = nodes[j].x - nodes[i].x;
35572
- const dy = nodes[j].y - nodes[i].y;
35573
- const dist = Math.sqrt(dx * dx + dy * dy) || 1;
35574
- const force = repulsion / (dist * dist) * temp;
35575
- const fx = dx / dist * force;
35576
- const fy = dy / dist * force;
35577
- nodes[i].fx -= fx;
35578
- nodes[i].fy -= fy;
35579
- nodes[j].fx += fx;
35580
- nodes[j].fy += fy;
35581
- }
35582
- }
35583
- const nodeById = new Map(nodes.map((n) => [n.id, n]));
35584
- const spring = (a, b, rest, k) => {
35585
- const dx = b.x - a.x;
35586
- const dy = b.y - a.y;
35587
- const dist = Math.sqrt(dx * dx + dy * dy) || 1;
35588
- const force = (dist - rest) * k * temp;
35589
- const fx = dx / dist * force;
35590
- const fy = dy / dist * force;
35591
- a.fx += fx;
35592
- a.fy += fy;
35593
- b.fx -= fx;
35594
- b.fy -= fy;
35595
- };
35596
- for (const edge of propEdges) {
35597
- const source = nodeById.get(edge.source);
35598
- const target = nodeById.get(edge.target);
35599
- if (!source || !target) continue;
35600
- drawnPairs.add(edgeKeyOf(edge.source, edge.target));
35601
- const w2 = Math.min(1, Math.max(0, edge.weight ?? 1));
35602
- spring(source, target, linkDistance * (0.35 + (1 - w2) * 0.3), 0.14);
35603
- }
35604
- for (const pair of activeSimilarity) {
35605
- const source = nodeById.get(pair.source);
35606
- const target = nodeById.get(pair.target);
35607
- if (!source || !target) continue;
35608
- const w2 = Math.min(1, Math.max(0, pair.weight));
35609
- spring(source, target, linkDistance * (1.35 - 0.55 * w2), 0.015);
35610
- }
35611
- const centroids = /* @__PURE__ */ new Map();
35612
- for (const node of nodes) {
35613
- const g = node.group ?? "__none__";
35614
- let c = centroids.get(g);
35615
- if (!c) {
35616
- c = { x: 0, y: 0, n: 0 };
35617
- centroids.set(g, c);
35618
- }
35619
- c.x += node.x;
35620
- c.y += node.y;
35621
- c.n += 1;
35622
- }
35623
- const multiCluster = centroids.size > 1;
35624
- for (const node of nodes) {
35625
- if (multiCluster) {
35626
- const c = centroids.get(node.group ?? "__none__");
35627
- if (c && c.n > 1) {
35628
- node.fx += (c.x / c.n - node.x) * 0.06 * temp;
35629
- node.fy += (c.y / c.n - node.y) * 0.06 * temp;
35630
- } else {
35631
- node.fx += (centerX - node.x) * 0.01 * temp;
35632
- node.fy += (centerY - node.y) * 0.01 * temp;
35633
- }
35634
- } else {
35635
- node.fx += (centerX - node.x) * 8e-3 * temp;
35636
- node.fy += (centerY - node.y) * 8e-3 * temp;
35637
- }
35638
- }
35639
- const damping = 0.9;
35640
- for (const node of nodes) {
35641
- node.vx = (node.vx + node.fx) * damping;
35642
- node.vy = (node.vy + node.fy) * damping;
35643
- node.x += node.vx;
35644
- node.y += node.vy;
35645
- }
35552
+ similarityNeighbors.set(id, new Set(arr.slice(0, SIMILARITY_NEIGHBORS).map((p) => p.target)));
35646
35553
  }
35647
- for (const node of nodes) {
35648
- node.x = Math.max(30, Math.min(w - 30, node.x));
35649
- node.y = Math.max(30, Math.min(h - 30, node.y));
35554
+ const activeSimilarity = propSimilarity.filter((pair) => {
35555
+ if (drawnPairs.has(edgeKeyOf(pair.source, pair.target))) return false;
35556
+ const a = similarityNeighbors.get(pair.source);
35557
+ const b = similarityNeighbors.get(pair.target);
35558
+ return (a?.has(pair.target) ?? false) || (b?.has(pair.source) ?? false);
35559
+ });
35560
+ const groupTarget = /* @__PURE__ */ new Map();
35561
+ const multiCluster = groups.length > 1;
35562
+ if (multiCluster) {
35563
+ const ring = Math.min(w, h) * 0.34;
35564
+ groups.forEach((g, i) => {
35565
+ const a = i / groups.length * 2 * Math.PI;
35566
+ groupTarget.set(g, { x: w / 2 + ring * Math.cos(a), y: h / 2 + ring * Math.sin(a) });
35567
+ });
35650
35568
  }
35569
+ const present = new Set(simNodes.map((n) => n.id));
35570
+ const edgeLinks = propEdges.filter((e) => present.has(e.source) && present.has(e.target)).map((e) => ({ source: e.source, target: e.target, weight: clamp01(e.weight ?? 1) }));
35571
+ const simLinks = activeSimilarity.filter((p) => present.has(p.source) && present.has(p.target)).map((p) => ({ source: p.source, target: p.target, weight: clamp01(p.weight) }));
35572
+ const collideRadius = (n) => Math.max(n.size || 8, (n.labelW ?? 0) / 2) + nodeSpacing / 2;
35573
+ const simulation = forceSimulation(simNodes).force("edge", forceLink(edgeLinks).id((d) => d.id).distance((l) => linkDistance * (0.35 + (1 - l.weight) * 0.3)).strength(0.9)).force("similarity", forceLink(simLinks).id((d) => d.id).distance((l) => linkDistance * (1.35 - 0.55 * l.weight)).strength(0.03)).force("charge", forceManyBody().strength(-repulsion * 0.5)).force("collide", forceCollide().radius(collideRadius).strength(0.9)).force("x", forceX((n) => groupTarget.get(n.group ?? "")?.x ?? w / 2).strength(multiCluster ? 0.05 : 0.02)).force("y", forceY((n) => groupTarget.get(n.group ?? "")?.y ?? h / 2).strength(multiCluster ? 0.05 : 0.02)).stop();
35574
+ for (let i = 0; i < 300; i++) simulation.tick();
35651
35575
  const pad = nodeSpacing / 2;
35652
35576
  const rectsOf = (n) => {
35653
35577
  const r = n.size || 8;
35654
- const lw = showLabels ? n.labelW ?? (n.labelW = n.label ? measureLabelWidth(n.label, labelFont) : 0) : 0;
35578
+ const lw = showLabels ? n.labelW ?? 0 : 0;
35655
35579
  return {
35656
35580
  circle: [n.x - r - pad, n.y - r - pad, n.x + r + pad, n.y + r + pad],
35657
35581
  label: [n.x - lw / 2 - pad, n.y + r + LABEL_GAP - 8, n.x + lw / 2 + pad, n.y + r + LABEL_GAP + LABEL_H]
@@ -35663,11 +35587,12 @@ var init_GraphCanvas = __esm({
35663
35587
  if (ox <= 0 || oy <= 0) return null;
35664
35588
  return ox <= oy ? { axis: "x", depth: ox, sign: r1[0] + r1[2] < r2[0] + r2[2] ? 1 : -1 } : { axis: "y", depth: oy, sign: r1[1] + r1[3] < r2[1] + r2[3] ? 1 : -1 };
35665
35589
  };
35666
- for (let pass = 0; pass < COLLIDE_PASSES; pass++) {
35667
- for (let i = 0; i < nodes.length; i++) {
35668
- for (let j = i + 1; j < nodes.length; j++) {
35669
- const a = nodes[i];
35670
- const b = nodes[j];
35590
+ for (let pass = 0; pass < 24; pass++) {
35591
+ let moved = false;
35592
+ for (let i = 0; i < simNodes.length; i++) {
35593
+ for (let j = i + 1; j < simNodes.length; j++) {
35594
+ const a = simNodes[i];
35595
+ const b = simNodes[j];
35671
35596
  const ra = rectsOf(a);
35672
35597
  const rb = rectsOf(b);
35673
35598
  let best = null;
@@ -35676,31 +35601,25 @@ var init_GraphCanvas = __esm({
35676
35601
  if (s && (!best || s.depth < best.depth)) best = s;
35677
35602
  }
35678
35603
  if (best) {
35604
+ moved = true;
35679
35605
  const push = best.depth / 2;
35680
35606
  if (best.axis === "x") {
35681
35607
  a.x = Math.max(30, Math.min(w - 30, a.x - push * best.sign));
35682
35608
  b.x = Math.max(30, Math.min(w - 30, b.x + push * best.sign));
35683
- a.vx = 0;
35684
- b.vx = 0;
35685
35609
  } else {
35686
35610
  a.y = Math.max(30, Math.min(h - 30, a.y - push * best.sign));
35687
35611
  b.y = Math.max(30, Math.min(h - 30, b.y + push * best.sign));
35688
- a.vy = 0;
35689
- b.vy = 0;
35690
35612
  }
35691
35613
  }
35692
35614
  }
35693
35615
  }
35616
+ if (!moved) break;
35694
35617
  }
35695
- };
35696
- if (fullRelayout) {
35697
- for (let i = 0; i < maxIterations; i++) tick(i);
35698
- for (let i = 0; i < 4; i++) tick(maxIterations, { skipForces: true });
35699
35618
  const fitPad = 40;
35700
35619
  let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
35701
35620
  for (const node of nodesRef.current) {
35702
35621
  const r = node.size || 8;
35703
- const lw = showLabels ? node.labelW ?? (node.labelW = node.label ? measureLabelWidth(node.label, labelFont) : 0) : 0;
35622
+ const lw = showLabels ? node.labelW ?? 0 : 0;
35704
35623
  minX = Math.min(minX, node.x - r, node.x - lw / 2);
35705
35624
  minY = Math.min(minY, node.y - r);
35706
35625
  maxX = Math.max(maxX, node.x + r, node.x + lw / 2);
@@ -35821,6 +35740,7 @@ var init_GraphCanvas = __esm({
35821
35740
  }
35822
35741
  ctx.stroke();
35823
35742
  if (showLabels && node.label) {
35743
+ const displayLabel = truncateLabel(node.label);
35824
35744
  ctx.font = `${isSelected || isHovered ? "600" : "500"} 12px ${fontFamily}`;
35825
35745
  ctx.textAlign = "center";
35826
35746
  ctx.textBaseline = "middle";
@@ -35828,9 +35748,9 @@ var init_GraphCanvas = __esm({
35828
35748
  ctx.lineWidth = 3;
35829
35749
  ctx.lineJoin = "round";
35830
35750
  ctx.strokeStyle = bgColor;
35831
- ctx.strokeText(node.label, node.x, ly);
35751
+ ctx.strokeText(displayLabel, node.x, ly);
35832
35752
  ctx.fillStyle = isSelected || isHovered ? fgColor : mutedColor;
35833
- ctx.fillText(node.label, node.x, ly);
35753
+ ctx.fillText(displayLabel, node.x, ly);
35834
35754
  }
35835
35755
  if (node.badge && node.badge > 1) {
35836
35756
  const bx = node.x + radius * 0.7;
@@ -35978,6 +35898,20 @@ var init_GraphCanvas = __esm({
35978
35898
  },
35979
35899
  [toCoords, nodeAt, onNodeDoubleClick]
35980
35900
  );
35901
+ const hoveredObj = hoveredNode ? nodesRef.current.find((n) => n.id === hoveredNode) : void 0;
35902
+ const canvasEl = canvasRef.current;
35903
+ const showLabelTooltip = Boolean(
35904
+ hoveredObj?.label && hoveredObj.label.length > MAX_LABEL_CHARS && hoveredObj.x != null && canvasEl
35905
+ );
35906
+ const labelTooltipStyle = (() => {
35907
+ if (!showLabelTooltip || !canvasEl || !hoveredObj || hoveredObj.x == null || hoveredObj.y == null) return void 0;
35908
+ const rect = canvasEl.getBoundingClientRect();
35909
+ return {
35910
+ left: rect.left + offset.x + hoveredObj.x * zoom,
35911
+ top: rect.top + offset.y + hoveredObj.y * zoom - 10,
35912
+ transform: "translate(-50%, -100%)"
35913
+ };
35914
+ })();
35981
35915
  if (isLoading) {
35982
35916
  return /* @__PURE__ */ jsx(LoadingState, { message: t("common.loading"), className });
35983
35917
  }
@@ -36031,23 +35965,43 @@ var init_GraphCanvas = __esm({
36031
35965
  ]
36032
35966
  }
36033
35967
  ),
36034
- /* @__PURE__ */ jsx(Box, { className: "w-full bg-background", children: /* @__PURE__ */ jsx(
36035
- "canvas",
36036
- {
36037
- ref: canvasRef,
36038
- width: Math.round(logicalW * (typeof window !== "undefined" && window.devicePixelRatio || 1)),
36039
- height: Math.round(height * (typeof window !== "undefined" && window.devicePixelRatio || 1)),
36040
- className: "w-full cursor-grab active:cursor-grabbing touch-none",
36041
- style: { height },
36042
- onPointerDown: gestureHandlers.onPointerDown,
36043
- onPointerMove: gestureHandlers.onPointerMove,
36044
- onPointerUp: gestureHandlers.onPointerUp,
36045
- onPointerCancel: gestureHandlers.onPointerCancel,
36046
- onPointerLeave: handlePointerLeave,
36047
- onWheel: gestureHandlers.onWheel,
36048
- onDoubleClick: handleDoubleClick
36049
- }
36050
- ) }),
35968
+ /* @__PURE__ */ jsxs(Box, { className: "w-full bg-background", children: [
35969
+ /* @__PURE__ */ jsx(
35970
+ "canvas",
35971
+ {
35972
+ ref: canvasRef,
35973
+ width: Math.round(logicalW * (typeof window !== "undefined" && window.devicePixelRatio || 1)),
35974
+ height: Math.round(height * (typeof window !== "undefined" && window.devicePixelRatio || 1)),
35975
+ className: "w-full cursor-grab active:cursor-grabbing touch-none",
35976
+ style: { height },
35977
+ onPointerDown: gestureHandlers.onPointerDown,
35978
+ onPointerMove: gestureHandlers.onPointerMove,
35979
+ onPointerUp: gestureHandlers.onPointerUp,
35980
+ onPointerCancel: gestureHandlers.onPointerCancel,
35981
+ onPointerLeave: handlePointerLeave,
35982
+ onWheel: gestureHandlers.onWheel,
35983
+ onDoubleClick: handleDoubleClick
35984
+ }
35985
+ ),
35986
+ typeof window !== "undefined" && showLabelTooltip && labelTooltipStyle && createPortal(
35987
+ /* @__PURE__ */ jsx(
35988
+ "div",
35989
+ {
35990
+ className: cn(
35991
+ "fixed z-50 px-3 py-2 max-w-xs",
35992
+ "bg-primary text-primary-foreground",
35993
+ "shadow-elevation-popover rounded-sm",
35994
+ "text-sm pointer-events-none",
35995
+ "break-words whitespace-normal"
35996
+ ),
35997
+ style: labelTooltipStyle,
35998
+ role: "tooltip",
35999
+ children: hoveredObj?.label
36000
+ }
36001
+ ),
36002
+ document.body
36003
+ )
36004
+ ] }),
36051
36005
  groups.length > 1 && /* @__PURE__ */ jsx(HStack, { gap: "md", className: "px-4 py-2 border-t border-border", wrap: true, children: groups.map((group, idx) => /* @__PURE__ */ jsxs(HStack, { gap: "xs", align: "center", children: [
36052
36006
  /* @__PURE__ */ jsx(
36053
36007
  Box,