vue3-network-graph 0.1.1 → 0.1.3

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.
package/README.md CHANGED
@@ -7,10 +7,10 @@
7
7
  - **连线模式**:直线 `straight` / 折线 `polyline` / 曲线 `curve` 三种 `lineMode`
8
8
  - **多线防重叠**:同一对节点间的多条连线(含双向)自动按槽位展开为平行线 / 平行弧 / 错位折线,任何模式下不再重叠
9
9
  - **连线文字沿线直立**:`fromText` / `centerText` / `toText` 三个位置沿路径切向旋转,切向超过 ±90° 自动翻转,文字永远可读
10
- - **多线合并**:同一对节点连线数超过 6 条(`MAX_BUNDLE`)自动合并为 1 条 + 数量角标;点击合并线会选中整组,`edge-select` 返回代表线并附带 `bundleEdges`(组内全部连线数据),`selection-change` 的 `edges` 包含组内全部连线
10
+ - **多线合并**:同一对节点连线数超过 6 条自动合并为 1 条 + 数量角标;点击合并线会选中整组,`edge-select` 返回代表线并附带 `bundleEdges`(组内全部连线数据),`selection-change` 的 `edges` 包含组内全部连线
11
11
  - **节点**:2D / 2.5D 图标、状态呼吸灯、文本节点、自定义图标插槽 `#node-icon`
12
12
  - **交互**:节点拖拽(**多选整体拖动**)、拖拽控制点调整节点尺寸、**画布无限平移**、滚轮缩放、节点/连线点击选中、**Ctrl+点击多选**、**Ctrl+左键框选节点**、流向动画(虚线流光 + 粒子)
13
- - **大图性能**:视口裁剪(只渲染视口内元素)+ 装饰动效按规模自动降级 + 边几何缓存;密集连线重叠处**节点优先**响应,千级节点可流畅平移 / 拖拽
13
+ - **大图性能**:装饰动效按规模自动降级(可配置覆盖);密集连线重叠处**节点优先**响应——千级节点可流畅平移 / 拖拽
14
14
  - **坐标能力**:组件外可调用 `clientToSvg`(鼠标坐标 → SVG 坐标)与 `svgToClient`(SVG 坐标 → 屏幕坐标)
15
15
  - **外观配置**:背景径向渐变(内外圈颜色 / 自定义 stops)、网格(颜色 / 间距 / 透明度)、选中描边与连线颜色,全部经 `config` 控制
16
16
  - **数据同步**:组件内外双向同步——props 深度监听(替换数组 / 就地修改均可)、改动经事件与 v-model 实时外发、`setData()` 支持组件外全量替换
