cladogram 0.5.5-beta.2 → 0.5.5-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,6 +25,50 @@ function canvasCodeLine(code, line) {
25
25
  return null;
26
26
  }
27
27
 
28
+ function canvasDecodeSpec(value) {
29
+ try { return decodeURIComponent(value); } catch { return null; }
30
+ }
31
+
32
+ function canvasParseTargetSpec(detail) {
33
+ if (!detail || typeof detail !== 'string') return null;
34
+ var spec = null;
35
+ if (detail.indexOf('clado:to:') === 0) spec = detail.slice(9);
36
+ else {
37
+ var idx = detail.indexOf(';to:');
38
+ if (idx >= 0) spec = detail.slice(idx + 4);
39
+ }
40
+ if (!spec) return null;
41
+ var legacy = /^([1-9]\d*):([1-9]\d*)$/.exec(spec);
42
+ if (legacy) return { kind: 'table', table: +legacy[1], row: +legacy[2] };
43
+ var table = /^table:([1-9]\d*):([1-9]\d*)(?::([^\s;]+))?$/.exec(spec);
44
+ if (table) return { kind: 'table', table: +table[1], row: +table[2], text: table[3] ? canvasDecodeSpec(table[3]) : null };
45
+ var code = /^code:([1-9]\d*):([1-9]\d*)(?::([^\s;]+))?$/.exec(spec);
46
+ if (code) return { kind: 'code', block: +code[1], line: +code[2], text: code[3] ? canvasDecodeSpec(code[3]) : null };
47
+ var mermaid = /^mermaid:([1-9]\d*):([\w.-]+)$/.exec(spec);
48
+ if (mermaid) return { kind: 'mermaid', block: +mermaid[1], nodeId: mermaid[2] };
49
+ var line = /^line:([^\s;]+)$/.exec(spec);
50
+ if (line) {
51
+ var text = canvasDecodeSpec(line[1]);
52
+ return text ? { kind: 'line', text: text } : null;
53
+ }
54
+ return null;
55
+ }
56
+
57
+ function canvasHasTargetSpec(detail) {
58
+ return !!canvasParseTargetSpec(detail);
59
+ }
60
+
61
+ function canvasFindMermaidNode(diagram, nodeId) {
62
+ var nodes = $$('g.node[id],g.stateGroup[id],g.entityBox[id],rect.entityBox[id],g.actor[id],rect.actor[id]', diagram);
63
+ var node = nodes.find(function (el) {
64
+ var originalId = el.getAttribute('data-id');
65
+ var id = el.id.startsWith(diagram.id + '-') ? el.id.slice(diagram.id.length + 1) : el.id;
66
+ return originalId === nodeId || id === nodeId || id === 'state-' + nodeId ||
67
+ id.replace(/^(?:flowchart|state|entity|classId)-/, '').replace(/-\d+$/, '') === nodeId;
68
+ });
69
+ return { node: node || null, nodes: nodes };
70
+ }
71
+
28
72
  function canvasBindMediaLinks(box) {
29
73
  $$('.zoomable', box).forEach(function (block) {
30
74
  var next = block.nextElementSibling;
@@ -47,10 +91,21 @@ function canvasBindMediaLinks(box) {
47
91
  link.mediaTrigger = trigger;
48
92
  link.mediaBlock.appendChild(trigger);
49
93
  });
94
+ // 绑定行内对称落点链接(clado:to:...),保留正常可见文字,同时清理原生 title 气泡
95
+ $$('a.edge:not(.media-jump)', box).forEach(function (link) {
96
+ if (link.mediaBlock) return;
97
+ var title = link.getAttribute('title');
98
+ if (title && title.indexOf('clado:to:') === 0 && canvasHasTargetSpec(title)) {
99
+ link.dataset.mediaPoint = title;
100
+ link.title = link.textContent;
101
+ }
102
+ });
50
103
  $$('.ztable tbody', box).forEach(function (table) {
51
104
  $$('tr', table).forEach(function (row, index) {
52
105
  $$('a.edge', row).forEach(function (link) {
53
- link.dataset.mediaPoint = 'clado:table:' + (index + 1);
106
+ var existing = link.dataset.mediaPoint;
107
+ var suffix = existing && existing.indexOf('clado:to:') === 0 ? ';' + existing.slice(6) : '';
108
+ link.dataset.mediaPoint = 'clado:table:' + (index + 1) + suffix;
54
109
  link.mediaBlock = table.closest('.ztable');
55
110
  });
56
111
  });
@@ -74,17 +129,12 @@ function canvasMediaSource(link, card) {
74
129
  } else {
75
130
  var diagram = $('.mermaid svg', block);
76
131
  if (!diagram) return null;
77
- var nodes = $$('g.node[id],g.stateGroup[id],g.entityBox[id],rect.entityBox[id],g.actor[id],rect.actor[id]', diagram);
78
- var node = nodes.find(function (el) {
79
- var originalId = el.getAttribute('data-id');
80
- var id = el.id.startsWith(diagram.id + '-') ? el.id.slice(diagram.id.length + 1) : el.id;
81
- return originalId === parts[2] || id === parts[2] || id === 'state-' + parts[2] ||
82
- id.replace(/^(?:flowchart|state|entity|classId)-/, '').replace(/-\d+$/, '') === parts[2];
83
- });
132
+ var found = canvasFindMermaidNode(diagram, parts[2]);
133
+ var node = found.node;
84
134
  if (!node) return null;
85
135
  rect = node.getBoundingClientRect();
86
136
  right = rect.right;
87
- obstacles = nodes.filter(function (other) { return other !== node; }).map(function (other) { return other.getBoundingClientRect(); });
137
+ obstacles = found.nodes.filter(function (other) { return other !== node; }).map(function (other) { return other.getBoundingClientRect(); });
88
138
  }
89
139
  if (!rect || !rect.height) return null;
90
140
  var y = rect.top + rect.height / 2;
@@ -132,41 +182,137 @@ function canvasPlaceMediaJumps(root) {
132
182
  });
133
183
  }
134
184
 
135
- function canvasTargetRow(container, targetId, detail) {
136
- var match = /;to:(\d+):(\d+)$/.exec(detail || '');
137
- if (!match) return null;
185
+ function canvasScopeElements(container, targetId, selector) {
138
186
  var section = $$('.slice[data-id]', container).find(function (el) { return el.dataset.id === targetId; });
139
- var tables = $$('table', section || container).filter(function (table) { return !!section || table.closest('.slice') === (container.matches('.slice') ? container : null); });
140
- var table = tables[+match[1] - 1];
141
- return table && $$('tbody > tr', table)[+match[2] - 1];
187
+ var root = section || container;
188
+ var ownSlice = section || (container.matches('.slice') ? container : null);
189
+ return $$(selector, root).filter(function (el) {
190
+ if (el.closest('.media-reference')) return false;
191
+ var s = el.closest('.slice');
192
+ return s === ownSlice;
193
+ });
194
+ }
195
+
196
+ function canvasFindTextRangeRect(element, needle) {
197
+ var walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT);
198
+ var full = '', nodes = [];
199
+ while (walker.nextNode()) {
200
+ var n = walker.currentNode;
201
+ if (n.parentElement && n.parentElement.closest('.media-reference,pre')) continue;
202
+ nodes.push({ node: n, start: full.length, end: full.length + n.length });
203
+ full += n.nodeValue || '';
204
+ }
205
+ var idx = full.indexOf(needle);
206
+ if (idx < 0) return null;
207
+ var endIdx = idx + needle.length;
208
+ var range = document.createRange();
209
+ var started = false;
210
+ for (var i = 0; i < nodes.length; i++) {
211
+ var entry = nodes[i];
212
+ if (!started && entry.end > idx) {
213
+ range.setStart(entry.node, Math.max(0, idx - entry.start));
214
+ started = true;
215
+ }
216
+ if (started && entry.end >= endIdx) {
217
+ range.setEnd(entry.node, Math.max(0, endIdx - entry.start));
218
+ var rects = Array.from(range.getClientRects()).filter(function (r) { return r.height; });
219
+ return rects[0] || range.getBoundingClientRect();
220
+ }
221
+ }
222
+ return null;
223
+ }
224
+
225
+ function canvasTargetAnchor(container, targetId, detail) {
226
+ var spec = canvasParseTargetSpec(detail);
227
+ if (!spec) return null;
228
+ if (spec.kind === 'table') {
229
+ var tables = canvasScopeElements(container, targetId, 'table');
230
+ var table = tables[spec.table - 1];
231
+ if (!table) return null;
232
+ var rows = $$('tbody > tr', table);
233
+ var row = rows[spec.row - 1];
234
+ if (spec.text && (!row || row.textContent.indexOf(spec.text) < 0)) {
235
+ row = rows.find(function (tr) { return tr.textContent.indexOf(spec.text) >= 0; }) || row;
236
+ }
237
+ if (!row) return null;
238
+ var rect = row.getBoundingClientRect();
239
+ var scrollEl = row.closest('.tscroll');
240
+ var x = scrollEl ? Math.max(rect.left, scrollEl.getBoundingClientRect().left) : rect.left;
241
+ return { x: x, y: rect.top + rect.height / 2, element: row };
242
+ }
243
+ if (spec.kind === 'code') {
244
+ var codes = canvasScopeElements(container, targetId, 'pre > code').filter(function (c) {
245
+ return !c.closest('.mermaid');
246
+ });
247
+ var code = codes[spec.block - 1];
248
+ if (!code) return null;
249
+ var targetLine = spec.line;
250
+ if (spec.text) {
251
+ var lines = code.textContent.split('\n');
252
+ if (!lines[targetLine - 1] || lines[targetLine - 1].indexOf(spec.text) < 0) {
253
+ var foundIdx = lines.findIndex(function (l) { return l.indexOf(spec.text) >= 0; });
254
+ if (foundIdx >= 0) targetLine = foundIdx + 1;
255
+ }
256
+ }
257
+ var lineRect = canvasCodeLine(code, targetLine);
258
+ if (!lineRect || !lineRect.height) return null;
259
+ var preRect = code.parentElement.getBoundingClientRect();
260
+ return { x: preRect.left, y: lineRect.top + lineRect.height / 2, element: code.parentElement };
261
+ }
262
+ if (spec.kind === 'mermaid') {
263
+ var diagrams = canvasScopeElements(container, targetId, '.mermaid svg');
264
+ var diagram = diagrams[spec.block - 1];
265
+ if (!diagram) return null;
266
+ var m = canvasFindMermaidNode(diagram, spec.nodeId);
267
+ if (!m.node) return null;
268
+ var mRect = m.node.getBoundingClientRect();
269
+ if (!mRect.height) return null;
270
+ return { x: mRect.left, y: mRect.top + mRect.height / 2, element: m.node };
271
+ }
272
+ if (spec.kind === 'line') {
273
+ var blocks = canvasScopeElements(container, targetId, 'li,p,blockquote,td,th').filter(function (el) {
274
+ return !el.closest('pre') && el.textContent.indexOf(spec.text) >= 0;
275
+ });
276
+ // 优先取最内层包含该锚点文本的元素(避免外层 li 抢占内层子 li)
277
+ blocks.sort(function (a, b) { return a.textContent.length - b.textContent.length; });
278
+ for (var i = 0; i < blocks.length; i++) {
279
+ var el = blocks[i];
280
+ var r = canvasFindTextRangeRect(el, spec.text);
281
+ if (r && r.height) {
282
+ var elRect = el.getBoundingClientRect();
283
+ return { x: Math.min(r.left, elRect.left), y: r.top + r.height / 2, element: el };
284
+ }
285
+ }
286
+ return null;
287
+ }
288
+ return null;
142
289
  }
143
290
 
144
291
  function canvasMediaTarget(card, targetId, detail) {
145
- var row = canvasTargetRow(card, targetId, detail);
146
- if (!row) return null;
147
- var rect = row.getBoundingClientRect(), scroll = row.closest('.tscroll').getBoundingClientRect();
148
- var x = Math.max(rect.left, scroll.left), y = rect.top + rect.height / 2;
292
+ var hit = canvasTargetAnchor(card, targetId, detail);
293
+ if (!hit) return null;
149
294
  var body = $('.body', card).getBoundingClientRect();
150
- return y >= body.top && y <= body.bottom && x <= body.right ? { x: x, y: y } : null;
295
+ return hit.y >= body.top && hit.y <= body.bottom && hit.x <= body.right ? { x: hit.x, y: hit.y } : null;
151
296
  }
152
297
 
153
298
  function canvasRevealMediaTarget(link, id) {
154
299
  var record = canvasRecords[id];
155
- if (!record || !link.dataset.mediaPoint?.includes(';to:')) return;
300
+ if (!record || !canvasHasTargetSpec(link.dataset.mediaPoint)) return;
156
301
  var mode = document.body.dataset.mode, container;
157
302
  if (mode === 'cards') container = render.card[record.card];
158
303
  else if (mode === 'mindmap') container = $('#mind .mnode[data-path="' + record.mindPath + '"]');
159
304
  else container = $('#article .slice[data-path="' + record.docPath + '"]');
160
- var row = container && canvasTargetRow(container, id, link.dataset.mediaPoint);
161
- if (!row) return;
162
- row.classList.add('media-target-flash');
163
- if (mode === 'doc') row.scrollIntoView({ block: 'center', inline: 'nearest' });
305
+ var hit = container && canvasTargetAnchor(container, id, link.dataset.mediaPoint);
306
+ if (!hit || !hit.element) return;
307
+ hit.element.classList.add('media-target-flash');
308
+ if (mode === 'doc') hit.element.scrollIntoView({ block: 'center', inline: 'nearest' });
164
309
  else {
165
- var stage = $('#stage').getBoundingClientRect(), rect = row.getBoundingClientRect();
166
- ty += stage.top + stage.height / 2 - (rect.top + rect.height / 2);
310
+ var stage = $('#stage').getBoundingClientRect();
311
+ ty += stage.top + stage.height / 2 - hit.y;
167
312
  apply();
313
+ if (mode === 'mindmap') drawMindWires(); else drawWires();
168
314
  }
169
- setTimeout(function () { row.classList.remove('media-target-flash'); }, 1100);
315
+ setTimeout(function () { hit.element.classList.remove('media-target-flash'); }, 1100);
170
316
  }
171
317
 
172
318
  function canvasSourceStem(svg, port, sourcePoint) {
@@ -39,18 +39,20 @@ function canvasDrawMindWires() {
39
39
  var anchored = Object.keys(nodes).find(function (key) { return key.indexOf(parentPath + '.') === 0 && nodes[key].record.title === link.dataset.anchor; });
40
40
  if (anchored) target = nodes[anchored];
41
41
  }
42
- if (target === source || (!link.mediaBlock && seen[target.path])) return;
43
- if (!link.mediaBlock) seen[target.path] = true;
42
+ var dedupKey = target.path + '|' + (link.dataset.mediaPoint || '');
43
+ if (target === source || (!link.mediaBlock && seen[dedupKey])) return;
44
+ if (!link.mediaBlock) seen[dedupKey] = true;
45
+ var landing = link.dataset.mediaPoint ? canvasMediaTarget(target.el, target.record.id, link.dataset.mediaPoint) : null;
46
+ var to;
47
+ if (landing) {
48
+ var input = document.createElement('span'); input.className = 'hbar incoming-anchor';
49
+ input.style.top = (canvasLocalPoint(svg, landing.x, landing.y).y - target.bounds.top - 10) + 'px';
50
+ input.dataset.mediaPoint = link.dataset.mediaPoint; input.dataset.sourceId = source.record.id; input.dataset.edge = target.record.id;
51
+ target.el.append(input); canvasMediaStem(input, landing, true); to = canvasPort(svg, input);
52
+ } else if (canvasHasTargetSpec(link.dataset.mediaPoint)) return;
44
53
  if (link.mediaBlock) {
45
54
  var point = canvasMediaSource(link, source.el);
46
55
  if (!point) return;
47
- var landing = canvasMediaTarget(target.el, target.record.id, link.dataset.mediaPoint), to;
48
- if (landing) {
49
- var input = document.createElement('span'); input.className = 'hbar incoming-anchor';
50
- input.style.top = (canvasLocalPoint(svg, landing.x, landing.y).y - target.bounds.top - 10) + 'px';
51
- input.dataset.mediaPoint = link.dataset.mediaPoint; input.dataset.sourceId = source.record.id; input.dataset.edge = target.record.id;
52
- target.el.append(input); canvasMediaStem(input, landing, true); to = canvasPort(svg, input);
53
- } else if (link.dataset.mediaPoint.includes(';to:')) return;
54
56
  var origin = canvasLocalPoint(svg, point.x, point.exitY);
55
57
  var port = output(source, origin.y, true);
56
58
  port.dataset.mediaPoint = link.dataset.mediaPoint;
@@ -62,7 +64,7 @@ function canvasDrawMindWires() {
62
64
  }
63
65
  var r = link.getBoundingClientRect(), p = canvasLocalPoint(svg, r.left, r.top + r.height / 2);
64
66
  if (!r.width || !r.height) return;
65
- connect(source, target, canvasPort(svg, output(source, p.y, true)), true, 'relation');
67
+ connect(source, target, canvasPort(svg, output(source, p.y, true)), true, 'relation', link.dataset.mediaPoint || '', to);
66
68
  });
67
69
  var children = Object.keys(nodes).filter(function (key) { return key.slice(0, key.lastIndexOf('.')) === path; });
68
70
  if (!children.length) return;
@@ -296,6 +296,7 @@
296
296
  p.className = soft ? 'port soft' : 'port';
297
297
  p.dataset.edge = a.dataset.edge;
298
298
  if (point) { p.dataset.mediaPoint = a.dataset.mediaPoint; p.mediaLink = a; }
299
+ else if (a.dataset.mediaPoint) { p.dataset.mediaPoint = a.dataset.mediaPoint; }
299
300
  var section = a.closest('.slice');
300
301
  p.dataset.sourceId = section ? section.dataset.id : cardEl.dataset.id;
301
302
  if (a.dataset.anchor) p.dataset.anchor = a.dataset.anchor;
@@ -313,8 +314,9 @@
313
314
  var seen = {};
314
315
  $$('a.edge:not(.media-jump)', sl).forEach(function (a) {
315
316
  var to = a.dataset.edge;
316
- if (!to || (!a.mediaBlock && seen[to])) return;
317
- if (!a.mediaBlock) seen[to] = 1;
317
+ var dedupKey = to + '|' + (a.dataset.anchorId || a.dataset.anchor || '') + '|' + (a.dataset.mediaPoint || '');
318
+ if (!to || (!a.mediaBlock && seen[dedupKey])) return;
319
+ if (!a.mediaBlock) seen[dedupKey] = 1;
318
320
  put(a, !!a.dataset.anchor);
319
321
  });
320
322
  });
