cladogram 0.6.0-beta.1 → 0.6.0-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/bin/clado.js +34 -2
- package/docs/release.md +16 -0
- package/package.json +1 -1
- package/plugins/agent/index.js +1544 -127
- package/plugins/agent/style.css +859 -56
- package/plugins/view-controls/style.css +7 -7
- package/src/agent/clado-tools.js +437 -103
- package/src/agent/pi-bridge.js +669 -60
- package/src/agent/pi-config.js +367 -31
- package/src/html/canvas-chain.js +26 -2
- package/src/html/canvas-focus.js +19 -13
- package/src/html/canvas-live-diff.js +927 -0
- package/src/html/canvas-ui.css +365 -0
- package/src/html/canvas-wires.js +5 -1
- package/src/html/page.js +1 -1
- package/src/html/plugin-ui.css +1 -1
- package/src/html/runtime-api.js +140 -10
- package/src/html/runtime.js +93 -26
- package/src/server/agent.js +46 -8
- package/src/server/assets/library-canvas.js +240 -5
- package/src/server/assets/library.css +428 -12
- package/src/server/assets/library.html +2 -0
- package/src/server/assets/library.js +703 -14
- package/src/server/code-inspect.js +183 -0
- package/src/server/mcp.js +4 -4
- package/src/server/workspace.js +260 -18
- package/src/workspace/graph-snapshot.js +695 -0
- package/src/workspace/service.js +11 -1
package/src/html/runtime.js
CHANGED
|
@@ -359,6 +359,7 @@
|
|
|
359
359
|
再点开放出来,里面还是你上次展开的样子。 */
|
|
360
360
|
|
|
361
361
|
var openEdge = {};
|
|
362
|
+
var userClosedEdge = {};
|
|
362
363
|
var VIS = {};
|
|
363
364
|
|
|
364
365
|
function edgeKey(a, b) { return a + '\u0000' + b; }
|
|
@@ -429,7 +430,13 @@
|
|
|
429
430
|
function toggleEdge(src, dst) {
|
|
430
431
|
if (!src || !dst || canvasVisibleCards) return;
|
|
431
432
|
var k = edgeKey(src, dst);
|
|
432
|
-
if (openEdge[k])
|
|
433
|
+
if (openEdge[k]) {
|
|
434
|
+
delete openEdge[k];
|
|
435
|
+
userClosedEdge[k] = 1;
|
|
436
|
+
} else {
|
|
437
|
+
openEdge[k] = 1;
|
|
438
|
+
delete userClosedEdge[k];
|
|
439
|
+
}
|
|
433
440
|
afterToggle(src);
|
|
434
441
|
}
|
|
435
442
|
|
|
@@ -523,26 +530,64 @@
|
|
|
523
530
|
* —— 这条正好把「回边」挡掉,环不会把深度顶上去;
|
|
524
531
|
* - 整张图全是环、一个根都没有时,所有卡都是 0,退化成一列,不会疯。
|
|
525
532
|
*/
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
var
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
533
|
+
// 严格 O(V + E) 线性时间防环拓扑分层:
|
|
534
|
+
// 1) 三色 DFS 去回边(每个节点和每条边仅访问一次,杜绝稠密/成环图上的 O(N!) 指数递归卡死)
|
|
535
|
+
// 2) Kahn 拓扑排序按最长正向路径计算列深
|
|
536
|
+
var depth = {}, rawInDeg = {}, outAdj = {}, color = {}, dagAdj = {}, dagInDeg = {};
|
|
537
|
+
DOC.cards.forEach(function (c) {
|
|
538
|
+
depth[c.id] = 0;
|
|
539
|
+
rawInDeg[c.id] = 0;
|
|
540
|
+
outAdj[c.id] = [];
|
|
541
|
+
color[c.id] = 0; // 0=未访问, 1=递归栈中, 2=已完成
|
|
542
|
+
dagAdj[c.id] = [];
|
|
543
|
+
dagInDeg[c.id] = 0;
|
|
544
|
+
});
|
|
545
|
+
DOC.edges.forEach(function (e) {
|
|
546
|
+
if (!e || e.length < 2) return;
|
|
547
|
+
var u = e[0], v = e[1];
|
|
548
|
+
if (u === v || !(u in depth) || !(v in depth)) return;
|
|
549
|
+
if (outAdj[u].indexOf(v) < 0) {
|
|
550
|
+
outAdj[u].push(v);
|
|
551
|
+
rawInDeg[v] += 1;
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
var breakCycles = function (startId) {
|
|
555
|
+
// 显式栈迭代 DFS,避免深链爆栈并保证 O(V + E)
|
|
556
|
+
var stack = [{ id: startId, idx: 0 }];
|
|
557
|
+
color[startId] = 1;
|
|
558
|
+
while (stack.length) {
|
|
559
|
+
var top = stack[stack.length - 1];
|
|
560
|
+
var nexts = outAdj[top.id];
|
|
561
|
+
if (top.idx < nexts.length) {
|
|
562
|
+
var nxt = nexts[top.idx++];
|
|
563
|
+
if (color[nxt] === 1) continue; // 回边:直接忽略,打破环
|
|
564
|
+
dagAdj[top.id].push(nxt);
|
|
565
|
+
dagInDeg[nxt] += 1;
|
|
566
|
+
if (color[nxt] === 0) {
|
|
567
|
+
color[nxt] = 1;
|
|
568
|
+
stack.push({ id: nxt, idx: 0 });
|
|
569
|
+
}
|
|
570
|
+
} else {
|
|
571
|
+
color[top.id] = 2;
|
|
572
|
+
stack.pop();
|
|
539
573
|
}
|
|
540
|
-
}
|
|
541
|
-
delete onStack[id];
|
|
574
|
+
}
|
|
542
575
|
};
|
|
543
|
-
DOC.cards.forEach(function (c) { if (!
|
|
544
|
-
|
|
545
|
-
|
|
576
|
+
DOC.cards.forEach(function (c) { if (!rawInDeg[c.id] && color[c.id] === 0) breakCycles(c.id); });
|
|
577
|
+
DOC.cards.forEach(function (c) { if (color[c.id] === 0) breakCycles(c.id); });
|
|
578
|
+
var kahnQ = [];
|
|
579
|
+
DOC.cards.forEach(function (c) { if (dagInDeg[c.id] === 0) kahnQ.push(c.id); });
|
|
580
|
+
var qHead = 0;
|
|
581
|
+
while (qHead < kahnQ.length) {
|
|
582
|
+
var uId = kahnQ[qHead++];
|
|
583
|
+
var nextList = dagAdj[uId];
|
|
584
|
+
for (var ni = 0; ni < nextList.length; ni++) {
|
|
585
|
+
var vId = nextList[ni];
|
|
586
|
+
if (depth[vId] < depth[uId] + 1) depth[vId] = depth[uId] + 1;
|
|
587
|
+
dagInDeg[vId] -= 1;
|
|
588
|
+
if (dagInDeg[vId] === 0) kahnQ.push(vId);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
546
591
|
var kids = {}, incoming = {}, focused = typeof canvasVisibleCards !== 'undefined' && canvasVisibleCards;
|
|
547
592
|
DOC.edges.forEach(function (e) {
|
|
548
593
|
if (!seen[e[0]] || !seen[e[1]] || (!focused && !openEdge[edgeKey(e[0], e[1])])) return;
|
|
@@ -1456,10 +1501,13 @@
|
|
|
1456
1501
|
document.body.dataset.mode = 'cards';
|
|
1457
1502
|
buildCards();
|
|
1458
1503
|
renderDoc();
|
|
1504
|
+
if (window.__CLADO_WORKSPACE_CHROME__) {
|
|
1505
|
+
DOC.edges.forEach(function (e) { openEdge[edgeKey(e[0], e[1])] = 1; });
|
|
1506
|
+
}
|
|
1459
1507
|
layout(); bindCards();
|
|
1460
1508
|
DOC.cards.forEach(function (c) { placePorts(render.card[c.id]); });
|
|
1461
1509
|
// 先把「谁看得见」算出来,fit() 才知道该按多大范围去缩 ——
|
|
1462
|
-
//
|
|
1510
|
+
// 独立静态页默认只展开根;工作台模式下默认展开全部连线让整张网一览无余。
|
|
1463
1511
|
VIS = visibleSet();
|
|
1464
1512
|
|
|
1465
1513
|
// 主题三级兜底:本机上次切过的 > 编译时写进 <html data-theme> 的 > light。
|
|
@@ -1492,18 +1540,37 @@
|
|
|
1492
1540
|
* 那一行一动,端口就错位了。ResizeObserver 只管尺寸,
|
|
1493
1541
|
* 而 placePorts 只动端口(绝对定位),不会引起新的尺寸变化,所以不会打转。
|
|
1494
1542
|
*/
|
|
1543
|
+
if (typeof markIsolatedCards === 'function') markIsolatedCards();
|
|
1544
|
+
|
|
1495
1545
|
if (window.ResizeObserver) {
|
|
1546
|
+
var roQueue = [];
|
|
1547
|
+
var roRaf = 0;
|
|
1496
1548
|
var ro = new ResizeObserver(function (ents) {
|
|
1497
1549
|
if (document.body.dataset.mode !== 'cards') return;
|
|
1498
|
-
var
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1550
|
+
for (var i = 0; i < ents.length; i++) {
|
|
1551
|
+
var c = ents[i].target;
|
|
1552
|
+
if (c && c.isConnected && c.classList && c.classList.contains('card') && VIS[c.dataset.id]) {
|
|
1553
|
+
if (roQueue.indexOf(c) < 0) roQueue.push(c);
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
if (!roQueue.length || roRaf) return;
|
|
1557
|
+
roRaf = requestAnimationFrame(function () {
|
|
1558
|
+
roRaf = 0;
|
|
1559
|
+
if (document.body.dataset.mode !== 'cards') { roQueue = []; return; }
|
|
1560
|
+
var batch = roQueue;
|
|
1561
|
+
roQueue = [];
|
|
1562
|
+
var hit = false;
|
|
1563
|
+
for (var j = 0; j < batch.length; j++) {
|
|
1564
|
+
var card = batch[j];
|
|
1565
|
+
if (card && card.isConnected && VIS[card.dataset.id]) {
|
|
1566
|
+
placePorts(card);
|
|
1567
|
+
hit = true;
|
|
1568
|
+
}
|
|
1503
1569
|
}
|
|
1570
|
+
if (hit) { applyVisibility(); drawWires(); }
|
|
1504
1571
|
});
|
|
1505
|
-
if (hit) { applyVisibility(); drawWires(); }
|
|
1506
1572
|
});
|
|
1573
|
+
window.__cladoCardRO = ro;
|
|
1507
1574
|
DOC.cards.forEach(function (c) { ro.observe(render.card[c.id]); });
|
|
1508
1575
|
}
|
|
1509
1576
|
|
package/src/server/agent.js
CHANGED
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
listRoles,
|
|
25
25
|
readAgentsMemory,
|
|
26
26
|
readCanvasComponentsConfig,
|
|
27
|
+
readRuntimeSettings,
|
|
27
28
|
removePiPackage,
|
|
28
29
|
resolvePiAgentDir,
|
|
29
30
|
resolveRole,
|
|
@@ -33,6 +34,7 @@ import {
|
|
|
33
34
|
savePiSkill,
|
|
34
35
|
saveProviderConfig,
|
|
35
36
|
saveRole,
|
|
37
|
+
saveRuntimeSettings,
|
|
36
38
|
setDefaultPiModel,
|
|
37
39
|
toggleCanvasComponent,
|
|
38
40
|
togglePiMcpServer,
|
|
@@ -43,6 +45,7 @@ import {
|
|
|
43
45
|
deleteSession,
|
|
44
46
|
getSessionMeta,
|
|
45
47
|
listSessions,
|
|
48
|
+
renameSession,
|
|
46
49
|
restoreCanvasSnapshot,
|
|
47
50
|
safeSessionId,
|
|
48
51
|
upsertSessionMeta,
|
|
@@ -116,11 +119,20 @@ function sendJson(res, status, data) {
|
|
|
116
119
|
res.end(JSON.stringify(data));
|
|
117
120
|
}
|
|
118
121
|
|
|
119
|
-
function assertSameOriginPost(req) {
|
|
120
|
-
const
|
|
121
|
-
if (
|
|
122
|
-
|
|
122
|
+
function assertSameOriginPost(req, remote = false) {
|
|
123
|
+
const origin = req.headers.origin;
|
|
124
|
+
if (!origin) return;
|
|
125
|
+
const host = String(req.headers.host || '').trim();
|
|
126
|
+
if (origin === 'http://' + host) return;
|
|
127
|
+
if (remote) {
|
|
128
|
+
const forwardedHost = String(req.headers['x-forwarded-host'] || '').split(',')[0].trim();
|
|
129
|
+
const allowed = new Set([host]);
|
|
130
|
+
if (forwardedHost) allowed.add(forwardedHost);
|
|
131
|
+
for (const h of allowed) {
|
|
132
|
+
if (h && (origin === 'http://' + h || origin === 'https://' + h)) return;
|
|
133
|
+
}
|
|
123
134
|
}
|
|
135
|
+
throw new DocumentError('来源不匹配。', 403);
|
|
124
136
|
}
|
|
125
137
|
|
|
126
138
|
export function createAgentHandler({
|
|
@@ -130,6 +142,7 @@ export function createAgentHandler({
|
|
|
130
142
|
workspaceService = null,
|
|
131
143
|
events = null,
|
|
132
144
|
sessionFactory = null,
|
|
145
|
+
remote = false,
|
|
133
146
|
}) {
|
|
134
147
|
const bridge = createPiBridge({
|
|
135
148
|
workspaceService,
|
|
@@ -155,6 +168,7 @@ export function createAgentHandler({
|
|
|
155
168
|
listProvidersAndModels(rt.workspace, agentDir),
|
|
156
169
|
inspectPiEcosystem(rt.workspace, agentDir),
|
|
157
170
|
]);
|
|
171
|
+
const runtimeSettings = readRuntimeSettings(rt.workspace, agentDir, modelsInfo.activeContextWindow);
|
|
158
172
|
const roles = listRoles(agentDir);
|
|
159
173
|
const memory = readAgentsMemory(rt.workspace);
|
|
160
174
|
const canvasComponents = readCanvasComponentsConfig(agentDir);
|
|
@@ -169,6 +183,9 @@ export function createAgentHandler({
|
|
|
169
183
|
defaultProvider: modelsInfo.defaultProvider,
|
|
170
184
|
defaultModel: modelsInfo.defaultModel,
|
|
171
185
|
activeModelLabel: modelsInfo.activeModelLabel,
|
|
186
|
+
activeContextWindow: modelsInfo.activeContextWindow,
|
|
187
|
+
activeMaxTokens: modelsInfo.activeMaxTokens,
|
|
188
|
+
runtimeSettings,
|
|
172
189
|
providers: modelsInfo.providers,
|
|
173
190
|
configuredModels: modelsInfo.configuredModels,
|
|
174
191
|
roles,
|
|
@@ -205,8 +222,13 @@ export function createAgentHandler({
|
|
|
205
222
|
}
|
|
206
223
|
|
|
207
224
|
if (req.method === 'POST') {
|
|
208
|
-
assertSameOriginPost(req);
|
|
225
|
+
assertSameOriginPost(req, remote);
|
|
209
226
|
const body = await readJsonBody(req);
|
|
227
|
+
if (body?.action === 'rename') {
|
|
228
|
+
const updated = renameSession(rt.stateDir, body.id, body.title, agentDir);
|
|
229
|
+
sendJson(res, 200, { ok: true, session: updated });
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
210
232
|
if (body?.action === 'archive') {
|
|
211
233
|
const updated = archiveSession(rt.stateDir, body.id, body.archived !== false, agentDir);
|
|
212
234
|
sendJson(res, 200, { ok: Boolean(updated), session: updated });
|
|
@@ -217,6 +239,16 @@ export function createAgentHandler({
|
|
|
217
239
|
sendJson(res, 200, result);
|
|
218
240
|
return;
|
|
219
241
|
}
|
|
242
|
+
if (body?.action === 'compact') {
|
|
243
|
+
const result = await bridge.compactSession({
|
|
244
|
+
workspace: rt.workspace,
|
|
245
|
+
stateDir: rt.stateDir,
|
|
246
|
+
sessionId: body.id,
|
|
247
|
+
customInstructions: body.instructions || '',
|
|
248
|
+
});
|
|
249
|
+
sendJson(res, 200, result);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
220
252
|
const created = upsertSessionMeta(rt.stateDir, {
|
|
221
253
|
id: body.id ? safeSessionId(body.id) : undefined,
|
|
222
254
|
title: body.title,
|
|
@@ -228,7 +260,7 @@ export function createAgentHandler({
|
|
|
228
260
|
}
|
|
229
261
|
|
|
230
262
|
if (req.method === 'DELETE') {
|
|
231
|
-
assertSameOriginPost(req);
|
|
263
|
+
assertSameOriginPost(req, remote);
|
|
232
264
|
const id = url.searchParams.get('id');
|
|
233
265
|
if (!id) throw new DocumentError('缺少会话 ID。', 400);
|
|
234
266
|
await bridge.cancel(rt.workspace, id);
|
|
@@ -250,7 +282,7 @@ export function createAgentHandler({
|
|
|
250
282
|
}
|
|
251
283
|
|
|
252
284
|
if (req.method !== 'POST') throw new DocumentError('仅支持 GET 或 POST。', 405);
|
|
253
|
-
assertSameOriginPost(req);
|
|
285
|
+
assertSameOriginPost(req, remote);
|
|
254
286
|
const body = await readJsonBody(req, 256 * 1024);
|
|
255
287
|
const action = String(body?.action || '').trim();
|
|
256
288
|
|
|
@@ -270,6 +302,12 @@ export function createAgentHandler({
|
|
|
270
302
|
sendJson(res, 200, { ...result, meta: await buildFullMeta(targetWorkspace) });
|
|
271
303
|
return;
|
|
272
304
|
}
|
|
305
|
+
if (action === 'save_runtime_settings') {
|
|
306
|
+
const modelsInfo = await listProvidersAndModels(rt.workspace, agentDir);
|
|
307
|
+
const runtimeSettings = await saveRuntimeSettings(body, rt.workspace, agentDir, modelsInfo.activeContextWindow);
|
|
308
|
+
sendJson(res, 200, { ok: true, runtimeSettings, meta: await buildFullMeta(targetWorkspace) });
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
273
311
|
if (action === 'save_role') {
|
|
274
312
|
const role = saveRole(body, agentDir);
|
|
275
313
|
sendJson(res, 200, { ok: true, role, meta: await buildFullMeta(targetWorkspace) });
|
|
@@ -349,7 +387,7 @@ export function createAgentHandler({
|
|
|
349
387
|
|
|
350
388
|
async handle(req, res, targetWorkspace) {
|
|
351
389
|
if (req.method !== 'POST') throw new DocumentError('仅支持 POST。', 405);
|
|
352
|
-
assertSameOriginPost(req);
|
|
390
|
+
assertSameOriginPost(req, remote);
|
|
353
391
|
const raw = await readJsonBody(req);
|
|
354
392
|
const parsed = agentRequestSchema.safeParse(raw);
|
|
355
393
|
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() {
|
|
@@ -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,30 @@
|
|
|
447
662
|
runtime.highlightActivity?.(event.data);
|
|
448
663
|
return;
|
|
449
664
|
}
|
|
665
|
+
if (event.data?.type === 'clado-workspace:live-diff') {
|
|
666
|
+
runtime.playLiveDiff?.(event.data.prevDoc, event.data.touch, event.data.follow);
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
if (event.data?.type === 'clado-workspace:canvas-draft') {
|
|
670
|
+
runtime.setDraftCard?.(event.data.draft);
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
if (event.data?.type === 'clado-workspace:start-link-pick') {
|
|
674
|
+
runtime.startLinkPick?.(event.data.source);
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
if (event.data?.type === 'clado-workspace:cancel-link-pick') {
|
|
678
|
+
runtime.cancelLinkPick?.();
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
450
681
|
if (event.data?.type === 'clado-workspace:focus-node') {
|
|
451
682
|
runtime.focusNodePath?.(event.data.node, event.data.label);
|
|
452
683
|
return;
|
|
453
684
|
}
|
|
685
|
+
if (event.data?.type === 'clado-workspace:focus-tag') {
|
|
686
|
+
runtime.focusTag?.(event.data.tagId);
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
454
689
|
if (event.data?.type !== 'clado-workspace:display') return;
|
|
455
690
|
if (runtime.isChainOpen?.()) { deferredDisplay=event.data;return; }
|
|
456
691
|
const {mode,theme,anchor}=event.data;
|