From 5129c3fe8a9a8fd7aaa51b598ac890c141f50df9 Mon Sep 17 00:00:00 2001 From: Sycun <165354365+Sycun@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:57:00 +0800 Subject: [PATCH] feat(frontend): organize project and assessment graphs around relationships --- web/frontend/src/components/graph-card.css | 119 ++++++++++ web/frontend/src/components/graph-card.jsx | 98 ++++++++ web/frontend/src/components/graph-parents.js | 14 ++ .../pages/deep-assessments/GraphCanvas.jsx | 184 ++++++--------- .../src/pages/deep-assessments/GraphTab.jsx | 62 +++--- .../src/pages/deep-assessments/graphLayout.js | 26 +-- .../src/pages/deep-assessments/page.css | 51 +---- .../src/pages/projects/fact-graph-canvas.jsx | 209 ++++-------------- .../src/pages/projects/fact-graph-layout.js | 62 ++++++ .../src/pages/projects/fact-graph.jsx | 3 +- web/frontend/src/pages/projects/page.css | 108 +-------- .../tests/project-graph-layout.test.mjs | 24 ++ web/frontend/tests/project-graph.spec.mjs | 2 +- web/frontend/tests/ui.spec.mjs | 11 +- ...qvda1.js => agents-management-Dodu_xyX.js} | 2 +- ...-docs-hqa3xayd.js => api-docs-SbQzksgU.js} | 2 +- ...-Db9RsbPD.js => asset-library-CdvOx2Zr.js} | 2 +- ...EsHbwQLL.js => asset-overview-CuW0POqe.js} | 2 +- ...et-CuH6sGKD.js => assetTarget-_EpbJis4.js} | 2 +- ...ents-CqM-9yO7.js => c2-events-CRpIC4Ou.js} | 2 +- ...s-GN81iTjo.js => c2-listeners-C7ATMHS_.js} | 2 +- ...ds-De_yxzvY.js => c2-payloads-0BxIBVju.js} | 2 +- ...es-BJynXE7u.js => c2-profiles-B0WRGxRo.js} | 2 +- ...ns-COSabnxA.js => c2-sessions-DTd2ihc2.js} | 2 +- ...ared-CBMfSMfk.js => c2-shared-BvpuXQ_S.js} | 2 +- ...ared-6rJUcauc.js => c2-shared-CaJDvpNC.js} | 2 +- ...tasks-CAw02yoE.js => c2-tasks-BbsJoe2e.js} | 2 +- web/static/app/assets/chat-BNyo07Xj.js | 5 + web/static/app/assets/chat-Bf0wpiOd.js | 5 - ...les-Dz50cTtS.js => chat-files-YP43Rlsi.js} | 2 +- web/static/app/assets/dashboard-D96-FxtW.js | 1 + web/static/app/assets/dashboard-bKEySRk7.js | 1 - .../app/assets/deep-assessments-CcVsvCi5.js | 2 + ...UQuK.css => deep-assessments-CkGNo6pd.css} | 2 +- .../app/assets/deep-assessments-HMIrCW68.js | 2 - .../app/assets/graph-parents-B2-7BD5z.js | 1 + .../app/assets/graph-parents-BbkuhUnk.css | 1 + .../{hitl-BOrfxR5h.js => hitl-BVnNUq81.js} | 2 +- .../{index-DVHtb0hB.js => index-Du_eCXFg.js} | 10 +- ...t-Ceh1XukD.js => info-collect-D-iiqv8L.js} | 2 +- .../assets/knowledge-management-B17AQ5MM.js | 1 - .../assets/knowledge-management-DMZ34vth.js | 1 + ...s => knowledge-retrieval-logs-BxuK8lZH.js} | 2 +- ...CGLR4OKt.js => mcp-management-DoQXW1UG.js} | 2 +- ...or-phRYWEF_.js => mcp-monitor-CzJUo1aH.js} | 2 +- ...-BZ9UPl_S.js => platform-rbac-Zg9SJE38.js} | 2 +- ...m-lQ1s0WSq.js => project-form-D1d6Azt-.js} | 2 +- web/static/app/assets/projects-1Rco1d-G.css | 1 + web/static/app/assets/projects-4d2MFYXi.js | 4 + web/static/app/assets/projects-Cr8E4beb.js | 4 - web/static/app/assets/projects-DajqVKFP.css | 1 - ...7imY_T.js => roles-management-ByaUI_WU.js} | 2 +- .../{semi-CDAktpYA.js => semi-BxhBcqdj.js} | 10 +- ...tings-BxakuJ3r.js => settings-BRjzzF7s.js} | 8 +- ...j31GL.js => skills-management-DYioP3HG.js} | 2 +- ...kuN7vOld.js => skills-monitor-DSiw9NQW.js} | 2 +- .../{tasks-BsxO6Llq.js => tasks-BLQal8Lr.js} | 2 +- ...ard-DsP0bXWe.js => tool-guard-DgtRwxWv.js} | 2 +- ...6s17x67.js => vulnerabilities-CBG2GjU8.js} | 2 +- ...shell-TP1IaWS8.js => webshell-bSBkrXOh.js} | 2 +- ...lows-CPuYj7pj.js => workflows-DDDy4zXA.js} | 2 +- web/static/app/index.html | 4 +- 62 files changed, 553 insertions(+), 542 deletions(-) create mode 100644 web/frontend/src/components/graph-card.css create mode 100644 web/frontend/src/components/graph-card.jsx create mode 100644 web/frontend/src/components/graph-parents.js create mode 100644 web/frontend/src/pages/projects/fact-graph-layout.js create mode 100644 web/frontend/tests/project-graph-layout.test.mjs rename web/static/app/assets/{agents-management-ChTqvda1.js => agents-management-Dodu_xyX.js} (65%) rename web/static/app/assets/{api-docs-hqa3xayd.js => api-docs-SbQzksgU.js} (97%) rename web/static/app/assets/{asset-library-Db9RsbPD.js => asset-library-CdvOx2Zr.js} (99%) rename web/static/app/assets/{asset-overview-EsHbwQLL.js => asset-overview-CuW0POqe.js} (82%) rename web/static/app/assets/{assetTarget-CuH6sGKD.js => assetTarget-_EpbJis4.js} (91%) rename web/static/app/assets/{c2-events-CqM-9yO7.js => c2-events-CRpIC4Ou.js} (95%) rename web/static/app/assets/{c2-listeners-GN81iTjo.js => c2-listeners-C7ATMHS_.js} (97%) rename web/static/app/assets/{c2-payloads-De_yxzvY.js => c2-payloads-0BxIBVju.js} (96%) rename web/static/app/assets/{c2-profiles-BJynXE7u.js => c2-profiles-B0WRGxRo.js} (97%) rename web/static/app/assets/{c2-sessions-COSabnxA.js => c2-sessions-DTd2ihc2.js} (98%) rename web/static/app/assets/{c2-shared-CBMfSMfk.js => c2-shared-BvpuXQ_S.js} (94%) rename web/static/app/assets/{c2-shared-6rJUcauc.js => c2-shared-CaJDvpNC.js} (95%) rename web/static/app/assets/{c2-tasks-CAw02yoE.js => c2-tasks-BbsJoe2e.js} (96%) create mode 100644 web/static/app/assets/chat-BNyo07Xj.js delete mode 100644 web/static/app/assets/chat-Bf0wpiOd.js rename web/static/app/assets/{chat-files-Dz50cTtS.js => chat-files-YP43Rlsi.js} (98%) create mode 100644 web/static/app/assets/dashboard-D96-FxtW.js delete mode 100644 web/static/app/assets/dashboard-bKEySRk7.js create mode 100644 web/static/app/assets/deep-assessments-CcVsvCi5.js rename web/static/app/assets/{deep-assessments-CwQsUQuK.css => deep-assessments-CkGNo6pd.css} (74%) delete mode 100644 web/static/app/assets/deep-assessments-HMIrCW68.js create mode 100644 web/static/app/assets/graph-parents-B2-7BD5z.js create mode 100644 web/static/app/assets/graph-parents-BbkuhUnk.css rename web/static/app/assets/{hitl-BOrfxR5h.js => hitl-BVnNUq81.js} (99%) rename web/static/app/assets/{index-DVHtb0hB.js => index-Du_eCXFg.js} (51%) rename web/static/app/assets/{info-collect-Ceh1XukD.js => info-collect-D-iiqv8L.js} (98%) delete mode 100644 web/static/app/assets/knowledge-management-B17AQ5MM.js create mode 100644 web/static/app/assets/knowledge-management-DMZ34vth.js rename web/static/app/assets/{knowledge-retrieval-logs-DZ2XnROY.js => knowledge-retrieval-logs-BxuK8lZH.js} (68%) rename web/static/app/assets/{mcp-management-CGLR4OKt.js => mcp-management-DoQXW1UG.js} (98%) rename web/static/app/assets/{mcp-monitor-phRYWEF_.js => mcp-monitor-CzJUo1aH.js} (99%) rename web/static/app/assets/{platform-rbac-BZ9UPl_S.js => platform-rbac-Zg9SJE38.js} (58%) rename web/static/app/assets/{project-form-lQ1s0WSq.js => project-form-D1d6Azt-.js} (88%) create mode 100644 web/static/app/assets/projects-1Rco1d-G.css create mode 100644 web/static/app/assets/projects-4d2MFYXi.js delete mode 100644 web/static/app/assets/projects-Cr8E4beb.js delete mode 100644 web/static/app/assets/projects-DajqVKFP.css rename web/static/app/assets/{roles-management-BK7imY_T.js => roles-management-ByaUI_WU.js} (59%) rename web/static/app/assets/{semi-CDAktpYA.js => semi-BxhBcqdj.js} (76%) rename web/static/app/assets/{settings-BxakuJ3r.js => settings-BRjzzF7s.js} (84%) rename web/static/app/assets/{skills-management-Bfmj31GL.js => skills-management-DYioP3HG.js} (69%) rename web/static/app/assets/{skills-monitor-kuN7vOld.js => skills-monitor-DSiw9NQW.js} (72%) rename web/static/app/assets/{tasks-BsxO6Llq.js => tasks-BLQal8Lr.js} (99%) rename web/static/app/assets/{tool-guard-DsP0bXWe.js => tool-guard-DgtRwxWv.js} (64%) rename web/static/app/assets/{vulnerabilities-B6s17x67.js => vulnerabilities-CBG2GjU8.js} (86%) rename web/static/app/assets/{webshell-TP1IaWS8.js => webshell-bSBkrXOh.js} (99%) rename web/static/app/assets/{workflows-CPuYj7pj.js => workflows-DDDy4zXA.js} (99%) diff --git a/web/frontend/src/components/graph-card.css b/web/frontend/src/components/graph-card.css new file mode 100644 index 00000000..c8c096a9 --- /dev/null +++ b/web/frontend/src/components/graph-card.css @@ -0,0 +1,119 @@ +.graph-node-layer { + position: absolute; + inset: 0; + pointer-events: none; + direction: ltr; +} +.graph-node { + position: absolute; + box-sizing: border-box; + border: 1px solid var(--semi-color-border); + border-radius: 8px; + padding: 0; + background: var(--semi-color-bg-1); + box-shadow: 0 1px 3px rgba(20, 31, 53, .10); + color: var(--semi-color-text-0); + font-family: inherit; + font-size: 14px; + font-weight: 500; + line-height: 1.2; + text-align: start; + cursor: pointer; + pointer-events: auto; +} +.graph-node:hover, .graph-node:focus-visible, .graph-node[aria-pressed="true"] { + border-color: var(--graph-node-color); + background: color-mix(in srgb, var(--graph-node-color) 5%, var(--semi-color-bg-1)); +} +.graph-node[aria-pressed="true"] { + box-shadow: 0 0 0 2px color-mix(in srgb, var(--graph-node-color) 19%, transparent), 0 3px 9px rgba(20, 31, 53, .13); +} +.graph-node-content { + display: flex; + align-items: center; + height: 100%; + min-width: 0; + padding-inline: 5px; + overflow: hidden; +} +.graph-node--compact { border-radius: 5px; box-shadow: 0 1px 3px rgba(20, 31, 53, .08); } +.graph-node--compact .graph-node-content { gap: 4px; } +.graph-node--compact .graph-node-content::before { + content: ""; + flex: 0 0 3px; + height: 11px; + border-radius: 2px; + background: var(--graph-node-color); +} +.graph-node--standard .graph-node-content { + flex-direction: column; + align-items: flex-start; + justify-content: center; + gap: 2px; +} +.graph-node--standard .graph-node-icon { width: 13px; height: 13px; } +.graph-node-kind { + display: inline-flex; + align-items: center; + gap: 5px; + flex-shrink: 0; + color: var(--semi-color-text-1); + font-size: calc(12px * var(--graph-node-text-scale, 1)); + font-weight: 650; +} +.graph-node-icon { + display: inline-grid; + place-items: center; + flex: 0 0 auto; + width: 16px; + height: 16px; + border-radius: 5px; + color: color-mix(in srgb, var(--graph-node-color) 75%, var(--semi-color-text-0)); + background: color-mix(in srgb, var(--graph-node-color) 13%, var(--semi-color-bg-1)); +} +.graph-node-title, .graph-node-keyline { + display: block; + width: 100%; + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} +.graph-node--detail { font-size: calc(15px * var(--graph-node-text-scale)); } +.graph-node:where([data-root="true"]) { + border-color: color-mix(in srgb, var(--graph-node-color) 31%, var(--semi-color-border)); + background: color-mix(in srgb, var(--graph-node-color) 5%, var(--semi-color-bg-1)); +} +.graph-node[data-boundary="true"] { border-style: dashed; } +.graph-node--detail .graph-node-content { + flex-direction: column; + justify-content: center; + align-items: flex-start; + gap: 3px; + padding: 5px 10px; +} +.graph-node--standard .graph-node-title { line-height: 16px; } +.graph-node--detail .graph-node-title, +.graph-node--standard[data-wrap="true"] .graph-node-title { + display: -webkit-box; + -webkit-box-orient: vertical; + -webkit-line-clamp: 2; + white-space: normal; + overflow-wrap: anywhere; +} +.graph-node-keyline { + color: var(--semi-color-text-2); + font: calc(12px * var(--graph-node-text-scale))/1.2 "SFMono-Regular", Consolas, monospace; +} +.graph-node-port { + position: absolute; + top: 50%; + width: 6px; + height: 6px; + transform: translateY(-50%); + border: 1px solid var(--semi-color-bg-1); + border-radius: 50%; + background: var(--graph-node-color); +} +.graph-node-port.is-left { left: -4px; } +.graph-node-port.is-right { right: -4px; } diff --git a/web/frontend/src/components/graph-card.jsx b/web/frontend/src/components/graph-card.jsx new file mode 100644 index 00000000..936f01ef --- /dev/null +++ b/web/frontend/src/components/graph-card.jsx @@ -0,0 +1,98 @@ +import { + IconAlertTriangle, IconBolt, IconBranch, IconFile, IconFlag, IconGlobe, + IconHelpCircle, IconKey, IconLayers, IconSearch, IconServer, IconTickCircle +} from '@douyinfe/semi-icons'; +import './graph-card.css'; + +export const CARD_WIDTH = 216; +export const CARD_HEIGHT = 98; + +const NODE_ICONS = { + target: IconGlobe, infra: IconServer, auth: IconKey, finding: IconSearch, chain: IconBranch, + exploit: IconBolt, poc: IconTickCircle, vulnerability: IconAlertTriangle, missing: IconHelpCircle, + goal: IconFlag, step: IconBranch, fact: IconFile, receipt: IconTickCircle, observation: IconSearch, + memory: IconLayers, digest: IconFile, guidance: IconBolt +}; + +export function GraphNodeIcon({ type }) { + const Icon = NODE_ICONS[type] || IconFile; + return ; +} + +export function GraphNodeCard({ id, title, type, category, color, factKey, boundary, ports = [], selected, card, onSelect }) { + const { density, box } = card; + const detail = density === 'detail'; + const textScale = Math.min(Math.max(box.width / CARD_WIDTH, 1), 1.6); + return ( + + ); +} + +export function visibleGraphCards(cy, { anchorId, allowCompact = () => true } = {}) { + const width = cy.width(); + const height = cy.height(); + const cardWidth = CARD_WIDTH * cy.zoom(); + const cardHeight = CARD_HEIGHT * cy.zoom(); + const density = cardHeight >= 68 ? 'detail' : cardHeight >= 36 ? 'standard' : 'compact'; + const cards = []; + for (const node of cy.nodes(':visible')) { + const id = node.id(); + if (node.hasClass('dim')) continue; + if (density === 'compact' && id !== anchorId && (cardWidth < 49 || !allowCompact(node))) continue; + const { x, y } = node.renderedPosition(); + const left = x - cardWidth / 2; + const top = y - cardHeight / 2; + if (left + cardWidth <= 0 || left >= width || top + cardHeight <= 0 || top >= height) continue; + let box = { left, top, width: cardWidth, height: cardHeight }; + // Keep the assessment goal readable in very small fitted overviews. + if (id === anchorId && cardWidth < 49) { + box = { left: Math.max(3, left), top: y - 13, width: Math.min(108, width - Math.max(3, left)), height: 26 }; + } + cards.push({ id, density, box }); + } + return cards; +} + +export function zoomGraph(cy, factor, renderedPosition = { x: cy.width() / 2, y: cy.height() / 2 }) { + cy.zoom({ level: cy.zoom() * factor, renderedPosition }); +} + +export function bindGraphCardWheel(layer, cy) { + const wheel = event => { + if (!event.deltaY) return; + event.preventDefault(); + const { left, top, height } = layer.getBoundingClientRect(); + const units = event.deltaMode === 1 ? 33 : event.deltaMode === 2 ? height : 1; + zoomGraph(cy, 10 ** (-event.deltaY * units / 1250), { x: event.clientX - left, y: event.clientY - top }); + }; + // Card buttons sit above the renderer. Keep pointer-centered zoom on either surface. + layer.addEventListener('wheel', wheel, { passive: false }); + return () => layer.removeEventListener('wheel', wheel); +} diff --git a/web/frontend/src/components/graph-parents.js b/web/frontend/src/components/graph-parents.js new file mode 100644 index 00000000..74233915 --- /dev/null +++ b/web/frontend/src/components/graph-parents.js @@ -0,0 +1,14 @@ +// Pick a layout parent for each node. Edges outside this forest remain in the graph. +export function graphParents(orderedEdges, isRoot) { + const parents = new Map(); + const parentEdges = new Set(); + for (const edge of orderedEdges) { + if (isRoot(edge.target) || parents.has(edge.target)) continue; + let ancestor = edge.source; + while (ancestor && ancestor !== edge.target) ancestor = parents.get(ancestor); + if (ancestor === edge.target) continue; + parents.set(edge.target, edge.source); + parentEdges.add(edge); + } + return { parents, parentEdges }; +} diff --git a/web/frontend/src/pages/deep-assessments/GraphCanvas.jsx b/web/frontend/src/pages/deep-assessments/GraphCanvas.jsx index 7a392095..08ee143c 100644 --- a/web/frontend/src/pages/deep-assessments/GraphCanvas.jsx +++ b/web/frontend/src/pages/deep-assessments/GraphCanvas.jsx @@ -1,32 +1,40 @@ -import { Fragment, useEffect, useRef, useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import { toastError } from '../../components/ui'; -import { GRAPH_KIND_COLOR } from './labels'; +import { bindGraphCardWheel, CARD_HEIGHT, CARD_WIDTH, GraphNodeCard, visibleGraphCards, zoomGraph } from '../../components/graph-card'; +import { useT } from '../../lib/i18n'; +import { GRAPH_KIND_COLOR, kindText } from './labels'; import { GRAPH_COLUMN_GAP, graphPositions, updatedGraphPositions } from './graphLayout'; const STYLE = [ { selector: 'node', style: { - 'background-color': 'data(color)', - width: 23, height: 23, - 'border-width': 2, 'border-color': '#ffffff' + shape: 'round-rectangle', width: CARD_WIDTH, height: CARD_HEIGHT, + 'background-color': 'data(color)', 'background-opacity': 0.13, + 'border-color': 'data(color)', 'border-opacity': 0.42, 'border-width': 1.2 } }, - { selector: 'node[kind = "goal"]', style: { width: 31, height: 31 } }, - { selector: 'node[boundary = 1]', style: { shape: 'round-rectangle', width: 17, height: 17 } }, - { selector: 'node.current', style: { 'border-color': '#0066ff', 'border-width': 4 } }, + { selector: 'node.current', style: { 'background-opacity': 0.78 } }, { selector: 'edge', style: { - width: 1.8, + width: 2.1, 'line-color': 'data(color)', 'target-arrow-color': 'data(color)', 'target-arrow-shape': 'triangle', 'arrow-scale': 0.8, 'curve-style': 'bezier', - opacity: 0.74 + opacity: 0.66 } }, + { + selector: 'edge.forward', + style: { 'source-endpoint': '50% 0px', 'target-endpoint': '-50% 0px' } + }, + { + selector: 'edge.backward', + style: { 'source-endpoint': '-50% 0px', 'target-endpoint': '50% 0px' } + }, { selector: 'edge.flow', style: { 'curve-style': 'round-taxi', 'taxi-direction': 'rightward', 'taxi-radius': 16 } @@ -40,6 +48,7 @@ const STYLE = [ }, { selector: 'edge[projection = "archive"]', style: { 'line-style': 'dashed' } }, { selector: 'edge[projection = "details"]', style: { 'line-style': 'dotted' } }, + { selector: 'edge.cross-link', style: { opacity: 0.31 } }, { selector: 'node.dim', style: { opacity: 0.32 } }, { selector: 'edge.dim', style: { opacity: 0.12 } }, { @@ -49,70 +58,7 @@ const STYLE = [ { selector: '.hidden', style: { display: 'none' } } ]; -const LABEL_WIDTH = 148; -const LABEL_HEIGHT = 22; -const LABEL_PADDING = 7; -const LABEL_PRIORITY = { goal: 0, finding: 1, step: 2, guidance: 3, fact: 4, digest: 5 }; -const overlaps = (a, b) => a.x < b.x + b.width && a.x + a.width > b.x - && a.y < b.y + b.height && a.y + a.height > b.y; - -function visibleLabels(cy, model, selected, hovered, measure) { - const width = cy.width(); - const height = cy.height(); - const zoom = cy.zoom(); - const items = cy.nodes(':visible').map(node => { - const item = model.nodes.get(node.id()); - const { x, y } = node.renderedPosition(); - const radius = node.renderedOuterWidth() / 2 + 2; - const priority = node === selected ? -2 : node === hovered ? -1 - : (LABEL_PRIORITY[item.kind] ?? 6) + (item.boundary ? 8 : 0); - return { node, item, x, y, radius, priority }; - }).filter(({ x, y, radius }) => x >= -radius && x <= width + radius && y >= -radius && y <= height + radius); - const occupied = items.map(({ x, y, radius }) => ({ - x: x - radius, y: y - radius, width: radius * 2, height: radius * 2 - })); - const labels = []; - items.sort((a, b) => a.priority - b.priority); - - for (const { node, item, x, y, radius, priority } of items) { - if (zoom < 0.64 && priority > 3) continue; - if (node.hasClass('dim') && priority >= 0) continue; - const title = (item.title || item.id).replace(/\s+/g, ' ').trim() || item.id; - const labelWidth = Math.min(LABEL_WIDTH, Math.ceil(measure(title)) + LABEL_PADDING * 2); - const left = Math.max(4, Math.min(x - labelWidth / 2, width - labelWidth - 4)); - const top = y + radius + 4; - const candidates = [{ x: left, y: top, width: labelWidth, height: LABEL_HEIGHT }]; - if (priority <= 3) { - candidates.push( - { ...candidates[0], y: y - radius - LABEL_HEIGHT - 4 }, - { ...candidates[0], x: x + radius + 5, y: y - LABEL_HEIGHT / 2 }, - { ...candidates[0], x: x - radius - labelWidth - 5, y: y - LABEL_HEIGHT / 2 } - ); - } - if (priority <= 0) { - // A fitted overview can leave only a few pixels between nodes on a phone. - for (const offset of [28, 54, 80, 106, 132]) { - candidates.push( - { ...candidates[0], y: top + offset, displaced: true }, - { ...candidates[0], y: y - radius - LABEL_HEIGHT - 4 - offset, displaced: true } - ); - } - } - const box = candidates.find(rect => rect.x >= 3 && rect.x + rect.width <= width - 3 - && rect.y >= 3 && rect.y + rect.height <= height - 3 - && !occupied.some(other => overlaps(rect, other))); - if (!box) continue; - occupied.push({ x: box.x - 3, y: box.y - 2, width: box.width + 6, height: LABEL_HEIGHT + 4 }); - const below = box.y > y; - const leader = box.displaced ? { - left: x, - top: below ? y + radius : box.y + LABEL_HEIGHT, - height: below ? box.y - y - radius : y - radius - box.y - LABEL_HEIGHT - } : null; - labels.push({ id: item.id, title, box, leader, selected: priority === -2 }); - } - return labels; -} +const COMPACT_KINDS = new Set(['goal', 'step', 'finding', 'guidance']); function fitGraph(cy) { const nodes = cy.nodes(':visible'); @@ -121,18 +67,11 @@ function fitGraph(cy) { if (cy.zoom() > 1) cy.zoom(1).center(nodes); } -function zoomGraph(cy, factor) { - cy.zoom({ - level: cy.zoom() * factor, - renderedPosition: { x: cy.width() / 2, y: cy.height() / 2 } - }); -} - function graphElements(model, positions) { return [ ...[...model.nodes.values()].map(node => ({ data: { - id: node.id, kind: node.kind, boundary: Number(Boolean(node.boundary)), + id: node.id, kind: node.kind, color: GRAPH_KIND_COLOR[node.kind] || '#86909c' }, position: positions.get(node.id) @@ -141,22 +80,27 @@ function graphElements(model, positions) { const source = positions.get(edge.source); const target = positions.get(edge.target); const dx = target.x - source.x; - const route = dx > GRAPH_COLUMN_GAP * 1.5 ? 'long-flow' : 'flow'; + const classes = []; + if (dx !== 0) classes.push(dx > 0 ? 'forward' : 'backward'); + if (dx > 0 && model.layoutEdges.has(edge.id)) classes.push(dx > GRAPH_COLUMN_GAP * 1.5 ? 'long-flow' : 'flow'); + if (!model.layoutEdges.has(edge.id)) classes.push('cross-link'); return { data: { ...edge, projection: edge.projection || '', bend: target.y < source.y ? -28 : 28 }, - classes: dx > 0 && model.layoutEdges.has(edge.id) ? route : '' + classes: classes.join(' ') }; }) ]; } export default function GraphCanvas({ model, graphKey, selectedId, showAll, locate, onSelect, chartRef }) { + const t = useT(); const containerRef = useRef(null); + const layerRef = useRef(null); const controlsRef = useRef({ model, selectedId, showAll, locate, onSelect }); - const [labels, setLabels] = useState([]); + const [cards, setCards] = useState([]); useEffect(() => { controlsRef.current = { model, selectedId, showAll, locate, onSelect }; @@ -165,20 +109,22 @@ export default function GraphCanvas({ model, graphKey, selectedId, showAll, loca useEffect(() => { const container = containerRef.current; + const layer = layerRef.current; if (!graphKey) { - setLabels([]); + setCards([]); return undefined; } let disposed = false; let cy; let frame; let resize; - let hovered; + let unbindWheel; + let anchorId; let appliedModel; let centeredRequest; let previousSelection; let previousShowAll = controlsRef.current.showAll; - setLabels([]); + setCards([]); import('cytoscape').then(({ default: cytoscape }) => { if (disposed) return; @@ -189,17 +135,14 @@ export default function GraphCanvas({ model, graphKey, selectedId, showAll, loca boxSelectionEnabled: false, autounselectify: true }); - const measurement = document.createElement('canvas').getContext('2d'); - measurement.font = `12px ${getComputedStyle(container).fontFamily}`; - const widths = new Map(); - const measure = title => { - if (!widths.has(title)) widths.set(title, measurement.measureText(title).width); - return widths.get(title); - }; + unbindWheel = bindGraphCardWheel(layer, cy); const selectedNode = () => cy.$id(controlsRef.current.selectedId)[0]; - const updateLabels = () => { + const updateCards = () => { cancelAnimationFrame(frame); - frame = requestAnimationFrame(() => setLabels(visibleLabels(cy, appliedModel, selectedNode(), hovered, measure))); + frame = requestAnimationFrame(() => setCards(visibleGraphCards(cy, { + anchorId, + allowCompact: element => COMPACT_KINDS.has(element.data('kind')) + }))); }; const syncSelection = () => { const { model, selectedId, showAll, locate } = controlsRef.current; @@ -232,7 +175,7 @@ export default function GraphCanvas({ model, graphKey, selectedId, showAll, loca } previousSelection = selected; previousShowAll = showAll; - updateLabels(); + updateCards(); }; const sync = (reset = false) => { const { model } = controlsRef.current; @@ -242,6 +185,7 @@ export default function GraphCanvas({ model, graphKey, selectedId, showAll, loca model, new Map(cy.nodes().map(node => [node.id(), node.position()])) ); appliedModel = model; + anchorId = [...model.nodes.values()].find(node => node.kind === 'goal')?.id; cy.json({ elements: graphElements(model, positions) }); } syncSelection(); @@ -254,13 +198,12 @@ export default function GraphCanvas({ model, graphKey, selectedId, showAll, loca resetLayout: () => sync(true) }; sync(); - cy.on('pan zoom position', updateLabels); + cy.on('pan zoom position', updateCards); cy.on('tap', event => { if (event.target === cy) controlsRef.current.onSelect(''); else if (event.target.isNode()) controlsRef.current.onSelect(event.target.id()); }); - cy.on('mouseover', 'node', event => { hovered = event.target; updateLabels(); }); - cy.on('mouseout', 'node', () => { hovered = undefined; updateLabels(); }); + let width = cy.width(); let height = cy.height(); @@ -281,7 +224,7 @@ export default function GraphCanvas({ model, graphKey, selectedId, showAll, loca } else if (!selected && (nextWidth < previousWidth || nextHeight < previousHeight)) { fitGraph(cy); } - updateLabels(); + updateCards(); }); resize.observe(container); }).catch(error => { if (!disposed) toastError(error, '图谱渲染失败'); }); @@ -290,6 +233,7 @@ export default function GraphCanvas({ model, graphKey, selectedId, showAll, loca disposed = true; cancelAnimationFrame(frame); resize?.disconnect(); + unbindWheel?.(); cy?.destroy(); chartRef.current = null; }; @@ -298,23 +242,27 @@ export default function GraphCanvas({ model, graphKey, selectedId, showAll, loca return (
-
- {labels.map(({ id, title, box, leader, selected }) => ( - - {leader ? - ))} + type={node.kind} + category={kindText(t, node.kind)} + color={GRAPH_KIND_COLOR[node.kind] || '#86909c'} + factKey={node.id} + boundary={node.boundary} + selected={node.id === selectedId} + onSelect={onSelect} + /> + ); + })}
); diff --git a/web/frontend/src/pages/deep-assessments/GraphTab.jsx b/web/frontend/src/pages/deep-assessments/GraphTab.jsx index 57ab8e1c..5317bf30 100644 --- a/web/frontend/src/pages/deep-assessments/GraphTab.jsx +++ b/web/frontend/src/pages/deep-assessments/GraphTab.jsx @@ -6,7 +6,8 @@ import { useT } from '../../lib/i18n'; import { useInterval, useResource } from '../../components/hooks'; import { SectionCard, ErrorBanner, JsonViewer, KeyValueList, toastError } from '../../components/ui'; import { formatDateTime } from '../../lib/format'; -import { kindText, stateText, truncateText } from './labels'; +import { GraphNodeIcon } from '../../components/graph-card'; +import { GRAPH_KIND_COLOR, kindText, stateText, truncateText } from './labels'; import GraphCanvas from './GraphCanvas'; import GraphBrief from './GraphBrief'; import { prepareGraph } from './graphLayout'; @@ -152,7 +153,7 @@ export default function GraphTab({
refresh()} /> @@ -210,30 +211,6 @@ export default function GraphTab({ /> ) : ( <> -
- - - - - - - {canExpand ? ( - - - {showAll ? `全部 ${model.edges.length} 条连线` : `主干 ${model.backbone.size} / ${model.edges.length} 条连线`} - {!showAll && collapsedNodes > 0 ? ` · 收起 ${collapsedNodes} 个明细节点` : null} - - - - ) : null} -
- {model.legend.length ? ( -
- {model.legend.map(({ label, color }) => ( -