@@ -39,19 +39,31 @@ app.use(Vue3NetworkGraph); // 之后模板里可直接使用 <Topology />
39
39
  ```vue
40
40
  <script setup>
41
41
  import { ref } from "vue";
42
- import {
43
- Topology,
44
- initialNodes,
45
- initialEdges,
46
- initialGroups,
47
- } from "vue3-network-graph";
42
+ import { Topology } from "vue3-network-graph";
48
43
  import "vue3-network-graph/style.css";
49
44
 
50
- const myNodes = ref(initialNodes);
51
- const myEdges = ref(initialEdges);
52
- const myGroups = ref(initialGroups);
45
+ /* 节点 / 连线(/ 分组框)都是普通数组,按你的场景准备即可 */
46
+ const myNodes = ref([
47
+ { id: "core", label: "核心", type: "core", state: "normal", x: 0, y: 0 },
48
+ {
49
+ id: "web-1",
50
+ label: "Web-1",
51
+ type: "server",
52
+ state: "normal",
53
+ x: 160,
54
+ y: -80,
55
+ },
56
+ { id: "db-1", label: "DB-1", type: "database", state: "idle", x: 160, y: 80 },
57
+ ]);
58
+ const myEdges = ref([
59
+ { id: "e1", from: "core", to: "web-1" },
60
+ { id: "e2", from: "core", to: "db-1", centerText: "查询" },
61
+ ]);
62
+ const myGroups = ref([
63
+ { id: "g1", label: "应用区", color: "#409eff", nodeIds: ["web-1", "db-1"] },
64
+ ]);
53
65
  const lineMode = ref("straight"); // straight | polyline | curve
54
- const viewMode = ref("2d"); // 2d | 3d
66
+ const viewMode = ref("2d"); // 2d | 3d
55
67
  const topoRef = ref(null);
56
68
  </script>
57
69
 
@@ -64,7 +76,12 @@ const topoRef = ref(null);
64
76
  v-model:view-mode="viewMode"
65
77
  :edges="myEdges"
66
78
  :groups="myGroups"
67
- :config="{ showGrid: false, nodeDraggable: true, nodeResizable: true, enableZoom: true }"
79
+ :config="{
80
+ showGrid: false,
81
+ nodeDraggable: true,
82
+ nodeResizable: true,
83
+ enableZoom: true,
84
+ }"
68
85
  @node-click="(n) => console.log(n)"
69
86
  @edge-select="(e) => console.log(e)"
70
87
  @selection-change="(sel) => console.log(sel.nodes, sel.edges)"
@@ -79,149 +96,137 @@ const topoRef = ref(null);
79
96
 
80
97
  ## 交互操作一览(鼠标 / 键盘)
81
98
 
82
- | 操作 | 手势 | 说明 |
83
- |---|---|---|
84
- | 选中节点 / 连线 | 左键单击 | 重复点击已选中项取消选中;点击空白处清空全部选中 |
85
- | 多选(点选) | Ctrl(macOS ⌘)+ 单击 | 将节点 / 连线加入或移出选中集合,支持节点与连线混合多选 |
86
- | 多选(框选) | Ctrl(macOS ⌘)+ 左键拖动 | 在空白画布拉出矩形选框,**与选框相交的节点**并入当前选中(并集语义;仅节点参与框选) |
87
- | 拖动单个节点 | 按住节点区域任意处拖动 | 需 `editable` 开启且 `config.nodeDraggable !== false`;节点盒及文字区(含图标-文字间隙)整体可命中;拖动未选中的节点会先选中它 |
88
- | 拖动多个节点 | 拖动任一**已选中**节点 | 选中集合整体移动,所有节点位移一致;松开后选中状态保留 |
89
- | 画布平移(无限) | 空白处按住左键拖动;或任意位置按住**中键**拖动 | 平移无边界限制;内容与网格一起移动;不改变选中状态 |
90
- | 缩放 | 滚轮 | 以鼠标位置为中心缩放(0.2x–5x) |
91
- | 调整节点尺寸 | 选中**单个**节点后拖动 8 个控制点 | 需 `editable` 且 `config.nodeResizable !== false` |
92
- | 拖拽创建连线 | 从节点手柄拖到目标节点 | 需 `config.showNodeHandles: true` |
99
+ | 操作 | 手势 | 说明 |
100
+ | ---------------- | ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
101
+ | 选中节点 / 连线 | 左键单击 | 重复点击已选中项取消选中;点击空白处清空全部选中 |
102
+ | 多选(点选) | Ctrl(macOS ⌘)+ 单击 | 将节点 / 连线加入或移出选中集合,支持节点与连线混合多选 |
103
+ | 多选(框选) | Ctrl(macOS ⌘)+ 左键拖动 | 在空白画布拉出矩形选框,**与选框相交的节点**并入当前选中(并集语义;仅节点参与框选) |
104
+ | 拖动单个节点 | 按住节点区域任意处拖动 | 需 `editable` 开启且 `config.nodeDraggable !== false`;节点盒及文字区(含图标-文字间隙)整体可命中;拖动未选中的节点会先选中它 |
105
+ | 拖动多个节点 | 拖动任一**已选中**节点 | 选中集合整体移动,所有节点位移一致;松开后选中状态保留 |
106
+ | 画布平移(无限) | 空白处按住左键拖动;或任意位置按住**中键**拖动 | 平移无边界限制;内容与网格一起移动;不改变选中状态 |
107
+ | 缩放 | 滚轮 | 以鼠标位置为中心缩放(0.2x–5x) |
108
+ | 调整节点尺寸 | 选中**单个**节点后拖动 8 个控制点 | 需 `editable` 且 `config.nodeResizable !== false` |
109
+ | 拖拽创建连线 | 从节点手柄拖到目标节点 | 需 `config.showNodeHandles: true` |
93
110
 
94
111
  > 左键在空白处的「点击」与「拖动」按小阈值区分(平移 4px / 框选 3px / 节点拖动 3px):小于阈值仍视为点击(清空或切换选中),超过阈值才进入拖动。拖拽手势结束后产生的“余波点击”会被组件抑制,不会误改选中状态。节点及其文字所在的完整区域为命中区域——盒内任意位置、图标与文字之间的空隙均可点击 / 拖动。
95
112
 
96
113
  ## 性能与大数据量
97
114
 
98
- 组件内置面向大图的渲染与开销控制,默认自动生效;演示数据(≤ 128 条连线)下与旧版视觉一致。
99
-
100
- ### 视口裁剪(自动)
101
-
102
- 只渲染与当前视口相交(含适量外扩边距)的节点与连线,视口外元素不进入 DOM;平移 / 缩放时自动增减。大图拖拽、平移开销由「画布总规模」降为「视口内规模」;框选 / 点击 / 拖拽等交互全部基于数据,不受裁剪影响。
115
+ 组件内置面向大图的性能优化,默认自动生效,无需配置。装饰动效(SVG 中开销最大的效果)超过规模阈值自动关闭:
103
116
 
104
117
  ### 装饰动效自动降级(可覆盖)
105
118
 
106
119
  流光 / 粒子动画、发光滤镜与呼吸灯是 SVG 中开销最大的效果,超过规模阈值自动关闭:
107
120
 
108
- | 动效 | 自动关闭阈值(默认) | 覆盖配置 |
109
- |---|---|---|
110
- | 流光 + 粒子动画 | 连线数 > 200 | `motionFx: true/false` 或 `fxMotionMaxEdges: n` |
111
- | 连线发光滤镜 | 连线数 > 500 | `glowFx: true/false` 或 `fxGlowMaxEdges: n` |
112
- | 节点呼吸灯 | 节点数 > 300 | `breatheFx: true/false` 或 `fxBreatheMaxNodes: n` |
121
+ | 动效 | 自动关闭阈值(默认) | 覆盖配置 |
122
+ | --------------- | -------------------- | ------------------------------------------------- |
123
+ | 流光 + 粒子动画 | 连线数 > 200 | `motionFx: true/false` 或 `fxMotionMaxEdges: n` |
124
+ | 连线发光滤镜 | 连线数 > 500 | `glowFx: true/false` 或 `fxGlowMaxEdges: n` |
125
+ | 节点呼吸灯 | 节点数 > 300 | `breatheFx: true/false` 或 `fxBreatheMaxNodes: n` |
113
126
 
114
127
  - 总开关 `config.fx`:`false` 全关(最省性能)/ `true` 全开 / 不设 = 自动;单项覆盖优先于总开关。
115
128
  - 典型用法:「性能模式」按钮 = `config.fx = false`。
116
129
 
117
130
  ### 交互优先级(密集图形)
118
131
 
119
- 连线点击感应层位于节点层下方:节点与连线重叠处,点击 / 拖动作用于**节点**;连线在未被遮挡处正常点击。保证密集图形下节点始终可选中、可拖动。
120
-
121
- ### 性能基准工具(`perf/`)
122
-
123
- ```bash
124
- npm run dev # 打开 http://localhost:5173/perf/stress.html?nodes=800&edges=1100
125
- node perf/bench.mjs --label=baseline # 无头 Chrome 自动基准:静置 / 平移 / 拖拽 / 缩放
126
- ```
127
-
128
- - `perf/stress.html`:参数化压力画布(`?nodes=800&edges=1100&fx=on|off`),HUD 实时显示渲染节点数、元素数与 fps。
129
- - `perf/bench.mjs`:CDP 自动跑四场景并输出 JSON(帧间隔、longtask、CPU 指标),可与 `perf/results-*.json` 对比。
130
- - 800/1100 参考:优化后首帧 1 秒以内(实测 0.3–0.5s)、静置 60fps、平移 / 拖拽单帧最慢 ≈0.1s(优化前首帧 23.6s、平移单帧最慢 18.0s、初始 DOM 元素 26,002 → 约 3,000)——具体以本机实测为准。
132
+ 节点与连线重叠处,点击 / 拖动作用于**节点**;连线在未被遮挡处正常点击——密集图形下节点始终可选中、可拖动。
131
133
 
132
134
  ## Props
133
135
 
134
- | Prop | 类型 | 默认值 | 说明 |
135
- |---|---|---|---|
136
- | `nodes` | `Array` | `[]` | 节点数组:`id` `label` `type` `state` `x` `y` `width` `height` `fontColor` `fontSize` `fontWeight` `borderColor` `borderWidth` `borderStyle` `background` `labelPosition`(`top`/`center`/`bottom`) `rotation`;`type: "text"` 为纯文本节点 |
137
- | `edges` | `Array` | `[]` | 连线数组:`id` `from` `to` `speed`(流速倍率) `fromAnchor` `toAnchor`(`center`/`edge` 边界交点) `fromText` `centerText` `toText` `endMarker`(`none`/`arrow`/`dot`) `fontSize` `fontColor` |
138
- | `groups` | `Array` | `[]` | 分组虚线框:`id` `label` `color` `nodeIds` `padding` `rotation` |
139
- | `lineMode` | `String` | `"curve"` | 连线模式:`straight` / `polyline` / `curve`(支持 `v-model:line-mode`) |
140
- | `viewMode` | `String` | `"2d"` | 视图模式:`2d` / `3d`(2.5D 卡片风格,支持 `v-model:view-mode`) |
141
- | `editable` | `Boolean` | `false` | 编辑交互总开关:开启节点拖拽与尺寸调整(画布平移 / 框选不受此开关限制,可用 `config` 单独控制) |
142
- | `width` / `height` | `Number` | 容器尺寸 | 画布基准尺寸 |
143
- | `config` | `Object` | `{}` | 交互与外观配置,见下表 |
136
+ | Prop | 类型 | 默认值 | 说明 |
137
+ | ------------------ | --------- | --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
138
+ | `nodes` | `Array` | `[]` | 节点数组:`id` `label` `type` `state` `x` `y` `width` `height` `fontColor` `fontSize` `fontWeight` `borderColor` `borderWidth` `borderStyle` `borderRadius` `background` `labelPosition`(`top`/`center`/`bottom`) `rotation`;`type: "text"` 为纯文本节点 |
139
+ | `edges` | `Array` | `[]` | 连线数组:`id` `from` `to` `speed`(流速倍率) `animated`(单条线动画开关,`false` 关闭该线流光/粒子) `fromAnchor` `toAnchor`(`center`/`edge` 边界交点) `fromText` `centerText` `toText` `endMarker`(`none`/`arrow`/`dot`) `fontSize` `fontColor` |
140
+ | `groups` | `Array` | `[]` | 分组虚线框:`id` `label` `color` `nodeIds` `padding` `rotation` |
141
+ | `lineMode` | `String` | `"curve"` | 连线模式:`straight` / `polyline` / `curve`(支持 `v-model:line-mode`) |
142
+ | `viewMode` | `String` | `"2d"` | 视图模式:`2d` / `3d`(2.5D 卡片风格,支持 `v-model:view-mode`) |
143
+ | `editable` | `Boolean` | `false` | 编辑交互总开关:开启节点拖拽与尺寸调整(画布平移 / 框选不受此开关限制,可用 `config` 单独控制) |
144
+ | `width` / `height` | `Number` | 容器尺寸 | 画布基准尺寸 |
145
+ | `config` | `Object` | `{}` | 交互与外观配置,见下表 |
144
146
 
145
- > `nodes` / `edges` / `groups` 默认均为空数组(空画布);通用演示数据可通过包导出的 `initialNodes` / `initialEdges` / `initialGroups` 使用。
147
+ > `nodes` / `edges` / `groups` 默认均为空数组(空画布),直接传入自己的数据即可。`type` 与 `state` 的取值配色可参考包导出的 `nodeColors`(类型色:`core` / `server` / `firewall` / `loadbalancer` / `database` / `cdn` / `cache` / `queue` / `text`)与 `nodeStates`(状态色,含 `normal` / `warning` / `error` 等 10 种)。
146
148
 
147
149
  ### config 配置项
148
150
 
149
- | 配置项 | 类型 | 默认值 | 说明 |
150
- |---|---|---|---|
151
- | `selectedBorderColor` | `String` | `#fbbf24` | 选中节点的描边颜色 |
152
- | `selectedLineColor` | `String` | `#fbbf24` | 选中连线的颜色 |
153
- | `nodeDraggable` | `Boolean` | `true` | 节点拖拽开关(需 `editable`) |
154
- | `nodeResizable` | `Boolean` | `true` | 拖拽控制点调整节点尺寸开关(需 `editable`;单选节点后显示 4 角 + 4 边共 8 个控制点) |
155
- | `enableZoom` | `Boolean` | `true` | 滚轮缩放开关 |
156
- | `enablePan` | `Boolean` | `true` | **画布拖拽平移开关**(空白左键拖动 / 中键拖动) |
157
- | `enableMarquee` | `Boolean` | `true` | **Ctrl + 左键框选开关** |
158
- | `showGrid` | `Boolean` | `true` | 网格背景(设 `false` 关闭) |
159
- | `gridColor` | `String` | `#1a2240` | 网格线颜色 |
160
- | `gridStep` | `Number` | `40` | 网格间距(1x 缩放下的 px,随缩放联动) |
161
- | `gridOpacity` | `Number` | `0.5` | 网格叠层透明度(0–1) |
162
- | `radialGradient` | `Object` | 见说明 | 画布背景径向渐变:`{ startColor, endColor }` 或 `{ cx, cy, r, stops: [...] }`;默认 `#0f1729 → #060a14` |
163
- | `showCenterPoint` | `Boolean` | `false` | 画布中心十字标记 |
164
- | `showEdgeTextBg` | `Boolean` | `false` | 连线文字底牌 |
165
- | `edgeTextBgColor` | `String` | `#0a0e1a` | 连线文字底牌颜色 |
166
- | `showNodeHandles` | `Boolean` | `false` | 节点手柄(开启后节点上下左右出现四个小圆圈,拖到另一节点松开自动连线) |
167
- | `showStatusDot` | `Boolean` | `true` | 节点右上角状态圆点开关(**默认展示**,设 `false` 隐藏全部) |
168
- | `watchDataChanges` | `Boolean` | `false` | 组件内数据变动检测(拖拽、尺寸调整、手柄连线、删除等,防抖 300ms 后触发 `data-change`) |
169
- | `fx` | `Boolean` | 自动 | 装饰动效总开关:`false` 全关(最省性能)/ `true` 全开 / 不设按规模自动(见「性能与大数据量」) |
170
- | `motionFx` / `glowFx` / `breatheFx` | `Boolean` | 自动 | 单项覆盖:流光+粒子动画 / 连线发光 / 呼吸灯 |
171
- | `fxMotionMaxEdges` | `Number` | `200` | 流光+粒子动画自动关闭的连线数阈值 |
172
- | `fxGlowMaxEdges` | `Number` | `500` | 连线发光自动关闭的连线数阈值 |
173
- | `fxBreatheMaxNodes` | `Number` | `300` | 呼吸灯自动关闭的节点数阈值 |
151
+ | 配置项 | 类型 | 默认值 | 说明 |
152
+ | ----------------------------------- | ----------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
153
+ | `selectedBorderColor` | `String` | `#fbbf24` | 选中节点的描边颜色 |
154
+ | `selectedLineColor` | `String` | `#fbbf24` | 选中连线的颜色 |
155
+ | `nodeDraggable` | `Boolean` | `true` | 节点拖拽开关(需 `editable`) |
156
+ | `nodeResizable` | `Boolean` | `true` | 拖拽控制点调整节点尺寸开关(需 `editable`;单选节点后显示 4 角 + 4 边共 8 个控制点) |
157
+ | `enableZoom` | `Boolean` | `true` | 滚轮缩放开关 |
158
+ | `enablePan` | `Boolean` | `true` | **画布拖拽平移开关**(空白左键拖动 / 中键拖动) |
159
+ | `enableMarquee` | `Boolean` | `true` | **Ctrl + 左键框选开关** |
160
+ | `maxSelectNodes` | `Number \| "unlimited"` | 不限制 | **最多选中节点数**:`0`(禁止选中节点)/ `1` / `2` 或任意非负整数;不设或 `"unlimited"` 不限制。超限的选中操作只补足到上限(取消选中不受限);降低上限时已选自动裁剪(保留前 N 个) |
161
+ | `maxSelectEdges` | `Number \| "unlimited"` | 不限制 | **最多选中连线数**(合并线按整组内全部连线计数);其余同 `maxSelectNodes` |
162
+ | `showGrid` | `Boolean` | `true` | 网格背景(设 `false` 关闭) |
163
+ | `gridColor` | `String` | `#1a2240` | 网格线颜色 |
164
+ | `gridStep` | `Number` | `40` | 网格间距(1x 缩放下的 px,随缩放联动) |
165
+ | `gridOpacity` | `Number` | `0.5` | 网格叠层透明度(0–1) |
166
+ | `radialGradient` | `Object` | 见说明 | 画布背景径向渐变:`{ startColor, endColor }` 或 `{ cx, cy, r, stops: [...] }`;默认 `#0f1729 → #060a14` |
167
+ | `showCenterPoint` | `Boolean` | `false` | 画布中心十字标记 |
168
+ | `showEdgeTextBg` | `Boolean` | `false` | 连线文字底牌 |
169
+ | `edgeTextBgColor` | `String` | `#0a0e1a` | 连线文字底牌颜色 |
170
+ | `showNodeHandles` | `Boolean` | `false` | 节点手柄(开启后节点上下左右出现四个小圆圈,拖到另一节点松开自动连线) |
171
+ | `showStatusDot` | `Boolean` | `true` | 节点右上角状态圆点开关(**默认展示**,设 `false` 隐藏全部) |
172
+ | `labelGap` | `Number` | `18` | 节点标签与节点盒边缘的**固定间距**(1x 缩放下 px),不随节点尺寸变化;`top` / 默认(下方)标签均适用 |
173
+ | `watchDataChanges` | `Boolean` | `false` | 组件内数据变动检测(拖拽、尺寸调整、手柄连线、删除等,防抖 300ms 后触发 `data-change`) |
174
+ | `fx` | `Boolean` | 自动 | 装饰动效总开关:`false` 全关(最省性能)/ `true` 全开 / 不设按规模自动(见「性能与大数据量」) |
175
+ | `motionFx` / `glowFx` / `breatheFx` | `Boolean` | 自动 | 单项覆盖:**连接线动画(流光+粒子)** / 连线发光 / 呼吸灯;单条线可用数据字段 `edge.animated: false` 单独关闭动画 |
176
+ | `fxMotionMaxEdges` | `Number` | `200` | 流光+粒子动画自动关闭的连线数阈值 |
177
+ | `fxGlowMaxEdges` | `Number` | `500` | 连线发光自动关闭的连线数阈值 |
178
+ | `fxBreatheMaxNodes` | `Number` | `300` | 呼吸灯自动关闭的节点数阈值 |
174
179
 
