cladogram 0.6.0-beta.2 → 0.6.0-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.
- package/bin/clado.js +34 -2
- package/docs/agent-context.md +62 -0
- package/docs/agent-reference.md +126 -0
- package/docs/release.md +15 -0
- package/package.json +3 -1
- package/plugins/agent/index.js +974 -124
- package/plugins/agent/style.css +502 -52
- package/src/agent/clado-tools.js +437 -103
- package/src/agent/pi-bridge.js +588 -43
- package/src/agent/pi-config.js +411 -35
- package/src/html/app-css.js +65 -1
- package/src/html/canvas-chain.js +26 -2
- package/src/html/canvas-focus.js +19 -13
- package/src/html/canvas-live-diff.js +1256 -0
- package/src/html/canvas-ui.css +508 -0
- package/src/html/canvas-wires.js +5 -1
- package/src/html/page.js +4 -2
- package/src/html/runtime-api.js +146 -10
- package/src/html/runtime.js +432 -29
- package/src/server/agent.js +55 -8
- package/src/server/assets/library-canvas.js +252 -9
- package/src/server/assets/library.css +416 -0
- package/src/server/assets/library.html +2 -0
- package/src/server/assets/library.js +744 -14
- package/src/server/code-inspect.js +183 -0
- package/src/server/mcp.js +4 -4
- package/src/server/workspace.js +266 -19
- package/src/workspace/graph-snapshot.js +695 -0
- package/src/workspace/service.js +11 -1
package/src/server/agent.js
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
deletePiSkill,
|
|
18
18
|
deleteProviderConfig,
|
|
19
19
|
deleteRole,
|
|
20
|
+
ensureCladoPlatformDocs,
|
|
20
21
|
importPiMcpJson,
|
|
21
22
|
inspectPiEcosystem,
|
|
22
23
|
installPiPackage,
|
|
@@ -24,15 +25,19 @@ import {
|
|
|
24
25
|
listRoles,
|
|
25
26
|
readAgentsMemory,
|
|
26
27
|
readCanvasComponentsConfig,
|
|
28
|
+
readRuntimeSettings,
|
|
27
29
|
removePiPackage,
|
|
30
|
+
resetCladoPlatformDocs,
|
|
28
31
|
resolvePiAgentDir,
|
|
29
32
|
resolveRole,
|
|
30
33
|
saveAgentsMemory,
|
|
34
|
+
saveCladoPlatformContext,
|
|
31
35
|
savePiMcpServer,
|
|
32
36
|
savePiPrompt,
|
|
33
37
|
savePiSkill,
|
|
34
38
|
saveProviderConfig,
|
|
35
39
|
saveRole,
|
|
40
|
+
saveRuntimeSettings,
|
|
36
41
|
setDefaultPiModel,
|
|
37
42
|
toggleCanvasComponent,
|
|
38
43
|
togglePiMcpServer,
|
|
@@ -117,11 +122,20 @@ function sendJson(res, status, data) {
|
|
|
117
122
|
res.end(JSON.stringify(data));
|
|
118
123
|
}
|
|
119
124
|
|
|
120
|
-
function assertSameOriginPost(req) {
|
|
121
|
-
const
|
|
122
|
-
if (
|
|
123
|
-
|
|
125
|
+
function assertSameOriginPost(req, remote = false) {
|
|
126
|
+
const origin = req.headers.origin;
|
|
127
|
+
if (!origin) return;
|
|
128
|
+
const host = String(req.headers.host || '').trim();
|
|
129
|
+
if (origin === 'http://' + host) return;
|
|
130
|
+
if (remote) {
|
|
131
|
+
const forwardedHost = String(req.headers['x-forwarded-host'] || '').split(',')[0].trim();
|
|
132
|
+
const allowed = new Set([host]);
|
|
133
|
+
if (forwardedHost) allowed.add(forwardedHost);
|
|
134
|
+
for (const h of allowed) {
|
|
135
|
+
if (h && (origin === 'http://' + h || origin === 'https://' + h)) return;
|
|
136
|
+
}
|
|
124
137
|
}
|
|
138
|
+
throw new DocumentError('来源不匹配。', 403);
|
|
125
139
|
}
|
|
126
140
|
|
|
127
141
|
export function createAgentHandler({
|
|
@@ -131,6 +145,7 @@ export function createAgentHandler({
|
|
|
131
145
|
workspaceService = null,
|
|
132
146
|
events = null,
|
|
133
147
|
sessionFactory = null,
|
|
148
|
+
remote = false,
|
|
134
149
|
}) {
|
|
135
150
|
const bridge = createPiBridge({
|
|
136
151
|
workspaceService,
|
|
@@ -156,8 +171,10 @@ export function createAgentHandler({
|
|
|
156
171
|
listProvidersAndModels(rt.workspace, agentDir),
|
|
157
172
|
inspectPiEcosystem(rt.workspace, agentDir),
|
|
158
173
|
]);
|
|
174
|
+
const runtimeSettings = readRuntimeSettings(rt.workspace, agentDir, modelsInfo.activeContextWindow);
|
|
159
175
|
const roles = listRoles(agentDir);
|
|
160
176
|
const memory = readAgentsMemory(rt.workspace);
|
|
177
|
+
const platformDocs = ensureCladoPlatformDocs(agentDir);
|
|
161
178
|
const canvasComponents = readCanvasComponentsConfig(agentDir);
|
|
162
179
|
const sessions = listSessions(rt.stateDir, { includeArchived: true, agentDir });
|
|
163
180
|
|
|
@@ -170,6 +187,9 @@ export function createAgentHandler({
|
|
|
170
187
|
defaultProvider: modelsInfo.defaultProvider,
|
|
171
188
|
defaultModel: modelsInfo.defaultModel,
|
|
172
189
|
activeModelLabel: modelsInfo.activeModelLabel,
|
|
190
|
+
activeContextWindow: modelsInfo.activeContextWindow,
|
|
191
|
+
activeMaxTokens: modelsInfo.activeMaxTokens,
|
|
192
|
+
runtimeSettings,
|
|
173
193
|
providers: modelsInfo.providers,
|
|
174
194
|
configuredModels: modelsInfo.configuredModels,
|
|
175
195
|
roles,
|
|
@@ -177,6 +197,7 @@ export function createAgentHandler({
|
|
|
177
197
|
canvasComponents,
|
|
178
198
|
workspacePlugins: rt.pluginDescriptors,
|
|
179
199
|
memory,
|
|
200
|
+
platformDocs,
|
|
180
201
|
sessions,
|
|
181
202
|
};
|
|
182
203
|
}
|
|
@@ -206,7 +227,7 @@ export function createAgentHandler({
|
|
|
206
227
|
}
|
|
207
228
|
|
|
208
229
|
if (req.method === 'POST') {
|
|
209
|
-
assertSameOriginPost(req);
|
|
230
|
+
assertSameOriginPost(req, remote);
|
|
210
231
|
const body = await readJsonBody(req);
|
|
211
232
|
if (body?.action === 'rename') {
|
|
212
233
|
const updated = renameSession(rt.stateDir, body.id, body.title, agentDir);
|
|
@@ -223,6 +244,16 @@ export function createAgentHandler({
|
|
|
223
244
|
sendJson(res, 200, result);
|
|
224
245
|
return;
|
|
225
246
|
}
|
|
247
|
+
if (body?.action === 'compact') {
|
|
248
|
+
const result = await bridge.compactSession({
|
|
249
|
+
workspace: rt.workspace,
|
|
250
|
+
stateDir: rt.stateDir,
|
|
251
|
+
sessionId: body.id,
|
|
252
|
+
customInstructions: body.instructions || '',
|
|
253
|
+
});
|
|
254
|
+
sendJson(res, 200, result);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
226
257
|
const created = upsertSessionMeta(rt.stateDir, {
|
|
227
258
|
id: body.id ? safeSessionId(body.id) : undefined,
|
|
228
259
|
title: body.title,
|
|
@@ -234,7 +265,7 @@ export function createAgentHandler({
|
|
|
234
265
|
}
|
|
235
266
|
|
|
236
267
|
if (req.method === 'DELETE') {
|
|
237
|
-
assertSameOriginPost(req);
|
|
268
|
+
assertSameOriginPost(req, remote);
|
|
238
269
|
const id = url.searchParams.get('id');
|
|
239
270
|
if (!id) throw new DocumentError('缺少会话 ID。', 400);
|
|
240
271
|
await bridge.cancel(rt.workspace, id);
|
|
@@ -256,7 +287,7 @@ export function createAgentHandler({
|
|
|
256
287
|
}
|
|
257
288
|
|
|
258
289
|
if (req.method !== 'POST') throw new DocumentError('仅支持 GET 或 POST。', 405);
|
|
259
|
-
assertSameOriginPost(req);
|
|
290
|
+
assertSameOriginPost(req, remote);
|
|
260
291
|
const body = await readJsonBody(req, 256 * 1024);
|
|
261
292
|
const action = String(body?.action || '').trim();
|
|
262
293
|
|
|
@@ -276,6 +307,12 @@ export function createAgentHandler({
|
|
|
276
307
|
sendJson(res, 200, { ...result, meta: await buildFullMeta(targetWorkspace) });
|
|
277
308
|
return;
|
|
278
309
|
}
|
|
310
|
+
if (action === 'save_runtime_settings') {
|
|
311
|
+
const modelsInfo = await listProvidersAndModels(rt.workspace, agentDir);
|
|
312
|
+
const runtimeSettings = await saveRuntimeSettings(body, rt.workspace, agentDir, modelsInfo.activeContextWindow);
|
|
313
|
+
sendJson(res, 200, { ok: true, runtimeSettings, meta: await buildFullMeta(targetWorkspace) });
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
279
316
|
if (action === 'save_role') {
|
|
280
317
|
const role = saveRole(body, agentDir);
|
|
281
318
|
sendJson(res, 200, { ok: true, role, meta: await buildFullMeta(targetWorkspace) });
|
|
@@ -341,6 +378,16 @@ export function createAgentHandler({
|
|
|
341
378
|
sendJson(res, 200, { ...result, meta: await buildFullMeta(targetWorkspace) });
|
|
342
379
|
return;
|
|
343
380
|
}
|
|
381
|
+
if (action === 'save_platform_context') {
|
|
382
|
+
const result = saveCladoPlatformContext(body.content, agentDir);
|
|
383
|
+
sendJson(res, 200, { ...result, meta: await buildFullMeta(targetWorkspace) });
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
if (action === 'reset_platform_context') {
|
|
387
|
+
const result = resetCladoPlatformDocs(agentDir);
|
|
388
|
+
sendJson(res, 200, { ...result, meta: await buildFullMeta(targetWorkspace) });
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
344
391
|
if (action === 'toggle_canvas_component') {
|
|
345
392
|
const canvasComponents = toggleCanvasComponent(body.id, body.enabled, agentDir);
|
|
346
393
|
sendJson(res, 200, { ok: true, canvasComponents, meta: await buildFullMeta(targetWorkspace) });
|
|
@@ -355,7 +402,7 @@ export function createAgentHandler({
|
|
|
355
402
|
|
|
356
403
|
async handle(req, res, targetWorkspace) {
|
|
357
404
|
if (req.method !== 'POST') throw new DocumentError('仅支持 POST。', 405);
|
|
358
|
-
assertSameOriginPost(req);
|
|
405
|
+
assertSameOriginPost(req, remote);
|
|
359
406
|
const raw = await readJsonBody(req);
|
|
360
407
|
const parsed = agentRequestSchema.safeParse(raw);
|
|
361
408
|
if (!parsed.success) throw new DocumentError('Agent 请求参数不合法。', 400);
|
|
@@ -44,7 +44,16 @@
|
|
|
44
44
|
if (selectionTarget(event)) { event.preventDefault(); event.stopImmediatePropagation(); }
|
|
45
45
|
}, true);
|
|
46
46
|
const world = doc.querySelector('#world');
|
|
47
|
-
|
|
47
|
+
let paintRaf = 0;
|
|
48
|
+
new MutationObserver(records => {
|
|
49
|
+
if (!paths.length || paintRaf) return;
|
|
50
|
+
const relevant = records.some(r => {
|
|
51
|
+
const t = r.target;
|
|
52
|
+
return t && t.closest && t.id !== 'wires' && t.id !== 'mwires' && !t.closest('#wires,#mwires,.port,.clado-obj');
|
|
53
|
+
});
|
|
54
|
+
if (!relevant) return;
|
|
55
|
+
paintRaf = requestAnimationFrame(() => { paintRaf = 0; if (paths.length) paint(doc, api); });
|
|
56
|
+
}).observe(world, { childList: true, subtree: true });
|
|
48
57
|
paint(doc, api);
|
|
49
58
|
}
|
|
50
59
|
function bindChain() {
|
|
@@ -73,13 +82,13 @@
|
|
|
73
82
|
return window.__CLADO_RESOURCES__?.[path] || (window.__CLADO_OFFLINE__ ? null : '/library/resource?file='+encodeURIComponent(path)+workspace);
|
|
74
83
|
}
|
|
75
84
|
function rewriteDocumentLinks() {
|
|
76
|
-
document.querySelectorAll('img[src]:not([data-resource])').forEach(
|
|
77
|
-
const src=
|
|
78
|
-
try { const url=resourceUrl(src);if(url){
|
|
85
|
+
document.querySelectorAll('img[src]:not([data-resource]),video[src]:not([data-resource]),audio[src]:not([data-resource])').forEach(el=>{
|
|
86
|
+
const src=el.getAttribute('src');if (!src || /^(?:[a-z][a-z0-9+.-]*:|\/\/|\/)/i.test(src))return;
|
|
87
|
+
try { const url=resourceUrl(src);if(url){el.dataset.resource='1';el.src=url;} } catch {}
|
|
79
88
|
});
|
|
80
89
|
document.querySelectorAll('a[href]:not(.edge):not([data-resource])').forEach(a=>{
|
|
81
90
|
const href=a.getAttribute('href');if (!href || /^(?:[a-z][a-z0-9+.-]*:|\/\/|\/|#)/i.test(href) || /\.mdc?(?:#|$)/i.test(href))return;
|
|
82
|
-
if (!/\.(?:png|jpe?g|gif|webp|svg|pdf|txt|csv|json)(?:#|$)/i.test(href))return;
|
|
91
|
+
if (!/\.(?:png|jpe?g|gif|webp|svg|pdf|txt|csv|json|mp4|webm|mov|ogv|mp3|wav|m4a|ogg|flac)(?:#|$)/i.test(href))return;
|
|
83
92
|
try { const url=resourceUrl(href);if(url){a.dataset.resource='1';a.href=url;} } catch {}
|
|
84
93
|
});
|
|
85
94
|
document.querySelectorAll('a[href]:not(.edge):not([data-canvas-file])').forEach(a => {
|
|
@@ -94,7 +103,16 @@
|
|
|
94
103
|
a.removeAttribute('target');
|
|
95
104
|
});
|
|
96
105
|
}
|
|
97
|
-
|
|
106
|
+
let rewriteRaf = 0;
|
|
107
|
+
new MutationObserver(records => {
|
|
108
|
+
if (rewriteRaf) return;
|
|
109
|
+
const relevant = records.some(r => {
|
|
110
|
+
const t = r.target;
|
|
111
|
+
return t && t.closest && t.id !== 'wires' && t.id !== 'mwires' && !t.closest('#wires,#mwires,.port,.clado-obj');
|
|
112
|
+
});
|
|
113
|
+
if (!relevant) return;
|
|
114
|
+
rewriteRaf = requestAnimationFrame(() => { rewriteRaf = 0; rewriteDocumentLinks(); });
|
|
115
|
+
}).observe(document.querySelector('#world'), { childList: true, subtree: true });
|
|
98
116
|
rewriteDocumentLinks();
|
|
99
117
|
async function settle() {
|
|
100
118
|
await new Promise(resolve => requestAnimationFrame(resolve));
|
|
@@ -351,6 +369,69 @@
|
|
|
351
369
|
});
|
|
352
370
|
}
|
|
353
371
|
|
|
372
|
+
const CODE_REF_RE = /^(?:[\w./-]+\.(?:js|ts|tsx|jsx|mjs|cjs|py|go|rs|java|kt|cpp|c|h|rb|php|cs|vue|svelte|css|html|json|yaml|yml|toml|sql)|[A-Za-z_]\w*(?:#|\.)[A-Za-z_]\w*)$/;
|
|
373
|
+
|
|
374
|
+
function extractCodeRef(text) {
|
|
375
|
+
const raw = String(text || '').trim();
|
|
376
|
+
if (!raw) return '';
|
|
377
|
+
if (CODE_REF_RE.test(raw)) return raw;
|
|
378
|
+
const m = raw.match(/(?:[\w./-]+\.(?:js|ts|tsx|jsx|mjs|cjs|py|go|rs|java|kt|cpp|c|h|rb|php|cs|vue|svelte|css|html|json)|[A-Za-z_]\w*#[A-Za-z_]\w*)/);
|
|
379
|
+
return m ? m[0] : '';
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function resolveCodeBlockLine(targetEl, clientY) {
|
|
383
|
+
const pre = targetEl?.closest?.('pre');
|
|
384
|
+
const code = pre?.querySelector?.('code');
|
|
385
|
+
if (!pre || !code) return null;
|
|
386
|
+
const container = targetEl.closest('.slice[data-id]') || targetEl.closest('#world > .card');
|
|
387
|
+
if (!container) return null;
|
|
388
|
+
const pres = Array.from(container.querySelectorAll('pre')).filter(p => !p.closest('.slice') || p.closest('.slice') === container);
|
|
389
|
+
const paragraph = Math.max(1, pres.indexOf(pre) + 1);
|
|
390
|
+
const lines = String(code.textContent || '').replace(/\r\n?/g, '\n').split('\n');
|
|
391
|
+
const rect = code.getBoundingClientRect();
|
|
392
|
+
const cs = getComputedStyle(code);
|
|
393
|
+
const lh = parseFloat(cs.lineHeight) || (parseFloat(cs.fontSize) || 12) * 1.5;
|
|
394
|
+
let line = 1;
|
|
395
|
+
if (rect.height > 0 && lines.length > 1) {
|
|
396
|
+
const approx = Math.floor((clientY - rect.top) / Math.max(10, lh)) + 1;
|
|
397
|
+
line = Math.max(1, Math.min(lines.length, approx));
|
|
398
|
+
}
|
|
399
|
+
return { paragraph, line, lineText: (lines[line - 1] || '').trim().slice(0, 80) };
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function resolveMermaidNode(targetEl) {
|
|
403
|
+
const mmd = targetEl?.closest?.('.mermaid');
|
|
404
|
+
if (!mmd) return null;
|
|
405
|
+
const container = targetEl.closest('.slice[data-id]') || targetEl.closest('#world > .card');
|
|
406
|
+
if (!container) return null;
|
|
407
|
+
const mmds = Array.from(container.querySelectorAll('.mermaid'));
|
|
408
|
+
const paragraph = Math.max(1, mmds.indexOf(mmd) + 1);
|
|
409
|
+
const g = targetEl.closest('g.node, g.cluster');
|
|
410
|
+
if (!g) return null;
|
|
411
|
+
const rawId = String(g.id || g.getAttribute('data-id') || '').trim();
|
|
412
|
+
const cleanId = rawId.replace(/^flowchart-/, '').replace(/-\d+$/, '') || rawId;
|
|
413
|
+
const label = String(g.textContent || '').replace(/\s+/g, ' ').trim().slice(0, 60);
|
|
414
|
+
if (!cleanId && !label) return null;
|
|
415
|
+
return { paragraph, nodeId: cleanId || label, nodeLabel: label || cleanId };
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function getCardTextSelection() {
|
|
419
|
+
const sel = window.getSelection?.();
|
|
420
|
+
if (!sel || sel.isCollapsed || !sel.rangeCount) return null;
|
|
421
|
+
const text = sel.toString().replace(/\s+/g, ' ').trim();
|
|
422
|
+
if (!text || text.length > 240) return null;
|
|
423
|
+
const range = sel.getRangeAt(0);
|
|
424
|
+
const ancestor = range.commonAncestorContainer;
|
|
425
|
+
const el = ancestor.nodeType === 1 ? ancestor : ancestor.parentElement;
|
|
426
|
+
if (!el || el.closest('input, textarea, [contenteditable="true"], .clado-link-pick-banner, .clado-sel-quickbar')) return null;
|
|
427
|
+
const mdBody = el.closest('.markdown-body');
|
|
428
|
+
const cardOrSlice = el.closest('.slice[data-id]') || el.closest('#world > .card') || el.closest('#article .slice[data-path]');
|
|
429
|
+
if (!mdBody || !cardOrSlice) return null;
|
|
430
|
+
const node = runtime.selectionPath?.(cardOrSlice);
|
|
431
|
+
if (!node || !node.length) return null;
|
|
432
|
+
return { text, node, rect: range.getBoundingClientRect(), el: cardOrSlice };
|
|
433
|
+
}
|
|
434
|
+
|
|
354
435
|
function resolveContextTarget(targetEl, clientX, clientY) {
|
|
355
436
|
const { wx, wy } = clientToWorld(clientX, clientY);
|
|
356
437
|
const subTrack = targetEl?.closest?.('[data-subtarget="track"]');
|
|
@@ -373,16 +454,72 @@
|
|
|
373
454
|
const obj = currentObjects.find(o => o.id === objEl.dataset.objId);
|
|
374
455
|
if (obj) return { kind: 'object', object: obj, wx, wy };
|
|
375
456
|
}
|
|
457
|
+
const selHit = getCardTextSelection();
|
|
458
|
+
if (selHit) {
|
|
459
|
+
return {
|
|
460
|
+
kind: 'text-selection',
|
|
461
|
+
node: selHit.node,
|
|
462
|
+
selectedText: selHit.text,
|
|
463
|
+
title: selHit.node[selHit.node.length - 1] || '选中文本',
|
|
464
|
+
label: '「' + selHit.text.slice(0, 28) + '」',
|
|
465
|
+
codeRef: extractCodeRef(selHit.text),
|
|
466
|
+
version: window.__CLADO_FILE_VERSION__,
|
|
467
|
+
wx,
|
|
468
|
+
wy,
|
|
469
|
+
};
|
|
470
|
+
}
|
|
376
471
|
const cardEl = targetEl?.closest?.('#world > .card');
|
|
377
472
|
if (cardEl && document.body.dataset.mode === 'cards') {
|
|
378
|
-
const
|
|
473
|
+
const sliceEl = targetEl?.closest?.('.slice[data-id]');
|
|
474
|
+
const path = runtime.selectionPath?.(sliceEl || cardEl);
|
|
475
|
+
const cardPath = runtime.selectionPath?.(cardEl);
|
|
379
476
|
const cardX = parseFloat(cardEl.style.left) || wx;
|
|
380
477
|
const cardY = parseFloat(cardEl.style.top) || wy;
|
|
381
478
|
const cardW = cardEl.offsetWidth || 360;
|
|
479
|
+
const title = path?.[path.length - 1] || cardEl.querySelector('.card-title h1')?.textContent || '卡片';
|
|
480
|
+
|
|
481
|
+
const codeHit = resolveCodeBlockLine(targetEl, clientY);
|
|
482
|
+
if (codeHit && path) {
|
|
483
|
+
return {
|
|
484
|
+
kind: 'code-line',
|
|
485
|
+
node: path,
|
|
486
|
+
title,
|
|
487
|
+
line: codeHit.line,
|
|
488
|
+
lineText: codeHit.lineText,
|
|
489
|
+
sourcePoint: { kind: 'code', paragraph: codeHit.paragraph, line: codeHit.line },
|
|
490
|
+
label: `${title} · 代码第 ${codeHit.line} 行`,
|
|
491
|
+
codeRef: extractCodeRef(codeHit.lineText) || extractCodeRef(title),
|
|
492
|
+
version: window.__CLADO_FILE_VERSION__,
|
|
493
|
+
wx: Math.round(cardX + cardW + 24),
|
|
494
|
+
wy: Math.round(cardY + 12),
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
const mmdHit = resolveMermaidNode(targetEl);
|
|
499
|
+
if (mmdHit && path) {
|
|
500
|
+
return {
|
|
501
|
+
kind: 'mermaid-node',
|
|
502
|
+
node: path,
|
|
503
|
+
title,
|
|
504
|
+
nodeId: mmdHit.nodeId,
|
|
505
|
+
nodeLabel: mmdHit.nodeLabel,
|
|
506
|
+
sourcePoint: { kind: 'mermaid', paragraph: mmdHit.paragraph, node: mmdHit.nodeId },
|
|
507
|
+
label: `图节点 · ${mmdHit.nodeLabel}`,
|
|
508
|
+
codeRef: extractCodeRef(mmdHit.nodeLabel) || extractCodeRef(title),
|
|
509
|
+
version: window.__CLADO_FILE_VERSION__,
|
|
510
|
+
wx: Math.round(cardX + cardW + 24),
|
|
511
|
+
wy: Math.round(cardY + 12),
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
const clickedCode = targetEl?.closest?.('code')?.textContent || '';
|
|
382
516
|
return {
|
|
383
517
|
kind: 'card',
|
|
384
|
-
node: path || null,
|
|
385
|
-
|
|
518
|
+
node: path || cardPath || null,
|
|
519
|
+
cardNode: cardPath || null,
|
|
520
|
+
title,
|
|
521
|
+
isIsolated: Boolean(cardEl.querySelector('.clado-card-isolated-pill')),
|
|
522
|
+
codeRef: extractCodeRef(clickedCode) || extractCodeRef(title),
|
|
386
523
|
version: window.__CLADO_FILE_VERSION__,
|
|
387
524
|
wx: Math.round(cardX + cardW + 24),
|
|
388
525
|
wy: Math.round(cardY + 12),
|
|
@@ -391,10 +528,88 @@
|
|
|
391
528
|
return { kind: 'blank', wx, wy };
|
|
392
529
|
}
|
|
393
530
|
|
|
531
|
+
// ============ 卡内划词极简操作条 + 代码符号快速跳转 ============
|
|
532
|
+
let selQuickbarEl = null;
|
|
533
|
+
let activeSelectionHit = null;
|
|
534
|
+
|
|
535
|
+
function hideSelQuickbar() {
|
|
536
|
+
if (selQuickbarEl) selQuickbarEl.remove();
|
|
537
|
+
selQuickbarEl = null;
|
|
538
|
+
activeSelectionHit = null;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
function showSelQuickbar(hit) {
|
|
542
|
+
hideSelQuickbar();
|
|
543
|
+
if (!hit || window.__CLADO_OFFLINE__ || chainOpen() || document.body.classList.contains('clado-link-picking')) return;
|
|
544
|
+
activeSelectionHit = hit;
|
|
545
|
+
const bar = document.createElement('div');
|
|
546
|
+
bar.className = 'clado-sel-quickbar';
|
|
547
|
+
bar.innerHTML =
|
|
548
|
+
'<button type="button" data-sq="ask">问 AI</button>'
|
|
549
|
+
+ '<span class="sep" aria-hidden="true"></span>'
|
|
550
|
+
+ '<button type="button" data-sq="edit">让 AI 修改</button>'
|
|
551
|
+
+ '<span class="sep" aria-hidden="true"></span>'
|
|
552
|
+
+ '<button type="button" data-sq="link">连接到…</button>';
|
|
553
|
+
document.body.appendChild(bar);
|
|
554
|
+
const bw = bar.offsetWidth || 210;
|
|
555
|
+
const bh = bar.offsetHeight || 32;
|
|
556
|
+
const cx = hit.rect.left + hit.rect.width / 2 - bw / 2;
|
|
557
|
+
const cy = hit.rect.top - bh - 8;
|
|
558
|
+
bar.style.left = Math.max(10, Math.min(window.innerWidth - bw - 10, cx)) + 'px';
|
|
559
|
+
bar.style.top = (cy > 10 ? cy : hit.rect.bottom + 8) + 'px';
|
|
560
|
+
|
|
561
|
+
bar.addEventListener('pointerdown', ev => { ev.stopPropagation(); });
|
|
562
|
+
bar.addEventListener('click', ev => {
|
|
563
|
+
const btn = ev.target.closest('button[data-sq]');
|
|
564
|
+
if (!btn || !activeSelectionHit) return;
|
|
565
|
+
ev.preventDefault();
|
|
566
|
+
ev.stopPropagation();
|
|
567
|
+
const action = btn.dataset.sq;
|
|
568
|
+
const cur = activeSelectionHit;
|
|
569
|
+
hideSelQuickbar();
|
|
570
|
+
if (action === 'link') {
|
|
571
|
+
runtime.startLinkPick?.({
|
|
572
|
+
kind: 'text-selection',
|
|
573
|
+
node: cur.node,
|
|
574
|
+
selectedText: cur.text,
|
|
575
|
+
label: '「' + cur.text.slice(0, 28) + '」',
|
|
576
|
+
});
|
|
577
|
+
} else {
|
|
578
|
+
post('selection-action', { mode: action, node: cur.node, selectedText: cur.text });
|
|
579
|
+
}
|
|
580
|
+
});
|
|
581
|
+
selQuickbarEl = bar;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
if (!window.__CLADO_OFFLINE__) {
|
|
585
|
+
document.addEventListener('pointerdown', ev => {
|
|
586
|
+
if (selQuickbarEl && !ev.target.closest('.clado-sel-quickbar')) hideSelQuickbar();
|
|
587
|
+
}, true);
|
|
588
|
+
document.addEventListener('mouseup', ev => {
|
|
589
|
+
if (ev.button !== 0 || ev.target.closest('.clado-sel-quickbar')) return;
|
|
590
|
+
setTimeout(() => {
|
|
591
|
+
const hit = getCardTextSelection();
|
|
592
|
+
if (hit && hit.text.length >= 2) showSelQuickbar(hit);
|
|
593
|
+
else hideSelQuickbar();
|
|
594
|
+
}, 24);
|
|
595
|
+
});
|
|
596
|
+
document.addEventListener('click', ev => {
|
|
597
|
+
if (!(ev.shiftKey || ev.altKey)) return;
|
|
598
|
+
const codeEl = ev.target.closest('.markdown-body code');
|
|
599
|
+
if (!codeEl) return;
|
|
600
|
+
const ref = extractCodeRef(codeEl.textContent);
|
|
601
|
+
if (!ref) return;
|
|
602
|
+
ev.preventDefault();
|
|
603
|
+
ev.stopImmediatePropagation();
|
|
604
|
+
post('open-code-ref', { ref });
|
|
605
|
+
}, true);
|
|
606
|
+
}
|
|
607
|
+
|
|
394
608
|
if (!window.__CLADO_OFFLINE__ && stageEl) {
|
|
395
609
|
stageEl.addEventListener('contextmenu', ev => {
|
|
396
610
|
if (document.body.dataset.mode !== 'cards' || chainOpen()) return;
|
|
397
611
|
ev.preventDefault();
|
|
612
|
+
hideSelQuickbar();
|
|
398
613
|
post('context-menu', {
|
|
399
614
|
x: ev.clientX,
|
|
400
615
|
y: ev.clientY,
|
|
@@ -447,10 +662,38 @@
|
|
|
447
662
|
runtime.highlightActivity?.(event.data);
|
|
448
663
|
return;
|
|
449
664
|
}
|
|
665
|
+
if (event.data?.type === 'clado-workspace:presence') {
|
|
666
|
+
runtime.setCardPresence?.(event.data.presence);
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
if (event.data?.type === 'clado-workspace:presence-config') {
|
|
670
|
+
runtime.setPresenceConfig?.(event.data.config);
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
if (event.data?.type === 'clado-workspace:live-diff') {
|
|
674
|
+
runtime.playLiveDiff?.(event.data.prevDoc, event.data.touch, event.data.follow);
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
if (event.data?.type === 'clado-workspace:canvas-draft') {
|
|
678
|
+
runtime.setDraftCard?.(event.data.draft);
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
if (event.data?.type === 'clado-workspace:start-link-pick') {
|
|
682
|
+
runtime.startLinkPick?.(event.data.source);
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
if (event.data?.type === 'clado-workspace:cancel-link-pick') {
|
|
686
|
+
runtime.cancelLinkPick?.();
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
450
689
|
if (event.data?.type === 'clado-workspace:focus-node') {
|
|
451
690
|
runtime.focusNodePath?.(event.data.node, event.data.label);
|
|
452
691
|
return;
|
|
453
692
|
}
|
|
693
|
+
if (event.data?.type === 'clado-workspace:focus-tag') {
|
|
694
|
+
runtime.focusTag?.(event.data.tagId);
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
454
697
|
if (event.data?.type !== 'clado-workspace:display') return;
|
|
455
698
|
if (runtime.isChainOpen?.()) { deferredDisplay=event.data;return; }
|
|
456
699
|
const {mode,theme,anchor}=event.data;
|