@almadar/ui 5.120.0 → 5.121.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.
@@ -42,6 +42,7 @@ var core$1 = require('@dnd-kit/core');
42
42
  var sortable = require('@dnd-kit/sortable');
43
43
  var utilities = require('@dnd-kit/utilities');
44
44
  var react = require('@xyflow/react');
45
+ var d3Force = require('d3-force');
45
46
  var patterns = require('@almadar/core/patterns');
46
47
 
47
48
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
@@ -35405,7 +35406,10 @@ function resolveColor3(color, el) {
35405
35406
  const resolved = getComputedStyle(el).getPropertyValue(m[1]).trim();
35406
35407
  return resolved || (m[2]?.trim() ?? "#888888");
35407
35408
  }
35408
- var GROUP_COLORS2, labelMeasureCtx, GraphCanvas;
35409
+ function truncateLabel(label) {
35410
+ return label.length > MAX_LABEL_CHARS ? label.slice(0, MAX_LABEL_CHARS - 1) + "\u2026" : label;
35411
+ }
35412
+ var GROUP_COLORS2, labelMeasureCtx, MAX_LABEL_CHARS, GraphCanvas;
35409
35413
  var init_GraphCanvas = __esm({
35410
35414
  "components/core/molecules/GraphCanvas.tsx"() {
35411
35415
  "use client";
@@ -35426,6 +35430,7 @@ var init_GraphCanvas = __esm({
35426
35430
  "var(--color-accent)"
35427
35431
  ];
35428
35432
  labelMeasureCtx = null;
35433
+ MAX_LABEL_CHARS = 22;
35429
35434
  GraphCanvas = ({
35430
35435
  title,
35431
35436
  nodes: propNodes = [],
@@ -35536,6 +35541,7 @@ var init_GraphCanvas = __esm({
35536
35541
  const w = viewW * layoutScale;
35537
35542
  const h = viewH * layoutScale;
35538
35543
  const labelFont = resolveColor3("var(--font-family)", canvas) || "system-ui";
35544
+ const clamp01 = (v) => Math.min(1, Math.max(0, v));
35539
35545
  const prevPos = /* @__PURE__ */ new Map();
35540
35546
  for (const pn of nodesRef.current) if (pn.x != null && pn.y != null) prevPos.set(pn.id, { x: pn.x, y: pn.y });
35541
35547
  const simNodes = propNodes.map((n, idx) => {
@@ -35560,7 +35566,7 @@ var init_GraphCanvas = __esm({
35560
35566
  y = h * 0.2 + rand() * h * 0.6;
35561
35567
  }
35562
35568
  }
35563
- return { ...n, x, y, vx: 0, vy: 0, fx: 0, fy: 0 };
35569
+ return { ...n, x, y, vx: 0, vy: 0 };
35564
35570
  });
35565
35571
  nodesRef.current = simNodes;
35566
35572
  const prevIds = /* @__PURE__ */ new Set([...prevPos.keys()]);
@@ -35569,17 +35575,15 @@ var init_GraphCanvas = __esm({
35569
35575
  const overlap = prevIds.size === 0 ? 0 : kept / Math.max(prevIds.size, newIdList.length);
35570
35576
  const fullRelayout = !laidOutRef.current || overlap < 0.5;
35571
35577
  if (layout === "force") {
35572
- const maxIterations = 300;
35573
- const COOL = 0.12;
35574
- const COLLIDE_PASSES = 6;
35575
- const LABEL_GAP = 12;
35576
- const LABEL_H = 16;
35577
- const SIMILARITY_NEIGHBORS = 5;
35578
- const SIMILARITY_MIN_WEIGHT = 0.25;
35579
- const drawnPairs = /* @__PURE__ */ new Set();
35580
- for (const edge of propEdges) drawnPairs.add(edgeKeyOf(edge.source, edge.target));
35581
- const similarityNeighbors = /* @__PURE__ */ new Map();
35582
- {
35578
+ if (fullRelayout) {
35579
+ const LABEL_GAP = 12;
35580
+ const LABEL_H = 16;
35581
+ for (const n of simNodes) n.labelW = showLabels && n.label ? measureLabelWidth(truncateLabel(n.label), labelFont) : 0;
35582
+ const SIMILARITY_NEIGHBORS = 5;
35583
+ const SIMILARITY_MIN_WEIGHT = 0.25;
35584
+ const drawnPairs = /* @__PURE__ */ new Set();
35585
+ for (const edge of propEdges) drawnPairs.add(edgeKeyOf(edge.source, edge.target));
35586
+ const similarityNeighbors = /* @__PURE__ */ new Map();
35583
35587
  const bySource = /* @__PURE__ */ new Map();
35584
35588
  for (const pair of propSimilarity) {
35585
35589
  if (pair.weight < SIMILARITY_MIN_WEIGHT) continue;
@@ -35590,113 +35594,33 @@ var init_GraphCanvas = __esm({
35590
35594
  }
35591
35595
  for (const [id, arr] of bySource) {
35592
35596
  arr.sort((a, b) => b.weight - a.weight);
35593
- const keep = new Set(arr.slice(0, SIMILARITY_NEIGHBORS).map((p) => p.target));
35594
- similarityNeighbors.set(id, keep);
35597
+ similarityNeighbors.set(id, new Set(arr.slice(0, SIMILARITY_NEIGHBORS).map((p) => p.target)));
35595
35598
  }
35596
- }
35597
- const activeSimilarity = propSimilarity.filter((pair) => {
35598
- if (drawnPairs.has(edgeKeyOf(pair.source, pair.target))) return false;
35599
- const a = similarityNeighbors.get(pair.source);
35600
- const b = similarityNeighbors.get(pair.target);
35601
- return (a?.has(pair.target) ?? false) || (b?.has(pair.source) ?? false);
35602
- });
35603
- const tick = (iter, options) => {
35604
- const skipForces = options?.skipForces ?? false;
35605
- const nodes = nodesRef.current;
35606
- const centerX = w / 2;
35607
- const centerY = h / 2;
35608
- const temp = skipForces ? 0 : Math.max(COOL, 1 - iter / maxIterations);
35609
- for (const node of nodes) {
35610
- node.fx = 0;
35611
- node.fy = 0;
35612
- }
35613
- if (!skipForces) {
35614
- for (let i = 0; i < nodes.length; i++) {
35615
- for (let j = i + 1; j < nodes.length; j++) {
35616
- const dx = nodes[j].x - nodes[i].x;
35617
- const dy = nodes[j].y - nodes[i].y;
35618
- const dist = Math.sqrt(dx * dx + dy * dy) || 1;
35619
- const force = repulsion / (dist * dist) * temp;
35620
- const fx = dx / dist * force;
35621
- const fy = dy / dist * force;
35622
- nodes[i].fx -= fx;
35623
- nodes[i].fy -= fy;
35624
- nodes[j].fx += fx;
35625
- nodes[j].fy += fy;
35626
- }
35627
- }
35628
- const nodeById = new Map(nodes.map((n) => [n.id, n]));
35629
- const spring = (a, b, rest, k) => {
35630
- const dx = b.x - a.x;
35631
- const dy = b.y - a.y;
35632
- const dist = Math.sqrt(dx * dx + dy * dy) || 1;
35633
- const force = (dist - rest) * k * temp;
35634
- const fx = dx / dist * force;
35635
- const fy = dy / dist * force;
35636
- a.fx += fx;
35637
- a.fy += fy;
35638
- b.fx -= fx;
35639
- b.fy -= fy;
35640
- };
35641
- for (const edge of propEdges) {
35642
- const source = nodeById.get(edge.source);
35643
- const target = nodeById.get(edge.target);
35644
- if (!source || !target) continue;
35645
- drawnPairs.add(edgeKeyOf(edge.source, edge.target));
35646
- const w2 = Math.min(1, Math.max(0, edge.weight ?? 1));
35647
- spring(source, target, linkDistance * (0.35 + (1 - w2) * 0.3), 0.14);
35648
- }
35649
- for (const pair of activeSimilarity) {
35650
- const source = nodeById.get(pair.source);
35651
- const target = nodeById.get(pair.target);
35652
- if (!source || !target) continue;
35653
- const w2 = Math.min(1, Math.max(0, pair.weight));
35654
- spring(source, target, linkDistance * (1.35 - 0.55 * w2), 0.015);
35655
- }
35656
- const centroids = /* @__PURE__ */ new Map();
35657
- for (const node of nodes) {
35658
- const g = node.group ?? "__none__";
35659
- let c = centroids.get(g);
35660
- if (!c) {
35661
- c = { x: 0, y: 0, n: 0 };
35662
- centroids.set(g, c);
35663
- }
35664
- c.x += node.x;
35665
- c.y += node.y;
35666
- c.n += 1;
35667
- }
35668
- const multiCluster = centroids.size > 1;
35669
- for (const node of nodes) {
35670
- if (multiCluster) {
35671
- const c = centroids.get(node.group ?? "__none__");
35672
- if (c && c.n > 1) {
35673
- node.fx += (c.x / c.n - node.x) * 0.06 * temp;
35674
- node.fy += (c.y / c.n - node.y) * 0.06 * temp;
35675
- } else {
35676
- node.fx += (centerX - node.x) * 0.01 * temp;
35677
- node.fy += (centerY - node.y) * 0.01 * temp;
35678
- }
35679
- } else {
35680
- node.fx += (centerX - node.x) * 8e-3 * temp;
35681
- node.fy += (centerY - node.y) * 8e-3 * temp;
35682
- }
35683
- }
35684
- const damping = 0.9;
35685
- for (const node of nodes) {
35686
- node.vx = (node.vx + node.fx) * damping;
35687
- node.vy = (node.vy + node.fy) * damping;
35688
- node.x += node.vx;
35689
- node.y += node.vy;
35690
- }
35691
- }
35692
- for (const node of nodes) {
35693
- node.x = Math.max(30, Math.min(w - 30, node.x));
35694
- node.y = Math.max(30, Math.min(h - 30, node.y));
35599
+ const activeSimilarity = propSimilarity.filter((pair) => {
35600
+ if (drawnPairs.has(edgeKeyOf(pair.source, pair.target))) return false;
35601
+ const a = similarityNeighbors.get(pair.source);
35602
+ const b = similarityNeighbors.get(pair.target);
35603
+ return (a?.has(pair.target) ?? false) || (b?.has(pair.source) ?? false);
35604
+ });
35605
+ const groupTarget = /* @__PURE__ */ new Map();
35606
+ const multiCluster = groups.length > 1;
35607
+ if (multiCluster) {
35608
+ const ring = Math.min(w, h) * 0.34;
35609
+ groups.forEach((g, i) => {
35610
+ const a = i / groups.length * 2 * Math.PI;
35611
+ groupTarget.set(g, { x: w / 2 + ring * Math.cos(a), y: h / 2 + ring * Math.sin(a) });
35612
+ });
35695
35613
  }
35614
+ const present = new Set(simNodes.map((n) => n.id));
35615
+ 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) }));
35616
+ 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) }));
35617
+ const collideRadius = (n) => Math.max(n.size || 8, (n.labelW ?? 0) / 2) + nodeSpacing / 2;
35618
+ const simulation = d3Force.forceSimulation(simNodes).force("edge", d3Force.forceLink(edgeLinks).id((d) => d.id).distance((l) => linkDistance * (0.35 + (1 - l.weight) * 0.3)).strength(0.9)).force("similarity", d3Force.forceLink(simLinks).id((d) => d.id).distance((l) => linkDistance * (1.35 - 0.55 * l.weight)).strength(0.03)).force("charge", d3Force.forceManyBody().strength(-repulsion * 0.5)).force("collide", d3Force.forceCollide().radius(collideRadius).strength(0.9)).force("x", d3Force.forceX((n) => groupTarget.get(n.group ?? "")?.x ?? w / 2).strength(multiCluster ? 0.05 : 0.02)).force("y", d3Force.forceY((n) => groupTarget.get(n.group ?? "")?.y ?? h / 2).strength(multiCluster ? 0.05 : 0.02)).stop();
35619
+ for (let i = 0; i < 300; i++) simulation.tick();
35696
35620
  const pad = nodeSpacing / 2;
35697
35621
  const rectsOf = (n) => {
35698
35622
  const r = n.size || 8;
35699
- const lw = showLabels ? n.labelW ?? (n.labelW = n.label ? measureLabelWidth(n.label, labelFont) : 0) : 0;
35623
+ const lw = showLabels ? n.labelW ?? 0 : 0;
35700
35624
  return {
35701
35625
  circle: [n.x - r - pad, n.y - r - pad, n.x + r + pad, n.y + r + pad],
35702
35626
  label: [n.x - lw / 2 - pad, n.y + r + LABEL_GAP - 8, n.x + lw / 2 + pad, n.y + r + LABEL_GAP + LABEL_H]
@@ -35708,11 +35632,12 @@ var init_GraphCanvas = __esm({
35708
35632
  if (ox <= 0 || oy <= 0) return null;
35709
35633
  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 };
35710
35634
  };
35711
- for (let pass = 0; pass < COLLIDE_PASSES; pass++) {
35712
- for (let i = 0; i < nodes.length; i++) {
35713
- for (let j = i + 1; j < nodes.length; j++) {
35714
- const a = nodes[i];
35715
- const b = nodes[j];
35635
+ for (let pass = 0; pass < 24; pass++) {
35636
+ let moved = false;
35637
+ for (let i = 0; i < simNodes.length; i++) {
35638
+ for (let j = i + 1; j < simNodes.length; j++) {
35639
+ const a = simNodes[i];
35640
+ const b = simNodes[j];
35716
35641
  const ra = rectsOf(a);
35717
35642
  const rb = rectsOf(b);
35718
35643
  let best = null;
@@ -35721,31 +35646,25 @@ var init_GraphCanvas = __esm({
35721
35646
  if (s && (!best || s.depth < best.depth)) best = s;
35722
35647
  }
35723
35648
  if (best) {
35649
+ moved = true;
35724
35650
  const push = best.depth / 2;
35725
35651
  if (best.axis === "x") {
35726
35652
  a.x = Math.max(30, Math.min(w - 30, a.x - push * best.sign));
35727
35653
  b.x = Math.max(30, Math.min(w - 30, b.x + push * best.sign));
35728
- a.vx = 0;
35729
- b.vx = 0;
35730
35654
  } else {
35731
35655
  a.y = Math.max(30, Math.min(h - 30, a.y - push * best.sign));
35732
35656
  b.y = Math.max(30, Math.min(h - 30, b.y + push * best.sign));
35733
- a.vy = 0;
35734
- b.vy = 0;
35735
35657
  }
35736
35658
  }
35737
35659
  }
35738
35660
  }
35661
+ if (!moved) break;
35739
35662
  }
35740
- };
35741
- if (fullRelayout) {
35742
- for (let i = 0; i < maxIterations; i++) tick(i);
35743
- for (let i = 0; i < 4; i++) tick(maxIterations, { skipForces: true });
35744
35663
  const fitPad = 40;
35745
35664
  let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
35746
35665
  for (const node of nodesRef.current) {
35747
35666
  const r = node.size || 8;
35748
- const lw = showLabels ? node.labelW ?? (node.labelW = node.label ? measureLabelWidth(node.label, labelFont) : 0) : 0;
35667
+ const lw = showLabels ? node.labelW ?? 0 : 0;
35749
35668
  minX = Math.min(minX, node.x - r, node.x - lw / 2);
35750
35669
  minY = Math.min(minY, node.y - r);
35751
35670
  maxX = Math.max(maxX, node.x + r, node.x + lw / 2);
@@ -35866,16 +35785,17 @@ var init_GraphCanvas = __esm({
35866
35785
  }
35867
35786
  ctx.stroke();
35868
35787
  if (showLabels && node.label) {
35869
- ctx.font = `${isSelected || isHovered ? "600" : "500"} 12px ${fontFamily}`;
35788
+ const displayLabel = truncateLabel(node.label);
35789
+ ctx.font = `${isSelected || isHovered ? "700" : "600"} 12px ${fontFamily}`;
35870
35790
  ctx.textAlign = "center";
35871
35791
  ctx.textBaseline = "middle";
35872
35792
  const ly = node.y + radius + 14;
35873
- ctx.lineWidth = 3;
35793
+ ctx.lineWidth = 4;
35874
35794
  ctx.lineJoin = "round";
35875
35795
  ctx.strokeStyle = bgColor;
35876
- ctx.strokeText(node.label, node.x, ly);
35877
- ctx.fillStyle = isSelected || isHovered ? fgColor : mutedColor;
35878
- ctx.fillText(node.label, node.x, ly);
35796
+ ctx.strokeText(displayLabel, node.x, ly);
35797
+ ctx.fillStyle = fgColor;
35798
+ ctx.fillText(displayLabel, node.x, ly);
35879
35799
  }
35880
35800
  if (node.badge && node.badge > 1) {
35881
35801
  const bx = node.x + radius * 0.7;
@@ -36023,6 +35943,20 @@ var init_GraphCanvas = __esm({
36023
35943
  },
36024
35944
  [toCoords, nodeAt, onNodeDoubleClick]
36025
35945
  );
35946
+ const hoveredObj = hoveredNode ? nodesRef.current.find((n) => n.id === hoveredNode) : void 0;
35947
+ const canvasEl = canvasRef.current;
35948
+ const showLabelTooltip = Boolean(
35949
+ hoveredObj?.label && hoveredObj.label.length > MAX_LABEL_CHARS && hoveredObj.x != null && canvasEl
35950
+ );
35951
+ const labelTooltipStyle = (() => {
35952
+ if (!showLabelTooltip || !canvasEl || !hoveredObj || hoveredObj.x == null || hoveredObj.y == null) return void 0;
35953
+ const rect = canvasEl.getBoundingClientRect();
35954
+ return {
35955
+ left: rect.left + offset.x + hoveredObj.x * zoom,
35956
+ top: rect.top + offset.y + hoveredObj.y * zoom - 10,
35957
+ transform: "translate(-50%, -100%)"
35958
+ };
35959
+ })();
36026
35960
  if (isLoading) {
36027
35961
  return /* @__PURE__ */ jsxRuntime.jsx(LoadingState, { message: t("common.loading"), className });
36028
35962
  }
@@ -36076,23 +36010,43 @@ var init_GraphCanvas = __esm({
36076
36010
  ]
36077
36011
  }
36078
36012
  ),
36079
- /* @__PURE__ */ jsxRuntime.jsx(Box, { className: "w-full bg-background", children: /* @__PURE__ */ jsxRuntime.jsx(
36080
- "canvas",
36081
- {
36082
- ref: canvasRef,
36083
- width: Math.round(logicalW * (typeof window !== "undefined" && window.devicePixelRatio || 1)),
36084
- height: Math.round(height * (typeof window !== "undefined" && window.devicePixelRatio || 1)),
36085
- className: "w-full cursor-grab active:cursor-grabbing touch-none",
36086
- style: { height },
36087
- onPointerDown: gestureHandlers.onPointerDown,
36088
- onPointerMove: gestureHandlers.onPointerMove,
36089
- onPointerUp: gestureHandlers.onPointerUp,
36090
- onPointerCancel: gestureHandlers.onPointerCancel,
36091
- onPointerLeave: handlePointerLeave,
36092
- onWheel: gestureHandlers.onWheel,
36093
- onDoubleClick: handleDoubleClick
36094
- }
36095
- ) }),
36013
+ /* @__PURE__ */ jsxRuntime.jsxs(Box, { className: "w-full bg-background", children: [
36014
+ /* @__PURE__ */ jsxRuntime.jsx(
36015
+ "canvas",
36016
+ {
36017
+ ref: canvasRef,
36018
+ width: Math.round(logicalW * (typeof window !== "undefined" && window.devicePixelRatio || 1)),
36019
+ height: Math.round(height * (typeof window !== "undefined" && window.devicePixelRatio || 1)),
36020
+ className: "w-full cursor-grab active:cursor-grabbing touch-none",
36021
+ style: { height },
36022
+ onPointerDown: gestureHandlers.onPointerDown,
36023
+ onPointerMove: gestureHandlers.onPointerMove,
36024
+ onPointerUp: gestureHandlers.onPointerUp,
36025
+ onPointerCancel: gestureHandlers.onPointerCancel,
36026
+ onPointerLeave: handlePointerLeave,
36027
+ onWheel: gestureHandlers.onWheel,
36028
+ onDoubleClick: handleDoubleClick
36029
+ }
36030
+ ),
36031
+ typeof window !== "undefined" && showLabelTooltip && labelTooltipStyle && reactDom.createPortal(
36032
+ /* @__PURE__ */ jsxRuntime.jsx(
36033
+ "div",
36034
+ {
36035
+ className: cn(
36036
+ "fixed z-50 px-3 py-2 max-w-xs",
36037
+ "bg-primary text-primary-foreground",
36038
+ "shadow-elevation-popover rounded-sm",
36039
+ "text-sm pointer-events-none",
36040
+ "break-words whitespace-normal"
36041
+ ),
36042
+ style: labelTooltipStyle,
36043
+ role: "tooltip",
36044
+ children: hoveredObj?.label
36045
+ }
36046
+ ),
36047
+ document.body
36048
+ )
36049
+ ] }),
36096
36050
  groups.length > 1 && /* @__PURE__ */ jsxRuntime.jsx(HStack, { gap: "md", className: "px-4 py-2 border-t border-border", wrap: true, children: groups.map((group, idx) => /* @__PURE__ */ jsxRuntime.jsxs(HStack, { gap: "xs", align: "center", children: [
36097
36051
  /* @__PURE__ */ jsxRuntime.jsx(
36098
36052
  Box,