175
180
  ## Events
176
181
 
177
- | 事件 | 参数 | 说明 |
178
- |---|---|---|
179
- | `update:nodes` | `nodes` | 节点数据更新(`v-model:nodes`);拖拽 / 尺寸调整过程中持续触发 |
180
- | `update:lineMode` / `update:viewMode` | `string` | 模式变更(`v-model:*`) |
181
- | `node-click` | `node` | 节点被点击 |
182
- | `node-select` | `node\|null` | 单选节点变化(多选场景请用 `selection-change`) |
183
- | `edge-select` | `edge\|null` | 连线选中变化(合并线附带 `bundleEdges` 数组) |
184
- | `selection-change` | `{ nodes, edges }` | 选中集合变化时触发;`nodes`/`edges` 为选中的节点与连线对象数组。**Ctrl 点选与 Ctrl 框选后的完整选中数据都从这里获取** |
185
- | `data-change` | `{ nodes, edges, summary }` | `watchDataChanges` 开启时,数据变动防抖(300ms)后触发;`nodes`/`edges` 为当前全量深拷贝数据,`summary` 为相对基线的 `added/removed/updated` id 列表(节点/连线各一组) |
186
- | `edge-create` | `edge` | 节点手柄拖拽连接两个节点后触发;组件内部已临时渲染这条连线,父组件应在此事件中持久化(如加入 edges 数组) |
187
- | `edge-remove` | `edges` | `removeSelected()` 删除连线后触发;payload 为被删除的连线对象数组(含因节点删除而级联删除的连线) |
188
- | `edge-update` | `edges` | `updateEdges()` 修改连线后触发;payload 为更新后的连线数组(父组件可按 id 合并回自己的 edges 数组) |
189
- | `node-drag-start` / `node-drag-end` | `node` / — | 节点拖拽开始 / 结束;拖动多选集合时同样触发,payload 为被按住的那个节点 |
190
- | `node-resize-start` / `node-resize-end` | `node` / `node\|null` | 尺寸调整开始 / 结束(调整过程持续触发 `update:nodes`) |
191
-
192
- **多选交互说明**:按住 `Ctrl`(macOS 为 `⌘`)点击节点或连线可加入/移出选区,支持节点与连线混合多选;`Ctrl + 左键拖拽`在空白处拉框批量选择节点(并集);普通点击只选中单个并清空其他选中;点击空白处清空全部选中。
182
+ | 事件 | 参数 | 说明 |
183
+ | --------------------------------------- | --------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
184
+ | `update:nodes` | `nodes` | 节点数据更新(`v-model:nodes`);拖拽 / 尺寸调整过程中持续触发 |
185
+ | `update:lineMode` / `update:viewMode` | `string` | 模式变更(`v-model:*`) |
186
+ | `node-click` | `node` | 节点被点击 |
187
+ | `node-select` | `node\|null` | 单选节点变化(多选场景请用 `selection-change`) |
188
+ | `edge-select` | `edge\|null` | 连线选中变化(合并线附带 `bundleEdges` 数组) |
189
+ | `selection-change` | `{ nodes, edges }` | 选中集合变化时触发;`nodes`/`edges` 为选中的节点与连线对象数组。**Ctrl 点选与 Ctrl 框选后的完整选中数据都从这里获取** |
190
+ | `data-change` | `{ nodes, edges, summary }` | `watchDataChanges` 开启时,数据变动防抖(300ms)后触发;`nodes`/`edges` 为当前全量深拷贝数据,`summary` 为相对基线的 `added/removed/updated` id 列表(节点/连线各一组) |
191
+ | `edge-create` | `edge` | 节点手柄拖拽连接两个节点后触发;组件内部已临时渲染这条连线,父组件应在此事件中持久化(如加入 edges 数组) |
192
+ | `edge-remove` | `edges` | `removeSelected()` 删除连线后触发;payload 为被删除的连线对象数组(含因节点删除而级联删除的连线) |
193
+ | `edge-update` | `edges` | `updateEdges()` 修改连线后触发;payload 为更新后的连线数组(父组件可按 id 合并回自己的 edges 数组) |
194
+ | `node-drag-start` / `node-drag-end` | `node` / — | 节点拖拽开始 / 结束;拖动多选集合时同样触发,payload 为被按住的那个节点 |
195
+ | `node-resize-start` / `node-resize-end` | `node` / `node\|null` | 尺寸调整开始 / 结束(调整过程持续触发 `update:nodes`) |
196
+
197
+ **多选交互说明**:按住 `Ctrl`(macOS 为 `⌘`)点击节点或连线可加入/移出选区,支持节点与连线混合多选;`Ctrl + 左键拖拽`在空白处拉框批量选择节点(并集);普通点击只选中单个并清空其他选中;点击空白处清空全部选中。选中数量可通过 `config.maxSelectNodes` / `config.maxSelectEdges` 限制(`0` = 禁止选中该类元素,不设 = 不限制)。
193
198
 
