@cheemsup/dsh-plugin-console 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +95 -3
  3. package/cordis.patch.yml +5 -0
  4. package/icon.svg +203 -0
  5. package/lib/client.activity.js +222 -0
  6. package/lib/client.apply.js +171 -0
  7. package/lib/client.changes.js +206 -0
  8. package/lib/client.cleanup.js +270 -0
  9. package/lib/client.control.js +247 -0
  10. package/lib/client.dialog.js +77 -0
  11. package/lib/client.disable-preview.js +904 -0
  12. package/lib/client.draft.js +280 -0
  13. package/lib/client.editor-messages.js +421 -0
  14. package/lib/client.editor-store.js +266 -0
  15. package/lib/client.editor.js +433 -0
  16. package/lib/client.effects.js +115 -0
  17. package/lib/client.impact-tree.js +487 -0
  18. package/lib/client.influence.js +361 -0
  19. package/lib/client.js +246 -0
  20. package/lib/client.page.js +646 -0
  21. package/lib/client.plugin-filters.js +62 -0
  22. package/lib/client.plugins.js +506 -0
  23. package/lib/client.preview-messages.js +506 -0
  24. package/lib/client.preview-raw.js +132 -0
  25. package/lib/client.preview-store.js +128 -0
  26. package/lib/client.preview-tools.js +194 -0
  27. package/lib/client.preview.js +448 -0
  28. package/lib/client.relation-graph.js +364 -0
  29. package/lib/client.schema.js +327 -0
  30. package/lib/client.search.js +409 -0
  31. package/lib/client.session-detail.js +503 -0
  32. package/lib/client.sessions.js +495 -0
  33. package/lib/client.shared.js +562 -0
  34. package/lib/client.store.js +452 -0
  35. package/lib/client.styles.js +589 -0
  36. package/lib/client.template-card.js +240 -0
  37. package/lib/client.tools.js +366 -0
  38. package/lib/client.workspace-picker.js +204 -0
  39. package/lib/host/activity.js +252 -0
  40. package/lib/host/assembly-preview.js +244 -0
  41. package/lib/host/attribution.js +341 -0
  42. package/lib/host/catalog.js +133 -0
  43. package/lib/host/categorize.js +40 -0
  44. package/lib/host/config-layers.js +589 -0
  45. package/lib/host/constants.js +69 -0
  46. package/lib/host/control.js +159 -0
  47. package/lib/host/disable-preview.js +651 -0
  48. package/lib/host/entries.js +289 -0
  49. package/lib/host/fiber.js +86 -0
  50. package/lib/host/fs.js +99 -0
  51. package/lib/host/health.js +178 -0
  52. package/lib/host/http.js +65 -0
  53. package/lib/host/impact.js +138 -0
  54. package/lib/host/injection-owners.js +60 -0
  55. package/lib/host/layers.js +132 -0
  56. package/lib/host/masks.js +145 -0
  57. package/lib/host/modes.js +96 -0
  58. package/lib/host/names.js +44 -0
  59. package/lib/host/patch-config.js +554 -0
  60. package/lib/host/patch-scan.js +103 -0
  61. package/lib/host/preview-conditions.js +281 -0
  62. package/lib/host/preview-contexts.js +174 -0
  63. package/lib/host/preview-envelope.js +31 -0
  64. package/lib/host/preview-instructions.js +450 -0
  65. package/lib/host/preview-messages.js +284 -0
  66. package/lib/host/preview-modules.js +233 -0
  67. package/lib/host/preview-skills.js +200 -0
  68. package/lib/host/prompt.js +213 -0
  69. package/lib/host/recipe-overlay.js +209 -0
  70. package/lib/host/request-preview.js +114 -0
  71. package/lib/host/routes/activation.js +96 -0
  72. package/lib/host/routes/activity.js +22 -0
  73. package/lib/host/routes/apply.js +93 -0
  74. package/lib/host/routes/cleanup.js +62 -0
  75. package/lib/host/routes/control.js +22 -0
  76. package/lib/host/routes/disable-preview.js +19 -0
  77. package/lib/host/routes/impact.js +19 -0
  78. package/lib/host/routes/operations.js +22 -0
  79. package/lib/host/routes/preview.js +58 -0
  80. package/lib/host/routes/session-influence.js +23 -0
  81. package/lib/host/routes/snapshot.js +21 -0
  82. package/lib/host/routes/template.js +54 -0
  83. package/lib/host/routes/templates.js +28 -0
  84. package/lib/host/scope-impact.js +125 -0
  85. package/lib/host/session-events.js +39 -0
  86. package/lib/host/session-influence.js +467 -0
  87. package/lib/host/sessions.js +293 -0
  88. package/lib/host/snapshot.js +218 -0
  89. package/lib/host/source-scan.js +225 -0
  90. package/lib/host/templates/cleanup.js +219 -0
  91. package/lib/host/templates/inheritance.js +144 -0
  92. package/lib/host/templates/materialize.js +317 -0
  93. package/lib/host/templates/message-overlay.js +184 -0
  94. package/lib/host/templates/repository.js +323 -0
  95. package/lib/host/templates/store.js +131 -0
  96. package/lib/host/tool-set.js +150 -0
  97. package/lib/host/tools.js +129 -0
  98. package/lib/index.js +106 -0
  99. package/locale/en.json +6 -0
  100. package/locale/zh.json +6 -0
  101. package/package.json +124 -6
