knowledge-mindmap.blade.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. <!doctype html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>知识图谱脑图(公开查看)</title>
  7. <link rel="preconnect" href="https://fonts.googleapis.com">
  8. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9. <link href="https://fonts.googleapis.com/css2?family=Inter:wght@500;600;700&display=swap" rel="stylesheet">
  10. <script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
  11. <script src="https://gw.alipayobjects.com/os/lib/antv/g6/5.0.18/dist/g6.min.js"></script>
  12. <style>
  13. :root {
  14. color-scheme: light;
  15. }
  16. body {
  17. margin: 0;
  18. font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  19. background: radial-gradient(circle at 10% 20%, #e0f2fe 0, transparent 20%), radial-gradient(circle at 90% 10%, #fce7f3 0, transparent 18%), #f8fafc;
  20. color: #0f172a;
  21. }
  22. .page {
  23. max-width: 1400px;
  24. margin: 0 auto;
  25. padding: 32px 20px 40px;
  26. }
  27. .card {
  28. background: white;
  29. border: 1px solid #e2e8f0;
  30. border-radius: 14px;
  31. box-shadow: 0 10px 30px rgba(15, 23, 42, 0.06);
  32. padding: 18px 20px;
  33. }
  34. .stat {
  35. display: inline-flex;
  36. align-items: center;
  37. gap: 8px;
  38. padding: 8px 12px;
  39. border-radius: 10px;
  40. background: #f8fafc;
  41. border: 1px dashed #e2e8f0;
  42. font-size: 12px;
  43. color: #475569;
  44. }
  45. #knowledge-mindmap {
  46. width: 100%;
  47. min-height: 760px;
  48. height: 78vh;
  49. border: 1px solid #e2e8f0;
  50. border-radius: 14px;
  51. background: white;
  52. }
  53. </style>
  54. </head>
  55. <body>
  56. <div class="page" x-data="knowledgeMindmap()" x-init="initMindmap()">
  57. <div class="card" style="margin-bottom: 16px;">
  58. <div style="display: flex; flex-wrap: wrap; justify-content: space-between; gap: 16px;">
  59. <div style="max-width: 780px;">
  60. <div style="font-size: 20px; font-weight: 700; color: #0f172a;">初中数学知识图谱 · 思维导图</div>
  61. <p style="margin: 8px 0 6px; font-size: 13px; color: #64748b; line-height: 1.5;">
  62. tree.json 提供完整层级(模块 → 知识点),edges.json 描述跨节点关系;基于 AntV G6 MindMap 布局,节点可逐层展开/折叠,线条带方向。
  63. </p>
  64. <div style="display: flex; gap: 10px; flex-wrap: wrap;">
  65. <span class="stat">节点总数:<span x-text="stats.nodes"></span></span>
  66. <span class="stat">跨边数量:<span x-text="stats.extraEdges"></span></span>
  67. </div>
  68. </div>
  69. <div style="display: flex; gap: 12px; flex-wrap: wrap; align-items: center; font-size: 12px; color: #475569;">
  70. <span style="display:inline-flex;align-items:center;gap:6px;">
  71. <span style="display:block;width:16px;height:6px;border-radius:999px;background:#2563eb;"></span> 前置
  72. </span>
  73. <span style="display:inline-flex;align-items:center;gap:6px;">
  74. <span style="display:block;width:16px;height:6px;border-radius:999px;background:#dc2626;"></span> 后继
  75. </span>
  76. <span style="display:inline-flex;align-items:center;gap:6px;">
  77. <span style="display:block;width:16px;height:6px;border-radius:999px;border:2px dashed #64748b;"></span> 兄弟
  78. </span>
  79. <span style="display:inline-flex;align-items:center;gap:6px;">
  80. <span style="display:block;width:16px;height:6px;border-radius:999px;background:#fcd34d;"></span> 联合
  81. </span>
  82. </div>
  83. </div>
  84. </div>
  85. <div id="knowledge-mindmap" aria-label="知识图谱脑图"></div>
  86. </div>
  87. <script>
  88. window.knowledgeMindmap = () => ({
  89. graph: null,
  90. treeData: null,
  91. relationEdges: [],
  92. stats: { nodes: 0, extraEdges: 0 },
  93. arrow(w = 12, h = 14, r = 5) {
  94. if (window.G6?.Arrow?.triangle) {
  95. return G6.Arrow.triangle(w, h, r);
  96. }
  97. return [
  98. ['M', 0, 0],
  99. ['L', w, h / 2],
  100. ['L', 0, h],
  101. ['Z'],
  102. ];
  103. },
  104. levelStyles: [
  105. {
  106. fill: '#0ea5e9',
  107. stroke: '#0369a1',
  108. labelColor: '#0f172a',
  109. fontSize: 17,
  110. fontWeight: 700,
  111. size: 34,
  112. },
  113. {
  114. fill: '#e0f2fe',
  115. stroke: '#38bdf8',
  116. labelColor: '#0f172a',
  117. fontSize: 16,
  118. fontWeight: 700,
  119. size: 30,
  120. },
  121. {
  122. fill: '#f1f5f9',
  123. stroke: '#cbd5e1',
  124. labelColor: '#0f172a',
  125. fontSize: 14,
  126. fontWeight: 600,
  127. size: 26,
  128. },
  129. ],
  130. relationStyles: {
  131. prerequisite: {
  132. type: 'quadratic',
  133. curveOffset: 60,
  134. style: {
  135. stroke: '#2563eb',
  136. lineWidth: 3.4,
  137. lineDash: [8, 6],
  138. endArrow: {
  139. path: null,
  140. fill: '#2563eb',
  141. d: 12,
  142. },
  143. startArrow: false,
  144. shadowBlur: 0,
  145. shadowColor: undefined,
  146. },
  147. label: '前置',
  148. },
  149. successor: {
  150. type: 'quadratic',
  151. curveOffset: 60,
  152. style: {
  153. stroke: '#dc2626',
  154. lineWidth: 3.4,
  155. lineDash: [8, 6],
  156. endArrow: {
  157. path: null,
  158. fill: '#dc2626',
  159. d: 12,
  160. },
  161. startArrow: false,
  162. shadowBlur: 0,
  163. shadowColor: undefined,
  164. },
  165. label: '后继',
  166. },
  167. sibling: {
  168. type: 'quadratic',
  169. curveOffset: 50,
  170. style: {
  171. stroke: '#64748b',
  172. lineDash: [6, 6],
  173. lineWidth: 3,
  174. endArrow: {
  175. path: null,
  176. fill: '#64748b',
  177. d: 10,
  178. },
  179. shadowBlur: 0,
  180. shadowColor: undefined,
  181. },
  182. label: '兄弟',
  183. },
  184. joint: {
  185. type: 'quadratic',
  186. curveOffset: 50,
  187. style: {
  188. stroke: '#fcd34d',
  189. lineWidth: 3,
  190. lineDash: [10, 8],
  191. endArrow: {
  192. path: null,
  193. fill: '#fbbf24',
  194. d: 10,
  195. },
  196. shadowBlur: 0,
  197. shadowColor: undefined,
  198. },
  199. label: '联合',
  200. },
  201. default: {
  202. type: 'quadratic',
  203. curveOffset: 50,
  204. style: {
  205. stroke: '#94a3b8',
  206. lineWidth: 3,
  207. lineDash: [10, 8],
  208. endArrow: {
  209. path: null,
  210. fill: '#94a3b8',
  211. d: 10,
  212. },
  213. shadowBlur: 0,
  214. shadowColor: undefined,
  215. },
  216. label: '',
  217. },
  218. },
  219. async initMindmap() {
  220. try {
  221. if (this.$nextTick) {
  222. await this.$nextTick();
  223. }
  224. if (!window.G6) {
  225. console.error('G6 未加载');
  226. return;
  227. }
  228. // 补充箭头路径
  229. Object.keys(this.relationStyles).forEach((key) => {
  230. const rel = this.relationStyles[key];
  231. if (rel?.style && rel.style.endArrow && !rel.style.endArrow.path) {
  232. rel.style.endArrow.path = this.arrow(rel.style.endArrow.d || 10, (rel.style.endArrow.d || 10) + 2, 4);
  233. }
  234. });
  235. await this.loadData();
  236. this.applyInitialCollapse(this.treeData);
  237. this.renderGraph();
  238. window.addEventListener('resize', () => this.resizeGraph());
  239. } catch (err) {
  240. console.error('初始化思维导图失败', err);
  241. const container = document.getElementById('knowledge-mindmap');
  242. if (container) {
  243. container.innerHTML = '<div style="padding:20px;color:#dc2626;">图数据加载失败,请检查 /data/tree.json 与 /data/edges.json 是否可访问。</div>';
  244. }
  245. }
  246. },
  247. async loadData() {
  248. const treeUrl = '/data/tree.json';
  249. const edgeUrl = '/data/edges.json';
  250. const [treeResp, edgesResp] = await Promise.all([fetch(treeUrl), fetch(edgeUrl)]);
  251. if (!treeResp.ok || !edgesResp.ok) {
  252. throw new Error(`数据加载失败 tree:${treeResp.status} edge:${edgesResp.status}`);
  253. }
  254. const rawTree = await treeResp.json();
  255. const edges = await edgesResp.json();
  256. const rawEdges = Array.isArray(edges) ? edges : edges?.edges || [];
  257. this.treeData = this.transformNode(rawTree);
  258. this.relationEdges = this.normalizeEdges(rawEdges);
  259. if (!this.treeData) {
  260. throw new Error('tree.json 为空或格式不正确');
  261. }
  262. this.stats = {
  263. nodes: this.countNodes(this.treeData),
  264. extraEdges: this.relationEdges.length,
  265. };
  266. },
  267. transformNode(node, depth = 0) {
  268. if (!node) return null;
  269. const id = node.code || node.id || node.label || `node-${Math.random().toString(36).slice(2, 8)}`;
  270. const label = node.name || node.label || node.code || node.id || '未命名节点';
  271. const meta = {
  272. code: node.code || node.id || '',
  273. name: label,
  274. direct_score: node.direct_score || [],
  275. related_score: node.related_score || [],
  276. skills: node.skills || [],
  277. };
  278. return {
  279. id,
  280. label,
  281. meta,
  282. depth,
  283. children: (node.children || []).map((child) => this.transformNode(child, depth + 1)).filter(Boolean),
  284. };
  285. },
  286. applyInitialCollapse(node, depth = 0) {
  287. if (!node) return;
  288. if (depth >= 2 && node.children.length > 0) {
  289. node.collapsed = true;
  290. }
  291. node.children.forEach((child) => this.applyInitialCollapse(child, depth + 1));
  292. },
  293. countNodes(node) {
  294. if (!node) return 0;
  295. return 1 + node.children.reduce((sum, child) => sum + this.countNodes(child), 0);
  296. },
  297. normalizeEdges(rawEdges) {
  298. const seen = new Set();
  299. const normalized = [];
  300. (rawEdges || []).forEach((edge, index) => {
  301. if (!edge?.source || !edge?.target) return;
  302. const key = `${edge.source}-${edge.target}-${edge.type}`;
  303. if (seen.has(key)) return;
  304. seen.add(key);
  305. const relationStyle = this.relationStyles[edge.type] || this.relationStyles.default;
  306. normalized.push({
  307. id: `rel-${index}-${edge.source}-${edge.target}`,
  308. source: edge.source,
  309. target: edge.target,
  310. type: relationStyle.type || 'quadratic',
  311. curveOffset: relationStyle.curveOffset || 50,
  312. style: relationStyle.style,
  313. label: relationStyle.label,
  314. comment: edge.comment || edge.note || '',
  315. });
  316. });
  317. return normalized;
  318. },
  319. renderGraph(containerEl = null) {
  320. if (!this.treeData) return;
  321. const container = containerEl || document.getElementById('knowledge-mindmap');
  322. if (!container) {
  323. console.error('容器未找到');
  324. return;
  325. }
  326. const ensuredId = container.id || 'knowledge-mindmap';
  327. if (!container.id) {
  328. container.id = ensuredId;
  329. }
  330. const bounds = container.getBoundingClientRect();
  331. const width = Math.max(bounds.width || container.clientWidth || 0, 600);
  332. const height = Math.max(bounds.height || container.clientHeight || 0, 600);
  333. const tooltipEl = document.createElement('div');
  334. tooltipEl.style.position = 'fixed';
  335. tooltipEl.style.pointerEvents = 'none';
  336. tooltipEl.style.zIndex = '9999';
  337. tooltipEl.style.display = 'none';
  338. document.body.appendChild(tooltipEl);
  339. const showTooltip = (html, x, y) => {
  340. tooltipEl.innerHTML = html;
  341. tooltipEl.style.left = `${x + 12}px`;
  342. tooltipEl.style.top = `${y + 12}px`;
  343. tooltipEl.style.display = 'block';
  344. };
  345. const hideTooltip = () => {
  346. tooltipEl.style.display = 'none';
  347. tooltipEl.innerHTML = '';
  348. };
  349. const G6Lib = window.G6?.default || window.G6;
  350. const TreeGraphClass = G6Lib?.TreeGraph || null;
  351. if (!TreeGraphClass) {
  352. console.error('G6 TreeGraph 不可用');
  353. return;
  354. }
  355. const graphData = this.decorateTree(this.treeData);
  356. const graphConfig = {
  357. container: ensuredId,
  358. width,
  359. height,
  360. data: graphData,
  361. linkCenter: true,
  362. modes: {
  363. default: [
  364. 'drag-canvas',
  365. 'zoom-canvas',
  366. {
  367. type: 'collapse-expand',
  368. trigger: 'click',
  369. onChange: function onChange(item, collapsed) {
  370. if (!item) return;
  371. item.getModel().collapsed = collapsed;
  372. return true;
  373. },
  374. },
  375. ],
  376. },
  377. layout: {
  378. type: 'mindmap',
  379. direction: 'H',
  380. getHeight: () => 32,
  381. getWidth: () => 140,
  382. getVGap: () => 32,
  383. getHGap: () => 110,
  384. },
  385. defaultNode: {
  386. size: 22,
  387. style: {
  388. stroke: '#94a3b8',
  389. fill: '#fff',
  390. radius: 4,
  391. shadowColor: undefined,
  392. shadowBlur: 0,
  393. lineWidth: 3,
  394. },
  395. labelCfg: {
  396. style: {
  397. fontSize: 13,
  398. fill: '#0f172a',
  399. fontWeight: 500,
  400. },
  401. position: 'right',
  402. offset: 12,
  403. },
  404. },
  405. defaultEdge: {
  406. type: 'cubic-horizontal',
  407. style: {
  408. stroke: '#cbd5f5',
  409. lineWidth: 3,
  410. shadowBlur: 0,
  411. shadowColor: undefined,
  412. },
  413. },
  414. nodeStateStyles: {
  415. selected: {
  416. lineWidth: 3.2,
  417. stroke: '#2563eb',
  418. fill: '#e0f2fe',
  419. },
  420. },
  421. edgeStateStyles: {
  422. highlight: {
  423. lineWidth: 3.4,
  424. stroke: '#fb923c',
  425. },
  426. },
  427. plugins: [],
  428. };
  429. try {
  430. this.graph = new TreeGraphClass(graphConfig);
  431. if (typeof this.graph.data === 'function') {
  432. this.graph.data(graphData);
  433. } else if (typeof this.graph.changeData === 'function') {
  434. this.graph.changeData(graphData);
  435. }
  436. if (typeof this.graph.render === 'function') {
  437. this.graph.render();
  438. }
  439. } catch (e) {
  440. console.error('创建图失败', e);
  441. return;
  442. }
  443. this.drawRelationEdges();
  444. this.graph.fitView(24);
  445. this.bindEvents();
  446. // 手动 tooltip
  447. const handleNodeEnter = (evt) => {
  448. const { clientX, clientY } = evt;
  449. showTooltip(this.buildTooltip(evt?.item?.getModel()), clientX, clientY);
  450. };
  451. const handleEdgeEnter = (evt) => {
  452. const model = evt?.item?.getModel() || {};
  453. const relation = model.label || '关联关系';
  454. const text = `${model.source || ''} → ${model.target || ''}`;
  455. const comment = model.comment ? `<div style="font-size:11px;color:#475569;margin-top:4px;white-space:pre-line;">备注:${model.comment}</div>` : '';
  456. const html = `
  457. <div style="border:1px solid #e2e8f0;border-radius:8px;background:white;padding:8px 10px;font-size:12px;color:#475569;box-shadow:0 10px 20px rgba(15,23,42,0.08);">
  458. <div style="font-weight:700;color:#0f172a;margin-bottom:4px;">${relation}</div>
  459. <div>${text}</div>
  460. ${comment}
  461. </div>
  462. `;
  463. const { clientX, clientY } = evt;
  464. showTooltip(html, clientX, clientY);
  465. };
  466. const handleLeave = () => hideTooltip();
  467. this.graph.on('node:mouseenter', handleNodeEnter);
  468. this.graph.on('node:mouseleave', handleLeave);
  469. this.graph.on('edge:mouseenter', handleEdgeEnter);
  470. this.graph.on('edge:mouseleave', handleLeave);
  471. // 全量刷新,避免缩放重影
  472. const canvas = this.graph && typeof this.graph.get === 'function' ? this.graph.get('canvas') : null;
  473. if (canvas && typeof canvas.set === 'function') {
  474. canvas.set('localRefresh', false);
  475. const ctx = typeof canvas.get === 'function' ? canvas.get('context') : null;
  476. if (ctx) {
  477. ctx.shadowColor = 'transparent';
  478. ctx.shadowBlur = 0;
  479. }
  480. }
  481. },
  482. decorateTree(node) {
  483. if (!node) return null;
  484. const { nodeStyle, labelCfg, size } = this.getNodeLevelStyle(node.depth);
  485. return {
  486. id: node.id,
  487. label: `${node.meta.code ? `${node.meta.code} · ` : ''}${node.label}`,
  488. meta: node.meta,
  489. collapsed: node.collapsed,
  490. depth: node.depth,
  491. size,
  492. style: nodeStyle,
  493. labelCfg,
  494. children: node.children.map((child) => this.decorateTree(child)).filter(Boolean),
  495. };
  496. },
  497. getNodeLevelStyle(depth = 0) {
  498. const style = this.levelStyles[depth] || this.levelStyles[this.levelStyles.length - 1];
  499. return {
  500. size: style.size || 22,
  501. nodeStyle: {
  502. fill: style.fill || '#fff',
  503. stroke: style.stroke || '#cbd5f5',
  504. lineWidth: 3,
  505. radius: 6,
  506. shadowColor: undefined,
  507. shadowBlur: 0,
  508. },
  509. labelCfg: {
  510. position: 'right',
  511. offset: 12,
  512. style: {
  513. fontSize: style.fontSize || 13,
  514. fontWeight: style.fontWeight || 600,
  515. fill: style.labelColor || '#0f172a',
  516. },
  517. },
  518. };
  519. },
  520. drawRelationEdges() {
  521. if (!this.graph || !this.relationEdges.length) return;
  522. const canAddEdge = typeof this.graph.addEdge === 'function';
  523. const canAddItem = typeof this.graph.addItem === 'function';
  524. const buildModel = (edge, index) => {
  525. const style = { ...(edge.style || {}), lineAppendWidth: 14 };
  526. if (style.endArrow && !style.endArrow.path) {
  527. style.endArrow = {
  528. ...style.endArrow,
  529. path: this.arrow(style.endArrow.d || 10, (style.endArrow.d || 10) + 2, 4),
  530. };
  531. }
  532. return {
  533. id: `extra-${index}`,
  534. source: edge.source,
  535. target: edge.target,
  536. type: edge.type || 'quadratic',
  537. curveOffset: edge.curveOffset || 50,
  538. style,
  539. label: edge.label,
  540. comment: edge.comment || '',
  541. labelCfg: {
  542. autoRotate: true,
  543. style: {
  544. fill: '#475569',
  545. fontSize: 11,
  546. background: {
  547. fill: 'rgba(255,255,255,0.85)',
  548. padding: [2, 4],
  549. radius: 4,
  550. },
  551. },
  552. },
  553. };
  554. };
  555. if (this.isTreeGraph && (canAddEdge || canAddItem)) {
  556. this.relationEdges.forEach((edge, index) => {
  557. const model = buildModel(edge, index);
  558. if (canAddEdge) {
  559. this.graph.addEdge(model);
  560. } else {
  561. this.graph.addItem('edge', model);
  562. }
  563. });
  564. if (typeof this.graph.paint === 'function') {
  565. this.graph.paint();
  566. }
  567. } else {
  568. const currentData = this.graph.save?.() || this.graphDataset || { nodes: [], edges: [] };
  569. const nodes = currentData.nodes || [];
  570. const mergedEdges = (currentData.edges || []).concat(
  571. this.relationEdges.map((edge, index) => buildModel(edge, index))
  572. );
  573. const merged = { nodes, edges: mergedEdges };
  574. if (typeof this.graph.changeData === 'function') {
  575. this.graph.changeData(merged);
  576. } else if (typeof this.graph.data === 'function' && typeof this.graph.render === 'function') {
  577. this.graph.data(merged);
  578. this.graph.render();
  579. }
  580. }
  581. },
  582. buildTooltip(model) {
  583. const meta = model?.meta;
  584. if (!meta) return '<div class="text-xs text-gray-600">无数据</div>';
  585. const range = (value) => (value?.length ? `${value[0]}-${value[1]}` : '未配置');
  586. const skills = (meta.skills || [])
  587. .slice(0, 6)
  588. .map((skill) => `<li style="line-height:1.1;">${skill.trim()}</li>`)
  589. .join('') || '<li>暂无技能</li>';
  590. return `
  591. <div style="min-width:230px;max-width:340px;border:1px solid #e2e8f0;border-radius:8px;background:white;padding:10px;font-size:12px;color:#475569;box-shadow:0 10px 30px rgba(15,23,42,0.12);">
  592. <div style="font-size:14px;font-weight:700;color:#0f172a;margin-bottom:4px;">${meta.code || model.id} · ${meta.name}</div>
  593. <div style="display:flex;gap:12px;font-size:12px;">
  594. <span>直接:${range(meta.direct_score)}</span>
  595. <span>关联:${range(meta.related_score)}</span>
  596. </div>
  597. <div style="margin-top:8px;">
  598. <div style="font-weight:600;">技能要点</div>
  599. <ul style="padding-left:18px;margin:6px 0; list-style: disc; display: grid; gap: 4px;">
  600. ${skills}
  601. </ul>
  602. </div>
  603. </div>
  604. `;
  605. },
  606. bindEvents() {
  607. if (!this.graph) return;
  608. this.graph.on('node:click', (evt) => {
  609. const nodeId = evt?.item?.getID();
  610. if (nodeId) this.highlightEdges(nodeId);
  611. });
  612. this.graph.on('canvas:click', () => this.resetHighlight());
  613. },
  614. highlightEdges(nodeId) {
  615. this.graph.getNodes().forEach((node) => {
  616. this.graph.clearItemStates(node);
  617. if (node.getID() === nodeId) {
  618. this.graph.setItemState(node, 'selected', true);
  619. }
  620. });
  621. this.graph.getEdges().forEach((edge) => {
  622. const { source, target } = edge.getModel();
  623. const linked = source === nodeId || target === nodeId;
  624. if (linked) {
  625. this.graph.setItemState(edge, 'highlight', true);
  626. } else {
  627. this.graph.clearItemStates(edge);
  628. }
  629. });
  630. },
  631. resetHighlight() {
  632. if (!this.graph) return;
  633. this.graph.getNodes().forEach((node) => this.graph.clearItemStates(node));
  634. this.graph.getEdges().forEach((edge) => this.graph.clearItemStates(edge));
  635. },
  636. resizeGraph() {
  637. if (!this.graph) return;
  638. const container = document.getElementById('knowledge-mindmap');
  639. if (!container) return;
  640. this.graph.changeSize(container.clientWidth, container.clientHeight);
  641. this.graph.fitView(24);
  642. },
  643. });
  644. </script>
  645. </body>
  646. </html>