@@ -326,8 +328,9 @@
326
328
  $$('a.edge:not(.media-jump)', body).forEach(function (a) {
327
329
  if (a.closest('.slice')) return;
328
330
  var to = a.dataset.edge;
329
- if (!to || (!a.mediaBlock && seen2[to])) return;
330
- if (!a.mediaBlock) seen2[to] = 1;
331
+ var dedupKey = to + '|' + (a.dataset.anchorId || a.dataset.anchor || '') + '|' + (a.dataset.mediaPoint || '');
332
+ if (!to || (!a.mediaBlock && seen2[dedupKey])) return;
333
+ if (!a.mediaBlock) seen2[dedupKey] = 1;
331
334
  put(a);
332
335
  });
333
336
  }
package/src/server/mcp.js CHANGED
@@ -23,7 +23,12 @@ const sourcePoint = z.discriminatedUnion('kind', [
23
23
  z.object({ kind: z.literal('table'), paragraph: indexFromOne, row: indexFromOne }).strict(),
24
24
  z.object({ kind: z.literal('mermaid'), paragraph: indexFromOne, nodeId: z.string().regex(/^[\w.-]+$/) }).strict()
25
25
  ]).optional().describe('同画布连线的起点。paragraph 来自 nodes.read;line 是代码块内行,空行计数;row 是表格数据行,不含表头;nodeId 是 Mermaid 节点 ID。行号从1开始。');
