cladogram 0.5.5-beta.2 → 0.5.5-beta.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/docs/release.md +6 -0
- package/package.json +1 -1
- package/src/agent/runtime.js +1 -1
- package/src/core/media-links.js +165 -11
- package/src/core/parse.js +25 -4
- package/src/html/canvas-ui.css +1 -1
- package/src/html/canvas-wires.js +2 -2
- package/src/html/media-anchors.js +174 -28
- package/src/html/mindmap-connectors.js +12 -10
- package/src/html/runtime.js +7 -4
- package/src/server/mcp.js +6 -1
- package/src/workspace/service.js +56 -19
package/docs/release.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# 版本更新说明
|
|
2
2
|
|
|
3
|
+
## 0.5.5-beta.3 · 对称「行连行」与稳定语义锚点(灰度版)
|
|
4
|
+
|
|
5
|
+
- 画布连线升级为 100% 标准 Markdown 对称表达:行内正文/列表行(`clado:to:line:<锚点>`)与代码块/表格/Mermaid 起点(`;to:line|code|table|mermaid:...`)均可精准落点到目标卡的正文行、代码行、表格数据行或 Mermaid 节点,彻底消除“仅 MCP 可见”的不对称能力。
|
|
6
|
+
- 引入稳定语义锚点与行号漂移兜底:目标正文行按语义短语定位、目标代码行与表格行支持附带语义 `text` 兜底,上方增删行时连线依然准确命中,并在解析阶段新增 `MD_MEDIA_LINK_TARGET` 诊断校验。
|
|
7
|
+
- MCP `clado_links` 全面放开对称 `targetPoint`(`line` / `code` / `table` / `mermaid`)并支持直接搭配行内 `position` 使用;卡片视图与脑图视图双视图一致渲染目标卡左侧精准蓝条、虚线引线与点击闪烁高亮。
|
|
8
|
+
|
|
3
9
|
## 0.5.5-beta.2 · 系统级内置原生 Agent、双协议流式与跟随 AI 视角(灰度版)
|
|
4
10
|
|
|
5
11
|
- 新增系统级内置原生 Agent(`clado.agent`),支持 OpenAI 兼容与 Anthropic 原生双协议真 Token 流式输出(含 Anthropic Prompt Caching 与可折叠 `思考过程` 实时渲染)。
|
package/package.json
CHANGED
package/src/agent/runtime.js
CHANGED
|
@@ -235,7 +235,7 @@ function toolDefinitions() {
|
|
|
235
235
|
type: 'function',
|
|
236
236
|
function: {
|
|
237
237
|
name: 'clado_links',
|
|
238
|
-
description: '建立或断开卡片/内容行连线(action: list|connect|disconnect
|
|
238
|
+
description: '建立或断开卡片/内容行连线(action: list|connect|disconnect;支持对称行连行:起点可用行内 position:{mode:"replace",anchor} 或 sourcePoint,落点可用 targetPoint 指定正文行 line+text / 代码行 code / 表格行 table / Mermaid 节点 mermaid)。',
|
|
239
239
|
parameters: {
|
|
240
240
|
type: 'object',
|
|
241
241
|
properties: {
|
package/src/core/media-links.js
CHANGED
|
@@ -5,8 +5,101 @@ const vendor = { exports: {} };
|
|
|
5
5
|
runInNewContext(readFileSync(new URL('../vendor/marked.umd.js', import.meta.url), 'utf8'), { module: vendor, exports: vendor.exports });
|
|
6
6
|
const { lexer } = vendor.exports;
|
|
7
7
|
|
|
8
|
+
function safeDecode(value) {
|
|
9
|
+
try { return decodeURIComponent(value); } catch { return null; }
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function hasTargetSpec(title) {
|
|
13
|
+
return Boolean(title && (/^clado:to:/.test(title) || /;to:/.test(title)));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function isTargetOnlyTitle(title) {
|
|
17
|
+
return Boolean(title && /^clado:to:/.test(title));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function mediaTargetPoint(title) {
|
|
21
|
+
if (!title || typeof title !== 'string') return null;
|
|
22
|
+
let spec = null;
|
|
23
|
+
if (title.startsWith('clado:to:')) spec = title.slice('clado:to:'.length);
|
|
24
|
+
else {
|
|
25
|
+
const idx = title.indexOf(';to:');
|
|
26
|
+
if (idx < 0) return null;
|
|
27
|
+
spec = title.slice(idx + ';to:'.length);
|
|
28
|
+
}
|
|
29
|
+
if (!spec || /\s/.test(spec)) return null;
|
|
30
|
+
|
|
31
|
+
// 旧版简写:to:<tableIdx>:<row>
|
|
32
|
+
const legacyTable = /^([1-9]\d*):([1-9]\d*)$/.exec(spec);
|
|
33
|
+
if (legacyTable) {
|
|
34
|
+
const table = +legacyTable[1], row = +legacyTable[2];
|
|
35
|
+
if (!Number.isSafeInteger(table) || !Number.isSafeInteger(row)) return null;
|
|
36
|
+
return { kind: 'table', table, row };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 对称表格行:to:table:<tableIdx>:<row>[:<encodedText>]
|
|
40
|
+
const tableMatch = /^table:([1-9]\d*):([1-9]\d*)(?::([^\s;]+))?$/.exec(spec);
|
|
41
|
+
if (tableMatch) {
|
|
42
|
+
const table = +tableMatch[1], row = +tableMatch[2];
|
|
43
|
+
if (!Number.isSafeInteger(table) || !Number.isSafeInteger(row)) return null;
|
|
44
|
+
const text = tableMatch[3] ? safeDecode(tableMatch[3]) : undefined;
|
|
45
|
+
if (tableMatch[3] && (!text || /[\r\n]/.test(text))) return null;
|
|
46
|
+
return { kind: 'table', table, row, ...(text ? { text } : {}) };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 对称代码块行:to:code:<blockIdx>:<line>[:<encodedText>]
|
|
50
|
+
const codeMatch = /^code:([1-9]\d*):([1-9]\d*)(?::([^\s;]+))?$/.exec(spec);
|
|
51
|
+
if (codeMatch) {
|
|
52
|
+
const block = +codeMatch[1], line = +codeMatch[2];
|
|
53
|
+
if (!Number.isSafeInteger(block) || !Number.isSafeInteger(line)) return null;
|
|
54
|
+
const text = codeMatch[3] ? safeDecode(codeMatch[3]) : undefined;
|
|
55
|
+
if (codeMatch[3] && (!text || /[\r\n]/.test(text))) return null;
|
|
56
|
+
return { kind: 'code', block, line, ...(text ? { text } : {}) };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 对称 Mermaid 节点:to:mermaid:<blockIdx>:<nodeId>
|
|
60
|
+
const mermaidMatch = /^mermaid:([1-9]\d*):([\w.-]+)$/.exec(spec);
|
|
61
|
+
if (mermaidMatch) {
|
|
62
|
+
const block = +mermaidMatch[1];
|
|
63
|
+
if (!Number.isSafeInteger(block)) return null;
|
|
64
|
+
return { kind: 'mermaid', block, nodeId: mermaidMatch[2] };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// 稳定语义正文/列表行锚点:to:line:<encodedText>
|
|
68
|
+
const lineMatch = /^line:([^\s;]+)$/.exec(spec);
|
|
69
|
+
if (lineMatch) {
|
|
70
|
+
const text = safeDecode(lineMatch[1]);
|
|
71
|
+
if (!text || !text.trim() || /[\r\n]/.test(text)) return null;
|
|
72
|
+
return { kind: 'line', text };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function formatTargetPoint(point, { standalone = false } = {}) {
|
|
79
|
+
if (!point || !point.kind) return '';
|
|
80
|
+
const prefix = standalone ? 'clado:to:' : 'to:';
|
|
81
|
+
if (point.kind === 'line') {
|
|
82
|
+
return prefix + 'line:' + encodeURIComponent(point.text);
|
|
83
|
+
}
|
|
84
|
+
if (point.kind === 'code') {
|
|
85
|
+
return prefix + 'code:' + point.block + ':' + point.line + (point.text ? ':' + encodeURIComponent(point.text) : '');
|
|
86
|
+
}
|
|
87
|
+
if (point.kind === 'mermaid') {
|
|
88
|
+
return prefix + 'mermaid:' + point.block + ':' + point.nodeId;
|
|
89
|
+
}
|
|
90
|
+
if (point.kind === 'table') {
|
|
91
|
+
if (!standalone && !point.text) return 'to:' + point.table + ':' + point.row;
|
|
92
|
+
return prefix + 'table:' + point.table + ':' + point.row + (point.text ? ':' + encodeURIComponent(point.text) : '');
|
|
93
|
+
}
|
|
94
|
+
return '';
|
|
95
|
+
}
|
|
96
|
+
|
|
8
97
|
export function mediaLinkPoint(title) {
|
|
9
|
-
|
|
98
|
+
if (!title || typeof title !== 'string') return null;
|
|
99
|
+
const toIdx = title.indexOf(';to:');
|
|
100
|
+
if (toIdx >= 0 && !mediaTargetPoint(title)) return null;
|
|
101
|
+
const head = toIdx >= 0 ? title.slice(0, toIdx) : title;
|
|
102
|
+
const match = /^clado:(code|table|mermaid):([^\s;]+)(?:;text:([^\s;]+))?$/.exec(head);
|
|
10
103
|
if (!match) return null;
|
|
11
104
|
const [, kind, value, encodedText] = match;
|
|
12
105
|
if (encodedText && kind !== 'code') return null;
|
|
@@ -16,11 +109,6 @@ export function mediaLinkPoint(title) {
|
|
|
16
109
|
catch { return null; }
|
|
17
110
|
}
|
|
18
111
|
|
|
19
|
-
export function mediaTargetPoint(title) {
|
|
20
|
-
const match = /;to:([1-9]\d*):([1-9]\d*)$/.exec(title || '');
|
|
21
|
-
return match ? { kind: 'table', table: +match[1], row: +match[2] } : null;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
112
|
export function mediaLinkTitle(point) {
|
|
25
113
|
return 'clado:' + point.kind + ':' + (point.line ?? point.row ?? point.nodeId) + (point.text ? ';text:' + encodeURIComponent(point.text) : '');
|
|
26
114
|
}
|
|
@@ -40,7 +128,7 @@ export function codeReferenceOffset(line, reference) {
|
|
|
40
128
|
}
|
|
41
129
|
|
|
42
130
|
export function mediaBlock(token) {
|
|
43
|
-
if (token?.type === 'table') return { kind: 'table', rows: token.rows.length };
|
|
131
|
+
if (token?.type === 'table') return { kind: 'table', rows: token.rows.length, rawRows: token.rows.map(r => r.map(c => c.text).join(' | ')) };
|
|
44
132
|
if (token?.type !== 'code') return null;
|
|
45
133
|
if (/^mermaid(?:\s|$)/i.test(token.lang || '')) return { kind: 'mermaid', source: token.text };
|
|
46
134
|
return { kind: 'code', lines: token.text.split('\n') };
|
|
@@ -51,15 +139,49 @@ export function blockMedia(text) {
|
|
|
51
139
|
return tokens.length === 1 ? mediaBlock(tokens[0]) : null;
|
|
52
140
|
}
|
|
53
141
|
|
|
54
|
-
export function
|
|
142
|
+
export function mediaBlockByIndex(markdown, kind, index) {
|
|
55
143
|
let offset = 0, count = 0;
|
|
56
144
|
for (const token of lexer(markdown)) {
|
|
57
|
-
|
|
145
|
+
const block = mediaBlock(token);
|
|
146
|
+
if (block?.kind === kind && ++count === index) {
|
|
147
|
+
return {
|
|
148
|
+
blockStart: offset,
|
|
149
|
+
blockEnd: offset + token.raw.trimEnd().length,
|
|
150
|
+
media: block,
|
|
151
|
+
...(kind === 'table' ? { rows: block.rows } : {}),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
58
154
|
offset += token.raw.length;
|
|
59
155
|
}
|
|
60
156
|
return null;
|
|
61
157
|
}
|
|
62
158
|
|
|
159
|
+
export function tableBlock(markdown, index) {
|
|
160
|
+
return mediaBlockByIndex(markdown, 'table', index);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function findTargetLineOffset(markdown, needle) {
|
|
164
|
+
if (!markdown || !needle || !needle.trim()) return null;
|
|
165
|
+
const lines = markdown.split('\n');
|
|
166
|
+
let fence = null, offset = 0;
|
|
167
|
+
for (let i = 0; i < lines.length; i++) {
|
|
168
|
+
const line = lines[i];
|
|
169
|
+
const f = /^ {0,3}(`{3,}|~{3,})/.exec(line);
|
|
170
|
+
if (f) {
|
|
171
|
+
if (!fence) fence = f[1];
|
|
172
|
+
else if (f[1][0] === fence[0] && f[1].length >= fence.length) fence = null;
|
|
173
|
+
offset += line.length + 1;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (!fence && line.includes(needle)) {
|
|
177
|
+
const idx = line.indexOf(needle);
|
|
178
|
+
return { lineNumber: i + 1, start: offset + idx, end: offset + idx + needle.length };
|
|
179
|
+
}
|
|
180
|
+
offset += line.length + 1;
|
|
181
|
+
}
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
|
|
63
185
|
export function mediaPointFits(media, point) {
|
|
64
186
|
if (media?.kind !== point?.kind) return false;
|
|
65
187
|
if (point.kind === 'code') {
|
|
@@ -68,7 +190,11 @@ export function mediaPointFits(media, point) {
|
|
|
68
190
|
if (!point.text) return true;
|
|
69
191
|
return codeReferenceOffset(line, point.text) >= 0;
|
|
70
192
|
}
|
|
71
|
-
if (point.kind === 'table')
|
|
193
|
+
if (point.kind === 'table') {
|
|
194
|
+
if (point.row > media.rows) return false;
|
|
195
|
+
if (!point.text) return true;
|
|
196
|
+
return Boolean(media.rawRows?.[point.row - 1]?.includes(point.text));
|
|
197
|
+
}
|
|
72
198
|
const statements = media.source.split(/\n|;/).filter(line => !/^\s*(?:%%|flowchart\b|graph\b|classDef\b|class\s|style\s|linkStyle\b|direction\b|end\s*$)/.test(line));
|
|
73
199
|
return statements.some(line => {
|
|
74
200
|
const graph = line.replace(/"(?:[^"\\]|\\.)*"|\[[^\]\n]*\]|\{[^{}\n]*\}|\([^()\n]*\)|\|[^|\n]*\|/g, ' ');
|
|
@@ -76,9 +202,37 @@ export function mediaPointFits(media, point) {
|
|
|
76
202
|
});
|
|
77
203
|
}
|
|
78
204
|
|
|
205
|
+
export function targetPointFits(markdown, point) {
|
|
206
|
+
if (!point || !point.kind) return false;
|
|
207
|
+
if (point.kind === 'line') return Boolean(findTargetLineOffset(markdown, point.text));
|
|
208
|
+
if (point.kind === 'table') {
|
|
209
|
+
const found = mediaBlockByIndex(markdown, 'table', point.table);
|
|
210
|
+
if (!found) return false;
|
|
211
|
+
if (point.row <= found.media.rows && (!point.text || found.media.rawRows?.[point.row - 1]?.includes(point.text))) return true;
|
|
212
|
+
if (point.text && found.media.rawRows?.some(r => r.includes(point.text))) return true;
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
if (point.kind === 'code') {
|
|
216
|
+
const found = mediaBlockByIndex(markdown, 'code', point.block);
|
|
217
|
+
if (!found) return false;
|
|
218
|
+
if (mediaPointFits(found.media, { kind: 'code', line: point.line, text: point.text })) return true;
|
|
219
|
+
// 语义漂移兜底:若目标代码块行号移动,但带了稳定 text 锚点且在块内唯一匹配,仍视为有效!
|
|
220
|
+
if (point.text) {
|
|
221
|
+
const hits = found.media.lines.filter(line => codeReferenceOffset(line, point.text) >= 0);
|
|
222
|
+
return hits.length === 1;
|
|
223
|
+
}
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
if (point.kind === 'mermaid') {
|
|
227
|
+
const found = mediaBlockByIndex(markdown, 'mermaid', point.block);
|
|
228
|
+
return Boolean(found && mediaPointFits(found.media, { kind: 'mermaid', nodeId: point.nodeId }));
|
|
229
|
+
}
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
|
|
79
233
|
// A standalone annotation follows its media block. Consecutive annotations share that block.
|
|
80
234
|
export function mediaLinkLocations(markdown, links) {
|
|
81
|
-
const annotated = links.filter(link => link.title?.startsWith('clado:'));
|
|
235
|
+
const annotated = links.filter(link => link.title?.startsWith('clado:') && !isTargetOnlyTitle(link.title));
|
|
82
236
|
if (!annotated.length) return new Map();
|
|
83
237
|
const locations = new Map();
|
|
84
238
|
let offset = 0, media = null, blockStart, blockEnd;
|
package/src/core/parse.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import { parse as parseYaml } from 'yaml';
|
|
8
8
|
import { markdownLinks } from './links.js';
|
|
9
|
-
import { mediaLinkLocations } from './media-links.js';
|
|
9
|
+
import { hasTargetSpec, isTargetOnlyTitle, mediaLinkLocations, mediaTargetPoint, targetPointFits } from './media-links.js';
|
|
10
10
|
|
|
11
11
|
/** 开头 `---` 包的 front matter */
|
|
12
12
|
const FRONT_MATTER = /^---[ \t]*\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n|$)/;
|
|
@@ -320,8 +320,17 @@ export function parseDocument(text, opts = {}) {
|
|
|
320
320
|
* 字段,把线连到对方的 `## id` 上才说得清是哪个字段对哪个字段。
|
|
321
321
|
* 键和 targets 一样用链接的**原文**(客户端是拿渲染出来的 href 查表的)。
|
|
322
322
|
*/
|
|
323
|
-
const anchors = Object.create(null), targetNodes = Object.create(null), titleOf = new Map();
|
|
324
|
-
for (const c of cards) {
|
|
323
|
+
const anchors = Object.create(null), targetNodes = Object.create(null), titleOf = new Map(), bodyOf = new Map();
|
|
324
|
+
for (const c of cards) {
|
|
325
|
+
titleOf.set(c.id, c.title);
|
|
326
|
+
bodyOf.set(c.id, c._entry.body.join('\n'));
|
|
327
|
+
const walk = ns => ns.forEach(n => {
|
|
328
|
+
titleOf.set(n.id, n.title);
|
|
329
|
+
bodyOf.set(n.id, n._entry.body.join('\n'));
|
|
330
|
+
walk(n.kids);
|
|
331
|
+
});
|
|
332
|
+
walk(c.nodes);
|
|
333
|
+
}
|
|
325
334
|
const edgePairs = [];
|
|
326
335
|
|
|
327
336
|
/** 从一张卡出发,逐层处理每个节点 */
|
|
@@ -343,7 +352,12 @@ export function parseDocument(text, opts = {}) {
|
|
|
343
352
|
// 围栏里示范用的 `[文字](目标)` 不该在图上长出一条线来。
|
|
344
353
|
for (const link of links) {
|
|
345
354
|
const lineNo = e.line + source.slice(0, link.start).split('\n').length;
|
|
346
|
-
if (link.title
|
|
355
|
+
if (isTargetOnlyTitle(link.title)) {
|
|
356
|
+
if (!mediaTargetPoint(link.title)) {
|
|
357
|
+
at('error', 'MD_MEDIA_LINK_TARGET', '连线落点语法不合法。', '使用 clado:to:line:<锚点文本>、clado:to:code:<块>:<行>、clado:to:table:<表>:<行> 或 clado:to:mermaid:<图>:<节点ID>。', lineNo);
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
} else if (link.title?.startsWith('clado:') && !mediaLocations.get(link.start)?.valid) {
|
|
347
361
|
at('error', 'MD_MEDIA_LINK_SOURCE', '连线起点未匹配到对应代码行、表格行或 Mermaid 节点。', '用 MCP 重新定位到正确的内容块。', lineNo);
|
|
348
362
|
continue;
|
|
349
363
|
}
|
|
@@ -395,6 +409,13 @@ export function parseDocument(text, opts = {}) {
|
|
|
395
409
|
targets[raw] = '';
|
|
396
410
|
continue;
|
|
397
411
|
}
|
|
412
|
+
if (hasTargetSpec(link.title)) {
|
|
413
|
+
const tp = mediaTargetPoint(link.title);
|
|
414
|
+
if (!tp || !targetPointFits(bodyOf.get(got.id) || '', tp)) {
|
|
415
|
+
at('error', 'MD_MEDIA_LINK_TARGET', '连线落点未匹配到目标节点内的对应正文行、代码行、表格行或 Mermaid 节点。', '核对目标卡片中的锚点文本、行号或块编号。', lineNo);
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
398
419
|
const mine = e.level === 1 ? card.id : node.id;
|
|
399
420
|
const theirs = cardOf.get(got.id) || got.id;
|
|
400
421
|
if (got.id === mine && !anchor) continue;
|
package/src/html/canvas-ui.css
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
.markdown-body a.media-jump{position:absolute;z-index:3;width:20px;height:20px;transform:translate(-50%,-50%);border:0;border-radius:50%;background:transparent}
|
|
13
13
|
.media-jump::after{content:'';position:absolute;inset:7px;border:1px solid var(--accent);border-radius:50%;background:var(--panel)}
|
|
14
14
|
.media-jump:hover::after,.media-jump:focus-visible::after{background:var(--accent)}
|
|
15
|
-
.markdown-body tr.media-target-flash>td{background:var(--accent-soft)}
|
|
15
|
+
.markdown-body tr.media-target-flash>td,.markdown-body .media-target-flash:not(tr){background:var(--accent-soft);border-radius:4px}
|
|
16
16
|
.media-stems{position:absolute;inset:0;width:100%;height:100%;overflow:visible;pointer-events:none;z-index:2}
|
|
17
17
|
.media-stems path{fill:none;stroke:var(--accent);stroke-width:1.2;stroke-opacity:.38;stroke-dasharray:3 4;stroke-linecap:round}
|
|
18
18
|
.media-stems circle{fill:var(--panel);stroke:var(--accent);stroke-width:1.2;stroke-opacity:.65}
|
package/src/html/canvas-wires.js
CHANGED
|
@@ -49,7 +49,7 @@ drawWires = function () {
|
|
|
49
49
|
input.dataset.edge = targetId;
|
|
50
50
|
target.append(input);
|
|
51
51
|
canvasMediaStem(input, landing, true);
|
|
52
|
-
} else if (port.dataset.mediaPoint
|
|
52
|
+
} else if (canvasHasTargetSpec(port.dataset.mediaPoint)) return;
|
|
53
53
|
else if (port.dataset.anchor) {
|
|
54
54
|
var heading = $$('.slice-head', target).find(function (h) { return port.dataset.anchorId ? h.closest('.slice').dataset.id === port.dataset.anchorId : h.textContent.trim() === port.dataset.anchor; });
|
|
55
55
|
if (heading) {
|
|
@@ -67,7 +67,7 @@ drawWires = function () {
|
|
|
67
67
|
}
|
|
68
68
|
var from = point ? canvasSourceStem(svg, port, point) : canvasPort(svg, port);
|
|
69
69
|
var d = canvasWirePath(from, canvasPort(svg, input));
|
|
70
|
-
paths.push('<path class="' + (port.dataset.soft ? 'w-soft' : '') + '" data-source-id="' + esc(sourceId) + '" data-target-id="' + esc(targetId) + '"' + (
|
|
70
|
+
paths.push('<path class="' + (port.dataset.soft ? 'w-soft' : '') + '" data-source-id="' + esc(sourceId) + '" data-target-id="' + esc(targetId) + '"' + (port.dataset.mediaPoint ? ' data-media-point="' + esc(port.dataset.mediaPoint) + '"' : '') + ' d="' + d + '"/>');
|
|
71
71
|
});
|
|
72
72
|
svg.innerHTML = paths.join('');
|
|
73
73
|
};
|
|
@@ -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
|
|
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
|
|
78
|
-
var node =
|
|
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
|
|
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
|
|
140
|
-
var
|
|
141
|
-
return
|
|
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
|
|
146
|
-
if (!
|
|
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
|
|
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
|
|
161
|
-
if (!
|
|
162
|
-
|
|
163
|
-
if (mode === 'doc')
|
|
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()
|
|
166
|
-
ty += stage.top + stage.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 () {
|
|
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
|
-
|
|
43
|
-
if (!link.mediaBlock
|
|
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;
|
package/src/html/runtime.js
CHANGED
|
@@ -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
|
-
|
|
317
|
-
if (!a.mediaBlock
|
|
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
|
-
|
|
330
|
-
if (!a.mediaBlock
|
|
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.
|
|
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('父节点标题数组;省略表示一级卡片。'),
|
package/src/workspace/service.js
CHANGED
|
@@ -7,7 +7,7 @@ import { ancestorTemplateContexts, contextFor, discoverWorkspaces, initialize, l
|
|
|
7
7
|
import { allReferences, linkHref, resolveReference, rewriteReferences } from './references.js';
|
|
8
8
|
import { editContent, paragraphs, paragraphLocations, contentLocation } from './content.js';
|
|
9
9
|
import { markdownLinks } from '../core/links.js';
|
|
10
|
-
import { blockMedia, mediaLinkLocations, mediaLinkTitle, mediaPointFits, mediaTargetPoint
|
|
10
|
+
import { blockMedia, findTargetLineOffset, formatTargetPoint, mediaBlockByIndex, mediaLinkLocations, mediaLinkTitle, mediaPointFits, mediaTargetPoint } from '../core/media-links.js';
|
|
11
11
|
import { sortDiagnostics, summarizeDiagnostics } from '../core/diagnostics.js';
|
|
12
12
|
|
|
13
13
|
const copySnapshot = state => ({ ...state, documents: new Map(state.documents), templates: new Map(state.templates) });
|
|
@@ -30,9 +30,28 @@ function locatedReferences(documents) {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
function targetMediaPoint(record, point) {
|
|
33
|
-
|
|
34
|
-
if (
|
|
35
|
-
|
|
33
|
+
if (!record || !point) return null;
|
|
34
|
+
if (point.kind === 'line') {
|
|
35
|
+
const found = findTargetLineOffset(record.node.content, point.text);
|
|
36
|
+
if (!found) return null;
|
|
37
|
+
const loc = contentLocation(record.node.content, found.start, found.end, record.node.location.bodyStartLine);
|
|
38
|
+
return { kind: 'line', text: point.text, paragraph: loc.paragraph, documentLine: loc.startLine };
|
|
39
|
+
}
|
|
40
|
+
const index = point.kind === 'table' ? point.table : point.block;
|
|
41
|
+
const block = mediaBlockByIndex(record.node.content, point.kind, index);
|
|
42
|
+
if (!block) return null;
|
|
43
|
+
if (point.kind === 'table') {
|
|
44
|
+
if (point.row > block.media.rows && !point.text) return null;
|
|
45
|
+
return publicMediaPoint(record, { ...block, point: { kind: 'table', row: point.row, ...(point.text ? { text: point.text } : {}) } });
|
|
46
|
+
}
|
|
47
|
+
if (point.kind === 'code') {
|
|
48
|
+
if (point.line > block.media.lines.length && !point.text) return null;
|
|
49
|
+
return publicMediaPoint(record, { ...block, point: { kind: 'code', line: point.line, ...(point.text ? { text: point.text } : {}) } });
|
|
50
|
+
}
|
|
51
|
+
if (point.kind === 'mermaid') {
|
|
52
|
+
return publicMediaPoint(record, { ...block, point: { kind: 'mermaid', nodeId: point.nodeId } });
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
36
55
|
}
|
|
37
56
|
|
|
38
57
|
function publicMediaPoint(record, media) {
|
|
@@ -63,7 +82,7 @@ function receipt(op, operationIndex, feedback = {}) {
|
|
|
63
82
|
}
|
|
64
83
|
|
|
65
84
|
function resolveResults(state, results) {
|
|
66
|
-
return results.map(({ contentText, contentRange, contentBodyVersion, mediaLinkOnly, ...result }) => {
|
|
85
|
+
return results.map(({ contentText, contentRange, contentBodyVersion, mediaLinkOnly, hasTargetPoint, ...result }) => {
|
|
67
86
|
if (result.kind === 'template') return { ...result, deleted: !state.templates.has(result.file), ...(state.templates.has(result.file) ? { version: versionOf(state.templates.get(result.file)) } : {}) };
|
|
68
87
|
if (result.deleted || !state.documents.has(result.file)) return { ...result, deleted: true };
|
|
69
88
|
const source = state.documents.get(result.file), tree = treeOf(source, result.file), located = { ...result, version: versionOf(source) };
|
|
@@ -79,11 +98,13 @@ function resolveResults(state, results) {
|
|
|
79
98
|
else {
|
|
80
99
|
located.contentLocationStatus = 'exact';
|
|
81
100
|
located.contentLocation = contentLocation(record.node.content, start, start + contentText.length, record.node.location.bodyStartLine);
|
|
82
|
-
if (mediaLinkOnly) {
|
|
101
|
+
if (mediaLinkOnly || hasTargetPoint) {
|
|
83
102
|
const links = markdownLinks(record.node.content);
|
|
84
103
|
const link = links.find(ref => ref.start === start);
|
|
85
|
-
|
|
86
|
-
|
|
104
|
+
if (mediaLinkOnly) {
|
|
105
|
+
const point = mediaLinkLocations(record.node.content, links).get(start);
|
|
106
|
+
if (point?.valid) located.sourcePoint = publicMediaPoint(record, point);
|
|
107
|
+
}
|
|
87
108
|
const targetPoint = mediaTargetPoint(link?.title), target = link && resolveReference(link.target, result.file, state.documents);
|
|
88
109
|
if (targetPoint && target?.path) located.targetPoint = targetMediaPoint(findNode(getTree(state, target.file), target.path), targetPoint);
|
|
89
110
|
}
|
|
@@ -195,8 +216,8 @@ function checkWriteBase(state, op, workspaceGuarded) {
|
|
|
195
216
|
}
|
|
196
217
|
if (!workspaceGuarded && type === 'node.move' && op.index !== undefined && op.targetFile && op.targetFile !== file) fail('跨文档按序号移动需要 expectedWorkspaceVersion。', 400, { code: 'WRITE_BASE_REQUIRED' });
|
|
197
218
|
if (workspaceGuarded || expectedFileVersion !== undefined) return;
|
|
198
|
-
const needsFileVersion = type === 'node.create' && op.index !== undefined || type === 'node.move' && op.index !== undefined || type === 'link.connect' && op.targetPoint;
|
|
199
|
-
if (needsFileVersion) fail('
|
|
219
|
+
const needsFileVersion = type === 'node.create' && op.index !== undefined || type === 'node.move' && op.index !== undefined || type === 'link.connect' && op.targetPoint && op.targetPoint.kind !== 'line';
|
|
220
|
+
if (needsFileVersion) fail('按同级序号或目标段落编号写入,需要 expectedFileVersion。', 400, { code: 'WRITE_BASE_REQUIRED' });
|
|
200
221
|
if (expectedNodeVersion !== undefined) return;
|
|
201
222
|
const safeWithoutVersion = ['document.create', 'document.copy', 'node.create'].includes(type) ||
|
|
202
223
|
type === 'node.update' && position?.anchor !== undefined ||
|
|
@@ -326,9 +347,8 @@ function applyOperation(ctx, state, op) {
|
|
|
326
347
|
const label = op.label || targetPath?.at(-1) || targetTree.doc.title;
|
|
327
348
|
if (/[\r\n\[\]]/.test(label)) fail('label 需要单行文字,不能含方括号。');
|
|
328
349
|
let placement = op.position;
|
|
329
|
-
if (op.
|
|
350
|
+
if ((op.sourcePoint || op.targetPoint) && targetFile !== file) fail('精确线端点要连接同一画布内的节点。');
|
|
330
351
|
if (op.sourcePoint) {
|
|
331
|
-
if (targetFile !== file) fail('精确线端点要连接同一画布内的节点。');
|
|
332
352
|
if (op.position || op.before || op.after) fail('sourcePoint 已经确定位置,独立连线标注不接受 position、before 或 after。');
|
|
333
353
|
const block = paragraphs(record.node.content)[op.sourcePoint.paragraph - 1];
|
|
334
354
|
if (!block || !mediaPointFits(blockMedia(block.text), op.sourcePoint)) fail('指定段落中找不到这个代码行、表格行或 Mermaid 节点。', 422, { code: 'INVALID_MEDIA_SOURCE' });
|
|
@@ -336,11 +356,28 @@ function applyOperation(ctx, state, op) {
|
|
|
336
356
|
}
|
|
337
357
|
let title = op.sourcePoint ? mediaLinkTitle(op.sourcePoint) : '';
|
|
338
358
|
if (op.targetPoint) {
|
|
339
|
-
const
|
|
340
|
-
|
|
341
|
-
if (
|
|
342
|
-
|
|
343
|
-
|
|
359
|
+
const tp = op.targetPoint;
|
|
360
|
+
let formattedTarget = '';
|
|
361
|
+
if (tp.kind === 'line') {
|
|
362
|
+
if (!findTargetLineOffset(targetRecord.node.content, tp.text)) {
|
|
363
|
+
fail('目标节点正文中找不到指定的行锚点文本:' + tp.text, 422, { code: 'INVALID_MEDIA_TARGET' });
|
|
364
|
+
}
|
|
365
|
+
formattedTarget = formatTargetPoint({ kind: 'line', text: tp.text }, { standalone: !op.sourcePoint });
|
|
366
|
+
} else {
|
|
367
|
+
const blocks = paragraphs(targetRecord.node.content).slice(0, tp.paragraph);
|
|
368
|
+
const last = blocks.at(-1);
|
|
369
|
+
if (last?.index !== tp.paragraph || !mediaPointFits(blockMedia(last.text), tp)) {
|
|
370
|
+
fail('目标段落中找不到指定的代码行、表格行或 Mermaid 节点。', 422, { code: 'INVALID_MEDIA_TARGET' });
|
|
371
|
+
}
|
|
372
|
+
const blockIndex = blocks.filter(block => blockMedia(block.text)?.kind === tp.kind).length;
|
|
373
|
+
const spec = tp.kind === 'table'
|
|
374
|
+
? { kind: 'table', table: blockIndex, row: tp.row, ...(tp.text ? { text: tp.text } : {}) }
|
|
375
|
+
: tp.kind === 'code'
|
|
376
|
+
? { kind: 'code', block: blockIndex, line: tp.line, ...(tp.text ? { text: tp.text } : {}) }
|
|
377
|
+
: { kind: 'mermaid', block: blockIndex, nodeId: tp.nodeId };
|
|
378
|
+
formattedTarget = formatTargetPoint(spec, { standalone: !op.sourcePoint });
|
|
379
|
+
}
|
|
380
|
+
title = title ? (title + ';' + formattedTarget) : formattedTarget;
|
|
344
381
|
}
|
|
345
382
|
const linkText = '[' + label + '](<' + href + '>' + (title ? ' "' + title + '"' : '') + ')';
|
|
346
383
|
const text = (op.before || '') + linkText + (op.after || '');
|
|
@@ -351,13 +388,13 @@ function applyOperation(ctx, state, op) {
|
|
|
351
388
|
if (op.sourcePoint && !mediaLinkLocations(edited.content, links).get(expectedStart)?.valid) fail('无法将链接绑定到指定内容。', 422, { code: 'INVALID_MEDIA_SOURCE' });
|
|
352
389
|
checkBody(edited.content);
|
|
353
390
|
state.documents.set(file, replaceNodeBody(tree, record.node, edited.content));
|
|
354
|
-
return { contentText: linkText, contentRange: { start: expectedStart, end: expectedStart + linkText.length }, contentBodyVersion: versionOf(edited.content), placement: placement || { mode: 'append' }, mediaLinkOnly: !!op.sourcePoint };
|
|
391
|
+
return { contentText: linkText, contentRange: { start: expectedStart, end: expectedStart + linkText.length }, contentBodyVersion: versionOf(edited.content), placement: placement || { mode: 'append' }, mediaLinkOnly: !!op.sourcePoint, hasTargetPoint: !!op.targetPoint };
|
|
355
392
|
}
|
|
356
393
|
if (type === 'link.disconnect') {
|
|
357
394
|
const matching = locatedReferences(before).filter(r => r.file === file && samePath(r.node, record.path) && (op.linkIndex === undefined || r.linkIndex === op.linkIndex) && (op.linkIndex !== undefined && op.targetFile === undefined && op.targetNode === undefined || r.target.file === (op.targetFile || file) && samePath(r.target.path, op.targetNode || null)));
|
|
358
395
|
if (!matching.length) fail('找不到这条链接。', 404);
|
|
359
396
|
let content = record.node.content;
|
|
360
|
-
for (const ref of matching.sort((a, b) => b.link.start - a.link.start)) content = content.slice(0, ref.link.start) + (ref.
|
|
397
|
+
for (const ref of matching.sort((a, b) => b.link.start - a.link.start)) content = content.slice(0, ref.link.start) + (ref.sourcePoint ? '' : ref.label) + content.slice(ref.link.end);
|
|
361
398
|
state.documents.set(file, replaceNodeBody(tree, record.node, content)); return;
|
|
362
399
|
}
|
|
363
400
|
fail('不支持的操作:' + type);
|