194
199
  连线文字支持三种写法:字符串 `"文本"`,或对象 `{ text, rotation, fontSize, color }`(`rotation` 为叠加的额外旋转角)。
195
200
 
196
201
  ### 插槽
197
202
 
198
- | 插槽 | 作用域参数 | 说明 |
199
- |---|---|---|
203
+ | 插槽 | 作用域参数 | 说明 |
204
+ | ------------ | --------------------------- | -------------------------------------- |
200
205
  | `#node-icon` | `{ node, color, viewMode }` | 自定义节点图标内容(默认显示内置图形) |
201
206
 
202
207
  ## 组件方法(通过模板 ref 调用)
203
208
 
204
- | 方法 | 说明 |
205
- |---|---|
206
- | `getData()` | 主动获取当前最新数据(深拷贝):返回 `{ nodes, edges }`,包含组件内全部本地改动(拖拽位置、尺寸调整、手柄新建连线等);随时可调,与 `watchDataChanges` 开关无关,修改返回值不影响组件内部状态 |
207
- | `resetDataBaseline()` | 重置数据变动检测基线:以调用时的数据为新基线,之后继续监听相对新基线的变动并通过 `data-change` 返回 |
208
- | `setData(data?)` | 组件外全量替换数据集(加载新图谱 / 恢复快照):清除组件内痕迹(本地新建连线、删除记录、字段补丁)并重置变动基线;传 `{ nodes }` 时节点经 `update:nodes` 回写父级,不传则从当前 props 重新同步;连线请同步更新你的 `edges` 数组;返回替换后的全量深拷贝 |
209
- | `removeSelected()` | 删除当前选中的节点与连线:删节点时**级联删除所有与该节点相连的连线**;节点经 `update:nodes`(v-model)同步,被删连线经 `edge-remove` 事件抛出;返回 `{ nodes, edges }`(被删数据深拷贝) |
210
- | `addNodes(nodes)` | 添加节点(数组或单个):缺少 `id` 自动生成 uuid,已存在 `id` 跳过(改用 `updateNodes`);经 `update:nodes` 同步;返回新增节点深拷贝 |
211
- | `addEdges(edges)` | 添加连线(数组或单个):要求 `from` / `to`,缺少 `id` 自动生成 uuid,重复 `id` 跳过;立即渲染并经 `edge-create` 事件抛出;返回新增连线深拷贝 |
212
- | `updateNodes(nodes)` | 按 `id` 局部更新节点(浅合并,`undefined` 忽略,未知 id 跳过);经 `update:nodes` 同步;返回更新后深拷贝 |
213
- | `updateEdges(edges)` | 按 `id` 局部更新连线(浅合并,未知 id 跳过);经 `edge-update` 事件抛出;返回更新后深拷贝 |
214
- | `clientToSvg(input, clientY?)` | **鼠标坐标 → SVG 坐标**:参数可传鼠标事件对象(`clientToSvg(e)`)或坐标对(`clientToSvg(clientX, clientY)`);返回 `{ x, y }`——即节点所在坐标系(已自动折算当前缩放与平移),可直接与 `node.x` / `node.y` 运算;SVG 未挂载或矩阵不可用时返回 `null` |
215
- | `svgToClient(x, y)` | **SVG 坐标 → 屏幕坐标**:输入 `node.x` / `node.y`,返回 `{ x, y }`(屏幕 `clientX/clientY` 口径),常用于把 DOM 浮层(右键菜单 / tooltip 等)精确定位到某个节点;SVG 未挂载时返回 `null` |
209
+ | 方法 | 说明 |
210
+ | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
211
+ | `getData()` | 主动获取当前最新数据(深拷贝):返回 `{ nodes, edges }`,包含组件内全部本地改动(拖拽位置、尺寸调整、手柄新建连线等);随时可调,与 `watchDataChanges` 开关无关,修改返回值不影响组件内部状态 |
212
+ | `resetDataBaseline()` | 重置数据变动检测基线:以调用时的数据为新基线,之后继续监听相对新基线的变动并通过 `data-change` 返回 |
213
+ | `setData(data?)` | 组件外全量替换数据集(加载新图谱 / 恢复快照):清除组件内痕迹(本地新建连线、删除记录、字段补丁)并重置变动基线;传 `{ nodes }` 时节点经 `update:nodes` 回写父级,不传则从当前 props 重新同步;连线请同步更新你的 `edges` 数组;返回替换后的全量深拷贝 |
214
+ | `removeSelected()` | 删除当前选中的节点与连线:删节点时**级联删除所有与该节点相连的连线**;节点经 `update:nodes`(v-model)同步,被删连线经 `edge-remove` 事件抛出;返回 `{ nodes, edges }`(被删数据深拷贝) |
215
+ | `addNodes(nodes)` | 添加节点(数组或单个):缺少 `id` 自动生成 uuid,已存在 `id` 跳过(改用 `updateNodes`);经 `update:nodes` 同步;返回新增节点深拷贝 |
216
+ | `addEdges(edges)` | 添加连线(数组或单个):要求 `from` / `to`,缺少 `id` 自动生成 uuid,重复 `id` 跳过;立即渲染并经 `edge-create` 事件抛出;返回新增连线深拷贝 |
217
+ | `updateNodes(nodes)` | 按 `id` 局部更新节点(浅合并,`undefined` 忽略,未知 id 跳过);经 `update:nodes` 同步;返回更新后深拷贝 |
218
+ | `updateEdges(edges)` | 按 `id` 局部更新连线(浅合并,未知 id 跳过);经 `edge-update` 事件抛出;返回更新后深拷贝 |
219
+ | `clientToSvg(input, clientY?)` | **鼠标坐标 → SVG 坐标**:参数可传鼠标事件对象(`clientToSvg(e)`)或坐标对(`clientToSvg(clientX, clientY)`);返回 `{ x, y }`——即节点所在坐标系(已自动折算当前缩放与平移),可直接与 `node.x` / `node.y` 运算;SVG 未挂载或矩阵不可用时返回 `null` |
220
+ | `svgToClient(x, y)` | **SVG 坐标 → 屏幕坐标**:输入 `node.x` / `node.y`,返回 `{ x, y }`(屏幕 `clientX/clientY` 口径),常用于把 DOM 浮层(右键菜单 / tooltip 等)精确定位到某个节点;SVG 未挂载时返回 `null` |
216
221
 