26
- const targetPoint = z.object({ kind: z.literal('table'), paragraph: indexFromOne, row: indexFromOne }).strict().optional().describe('可选表格落点,位于 targetNode 的正文;paragraph/row 从1开始,row 不含表头。与 sourcePoint 一起使用。');
26
+ const targetPoint = z.discriminatedUnion('kind', [
27
+ z.object({ kind: z.literal('line'), text: z.string().min(1).max(200).regex(/^[^\r\n]*\S[^\r\n]*$/).describe('目标节点正文或列表行中的稳定语义锚点文本,行号变动时仍可精准落点。') }).strict(),
28
+ z.object({ kind: z.literal('code'), paragraph: indexFromOne, line: indexFromOne, text: z.string().min(1).max(200).regex(/^[^\r\n]*\S[^\r\n]*$/).optional().describe('可选代码行内稳定语义锚点,行号微调时自动定位。') }).strict(),
29
+ z.object({ kind: z.literal('table'), paragraph: indexFromOne, row: indexFromOne, text: z.string().min(1).max(200).regex(/^[^\r\n]*\S[^\r\n]*$/).optional() }).strict(),
30
+ z.object({ kind: z.literal('mermaid'), paragraph: indexFromOne, nodeId: z.string().regex(/^[\w.-]+$/) }).strict(),
31
+ ]).optional().describe('同画布连线的精确落点,位于 targetNode 正文;支持正文/列表行(line+text)、代码行(code)、表格行(table)或 Mermaid 节点(mermaid),可与行内链接 position 或 sourcePoint 任意对称组合。');
27
32
  const nodeFields = {
28
33
  file: z.string().optional().describe('相对 documentRoot 的路径,支持 .md(推荐)与 .mdc。'),
29
34
  node: path().optional(), parent: path().optional().describe('父节点标题数组;省略表示一级卡片。'),