@almadar/ui 5.119.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.
@@ -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 = [],
@@ -35530,9 +35535,13 @@ var init_GraphCanvas = __esm({
35530
35535
  React83.useEffect(() => {
35531
35536
  const canvas = canvasRef.current;
35532
35537
  if (!canvas || propNodes.length === 0) return;
35533
- const w = logicalW;
35534
- const h = height;
35538
+ const viewW = logicalW;
35539
+ const viewH = height;
35540
+ const layoutScale = Math.max(1, Math.min(3, Math.sqrt(propNodes.length / 12)));
35541
+ const w = viewW * layoutScale;
35542
+ const h = viewH * layoutScale;
35535
35543
  const labelFont = resolveColor3("var(--font-family)", canvas) || "system-ui";
35544
+ const clamp01 = (v) => Math.min(1, Math.max(0, v));
35536
35545
  const prevPos = /* @__PURE__ */ new Map();
35537
35546
  for (const pn of nodesRef.current) if (pn.x != null && pn.y != null) prevPos.set(pn.id, { x: pn.x, y: pn.y });
35538
35547
  const simNodes = propNodes.map((n, idx) => {
@@ -35557,7 +35566,7 @@ var init_GraphCanvas = __esm({
35557
35566
  y = h * 0.2 + rand() * h * 0.6;
35558
35567
  }
35559
35568
  }
35560
- return { ...n, x, y, vx: 0, vy: 0, fx: 0, fy: 0 };
35569
+ return { ...n, x, y, vx: 0, vy: 0 };
35561
35570
  });
35562
35571
  nodesRef.current = simNodes;
35563
35572
  const prevIds = /* @__PURE__ */ new Set([...prevPos.keys()]);
@@ -35566,105 +35575,52 @@ var init_GraphCanvas = __esm({
35566
35575
  const overlap = prevIds.size === 0 ? 0 : kept / Math.max(prevIds.size, newIdList.length);
35567
35576
  const fullRelayout = !laidOutRef.current || overlap < 0.5;
35568
35577
  if (layout === "force") {
35569
- const maxIterations = 300;
35570
- const COOL = 0.12;
35571
- const COLLIDE_PASSES = 6;
35572
- const LABEL_GAP = 12;
35573
- const LABEL_H = 16;
35574
- const tick = (iter) => {
35575
- const nodes = nodesRef.current;
35576
- const centerX = w / 2;
35577
- const centerY = h / 2;
35578
- const temp = Math.max(COOL, 1 - iter / maxIterations);
35579
- for (const node of nodes) {
35580
- node.fx = 0;
35581
- node.fy = 0;
35582
- }
35583
- for (let i = 0; i < nodes.length; i++) {
35584
- for (let j = i + 1; j < nodes.length; j++) {
35585
- const dx = nodes[j].x - nodes[i].x;
35586
- const dy = nodes[j].y - nodes[i].y;
35587
- const dist = Math.sqrt(dx * dx + dy * dy) || 1;
35588
- const force = repulsion / (dist * dist) * temp;
35589
- const fx = dx / dist * force;
35590
- const fy = dy / dist * force;
35591
- nodes[i].fx -= fx;
35592
- nodes[i].fy -= fy;
35593
- nodes[j].fx += fx;
35594
- nodes[j].fy += fy;
35595
- }
35596
- }
35597
- const nodeById = new Map(nodes.map((n) => [n.id, n]));
35598
- const spring = (a, b, rest, k) => {
35599
- const dx = b.x - a.x;
35600
- const dy = b.y - a.y;
35601
- const dist = Math.sqrt(dx * dx + dy * dy) || 1;
35602
- const force = (dist - rest) * k * temp;
35603
- const fx = dx / dist * force;
35604
- const fy = dy / dist * force;
35605
- a.fx += fx;
35606
- a.fy += fy;
35607
- b.fx -= fx;
35608
- b.fy -= fy;
35609
- };
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;
35610
35584
  const drawnPairs = /* @__PURE__ */ new Set();
35611
- for (const edge of propEdges) {
35612
- const source = nodeById.get(edge.source);
35613
- const target = nodeById.get(edge.target);
35614
- if (!source || !target) continue;
35615
- drawnPairs.add(edgeKeyOf(edge.source, edge.target));
35616
- const w2 = Math.min(1, Math.max(0, edge.weight ?? 1));
35617
- spring(source, target, linkDistance * (0.35 + (1 - w2) * 0.3), 0.14);
35618
- }
35585
+ for (const edge of propEdges) drawnPairs.add(edgeKeyOf(edge.source, edge.target));
35586
+ const similarityNeighbors = /* @__PURE__ */ new Map();
35587
+ const bySource = /* @__PURE__ */ new Map();
35619
35588
  for (const pair of propSimilarity) {
35589
+ if (pair.weight < SIMILARITY_MIN_WEIGHT) continue;
35620
35590
  if (drawnPairs.has(edgeKeyOf(pair.source, pair.target))) continue;
35621
- const source = nodeById.get(pair.source);
35622
- const target = nodeById.get(pair.target);
35623
- if (!source || !target) continue;
35624
- const w2 = Math.min(1, Math.max(0, pair.weight));
35625
- spring(source, target, linkDistance * (1.35 - 0.55 * w2), 0.015);
35591
+ const arr = bySource.get(pair.source) ?? [];
35592
+ arr.push(pair);
35593
+ bySource.set(pair.source, arr);
35626
35594
  }
35627
- const centroids = /* @__PURE__ */ new Map();
35628
- for (const node of nodes) {
35629
- const g = node.group ?? "__none__";
35630
- let c = centroids.get(g);
35631
- if (!c) {
35632
- c = { x: 0, y: 0, n: 0 };
35633
- centroids.set(g, c);
35634
- }
35635
- c.x += node.x;
35636
- c.y += node.y;
35637
- c.n += 1;
35595
+ for (const [id, arr] of bySource) {
35596
+ arr.sort((a, b) => b.weight - a.weight);
35597
+ similarityNeighbors.set(id, new Set(arr.slice(0, SIMILARITY_NEIGHBORS).map((p) => p.target)));
35638
35598
  }
35639
- const multiCluster = centroids.size > 1;
35640
- for (const node of nodes) {
35641
- if (multiCluster) {
35642
- const c = centroids.get(node.group ?? "__none__");
35643
- if (c && c.n > 1) {
35644
- node.fx += (c.x / c.n - node.x) * 0.04 * temp;
35645
- node.fy += (c.y / c.n - node.y) * 0.04 * temp;
35646
- } else {
35647
- node.fx += (centerX - node.x) * 0.01 * temp;
35648
- node.fy += (centerY - node.y) * 0.01 * temp;
35649
- }
35650
- } else {
35651
- node.fx += (centerX - node.x) * 8e-3 * temp;
35652
- node.fy += (centerY - node.y) * 8e-3 * temp;
35653
- }
35654
- }
35655
- const damping = 0.9;
35656
- for (const node of nodes) {
35657
- node.vx = (node.vx + node.fx) * damping;
35658
- node.vy = (node.vy + node.fy) * damping;
35659
- node.x += node.vx;
35660
- node.y += node.vy;
35661
- node.x = Math.max(30, Math.min(w - 30, node.x));
35662
- 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
+ });
35663
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();
35664
35620
  const pad = nodeSpacing / 2;
35665
35621
  const rectsOf = (n) => {
35666
35622
  const r = n.size || 8;
35667
- const lw = showLabels ? n.labelW ?? (n.labelW = n.label ? measureLabelWidth(n.label, labelFont) : 0) : 0;
35623
+ const lw = showLabels ? n.labelW ?? 0 : 0;
35668
35624
  return {
35669
35625
  circle: [n.x - r - pad, n.y - r - pad, n.x + r + pad, n.y + r + pad],
35670
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]
@@ -35676,11 +35632,12 @@ var init_GraphCanvas = __esm({
35676
35632
  if (ox <= 0 || oy <= 0) return null;
35677
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 };
35678
35634
  };
35679
- for (let pass = 0; pass < COLLIDE_PASSES; pass++) {
35680
- for (let i = 0; i < nodes.length; i++) {
35681
- for (let j = i + 1; j < nodes.length; j++) {
35682
- const a = nodes[i];
35683
- 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];
35684
35641
  const ra = rectsOf(a);
35685
35642
  const rb = rectsOf(b);
35686
35643
  let best = null;
@@ -35689,25 +35646,35 @@ var init_GraphCanvas = __esm({
35689
35646
  if (s && (!best || s.depth < best.depth)) best = s;
35690
35647
  }
35691
35648
  if (best) {
35649
+ moved = true;
35692
35650
  const push = best.depth / 2;
35693
35651
  if (best.axis === "x") {
35694
35652
  a.x = Math.max(30, Math.min(w - 30, a.x - push * best.sign));
35695
35653
  b.x = Math.max(30, Math.min(w - 30, b.x + push * best.sign));
35696
- a.vx = 0;
35697
- b.vx = 0;
35698
35654
  } else {
35699
35655
  a.y = Math.max(30, Math.min(h - 30, a.y - push * best.sign));
35700
35656
  b.y = Math.max(30, Math.min(h - 30, b.y + push * best.sign));
35701
- a.vy = 0;
35702
- b.vy = 0;
35703
35657
  }
35704
35658
  }
35705
35659
  }
35706
35660
  }
35661
+ if (!moved) break;
35707
35662
  }
35708
- };
35709
- if (fullRelayout) {
35710
- for (let i = 0; i < maxIterations; i++) tick(i);
35663
+ const fitPad = 40;
35664
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
35665
+ for (const node of nodesRef.current) {
35666
+ const r = node.size || 8;
35667
+ const lw = showLabels ? node.labelW ?? 0 : 0;
35668
+ minX = Math.min(minX, node.x - r, node.x - lw / 2);
35669
+ minY = Math.min(minY, node.y - r);
35670
+ maxX = Math.max(maxX, node.x + r, node.x + lw / 2);
35671
+ maxY = Math.max(maxY, node.y + r + LABEL_GAP + LABEL_H);
35672
+ }
35673
+ const bboxW = Math.max(1, maxX - minX + fitPad * 2);
35674
+ const bboxH = Math.max(1, maxY - minY + fitPad * 2);
35675
+ const nextZoom = Math.min(1, viewW / bboxW, viewH / bboxH);
35676
+ setZoom(nextZoom);
35677
+ setOffset({ x: fitPad - minX * nextZoom, y: fitPad - minY * nextZoom });
35711
35678
  laidOutRef.current = true;
35712
35679
  } else {
35713
35680
  const centroids = /* @__PURE__ */ new Map();
@@ -35818,6 +35785,7 @@ var init_GraphCanvas = __esm({
35818
35785
  }
35819
35786
  ctx.stroke();
35820
35787
  if (showLabels && node.label) {
35788
+ const displayLabel = truncateLabel(node.label);
35821
35789
  ctx.font = `${isSelected || isHovered ? "600" : "500"} 12px ${fontFamily}`;
35822
35790
  ctx.textAlign = "center";
35823
35791
  ctx.textBaseline = "middle";
@@ -35825,9 +35793,9 @@ var init_GraphCanvas = __esm({
35825
35793
  ctx.lineWidth = 3;
35826
35794
  ctx.lineJoin = "round";
35827
35795
  ctx.strokeStyle = bgColor;
35828
- ctx.strokeText(node.label, node.x, ly);
35796
+ ctx.strokeText(displayLabel, node.x, ly);
35829
35797
  ctx.fillStyle = isSelected || isHovered ? fgColor : mutedColor;
35830
- ctx.fillText(node.label, node.x, ly);
35798
+ ctx.fillText(displayLabel, node.x, ly);
35831
35799
  }
35832
35800
  if (node.badge && node.badge > 1) {
35833
35801
  const bx = node.x + radius * 0.7;
@@ -35975,6 +35943,20 @@ var init_GraphCanvas = __esm({
35975
35943
  },
35976
35944
  [toCoords, nodeAt, onNodeDoubleClick]
35977
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
+ })();
35978
35960
  if (isLoading) {
35979
35961
  return /* @__PURE__ */ jsxRuntime.jsx(LoadingState, { message: t("common.loading"), className });
35980
35962
  }
@@ -36028,23 +36010,43 @@ var init_GraphCanvas = __esm({
36028
36010
  ]
36029
36011
  }
36030
36012
  ),
36031
- /* @__PURE__ */ jsxRuntime.jsx(Box, { className: "w-full bg-background", children: /* @__PURE__ */ jsxRuntime.jsx(
36032
- "canvas",
36033
- {
36034
- ref: canvasRef,
36035
- width: Math.round(logicalW * (typeof window !== "undefined" && window.devicePixelRatio || 1)),
36036
- height: Math.round(height * (typeof window !== "undefined" && window.devicePixelRatio || 1)),
36037
- className: "w-full cursor-grab active:cursor-grabbing touch-none",
36038
- style: { height },
36039
- onPointerDown: gestureHandlers.onPointerDown,
36040
- onPointerMove: gestureHandlers.onPointerMove,
36041
- onPointerUp: gestureHandlers.onPointerUp,
36042
- onPointerCancel: gestureHandlers.onPointerCancel,
36043
- onPointerLeave: handlePointerLeave,
36044
- onWheel: gestureHandlers.onWheel,
36045
- onDoubleClick: handleDoubleClick
36046
- }
36047
- ) }),
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
+ ] }),
36048
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: [
36049
36051
  /* @__PURE__ */ jsxRuntime.jsx(
36050
36052
  Box,