@@ -0,0 +1,487 @@
1
+ /**
2
+ * 禁用预演的上下游关系树:纯计算,不碰 React。
3
+ *
4
+ * 为什么是树而不是三列清单:插件的依赖可以往下走很多层(一个服务有多个消费者,
5
+ * 消费者又提供别的服务),扁平的「上游 / 目标 / 下游」清单表达不了层级,也容易
6
+ * 把「第二层受影响」误读成「直接依赖」。这里按行 id 去重、按深度截断,环路只画
7
+ * 一条回边,不重复展开。
8
+ *
9
+ * 图只做「谁依赖谁」与「谁覆盖谁」两种边;节点上的数量与状态由视图从快照补,
10
+ * 不在模型里重复存一份。
11
+ * @module dsh-plugin-console/client-impact-tree
12
+ */
13
+
14
+ window.__ModuleLoader__.load({
15
+ id: '@cheemsup/dsh-plugin-console',
16
+ chunk: 'client.impact-tree.js',
17
+ factory: () => {
18
+ /** 上游默认展示两层(目标 → 直接依赖 → 再下一层);下游按 host 闭包完整展开。 */
19
+ const DEFAULT_DEPTH = 2
20
+
21
+ /** entryId / rowId 双向索引。 */
22
+ function indexEntries(entries) {
23
+ const byEntryId = new Map()
24
+ const byRowId = new Map()
25
+ for (const entry of entries) {
26
+ byEntryId.set(entry.entryId, entry)
27
+ const list = byRowId.get(entry.rowId) ?? []
28
+ list.push(entry)
29
+ byRowId.set(entry.rowId, list)
30
+ }
31
+ return { byEntryId, byRowId }
32
+ }
33
+
34
+ /** 节点展示名;快照里找不到条目时退回原始 id。 */
35
+ function titleOf(entry, fallback) {
36
+ if (entry === undefined) return String(fallback)
37
+ return entry.title || entry.packageName || entry.moduleName || entry.entryId || String(fallback)
38
+ }
39
+
40
+ /** 树的最大深度(两侧各算一次)。 */
41
+ function maxDepthOf(nodes) {
42
+ let depth = 0
43
+ for (const node of nodes) {
44
+ depth = Math.max(depth, node.depth, maxDepthOf(node.children ?? []))
45
+ }
46
+ return depth
47
+ }
48
+
49
+ /**
50
+ * 建关系树。
51
+ *
52
+ * @param {object} snapshot - 当前快照。
53
+ * @param {object} target - `data.target`(至少含 rowId / entryId)。
54
+ * @param {object} [options] - `{ depth, maxNodes, full, serviceOnly }`;`full` 表示上下游都递归到底。
55
+ * @returns {{root: object | null, upstream: Array<object>, downstream: Array<object>, truncated: boolean, notes: string[]}} 关系树。
56
+ */
57
+ function buildRelationTree(snapshot, target, options = {}) {
58
+ const full = options.full === true
59
+ const depthLimit = full ? Infinity : Math.min(4, Math.max(1, Number.isInteger(options.depth) ? options.depth : DEFAULT_DEPTH))
60
+ // 不传 maxNodes 表示不设总预算:下游必须能完整画出闭包里的每一个插件。
61
+ // 显式传入时仍按旧口径截断,供需要的调用方保留概览模式。
62
+ const maxNodes = Number.isInteger(options.maxNodes) ? Math.max(8, options.maxNodes) : Infinity
63
+ const entries = Array.isArray(snapshot?.entries) ? snapshot.entries : []
64
+ const services = snapshot?.services ?? {}
65
+ const index = indexEntries(entries)
66
+ const rootEntry = target?.entryId === undefined ? undefined : index.byEntryId.get(target.entryId)
67
+ if (rootEntry === undefined && (target?.rowId === undefined || target?.rowId === null)) {
68
+ return { root: null, upstream: [], downstream: [], truncated: false, notes: ['找不到目标条目'] }
69
+ }
70
+ const rootId = target.rowId ?? rootEntry?.rowId
71
+ const root = {
72
+ key: 'root',
73
+ rowId: rootId,
74
+ entryId: target.entryId ?? rootEntry?.entryId,
75
+ title: target.title ?? titleOf(rootEntry, rootId),
76
+ moduleName: target.moduleName ?? rootEntry?.moduleName,
77
+ side: 'root',
78
+ depth: 0,
79
+ edgeKind: null,
80
+ edgeLabel: null,
81
+ children: [],
82
+ }
83
+ const notes = []
84
+ let count = 1
85
+ let truncated = false
86
+ let sequence = 0
87
+
88
+ /** 新建节点;`again` 是「已经在上方出现过」的叶子。 */
89
+ const makeNode = (entry, side, depth, edgeKind, edgeLabel, fallback, again) => {
90
+ sequence++
91
+ return {
92
+ key: `${side}#${sequence}`,
93
+ rowId: entry?.rowId ?? fallback,
94
+ entryId: entry?.entryId ?? fallback,
95
+ title: titleOf(entry, fallback),
96
+ moduleName: entry?.moduleName ?? null,
97
+ side,
98
+ depth,
99
+ edgeKind,
100
+ edgeLabel,
101
+ children: [],
102
+ again: again === true,
103
+ }
104
+ }
105
+ const canAdd = () => {
106
+ if (count < maxNodes) return true
107
+ truncated = true
108
+ return false
109
+ }
110
+
111
+ /** 两侧各自去重:同一个行只画一次,避免 DAG 被摊成重复节点。 */
112
+ const seenUp = new Set([rootId])
113
+ const seenDown = new Set([rootId])
114
+
115
+ /** 上游:目标需要的服务由谁提供,再顺着提供者的需求往上走。 */
116
+ const walkUp = (entry, depth, ancestors) => {
117
+ const children = []
118
+ for (const service of entry?.requires ?? []) {
119
+ for (const providerId of services[service]?.providers ?? []) {
120
+ if (!canAdd()) return children
121
+ const provider = index.byEntryId.get(providerId)
122
+ const rowId = provider?.rowId ?? providerId
123
+ if (ancestors.has(rowId)) {
124
+ children.push(makeNode(provider, 'upstream', depth, 'service', service, rowId, true))
125
+ count++
126
+ continue
127
+ }
128
+ if (seenUp.has(rowId)) continue
129
+ const node = makeNode(provider, 'upstream', depth, 'service', service, rowId, false)
130
+ seenUp.add(rowId)
131
+ count++
132
+ children.push(node)
133
+ if (depth < depthLimit) {
134
+ node.children = walkUp(provider ?? { requires: [] }, depth + 1, new Set([...ancestors, rowId]))
135
+ }
136
+ }
137
+ }
138
+ return children
139
+ }
140
+
141
+ /** 下游:目标提供的服务被谁消费,再顺着消费者的提供项往下走。 */
142
+ const walkDown = (entry, depth, ancestors) => {
143
+ const children = []
144
+ for (const service of entry?.provides ?? []) {
145
+ for (const consumerId of services[service]?.consumers ?? []) {
146
+ if (!canAdd()) return children
147
+ const consumer = index.byEntryId.get(consumerId)
148
+ const rowId = consumer?.rowId ?? consumerId
149
+ if (ancestors.has(rowId)) {
150
+ children.push(makeNode(consumer, 'downstream', depth, 'service', service, rowId, true))
151
+ count++
152
+ continue
153
+ }
154
+ if (seenDown.has(rowId)) continue
155
+ const node = makeNode(consumer, 'downstream', depth, 'service', service, rowId, false)
156
+ seenDown.add(rowId)
157
+ count++
158
+ children.push(node)
159
+ if (depth < depthLimit) {
160
+ node.children = walkDown(consumer ?? { provides: [] }, depth + 1, new Set([...ancestors, rowId]))
161
+ }
162
+ }
163
+ }
164
+ return children
165
+ }
166
+
167
+ const upstreamRoots = walkUp(rootEntry ?? { requires: target.requires ?? [] }, 1, new Set([rootId]))
168
+ if (!full && upstreamRoots.length > 0) notes.push(`影响图聚焦与选定插件最相邻的插件以及下游插件`)
169
+ const chainRows = full ? [] : (Array.isArray(options.chain?.blocked) ? options.chain.blocked : [])
170
+ // 不传 chainDepth 表示下游不截断:用户要看到这次禁用波及的全部插件。
171
+ const graphChainDepth = Number.isInteger(options.chainDepth) ? options.chainDepth : Infinity
172
+ let downstreamRoots
173
+
174
+ if (chainRows.length > 0) {
175
+ // 完整依赖链模式:下游只画「真的会不可用」的条目,层级与路径都来自 host
176
+ // 的闭包结果。分组子行是距离 0 的起点,先放进来,后续链路可以挂在它们下面。
177
+ downstreamRoots = []
178
+ const nodesById = new Map()
179
+ for (const child of options.groupChildren ?? []) {
180
+ if (!canAdd()) break
181
+ const entry = index.byEntryId.get(child.entryId)
182
+ const node = makeNode(
183
+ entry ?? { rowId: child.rowId, entryId: child.entryId, title: child.title, moduleName: null },
184
+ 'downstream', 1, 'group-child', '同组子行', child.rowId, false,
185
+ )
186
+ node.entryId = child.entryId
187
+ node.distance = 0
188
+ count++
189
+ downstreamRoots.push(node)
190
+ nodesById.set(child.entryId, node)
191
+ }
192
+ let depthCut = false
193
+ for (const row of chainRows) {
194
+ if (row.distance > graphChainDepth) {
195
+ depthCut = true
196
+ break
197
+ }
198
+ if (!canAdd()) break
199
+ const entry = index.byEntryId.get(row.entryId)
200
+ const node = makeNode(
201
+ entry ?? { rowId: row.rowId, entryId: row.entryId, title: row.title, moduleName: row.moduleName },
202
+ 'downstream', row.distance, 'blocked', row.causeService ?? '同组', row.rowId, false,
203
+ )
204
+ node.entryId = row.entryId
205
+ node.blocked = true
206
+ node.distance = row.distance
207
+ node.missingServices = Array.isArray(row.missingServices) ? row.missingServices : []
208
+ count++
209
+ const parent = typeof row.viaEntryId === 'string' && row.viaEntryId !== root.entryId
210
+ ? nodesById.get(row.viaEntryId)
211
+ : undefined
212
+ if (parent !== undefined) parent.children.push(node)
213
+ else downstreamRoots.push(node)
214
+ nodesById.set(row.entryId, node)
215
+ }
216
+ if (depthCut) notes.push(`链条较深:图只画到第 ${graphChainDepth} 层,完整链路见「影响链路」。`)
217
+ } else {
218
+ downstreamRoots = walkDown(rootEntry ?? { provides: target.provides ?? [] }, 1, new Set([rootId]))
219
+
220
+ if (options.serviceOnly !== true) {
221
+ // 配置引用:config 值里**完全等于**目标 id / 模块名 / 包名的行。
222
+ // 不做子串匹配——那会把 `dsh-tool-a-extra` 误算成 `dsh-tool-a` 的引用。
223
+ const identifiers = new Set(
224
+ [target.rowId, target.entryId, target.moduleName, target.packageName].filter((name) => typeof name === 'string' && name !== ''),
225
+ )
226
+ for (const entry of entries) {
227
+ if (entry.entryId === root.entryId) continue
228
+ if (seenDown.has(entry.rowId)) continue
229
+ const hit = (entry.configEntries ?? []).find((row) => identifiers.has(row.value))
230
+ if (hit === undefined) continue
231
+ if (!canAdd()) break
232
+ count++
233
+ seenDown.add(entry.rowId)
234
+ downstreamRoots.push(makeNode(entry, 'downstream', 1, 'config-reference', hit.key, entry.rowId, false))
235
+ }
236
+
237
+ // 同组子行:按 Loader id 前缀推断;条件禁用与动态挂载可能不同,界面会写明。
238
+ if (typeof root.entryId === 'string') {
239
+ for (const entry of entries) {
240
+ if (!entry.entryId.startsWith(root.entryId + ':')) continue
241
+ if (seenDown.has(entry.rowId)) continue
242
+ if (!canAdd()) break
243
+ count++
244
+ seenDown.add(entry.rowId)
245
+ downstreamRoots.push(makeNode(entry, 'downstream', 1, 'group-child', '同组子行', entry.rowId, false))
246
+ }
247
+ }
248
+ }
249
+ }
250
+
251
+ if (full) notes.push('完整链路按 provides/requires 声明展开')
252
+ if (truncated) notes.push(`关系图最多展示 ${maxNodes} 个节点,其余上下游已省略。`)
253
+ return { root, upstream: upstreamRoots, downstream: downstreamRoots, truncated, notes }
254
+ }
255
+
256
+ /**
257
+ * 以选中节点为中心,递归展开完整的上下游依赖链。
258
+ *
259
+ * 与 {@link buildRelationTree} 的区别:不按受影响状态筛选下游,也不只看上游 2 层;
260
+ * 两侧都沿 `provides` / `requires` 声明走到没有更多提供者或消费者为止。
261
+ * @param {object} snapshot - 当前快照。
262
+ * @param {object} target - `data.target`。
263
+ * @param {object} [options] - 透传的选项。
264
+ * @returns {object} 同 `buildRelationTree` 的树。
265
+ */
266
+ function buildFullChain(snapshot, target, options = {}) {
267
+ return buildRelationTree(snapshot, target, { ...options, full: true, serviceOnly: true })
268
+ }
269
+
270
+ /**
271
+ * 一条边:父节点在靠近目标的一侧,子节点在更外侧。
272
+ *
273
+ * 箭头统一表示「提供者 → 消费者」:下游是目标(或上游提供者)指向下游消费者;
274
+ * 上游是更外侧的提供者指向靠近目标、需要该服务的消费者,所以上游箭头落在
275
+ * 父节点一端,而不是继续朝外。
276
+ */
277
+ function edgeBetween(parent, child) {
278
+ const upstream = child.side === 'upstream'
279
+ const fromNode = upstream ? child : parent
280
+ const toNode = upstream ? parent : child
281
+ const fromX = fromNode.x + fromNode.width
282
+ const toX = toNode.x
283
+ const y1 = fromNode.y + fromNode.height / 2
284
+ const y2 = toNode.y + toNode.height / 2
285
+ const direction = Math.sign(toX - fromX) || 1
286
+ const span = Math.max(20, Math.abs(toX - fromX) / 2)
287
+ return {
288
+ key: fromNode.key + '>' + toNode.key,
289
+ fromKey: fromNode.key,
290
+ toKey: toNode.key,
291
+ kind: child.edgeKind,
292
+ label: child.edgeLabel,
293
+ d: `M${fromX} ${y1} C${fromX + direction * span} ${y1} ${toX - direction * span} ${y2} ${toX} ${y2}`,
294
+ arrow: `${toX},${y2} ${toX - direction * 6},${y2 - 3.5} ${toX - direction * 6},${y2 + 3.5}`,
295
+ labelX: (fromX + toX) / 2,
296
+ labelY: (y1 + y2) / 2 - 5,
297
+ }
298
+ }
299
+
300
+ /**
301
+ * 估算一个字符占几格:汉字/全角按 2,其余按 1。
302
+ * SVG 没有自动换行,只能按近似宽度自己折行。
303
+ */
304
+ function charWidth(char) {
305
+ return /[⺀-鿿豈-﫿＀-￯]/.test(char) ? 2 : 1
306
+ }
307
+
308
+ /**
309
+ * 按节点宽度折行,最多 `maxLines` 行,再长才省略。
310
+ * 节点左右各留 10px;12px 字号下一个西文字符约 6.6px,约合 1 格。
311
+ * @param {string} title - 插件名。
312
+ * @param {number} maxLines - 最多显示几行。
313
+ * @param {number} width - 节点宽度(px)。
314
+ * @returns {string[]} 每行的文字。
315
+ */
316
+ function wrapGraphTitle(title, maxLines, width) {
317
+ const perLine = Math.max(8, Math.floor((width - 20) / 6.6))
318
+ const lines = []
319
+ let line = ''
320
+ let units = 0
321
+ for (const char of String(title ?? '')) {
322
+ const size = charWidth(char)
323
+ if (line !== '' && units + size > perLine) {
324
+ lines.push(line)
325
+ line = ''
326
+ units = 0
327
+ }
328
+ line += char
329
+ units += size
330
+ }
331
+ if (line !== '') lines.push(line)
332
+ if (lines.length <= maxLines) return lines
333
+ // 超出最后一行的部分省略:给「…」留一格。
334
+ let kept = ''
335
+ let used = 0
336
+ for (const char of lines[maxLines - 1]) {
337
+ const size = charWidth(char)
338
+ if (used + size > perLine - 2) break
339
+ kept += char
340
+ used += size
341
+ }
342
+ return [...lines.slice(0, maxLines - 1), kept + '…']
343
+ }
344
+
345
+ /**
346
+ * 固定几何的树布局。
347
+ *
348
+ * 目标居中,上游向左侧按深度分列,下游向右侧分列;每侧按后序给叶子排 y,
349
+ * 父节点取首末子节点的中点。没有力导向、没有随机数——同一份数据永远得到
350
+ * 同一张图,便于眼动定位与自检。
351
+ * @param {object} tree - `buildRelationTree()` 的结果。
352
+ * @param {object} [options] - `{ nodeWidth, nodeHeight, gapX, gapY, padding }`。
353
+ * @returns {{nodes: Array<object>, edges: Array<object>, width: number, height: number}} 布局。
354
+ */
355
+ function layoutRelationTree(tree, options = {}) {
356
+ // 方块只放插件名(状态收进悬浮提示),所以把空间都给文字:两行名称、
357
+ // 更宽的行宽。放大查看交给视图层的缩放,这里仍保持固定几何。
358
+ const nodeWidth = options.nodeWidth ?? 240
359
+ const nodeHeight = options.nodeHeight ?? 64
360
+ const gapX = options.gapX ?? 96
361
+ const gapY = options.gapY ?? 14
362
+ const padding = options.padding ?? 8
363
+ if (tree?.root === null || tree?.root === undefined) {
364
+ return { nodes: [], edges: [], width: 0, height: 0 }
365
+ }
366
+
367
+ const upDepth = maxDepthOf(tree.upstream ?? [])
368
+ const downDepth = maxDepthOf(tree.downstream ?? [])
369
+ const rootX = padding + upDepth * (nodeWidth + gapX)
370
+ const nodes = []
371
+ const edges = []
372
+
373
+ /** 递归排一侧;`cursor` 是对象,保证同一个引用在整棵树里累加。 */
374
+ const assign = (node, side, cursor) => {
375
+ const childLayouts = (node.children ?? []).map((child) => assign(child, side, cursor))
376
+ let y
377
+ if (childLayouts.length > 0) {
378
+ y = (childLayouts[0].y + childLayouts[childLayouts.length - 1].y) / 2
379
+ } else {
380
+ y = padding + cursor.value * (nodeHeight + gapY)
381
+ cursor.value++
382
+ }
383
+ const layout = {
384
+ key: node.key,
385
+ rowId: node.rowId,
386
+ title: node.title,
387
+ moduleName: node.moduleName,
388
+ side,
389
+ depth: node.depth,
390
+ x: side === 'upstream' ? rootX - node.depth * (nodeWidth + gapX) : rootX + node.depth * (nodeWidth + gapX),
391
+ y,
392
+ width: nodeWidth,
393
+ height: nodeHeight,
394
+ edgeKind: node.edgeKind,
395
+ edgeLabel: node.edgeLabel,
396
+ again: node.again === true,
397
+ blocked: node.blocked === true,
398
+ missingServices: node.missingServices ?? [],
399
+ distance: node.distance ?? node.depth,
400
+ hasChildren: childLayouts.length > 0,
401
+ }
402
+ nodes.push(layout)
403
+ for (const child of childLayouts) edges.push(edgeBetween(layout, child))
404
+ return layout
405
+ }
406
+
407
+ const upCursor = { value: 0 }
408
+ const downCursor = { value: 0 }
409
+ const upstreamLayouts = (tree.upstream ?? []).map((node) => assign(node, 'upstream', upCursor))
410
+ const downstreamLayouts = (tree.downstream ?? []).map((node) => assign(node, 'downstream', downCursor))
411
+
412
+ const lowest = nodes.reduce((value, node) => Math.max(value, node.y), 0)
413
+ const rootY = lowest === 0 ? padding : padding + (lowest - padding) / 2
414
+ const root = {
415
+ key: 'root',
416
+ rowId: tree.root.rowId,
417
+ title: tree.root.title,
418
+ moduleName: tree.root.moduleName,
419
+ side: 'root',
420
+ depth: 0,
421
+ x: rootX,
422
+ y: rootY,
423
+ width: nodeWidth,
424
+ height: nodeHeight,
425
+ edgeKind: null,
426
+ edgeLabel: null,
427
+ again: false,
428
+ hasChildren: upstreamLayouts.length + downstreamLayouts.length > 0,
429
+ }
430
+ for (const layout of [...upstreamLayouts, ...downstreamLayouts]) edges.push(edgeBetween(root, layout))
431
+ nodes.push(root)
432
+
433
+ return {
434
+ nodes,
435
+ edges,
436
+ width: rootX + downDepth * (nodeWidth + gapX) + nodeWidth + padding,
437
+ height: Math.max(lowest, rootY) + nodeHeight + padding,
438
+ }
439
+ }
440
+
441
+ /**
442
+ * 摊平成节点列表;视图用它统计上下游客数与受阻节点数。
443
+ * @param {object} tree - `buildRelationTree()` 的结果。
444
+ * @returns {Array<object>} 深度优先的节点列表。
445
+ */
446
+ function flattenRelationTree(tree) {
447
+ const rows = []
448
+ if (tree?.root === null || tree?.root === undefined) return rows
449
+ rows.push({
450
+ key: 'root',
451
+ rowId: tree.root.rowId,
452
+ title: tree.root.title,
453
+ side: 'root',
454
+ depth: 0,
455
+ edgeKind: null,
456
+ edgeLabel: null,
457
+ again: false,
458
+ blocked: false,
459
+ missingServices: [],
460
+ distance: 0,
461
+ hasChildren: (tree.upstream?.length ?? 0) + (tree.downstream?.length ?? 0) > 0,
462
+ })
463
+ const walk = (node) => {
464
+ rows.push({
465
+ key: node.key,
466
+ rowId: node.rowId,
467
+ title: node.title,
468
+ side: node.side,
469
+ depth: node.depth,
470
+ edgeKind: node.edgeKind,
471
+ edgeLabel: node.edgeLabel,
472
+ again: node.again === true,
473
+ blocked: node.blocked === true,
474
+ missingServices: node.missingServices ?? [],
475
+ distance: node.distance ?? node.depth,
476
+ hasChildren: (node.children?.length ?? 0) > 0,
477
+ })
478
+ for (const child of node.children ?? []) walk(child)
479
+ }
480
+ for (const node of tree.upstream ?? []) walk(node)
481
+ for (const node of tree.downstream ?? []) walk(node)
482
+ return rows
483
+ }
484
+
485
+ return { buildRelationTree, buildFullChain, layoutRelationTree, flattenRelationTree, wrapGraphTitle }
486
+ },
487
+ })