217
222
  ```js
218
223
  const topoRef = ref(null);
219
224
 
220
225
  // —— 数据方法 ——
221
- topoRef.value.resetDataBaseline(); // 保存完当前数据后,重置监听基线
226
+ topoRef.value.resetDataBaseline(); // 保存完当前数据后,重置监听基线
222
227
  const { nodes, edges } = topoRef.value.getData(); // 组件外随时主动取数(保存 / 上报前)
223
- topoRef.value.setData({ nodes: nextNodes }); // 外部换一整套数据:先更新你自己的 nodes / edges,再清理组件内痕迹
224
- const removed = topoRef.value.removeSelected(); // 删除选中(删节点级联删除相连线)
228
+ topoRef.value.setData({ nodes: nextNodes }); // 外部换一整套数据:先更新你自己的 nodes / edges,再清理组件内痕迹
229
+ const removed = topoRef.value.removeSelected(); // 删除选中(删节点级联删除相连线)
225
230
  topoRef.value.addNodes([{ label: "新节点", x: 0, y: 0 }]);
226
231
  topoRef.value.addEdges([{ from: "a", to: "b" }]);
227
232
  topoRef.value.updateNodes([{ id: "node-1", label: "改名", width: 120 }]);
package/dist/index.d.ts CHANGED
@@ -28,6 +28,8 @@ export interface TopologyNode {
28
28
  borderWidth?: number;
29
29
  borderStyle?: "dashed" | string;
30
30
  background?: string;
31
+ /** Node box corner radius in px (default 6); applies to the node background, border and selection box. */
32
+ borderRadius?: number;
31
33
  labelPosition?: "top" | "center" | "bottom";
32
34
  [key: string]: unknown;
33
35
  }
@@ -42,6 +44,8 @@ export interface TopologyEdge {
42
44
  to: string;
43
45
  /** flow speed multiplier */
44
46
  speed?: number;
47
+ /** Per-edge animation switch: `false` stops this edge's flow + particle layers (works even when the global fx config is on); `true` / unset follow the global config */
48
+ animated?: boolean;
45
49
  fromAnchor?: "center" | "edge";
46
50
  toAnchor?: "center" | "edge";
47
51
  fromText?: TopologyEdgeLabel;
@@ -93,6 +97,15 @@ export interface TopologyConfig {
93
97
  enableZoom?: boolean;
94
98
  enablePan?: boolean;
95
99
  enableMarquee?: boolean;
100
+ /**
101
+ * Max number of selectable nodes: a non-negative number (0 = nodes cannot
102
+ * be selected, 1 / 2 = at most that many) or "unlimited" / unset = no cap
103
+ * (default). Over-sized gestures only add what fits; lowering the cap
104
+ * trims the selection down to it (first N kept).
105
+ */
106
+ maxSelectNodes?: number | "unlimited";
107
+ /** Same as `maxSelectNodes`, for edges (merged bundles count every edge). */
108
+ maxSelectEdges?: number | "unlimited";
96
109
  showGrid?: boolean;
97
110
  gridColor?: string;
98
111
  /** Grid cell size in px at 1x zoom (default 40); scales with zoom */
@@ -107,7 +120,23 @@ export interface TopologyConfig {
107
120
  showNodeHandles?: boolean;
108
121
  /** Status dot at each node's top-right corner (shown by default) */
109
122
  showStatusDot?: boolean;
123
+ /** Fixed gap (px at 1x zoom) between the node box edge and its label, regardless of node size (default 18). */
124
+ labelGap?: number;
110
125
  watchDataChanges?: boolean;
126
+ /** Effects master switch: false = all off / true = all on / unset = auto by scale */
127
+ fx?: boolean;
128
+ /** Per-feature override — connection-line animation (flow + particles) */
129
+ motionFx?: boolean;
130
+ /** Per-feature override — edge glow filter */
131
+ glowFx?: boolean;
132
+ /** Per-feature override — node breathing ring */
133
+ breatheFx?: boolean;
134
+ /** Edge count above which flow + particle animation auto-disables (default 200) */
135
+ fxMotionMaxEdges?: number;
136
+ /** Edge count above which edge glow auto-disables (default 500) */
137
+ fxGlowMaxEdges?: number;
138
+ /** Node count above which breathing rings auto-disable (default 300) */
139
+ fxBreatheMaxNodes?: number;
111
140
  }
112
141
 
113
142
  export interface TopologyData {
@@ -199,9 +228,6 @@ export declare const nodeStates: Record<
199
228
  { color: string; label: string }
200
229
  >;
201
230
  export declare const nodeColors: Record<string, string>;
202
- export declare const initialNodes: TopologyNode[];
203
- export declare const initialEdges: TopologyEdge[];
204
- export declare const initialGroups: TopologyGroup[];
205
231
 
206
232
  export interface Vue3NetworkGraphPlugin extends Plugin {
207
233
  install(app: App): void;