@yuku123/z-cognition-component 1.0.2 → 1.0.4

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.
@@ -0,0 +1,1086 @@
1
+ import { Graph as Je } from "@antv/g6";
2
+ import { useRef as L, useState as De, useEffect as ee, useCallback as w } from "react";
3
+ import { forceSimulation as Me, forceLink as Ie, forceManyBody as Oe, forceX as Le, forceY as we, forceCollide as Te } from "d3-force";
4
+ const ye = "#3d7dd4", Qe = "#89b4e8", Ne = "#1e4f8f", et = "#94a3b8", tt = "#64748b", nt = "#94a3b8", xe = "#64748b", Ce = "#0052d9", Pe = "#003cab", rt = "rgba(0, 82, 217, 0.25)", Ee = 52, ot = "#fff", st = 11, oe = "__uncategorized__", ve = [
5
+ { fill: "#722ed1", stroke: "#531cd3" },
6
+ // 紫
7
+ { fill: "#1890ff", stroke: "#006cd4" },
8
+ // 蓝
9
+ { fill: "#13c2c2", stroke: "#08979b" },
10
+ // 青
11
+ { fill: "#52c41a", stroke: "#389e0d" },
12
+ // 绿
13
+ { fill: "#fa8c16", stroke: "#d4670c" },
14
+ // 橙
15
+ { fill: "#f5222d", stroke: "#cf1717" },
16
+ // 红
17
+ { fill: "#eb2f96", stroke: "#c41d78" },
18
+ // 粉
19
+ { fill: "#faad14", stroke: "#d48806" },
20
+ // 金
21
+ { fill: "#2f54eb", stroke: "#1d39c4" },
22
+ // 极客蓝
23
+ { fill: "#a0d911", stroke: "#7cb305" }
24
+ // 青柠
25
+ ], ae = { fill: Ce, stroke: Pe };
26
+ function Re(a, i) {
27
+ const c = a.replace("#", ""), u = parseInt(c.slice(0, 2), 16), o = parseInt(c.slice(2, 4), 16), t = parseInt(c.slice(4, 6), 16);
28
+ return `rgba(${u}, ${o}, ${t}, ${i})`;
29
+ }
30
+ function kt(a) {
31
+ const i = /* @__PURE__ */ new Map();
32
+ return [...a].sort(
33
+ (u, o) => (u.sortOrder ?? 0) - (o.sortOrder ?? 0)
34
+ ).forEach((u, o) => {
35
+ const t = u.categoryCode || oe;
36
+ if (i.has(t)) return;
37
+ const g = t === oe ? ae : ve[o % ve.length];
38
+ i.set(t, {
39
+ fill: g.fill,
40
+ stroke: g.stroke,
41
+ shadowColor: Re(g.fill, 0.25)
42
+ });
43
+ }), i.has(oe) || i.set(oe, {
44
+ fill: ae.fill,
45
+ stroke: ae.stroke,
46
+ shadowColor: Re(ae.fill, 0.25)
47
+ }), i;
48
+ }
49
+ const ct = "#f59e0b", at = 2, it = "#64748b", lt = 10, dt = "#2563eb", ut = "#ffffff", ft = "#f59e0b";
50
+ function gt(a, i, c) {
51
+ const u = a.data.name || a.data.code || a.data.id || "", o = i != null ? `${u}
52
+ ${i}` : u, t = (c == null ? void 0 : c.fill) ?? Ce, g = (c == null ? void 0 : c.stroke) ?? Pe, p = (c == null ? void 0 : c.shadowColor) ?? rt;
53
+ return {
54
+ // ─── 对应 ECharts itemStyle ───
55
+ fill: t,
56
+ stroke: g,
57
+ lineWidth: 1,
58
+ // opacity 基线必须显式声明:G6 v5 移除 state 时只回退「基线有值」的属性,
59
+ // 缺省时 focusDim 写入的 opacity 0.2 会永久残留(emphasis 节点变半透明)
60
+ opacity: 1,
61
+ shadowColor: p,
62
+ shadowBlur: 8,
63
+ // ─── 对应 ECharts symbolSize ───
64
+ size: Ee,
65
+ // 节点渲染在边之上,避免边线覆盖节点
66
+ zIndex: 10,
67
+ // ─── 对应 ECharts label ───
68
+ labelText: o,
69
+ labelFill: ot,
70
+ labelFontSize: st,
71
+ labelFontWeight: "bold",
72
+ // G6 v5 用 labelPlacement 控制标签位置(非 labelPosition),默认 'bottom'
73
+ labelPlacement: "center"
74
+ };
75
+ }
76
+ function ht(a, i) {
77
+ const c = a.data.displayName || "", u = i != null ? `${c}
78
+ ${i}` : c, o = a.data.isSelfAssoc;
79
+ return {
80
+ stroke: ct,
81
+ lineWidth: at,
82
+ opacity: 0.7,
83
+ // 边渲染在节点之下
84
+ zIndex: 1,
85
+ ...o ? { endArrow: !0, endArrowSize: 8 } : {},
86
+ // 边 label(对应 ECharts edgeLabel)
87
+ labelText: u,
88
+ labelFill: it,
89
+ labelFontSize: lt,
90
+ labelFontWeight: "bold",
91
+ // 标签沿连线方向自动旋转(参考图中边上文字斜排)
92
+ labelAutoRotate: !0,
93
+ // 白底衬底:边标签直接压在连线上会与中线文字互相穿透,衬底隔开
94
+ labelBackground: !0,
95
+ labelBackgroundFill: "#fff",
96
+ labelBackgroundOpacity: 0.85,
97
+ labelBackgroundRadius: 4,
98
+ labelPadding: [2, 4]
99
+ };
100
+ }
101
+ const K = {
102
+ // hover/选中 emphasis
103
+ emphasis: {
104
+ node: {
105
+ fill: dt,
106
+ stroke: ut,
107
+ lineWidth: 3,
108
+ // 显式覆盖 focusDim 残留的 0.2(G6 v5 state opacity 不随移除回退)
109
+ opacity: 1,
110
+ shadowColor: "rgba(37, 99, 235, 0.55)",
111
+ shadowBlur: 16
112
+ },
113
+ edge: {
114
+ stroke: ft,
115
+ lineWidth: 4,
116
+ shadowColor: "rgba(245, 158, 11, 0.45)",
117
+ shadowBlur: 8
118
+ }
119
+ },
120
+ // 路径高亮节点(对应 PATH_HL_COLOR 等)
121
+ pathHighlight: {
122
+ node: {
123
+ fill: ye,
124
+ stroke: Qe,
125
+ lineWidth: 2,
126
+ shadowColor: "rgba(61, 125, 212, 0.75)",
127
+ shadowBlur: 20,
128
+ labelFill: "#fff",
129
+ labelFontWeight: "bold"
130
+ },
131
+ edge: {
132
+ stroke: ye,
133
+ lineWidth: 4,
134
+ opacity: 1,
135
+ shadowColor: "rgba(61, 125, 212, 0.55)",
136
+ shadowBlur: 12,
137
+ labelFill: "#1e3a5f",
138
+ labelFontWeight: "bold"
139
+ }
140
+ },
141
+ // 路径末尾节点(对应 PATH_HL_DEEP)
142
+ pathLast: {
143
+ node: {
144
+ fill: ye,
145
+ stroke: Ne,
146
+ lineWidth: 4,
147
+ shadowColor: "rgba(61, 125, 212, 0.75)",
148
+ shadowBlur: 20,
149
+ labelFill: "#fff",
150
+ labelFontWeight: "bold"
151
+ },
152
+ edge: {
153
+ stroke: Ne,
154
+ lineWidth: 4,
155
+ opacity: 1,
156
+ shadowColor: "rgba(61, 125, 212, 0.55)",
157
+ shadowBlur: 12,
158
+ labelFill: "#1e3a5f",
159
+ labelFontWeight: "bold"
160
+ }
161
+ },
162
+ // 路径弱化节点(对应 PATH_DIM_NODE_COLOR 等)
163
+ pathDim: {
164
+ node: {
165
+ fill: et,
166
+ stroke: tt,
167
+ lineWidth: 1,
168
+ opacity: 1,
169
+ labelFill: xe
170
+ },
171
+ edge: {
172
+ stroke: nt,
173
+ lineWidth: 1,
174
+ opacity: 0.9,
175
+ labelFill: xe
176
+ }
177
+ },
178
+ // 聚焦弱化节点(概览右栏聚焦态:非相关节点整体压暗突出焦点邻域,不动配色)
179
+ focusDim: {
180
+ node: {
181
+ opacity: 0.2
182
+ },
183
+ // 与 pathDim.edge 对齐:邻接弱化节点的边同步降透明,避免亮边穿插弱化区
184
+ edge: {
185
+ opacity: 0.2
186
+ }
187
+ }
188
+ };
189
+ function Ae(a, i) {
190
+ const c = /* @__PURE__ */ new Map();
191
+ a.forEach((t) => c.set(t.id, /* @__PURE__ */ new Set())), i.forEach((t) => {
192
+ var y, N;
193
+ const g = String(t.source), p = String(t.target);
194
+ (y = c.get(g)) == null || y.add(p), (N = c.get(p)) == null || N.add(g);
195
+ });
196
+ const u = /* @__PURE__ */ new Map();
197
+ let o = 0;
198
+ return a.forEach((t) => {
199
+ var p;
200
+ if (u.has(t.id)) return;
201
+ const g = [t.id];
202
+ for (u.set(t.id, o); g.length > 0; ) {
203
+ const y = g.shift();
204
+ (p = c.get(y)) == null || p.forEach((N) => {
205
+ u.has(N) || (u.set(N, o), g.push(N));
206
+ });
207
+ }
208
+ o += 1;
209
+ }), u;
210
+ }
211
+ function He(a, i, c, u = !1) {
212
+ const o = /* @__PURE__ */ new Map();
213
+ a.forEach((b) => {
214
+ o.set(b, (o.get(b) ?? 0) + 1);
215
+ });
216
+ const t = Array.from(o.keys()).sort(
217
+ (b, k) => (o.get(k) ?? 0) - (o.get(b) ?? 0)
218
+ ), g = i / 2, p = c / 2, y = /* @__PURE__ */ new Map();
219
+ if (t.length === 1)
220
+ return y.set(t[0], { x: g, y: p }), y;
221
+ const N = t.filter((b) => (o.get(b) ?? 0) === 1), x = t.filter((b) => (o.get(b) ?? 0) > 1);
222
+ if (u && (x.length === 0 || N.length > 12)) {
223
+ const b = Math.min(i, c) * 0.08, k = Math.ceil(Math.sqrt(N.length)), H = Math.ceil(N.length / k), F = (i - b * 2) / k, W = (c - b * 2) / H;
224
+ return N.forEach((T, G) => {
225
+ y.set(T, {
226
+ x: b + (G % k + 0.5) * F,
227
+ y: b + (Math.floor(G / k) + 0.5) * W
228
+ });
229
+ }), x.length === 0 || x.forEach((T, G) => {
230
+ if (G === 0) {
231
+ y.set(T, { x: g, y: p });
232
+ return;
233
+ }
234
+ const h = (G - 1) / Math.max(x.length - 1, 1) * Math.PI * 2 - Math.PI / 2, _ = Math.min(i, c) * 0.3;
235
+ y.set(T, {
236
+ x: g + Math.cos(h) * _,
237
+ y: p + Math.sin(h) * _
238
+ });
239
+ }), y;
240
+ }
241
+ if (N.length === t.length) {
242
+ const b = Math.min(i, c) * 0.32;
243
+ return t.forEach((k, H) => {
244
+ const F = H / t.length * Math.PI * 2 - Math.PI / 2;
245
+ y.set(k, {
246
+ x: g + Math.cos(F) * b,
247
+ y: p + Math.sin(F) * b
248
+ });
249
+ }), y;
250
+ }
251
+ return t.forEach((b, k) => {
252
+ if (!y.has(b))
253
+ if (!y.size)
254
+ y.set(b, { x: g, y: p });
255
+ else {
256
+ const H = k / (t.length - 1) * Math.PI * 2, F = Math.min(i, c) * 0.28;
257
+ y.set(b, {
258
+ x: g + Math.cos(H) * F,
259
+ y: p + Math.sin(H) * F
260
+ });
261
+ }
262
+ }), y;
263
+ }
264
+ const mt = 0.55, pt = 1.2;
265
+ function Fe(a, i, c, u, o, t, g) {
266
+ if (!g || a === 0)
267
+ return { collideRadius: u * 1.3, linkDistance: o, chargeStrength: t };
268
+ const p = u * 1.15, y = Math.max(i - p * 2, 1), N = Math.max(c - p * 2, 1), x = y * N, A = Math.min(
269
+ u * 1.3,
270
+ Math.max(
271
+ u * 0.4,
272
+ Math.sqrt(x * mt / (Math.PI * a))
273
+ )
274
+ ), b = Math.min(
275
+ 1,
276
+ Math.sqrt(x * pt ** 2 / a) / o
277
+ );
278
+ return {
279
+ collideRadius: A,
280
+ linkDistance: Math.max(u * 0.75, o * b),
281
+ chargeStrength: t * b * b
282
+ };
283
+ }
284
+ function yt(a, i, c, u) {
285
+ const {
286
+ nodeSize: o = Ee,
287
+ linkDistance: t = 140,
288
+ chargeStrength: g = -450,
289
+ spreadIsolated: p = !1,
290
+ adaptiveDensity: y = !1,
291
+ seed: N
292
+ } = u ?? {}, { nodes: x, edges: A } = a;
293
+ if (x.length === 0) return;
294
+ const b = Math.min(i, c) * 0.14, k = x.map((D, v) => {
295
+ const C = N == null ? void 0 : N[D.id];
296
+ return {
297
+ id: D.id,
298
+ x: (C == null ? void 0 : C.x) ?? i / 2 + Math.cos(v / Math.max(x.length, 1) * Math.PI * 2) * b,
299
+ y: (C == null ? void 0 : C.y) ?? c / 2 + Math.sin(v / Math.max(x.length, 1) * Math.PI * 2) * b,
300
+ vx: 0,
301
+ vy: 0,
302
+ ...C ? { fx: C.x, fy: C.y } : {}
303
+ };
304
+ }), H = A.filter((D) => D.source !== D.target).map((D) => ({
305
+ source: D.source,
306
+ target: D.target
307
+ })), F = Ae(k, H), W = He(
308
+ F,
309
+ i,
310
+ c,
311
+ p
312
+ ), T = Fe(
313
+ x.length,
314
+ i,
315
+ c,
316
+ o,
317
+ t,
318
+ g,
319
+ y
320
+ ), G = (D) => {
321
+ const v = F.get(D);
322
+ return v === void 0 ? void 0 : W.get(v);
323
+ }, h = Me(k).force(
324
+ "link",
325
+ Ie(H).id((D) => D.id).distance(T.linkDistance)
326
+ ).force("charge", Oe().strength(T.chargeStrength)).force(
327
+ "x",
328
+ Le((D) => {
329
+ var v;
330
+ return ((v = G(D.id)) == null ? void 0 : v.x) ?? i / 2;
331
+ }).strength(0.1)
332
+ ).force(
333
+ "y",
334
+ we((D) => {
335
+ var v;
336
+ return ((v = G(D.id)) == null ? void 0 : v.y) ?? c / 2;
337
+ }).strength(0.1)
338
+ ).force("collide", Te(T.collideRadius)).velocityDecay(0.3).alphaDecay(0.0228).stop();
339
+ for (let D = 0; D < 500; D++)
340
+ h.tick();
341
+ const _ = o * 1.15, q = (D, v, C) => Math.max(v, Math.min(C, D));
342
+ x.forEach((D, v) => {
343
+ D.style || (D.style = {}), D.style.x = y ? k[v].x ?? i / 2 : q(k[v].x ?? i / 2, _, Math.max(_, i - _)), D.style.y = y ? k[v].y ?? c / 2 : q(k[v].y ?? c / 2, _, Math.max(_, c - _));
344
+ });
345
+ }
346
+ function ke(a, i, c, u, o) {
347
+ const {
348
+ nodeSize: t = Ee,
349
+ linkDistance: g = 140,
350
+ chargeStrength: p = -450,
351
+ spreadIsolated: y = !1,
352
+ adaptiveDensity: N = !1
353
+ } = o ?? {}, { nodes: x, edges: A } = a;
354
+ if (x.length === 0) return null;
355
+ const b = x.map((h) => {
356
+ var _, q;
357
+ return {
358
+ id: h.id,
359
+ x: ((_ = h.style) == null ? void 0 : _.x) ?? i / 2,
360
+ y: ((q = h.style) == null ? void 0 : q.y) ?? c / 2,
361
+ vx: 0,
362
+ vy: 0
363
+ };
364
+ }), k = A.filter((h) => h.source !== h.target).map((h) => ({ source: h.source, target: h.target })), H = Ae(b, k), F = He(
365
+ H,
366
+ i,
367
+ c,
368
+ y
369
+ ), W = (h) => {
370
+ const _ = H.get(h);
371
+ return _ === void 0 ? void 0 : F.get(_);
372
+ }, T = Fe(
373
+ x.length,
374
+ i,
375
+ c,
376
+ t,
377
+ g,
378
+ p,
379
+ N
380
+ );
381
+ return { simulation: Me(b).force(
382
+ "link",
383
+ Ie(k).id((h) => h.id).distance(T.linkDistance)
384
+ ).force("charge", Oe().strength(T.chargeStrength)).force(
385
+ "x",
386
+ Le((h) => {
387
+ var _;
388
+ return ((_ = W(h.id)) == null ? void 0 : _.x) ?? i / 2;
389
+ }).strength(0.1)
390
+ ).force(
391
+ "y",
392
+ we((h) => {
393
+ var _;
394
+ return ((_ = W(h.id)) == null ? void 0 : _.y) ?? c / 2;
395
+ }).strength(0.1)
396
+ ).force("collide", Te(T.collideRadius)).velocityDecay(0.3).alphaDecay(0.0228).on("tick", u).alpha(0).stop(), d3Nodes: b };
397
+ }
398
+ const Et = [
399
+ // 滚轮缩放(对应 scaleLimit: { min: 0.5, max: 3 })
400
+ {
401
+ type: "zoom-canvas",
402
+ key: "zoom-canvas",
403
+ zoomRange: [0.2, 3]
404
+ },
405
+ // 画布平移(对应 roam: true 的平移部分)
406
+ {
407
+ type: "drag-canvas",
408
+ key: "drag-canvas"
409
+ },
410
+ // 节点拖拽(对应 draggable: true)
411
+ {
412
+ type: "drag-element",
413
+ key: "drag-element"
414
+ },
415
+ // hover 高亮(对应 ECharts emphasis)
416
+ {
417
+ type: "hover-activate",
418
+ key: "hover-activate",
419
+ degree: 0,
420
+ state: "emphasis"
421
+ }
422
+ ];
423
+ function _t(a, i, c, u) {
424
+ return {
425
+ nodes: a.data.map((o) => {
426
+ const t = o.id, g = o.code ?? t, p = o.name ?? "", y = o.categoryCode || oe;
427
+ return {
428
+ id: t,
429
+ data: {
430
+ ...o,
431
+ code: g,
432
+ name: p,
433
+ ...o.categoryCode ? { categoryCode: o.categoryCode } : {}
434
+ },
435
+ style: gt(
436
+ { data: { code: g, name: p } },
437
+ i == null ? void 0 : i[g.toLowerCase()],
438
+ u == null ? void 0 : u.get(y)
439
+ )
440
+ };
441
+ }),
442
+ edges: a.links.map((o, t) => {
443
+ const g = o.code || o.value, p = `edge-${o.source}-${o.target}-${g}-${t}`, y = g ?? "", N = o.displayName || o.value || "", x = o.sourceName || "", A = o.targetName || "";
444
+ return {
445
+ id: p,
446
+ source: o.source,
447
+ // source===target 时 G6 原生渲染自关联 loop edge
448
+ target: o.target,
449
+ data: {
450
+ ...o,
451
+ code: y,
452
+ displayName: N,
453
+ sourceName: x,
454
+ targetName: A
455
+ },
456
+ style: ht(
457
+ {
458
+ source: o.source,
459
+ target: o.target,
460
+ data: {
461
+ code: y,
462
+ displayName: N,
463
+ sourceName: x,
464
+ targetName: A,
465
+ isSelfAssoc: o.isSelfAssoc
466
+ }
467
+ },
468
+ c == null ? void 0 : c[y.toLowerCase()]
469
+ )
470
+ };
471
+ })
472
+ };
473
+ }
474
+ const de = [
475
+ "#5470c6",
476
+ "#91cc75",
477
+ "#fac858",
478
+ "#ee6666",
479
+ "#73c0de",
480
+ "#3ba272",
481
+ "#fc8452",
482
+ "#9a60b4",
483
+ "#ea7ccc",
484
+ "#48b8d0"
485
+ ];
486
+ function bt(a, i) {
487
+ const c = a.replace("#", ""), u = c.length === 3 ? c.split("").map((y) => y + y).join("") : c, o = parseInt(u, 16), t = Math.round((o >> 16 & 255) * (1 - i)), g = Math.round((o >> 8 & 255) * (1 - i)), p = Math.round((o & 255) * (1 - i));
488
+ return `#${((1 << 24) + (t << 16) + (g << 8) + p).toString(16).slice(1)}`;
489
+ }
490
+ function St(a, i) {
491
+ const c = a.data.category ?? 0, u = i[c % i.length] ?? de[0], o = a.data.value ?? 0, t = Math.min(30 + o * 3, 60);
492
+ return {
493
+ fill: u,
494
+ stroke: bt(u, 0.22),
495
+ lineWidth: 2,
496
+ shadowBlur: 6,
497
+ shadowColor: "rgba(0,0,0,0.1)",
498
+ size: t,
499
+ zIndex: 10,
500
+ labelText: a.data.name || a.id,
501
+ labelFill: "#333",
502
+ labelFontSize: 10,
503
+ labelPlacement: "right"
504
+ };
505
+ }
506
+ function Dt(a, i) {
507
+ var c;
508
+ return {
509
+ stroke: i ?? "#b0c4de",
510
+ lineWidth: 1.5,
511
+ opacity: 0.6,
512
+ zIndex: 1,
513
+ endArrow: !0,
514
+ endArrowSize: 8,
515
+ labelText: ((c = a.data) == null ? void 0 : c.label) || "",
516
+ labelFill: "#888",
517
+ labelFontSize: 9,
518
+ labelAutoRotate: !0
519
+ };
520
+ }
521
+ function Mt(a, i, c) {
522
+ const u = c.length > 0 ? c.map((t) => {
523
+ var g;
524
+ return ((g = t.itemStyle) == null ? void 0 : g.color) || de[0];
525
+ }) : de, o = /* @__PURE__ */ new Map();
526
+ return {
527
+ nodes: a.map((t) => {
528
+ const g = t.category ?? 0, p = u[g % u.length] ?? de[0];
529
+ return o.set(t.id, p), {
530
+ id: t.id,
531
+ data: {
532
+ // 实例节点无 schema code,以所属对象类型编码充当
533
+ code: t.objectCode ?? "",
534
+ name: t.name ?? "",
535
+ // 截断前的原始全称:tooltip 优先展示,节点标签只显示截断名
536
+ ...t.fullName ? { fullName: t.fullName } : {},
537
+ objectCode: t.objectCode,
538
+ objectName: t.objectName,
539
+ category: t.category,
540
+ props: t.props,
541
+ value: t.value,
542
+ fillColor: p
543
+ },
544
+ style: St(
545
+ {
546
+ id: t.id,
547
+ data: { name: t.name, category: t.category, value: t.value }
548
+ },
549
+ u
550
+ )
551
+ };
552
+ }),
553
+ edges: i.map((t, g) => {
554
+ const p = t.label ?? t.code ?? "";
555
+ return {
556
+ id: `edge-${t.source}-${t.target}-${p}-${g}`,
557
+ source: t.source,
558
+ target: t.target,
559
+ data: {
560
+ code: p,
561
+ displayName: p,
562
+ sourceName: "",
563
+ targetName: "",
564
+ label: p
565
+ },
566
+ style: Dt(
567
+ { data: { label: t.label } },
568
+ o.get(t.source)
569
+ )
570
+ };
571
+ })
572
+ };
573
+ }
574
+ const _e = 120, Nt = [10, 10, 10, 10];
575
+ async function ie(a) {
576
+ if (await a.fitView(), !a.destroyed && a.getZoom() > 1) {
577
+ if (await a.zoomTo(1), a.destroyed) return;
578
+ await a.fitCenter();
579
+ }
580
+ }
581
+ function le(a) {
582
+ return a.context;
583
+ }
584
+ function P(a) {
585
+ return typeof a == "object" && a !== null ? a : {};
586
+ }
587
+ function I(a) {
588
+ return typeof a == "string" ? a : "";
589
+ }
590
+ function V(a, i = 0) {
591
+ return typeof a == "number" && Number.isFinite(a) ? a : i;
592
+ }
593
+ function We(a) {
594
+ const i = P(a).preventDefault;
595
+ typeof i == "function" && i.call(a);
596
+ }
597
+ function te(a, i, c) {
598
+ const u = P(a), o = I(P(u.target).id) || I(u.itemId);
599
+ let t = {};
600
+ if (c && o)
601
+ try {
602
+ const A = i === "node" ? c.getNodeData([o]) : c.getEdgeData([o]);
603
+ t = P(A == null ? void 0 : A[0]);
604
+ } catch {
605
+ }
606
+ const g = P(t.data), p = {
607
+ ...g,
608
+ id: I(t.id) || o,
609
+ code: I(g.code) || I(g.value) || I(t.id) || o,
610
+ name: I(g.name) || I(g.displayName) || ""
611
+ };
612
+ I(t.source) && (p.source = I(t.source)), I(t.target) && (p.target = I(t.target));
613
+ const y = P(u.viewport ?? u.canvasPoint ?? u.point), N = P(u.client ?? u.clientPoint), x = P(u.nativeEvent);
614
+ return {
615
+ dataType: i,
616
+ data: p,
617
+ name: I(g.name) || I(g.displayName) || o,
618
+ event: {
619
+ event: {
620
+ offsetX: V(y.x),
621
+ offsetY: V(y.y),
622
+ clientX: V(N.x, V(x.clientX)),
623
+ clientY: V(N.y, V(x.clientY)),
624
+ metaKey: !!(u.metaKey ?? P(u.originalEvent).metaKey ?? x.metaKey),
625
+ ctrlKey: !!(u.ctrlKey ?? P(u.originalEvent).ctrlKey ?? x.ctrlKey),
626
+ preventDefault: () => We(a)
627
+ }
628
+ }
629
+ };
630
+ }
631
+ function It(a) {
632
+ const {
633
+ // layout 已移除:G6 v5 animation:false 时 preLayout 同步失效,
634
+ // 改用 computeForceLayout 预计算位置写入 style.x/y
635
+ zoomRange: i = [0.2, 3],
636
+ onNodeClick: c,
637
+ onNodeDoubleClick: u,
638
+ onEdgeClick: o,
639
+ onNodeContextMenu: t,
640
+ onNodeHover: g,
641
+ onEdgeHover: p,
642
+ onHoverLeave: y,
643
+ onCanvasClick: N,
644
+ onViewportChange: x,
645
+ tooltipFormatter: A,
646
+ forceLayoutOptions: b,
647
+ ctxMenuOpenRef: k,
648
+ nodeInstanceCountRef: H,
649
+ edgeInstanceCountRef: F
650
+ } = a, W = L(null), [T, G] = De(!1), h = L(null), [_, q] = De(!1), D = L(!1), v = L(null), C = L(null), Y = L(null), ue = L(null), be = L(() => {
651
+ }), se = L(null), $ = L(!1), z = L(null), ne = L([]), fe = L(null), ge = L(!1), ce = L(void 0);
652
+ ee(() => {
653
+ ce.current = b;
654
+ });
655
+ const he = L(0), Z = L({
656
+ onNodeClick: c,
657
+ onNodeDoubleClick: u,
658
+ onEdgeClick: o,
659
+ onNodeContextMenu: t,
660
+ onNodeHover: g,
661
+ onEdgeHover: p,
662
+ onHoverLeave: y,
663
+ onCanvasClick: N,
664
+ onViewportChange: x,
665
+ tooltipFormatter: A,
666
+ ctxMenuOpenRef: k,
667
+ nodeInstanceCountRef: H,
668
+ edgeInstanceCountRef: F
669
+ });
670
+ ee(() => {
671
+ Z.current = {
672
+ onNodeClick: c,
673
+ onNodeDoubleClick: u,
674
+ onEdgeClick: o,
675
+ onNodeContextMenu: t,
676
+ onNodeHover: g,
677
+ onEdgeHover: p,
678
+ onHoverLeave: y,
679
+ onCanvasClick: N,
680
+ onViewportChange: x,
681
+ tooltipFormatter: A,
682
+ ctxMenuOpenRef: k,
683
+ nodeInstanceCountRef: H,
684
+ edgeInstanceCountRef: F
685
+ };
686
+ });
687
+ const ze = w((d) => {
688
+ d ? (W.current = d, G(!0)) : (W.current = null, G(!1));
689
+ }, []);
690
+ ee(() => {
691
+ if (!T) return;
692
+ const d = W.current;
693
+ if (!d || h.current) return;
694
+ let n = !0;
695
+ const f = {
696
+ container: d,
697
+ autoResize: !0,
698
+ animation: !1,
699
+ data: { nodes: [], edges: [] },
700
+ padding: Nt,
701
+ // 不设置 G6 内置 layout — 使用 computeForceLayout 预计算位置
702
+ // 避免 G6 preLayout 在 animation:false 时与预计算位置冲突
703
+ zoomRange: i,
704
+ behaviors: Et,
705
+ // 处理平行边和自环边:分散模式自动将多条自环分配到 8 个位置
706
+ transforms: [{ type: "process-parallel-edges" }],
707
+ node: {
708
+ type: "circle",
709
+ state: {
710
+ emphasis: K.emphasis.node,
711
+ pathHighlight: K.pathHighlight.node,
712
+ pathLast: K.pathLast.node,
713
+ pathDim: K.pathDim.node,
714
+ focusDim: K.focusDim.node
715
+ }
716
+ },
717
+ edge: {
718
+ type: "quadratic",
719
+ state: {
720
+ emphasis: K.emphasis.edge,
721
+ pathHighlight: K.pathHighlight.edge,
722
+ pathLast: K.pathLast.edge,
723
+ pathDim: K.pathDim.edge,
724
+ focusDim: K.focusDim.edge
725
+ }
726
+ },
727
+ plugins: [
728
+ {
729
+ type: "tooltip",
730
+ key: "tooltip",
731
+ getContent: (s, r) => {
732
+ var R;
733
+ const e = Z.current;
734
+ if (!e.tooltipFormatter || (R = e.ctxMenuOpenRef) != null && R.current || !(r != null && r[0])) return "";
735
+ const l = P(r[0]), m = "source" in l ? "edge" : "node", E = {
736
+ ...P(l.data),
737
+ id: I(l.id)
738
+ };
739
+ return I(l.source) && (E.source = I(l.source)), I(l.target) && (E.target = I(l.target)), e.tooltipFormatter(E, m);
740
+ }
741
+ }
742
+ ]
743
+ };
744
+ try {
745
+ const s = new Je(f);
746
+ h.current = s, s.on("node:click", (r) => {
747
+ var l, m;
748
+ const e = te(r, "node", s);
749
+ (m = (l = Z.current).onNodeClick) == null || m.call(l, e);
750
+ }), s.on("node:dblclick", (r) => {
751
+ var l, m;
752
+ const e = te(r, "node", s);
753
+ (m = (l = Z.current).onNodeDoubleClick) == null || m.call(l, e);
754
+ }), s.on("edge:click", (r) => {
755
+ var l, m;
756
+ const e = te(r, "edge", s);
757
+ (m = (l = Z.current).onEdgeClick) == null || m.call(l, e);
758
+ }), s.on("node:contextmenu", (r) => {
759
+ var l, m;
760
+ We(r);
761
+ const e = te(r, "node", s);
762
+ (m = (l = Z.current).onNodeContextMenu) == null || m.call(l, e);
763
+ }), s.on("canvas:click", () => {
764
+ var r, e;
765
+ (e = (r = Z.current).onCanvasClick) == null || e.call(r);
766
+ }), s.on("aftertransform", () => {
767
+ var r, e;
768
+ he.current > 0 || (e = (r = Z.current).onViewportChange) == null || e.call(r);
769
+ }), s.on("node:pointerenter", (r) => {
770
+ var l, m;
771
+ const e = te(r, "node", s);
772
+ (m = (l = Z.current).onNodeHover) == null || m.call(l, e);
773
+ }), s.on("edge:pointerenter", (r) => {
774
+ var l, m;
775
+ const e = te(r, "edge", s);
776
+ (m = (l = Z.current).onEdgeHover) == null || m.call(l, e);
777
+ }), s.on("node:pointerleave", () => {
778
+ var r, e;
779
+ (e = (r = Z.current).onHoverLeave) == null || e.call(r);
780
+ }), s.on("edge:pointerleave", () => {
781
+ var r, e;
782
+ (e = (r = Z.current).onHoverLeave) == null || e.call(r);
783
+ }), s.on("node:dragstart", (r) => {
784
+ var O;
785
+ const e = P(r), l = I(P(e.target).id) || I(e.itemId);
786
+ if (!l) return;
787
+ const m = ne.current.find((j) => j.id === l);
788
+ if (!m || !z.current) return;
789
+ fe.current = l;
790
+ const E = (O = s.getNodeData([l])) == null ? void 0 : O[0], R = V(P(E == null ? void 0 : E.style).x, m.x ?? 0), S = V(P(E == null ? void 0 : E.style).y, m.y ?? 0);
791
+ m.fx = R, m.fy = S, z.current.alphaTarget(0.3).alpha(0.3).restart();
792
+ }), s.on("node:drag", (r) => {
793
+ const e = fe.current;
794
+ if (!e || !z.current) return;
795
+ const l = ne.current.find((O) => O.id === e);
796
+ if (!l) return;
797
+ const m = P(r), E = s.getZoom(), R = V(m.dx) / E, S = V(m.dy) / E;
798
+ l.fx = (l.fx ?? l.x ?? 0) + R, l.fy = (l.fy ?? l.y ?? 0) + S;
799
+ }), s.on("node:dragend", () => {
800
+ z.current && z.current.alphaTarget(0), fe.current = null;
801
+ }), queueMicrotask(() => {
802
+ n && q(!0);
803
+ });
804
+ } catch (s) {
805
+ console.error("[G6] Failed to create graph:", s);
806
+ }
807
+ return () => {
808
+ n = !1, z.current && (z.current.stop(), z.current = null), Y.current && (clearTimeout(Y.current), Y.current = null), C.current = null, h.current && (h.current.destroy(), h.current = null, $.current = !1, v.current = null, q(!1));
809
+ };
810
+ }, [T]), ee(() => {
811
+ if (!T) return;
812
+ const d = W.current;
813
+ if (!d) return;
814
+ const n = new ResizeObserver(() => {
815
+ var f;
816
+ if (C.current) {
817
+ be.current();
818
+ return;
819
+ }
820
+ (f = se.current) == null || f.call(se);
821
+ });
822
+ return n.observe(d), ue.current = n, () => {
823
+ n.disconnect(), ue.current === n && (ue.current = null);
824
+ };
825
+ }, [T]);
826
+ const M = w(() => {
827
+ var f;
828
+ const d = h.current;
829
+ if (!d || d.destroyed) return;
830
+ const n = (f = le(d)) == null ? void 0 : f.canvas;
831
+ n != null && n.getLayers && Object.values(n.getLayers()).forEach((s) => {
832
+ var e;
833
+ const r = (e = s.getRenderingService) == null ? void 0 : e.call(s);
834
+ r != null && r.dirty && r.dirty(), !s.frameId && typeof s.run == "function" && s.run(), typeof s.render == "function" && s.render();
835
+ });
836
+ }, []), X = w(
837
+ async (d) => {
838
+ he.current += 1;
839
+ try {
840
+ await d();
841
+ } finally {
842
+ he.current -= 1;
843
+ }
844
+ },
845
+ []
846
+ ), me = w(() => {
847
+ const d = h.current;
848
+ if (!d || d.destroyed) return;
849
+ const n = ne.current;
850
+ if (!n.length) return;
851
+ const f = n.filter((s) => s.fx == null && s.fy == null).map((s) => ({ id: s.id, style: { x: s.x, y: s.y } }));
852
+ f.length !== 0 && (d.updateData({ nodes: f, edges: [] }), M());
853
+ }, [M]), Se = L({ emphasisNodes: /* @__PURE__ */ new Set(), emphasisEdgeCodes: /* @__PURE__ */ new Set(), dimmedNodes: /* @__PURE__ */ new Set() }), Q = w(
854
+ (d) => {
855
+ const n = d ?? h.current;
856
+ if (!n || n.destroyed || !$.current) return;
857
+ const { emphasisNodes: f, emphasisEdgeCodes: s, dimmedNodes: r } = Se.current, e = {};
858
+ n.getNodeData().forEach((l) => {
859
+ const m = String(l.id), R = (n.getElementState(m) ?? []).filter((S) => S !== "emphasis" && S !== "focusDim");
860
+ f.has(m) && R.push("emphasis"), r.has(m) && R.push("focusDim"), e[m] = R;
861
+ }), n.getEdgeData().forEach((l) => {
862
+ var O, j;
863
+ const m = String(l.id), R = (n.getElementState(m) ?? []).filter((B) => B !== "emphasis" && B !== "focusDim"), S = String(((O = l.data) == null ? void 0 : O.code) ?? ((j = l.data) == null ? void 0 : j.value) ?? "");
864
+ S && s.has(S) && R.push("emphasis"), (r.has(String(l.source)) || r.has(String(l.target))) && R.push("focusDim"), e[m] = R;
865
+ }), Promise.resolve(n.setElementState(e)).then(() => M()).catch(() => {
866
+ });
867
+ },
868
+ [M]
869
+ ), Be = w(
870
+ (d, n, f) => {
871
+ Se.current = {
872
+ emphasisNodes: new Set(d),
873
+ emphasisEdgeCodes: new Set(n),
874
+ dimmedNodes: new Set(f)
875
+ }, Q();
876
+ },
877
+ [Q]
878
+ ), U = w(
879
+ (d, n, f, s, r) => {
880
+ var m, E, R;
881
+ const e = h.current;
882
+ if (!e || e.destroyed)
883
+ return;
884
+ let l;
885
+ if (r && v.current) {
886
+ const S = {};
887
+ v.current.nodes.forEach((O) => {
888
+ var J, re;
889
+ const j = (J = O.style) == null ? void 0 : J.x, B = (re = O.style) == null ? void 0 : re.y;
890
+ typeof j == "number" && typeof B == "number" && (S[O.id] = { x: j, y: B });
891
+ }), Object.keys(S).length && (l = S);
892
+ }
893
+ yt(d, n, f, {
894
+ ...ce.current,
895
+ ...l ? { seed: l } : {}
896
+ }), e.setData(d), (R = (E = (m = le(e)) == null ? void 0 : m.model) == null ? void 0 : E.refreshData) == null || R.call(E), $.current = !1, !v.current || v.current.nodes.length === 0 ? e.render().then(async () => {
897
+ if (h.current !== e || e.destroyed || (await X(() => ie(e)), h.current !== e || e.destroyed)) return;
898
+ M(), z.current && z.current.stop();
899
+ const S = ke(
900
+ d,
901
+ n,
902
+ f,
903
+ me,
904
+ ce.current
905
+ );
906
+ S && (z.current = S.simulation, ne.current = S.d3Nodes), $.current = !0, Q(e);
907
+ }).catch((S) => {
908
+ console.error("[G6] render failed:", S), h.current === e && ($.current = !0);
909
+ }) : e.draw().then(async () => {
910
+ var j, B, J;
911
+ if (h.current !== e || e.destroyed) return;
912
+ const S = ge.current;
913
+ ge.current = !1, S || M(), z.current && z.current.stop();
914
+ const O = ke(
915
+ d,
916
+ n,
917
+ f,
918
+ me,
919
+ ce.current
920
+ );
921
+ O && (z.current = O.simulation, ne.current = O.d3Nodes), !(s && !S && (await X(() => ie(e)), h.current !== e || e.destroyed)) && ($.current = !0, S ? ((J = (B = (j = le(e)) == null ? void 0 : j.model) == null ? void 0 : B.refreshData) == null || J.call(B), e.draw().then(async () => {
922
+ h.current !== e || e.destroyed || (await X(() => ie(e)), !(h.current !== e || e.destroyed) && (M(), Q(e)));
923
+ }).catch(() => {
924
+ h.current === e && M();
925
+ })) : Q(e));
926
+ }).catch((S) => {
927
+ console.error("[G6] draw failed:", S), h.current === e && ($.current = !0);
928
+ }), v.current = d;
929
+ },
930
+ [M, me, X, Q]
931
+ ), pe = w(() => {
932
+ Y.current && clearTimeout(Y.current), Y.current = setTimeout(() => {
933
+ const d = C.current, n = W.current;
934
+ if (!d || !n) return;
935
+ const f = n.clientWidth, s = n.clientHeight;
936
+ f <= 0 || s <= 0 || (C.current = null, U(d, f, s));
937
+ }, _e);
938
+ }, [U]);
939
+ ee(() => {
940
+ be.current = pe;
941
+ });
942
+ const Ge = w(() => {
943
+ Y.current && clearTimeout(Y.current), Y.current = setTimeout(() => {
944
+ const d = W.current, n = v.current;
945
+ if (!d || !n || n.nodes.length === 0) return;
946
+ const f = d.clientWidth, s = d.clientHeight;
947
+ f <= 0 || s <= 0 || U(n, f, s, !0);
948
+ }, _e);
949
+ }, [U]);
950
+ ee(() => {
951
+ se.current = Ge;
952
+ });
953
+ const $e = w(
954
+ (d, n) => {
955
+ const f = h.current;
956
+ if (!f || f.destroyed)
957
+ return;
958
+ if (n != null && n.settle && (ge.current = !0), !v.current || v.current.nodes.length === 0) {
959
+ C.current = d, pe();
960
+ return;
961
+ }
962
+ const r = W.current, e = (r == null ? void 0 : r.clientWidth) || 800, l = (r == null ? void 0 : r.clientHeight) || 600;
963
+ U(d, e, l, n == null ? void 0 : n.fit, n == null ? void 0 : n.keepPositions);
964
+ },
965
+ [U, pe]
966
+ ), Ze = w(
967
+ (d) => {
968
+ const n = h.current;
969
+ if (!n || n.destroyed) return;
970
+ const f = n.getNodeData(), s = n.getEdgeData(), r = {};
971
+ f.forEach((l) => {
972
+ r[String(l.id)] = [];
973
+ }), s.forEach((l) => {
974
+ r[String(l.id)] = [];
975
+ });
976
+ const e = f.some((l) => String(l.id) === d);
977
+ !$.current || f.length === 0 || Promise.resolve(n.setElementState(r)).then(() => {
978
+ if (e)
979
+ return n.setElementState(d, "emphasis");
980
+ }).then(() => M()).catch(() => {
981
+ });
982
+ },
983
+ [M]
984
+ ), je = w(
985
+ (d, n, f, s) => {
986
+ const r = h.current;
987
+ if (!r || r.destroyed) return;
988
+ const e = {}, l = r.getNodeData();
989
+ l.forEach((E) => {
990
+ var O;
991
+ const R = String(E.id), S = String(((O = E.data) == null ? void 0 : O.code) || E.id);
992
+ d.has(S) || d.has(R) ? S === f || R === f ? e[R] = ["pathLast"] : e[R] = ["pathHighlight"] : e[R] = ["pathDim"];
993
+ }), r.getEdgeData().forEach((E) => {
994
+ var J, re;
995
+ const R = String(E.id), S = String(((J = E.data) == null ? void 0 : J.value) ?? ((re = E.data) == null ? void 0 : re.code) ?? ""), O = String(E.source), j = String(E.target), B = `${O}->${j}::${S}`;
996
+ n.has(B) || n.has(S) ? B === s || S === s ? e[R] = ["pathLast"] : e[R] = ["pathHighlight"] : e[R] = ["pathDim"];
997
+ }), !(!$.current || l.length === 0) && (D.current = !0, Promise.resolve(r.setElementState(e)).then(() => M()).catch(() => {
998
+ }));
999
+ },
1000
+ [M]
1001
+ ), Ke = w(() => {
1002
+ const d = h.current;
1003
+ if (!d || d.destroyed || !D.current || !$.current) return;
1004
+ const n = d.getNodeData(), f = d.getEdgeData(), s = {};
1005
+ n.forEach((r) => {
1006
+ s[String(r.id)] = [];
1007
+ }), f.forEach((r) => {
1008
+ s[String(r.id)] = [];
1009
+ }), D.current = !1, Promise.resolve(d.setElementState(s)).then(() => M()).catch(() => {
1010
+ });
1011
+ }, [M]), Ve = w(() => {
1012
+ const d = h.current;
1013
+ if (!d || d.destroyed) return;
1014
+ const n = v.current;
1015
+ if (!n || n.nodes.length === 0) {
1016
+ X(() => ie(d)).then(
1017
+ () => M()
1018
+ );
1019
+ return;
1020
+ }
1021
+ const f = W.current, s = (f == null ? void 0 : f.clientWidth) || 800, r = (f == null ? void 0 : f.clientHeight) || 600;
1022
+ U(n, s, r, !0);
1023
+ }, [U, M, X]), Ye = w(
1024
+ (d) => {
1025
+ const n = h.current;
1026
+ if (!n || n.destroyed || !$.current) return;
1027
+ const f = Math.max(n.getZoom(), 1);
1028
+ X(async () => {
1029
+ await n.focusElement(d, { duration: 300 }), !(n.destroyed || n.getZoom() === f) && await n.zoomTo(f, { duration: 250 });
1030
+ }).then(() => M()).catch(() => {
1031
+ });
1032
+ },
1033
+ [M, X]
1034
+ ), qe = w(
1035
+ (d) => {
1036
+ const n = h.current;
1037
+ if (!n || n.destroyed || !$.current) return;
1038
+ const f = new Set(d), s = {};
1039
+ n.getNodeData().forEach((r) => {
1040
+ const e = String(r.id), m = (n.getElementState(e) ?? []).filter((E) => E !== "focusDim");
1041
+ f.has(e) && m.push("focusDim"), s[e] = m;
1042
+ }), n.getEdgeData().forEach((r) => {
1043
+ const e = String(r.id), m = (n.getElementState(e) ?? []).filter((E) => E !== "focusDim");
1044
+ (f.has(String(r.source)) || f.has(String(r.target))) && m.push("focusDim"), s[e] = m;
1045
+ }), Promise.resolve(n.setElementState(s)).then(() => M()).catch(() => {
1046
+ });
1047
+ },
1048
+ [M]
1049
+ ), Xe = w(() => {
1050
+ var n, f, s;
1051
+ const d = h.current;
1052
+ if (!(!d || d.destroyed))
1053
+ try {
1054
+ const r = (s = (f = (n = le(d)) == null ? void 0 : n.plugin) == null ? void 0 : f.get) == null ? void 0 : s.call(f, "tooltip");
1055
+ r && typeof r.hide == "function" && r.hide();
1056
+ } catch {
1057
+ }
1058
+ }, []), Ue = w(() => h.current, []);
1059
+ return {
1060
+ containerRef: ze,
1061
+ setData: $e,
1062
+ highlightNode: Ze,
1063
+ applyPathStyle: je,
1064
+ resetPathStyle: Ke,
1065
+ fitView: Ve,
1066
+ focusNode: Ye,
1067
+ setNodeDim: qe,
1068
+ setViewState: Be,
1069
+ hideTooltip: Xe,
1070
+ ready: _,
1071
+ getGraph: Ue
1072
+ };
1073
+ }
1074
+ export {
1075
+ Et as G,
1076
+ Ce as N,
1077
+ K as S,
1078
+ oe as U,
1079
+ Pe as a,
1080
+ kt as b,
1081
+ yt as c,
1082
+ ke as d,
1083
+ Mt as e,
1084
+ _t as t,
1085
+ It as u
1086
+ };