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.
@@ -120,6 +120,260 @@
120
120
  });
121
121
  }
122
122
 
123
+ /* ══ ①-b 富媒体块渲染注册表(SVG 矢量/动画、UI 沙盒预览、可扩展插件入口) ═══ */
124
+
125
+ var CladoBlockRegistry = (function () {
126
+ var handlers = [];
127
+ return {
128
+ register: function (match, renderFn) {
129
+ if (typeof renderFn !== 'function') return;
130
+ handlers.push({ match: match, render: renderFn });
131
+ },
132
+ resolve: function (lang, codeText, ctx) {
133
+ for (var i = handlers.length - 1; i >= 0; i--) {
134
+ var h = handlers[i];
135
+ var ok = typeof h.match === 'function'
136
+ ? h.match(lang, codeText)
137
+ : Array.isArray(h.match)
138
+ ? h.match.indexOf(lang) >= 0
139
+ : h.match === lang;
140
+ if (ok) {
141
+ var out = h.render(codeText, lang, ctx);
142
+ if (out) return out;
143
+ }
144
+ }
145
+ return null;
146
+ },
147
+ list: function () { return handlers.slice(); },
148
+ };
149
+ })();
150
+ window.CladoBlockRegistry = CladoBlockRegistry;
151
+
152
+ var CLADO_SVG_TAGS = new Set(
153
+ 'svg g defs symbol use title desc path circle ellipse rect line polyline polygon text tspan textpath lineargradient radialgradient stop clippath mask pattern marker filter fegaussianblur fedropshadow feoffset femerge femergenode fecolormatrix feblend feflood fecomposite style animate animatetransform animatemotion mpath set'.split(' ')
154
+ );
155
+
156
+ function sanitizeSvgMarkup(raw) {
157
+ var src = String(raw || '').trim();
158
+ if (!src || src.indexOf('<svg') < 0) return null;
159
+ var svg = null;
160
+ try {
161
+ var parsed = new DOMParser().parseFromString(src, 'image/svg+xml');
162
+ if (parsed && !parsed.querySelector('parsererror') && parsed.documentElement && parsed.documentElement.localName === 'svg') {
163
+ svg = document.importNode(parsed.documentElement, true);
164
+ }
165
+ } catch (e) {}
166
+ if (!svg) {
167
+ var tpl = document.createElement('template');
168
+ tpl.innerHTML = src;
169
+ var found = tpl.content.querySelector('svg');
170
+ if (found) svg = found.cloneNode(true);
171
+ }
172
+ if (!svg) return null;
173
+
174
+ var cleanNode = function (el) {
175
+ var tag = String(el.localName || el.tagName || '').toLowerCase();
176
+ if (!CLADO_SVG_TAGS.has(tag)) {
177
+ el.remove();
178
+ return;
179
+ }
180
+ Array.from(el.attributes || []).forEach(function (attr) {
181
+ var name = String(attr.name || '').toLowerCase();
182
+ var val = String(attr.value || '');
183
+ if (/^on/.test(name)) { el.removeAttribute(attr.name); return; }
184
+ if (name === 'href' || name === 'xlink:href') {
185
+ if (!/^#[a-zA-Z0-9_.:-]+$/.test(val.trim())) el.removeAttribute(attr.name);
186
+ return;
187
+ }
188
+ if (name === 'style' && /(?:javascript:|expression\s*\(|@import|url\s*\(\s*['"]?\s*(?:https?:|data:|javascript:))/i.test(val)) {
189
+ el.removeAttribute(attr.name);
190
+ return;
191
+ }
192
+ if (/javascript:/i.test(val.replace(/[\x00-\x20]/g, ''))) {
193
+ el.removeAttribute(attr.name);
194
+ }
195
+ });
196
+ if (tag === 'style') {
197
+ el.textContent = String(el.textContent || '')
198
+ .replace(/@import[^;]*;?/gi, '')
199
+ .replace(/expression\s*\([^)]*\)/gi, '')
200
+ .replace(/javascript:/gi, '')
201
+ .replace(/-moz-binding\s*:[^;]*;?/gi, '')
202
+ .replace(/url\s*\(\s*['"]?\s*(?:https?:|data:|javascript:)[^)]*\)/gi, '');
203
+ return;
204
+ }
205
+ Array.from(el.children || []).forEach(cleanNode);
206
+ };
207
+ cleanNode(svg);
208
+
209
+ if (!svg.getAttribute('viewBox')) {
210
+ var w = parseFloat(svg.getAttribute('width') || '');
211
+ var h = parseFloat(svg.getAttribute('height') || '');
212
+ if (w > 0 && h > 0) svg.setAttribute('viewBox', '0 0 ' + w + ' ' + h);
213
+ }
214
+ return svg;
215
+ }
216
+
217
+ var cladoUiFrameSeq = 0;
218
+ var cladoUiFrames = new Map();
219
+ window.addEventListener('message', function (ev) {
220
+ var d = ev.data;
221
+ if (!d || d.type !== 'clado-ui-height' || !d.id) return;
222
+ var fr = cladoUiFrames.get(String(d.id));
223
+ if (!fr || !fr.isConnected) { cladoUiFrames.delete(String(d.id)); return; }
224
+ var nextH = Math.max(180, Math.min(540, Math.ceil(Number(d.height) || 260)));
225
+ if (Math.abs((parseInt(fr.style.height, 10) || 260) - nextH) > 4) {
226
+ fr.style.height = nextH + 'px';
227
+ if (document.body.dataset.mode === 'cards') {
228
+ try { repinPorts(); drawWires(); } catch (e) {}
229
+ }
230
+ }
231
+ });
232
+
233
+ function buildUiSandboxDoc(rawCode, frameId) {
234
+ var src = String(rawCode || '');
235
+ var dark = document.documentElement.dataset.theme === 'dark';
236
+ var baseStyle =
237
+ '<style>'
238
+ + ':root{color-scheme:' + (dark ? 'dark' : 'light') + ';--bg:' + (dark ? '#0f172a' : '#f8fafc') + ';--surface:' + (dark ? '#1e293b' : '#ffffff') + ';--text:' + (dark ? '#f1f5f9' : '#0f172a') + ';--muted:' + (dark ? '#94a3b8' : '#64748b') + ';--border:' + (dark ? 'rgba(148,163,184,.22)' : 'rgba(15,23,42,.10)') + ';--accent:#0ea5e9;}'
239
+ + '*,*::before,*::after{box-sizing:border-box}'
240
+ + 'html,body{margin:0;padding:0;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC","Noto Sans SC",sans-serif;background:var(--bg);color:var(--text);font-size:13px;line-height:1.5;-webkit-font-smoothing:antialiased}'
241
+ + 'body{padding:12px}'
242
+ + 'button,input,select,textarea{font:inherit}'
243
+ + '</style>';
244
+ var resizeHook = frameId
245
+ ? ('\x3cscript>(function(){var id=' + JSON.stringify(frameId) + ';function send(){try{var h=Math.max(document.body?document.body.scrollHeight:0,document.documentElement?document.documentElement.scrollHeight:0);parent.postMessage({type:"clado-ui-height",id:id,height:h},"*");}catch(e){}}window.addEventListener("load",send);if(typeof ResizeObserver==="function"){new ResizeObserver(send).observe(document.documentElement);}setTimeout(send,60);setTimeout(send,240);})();\x3c/script>')
246
+ : '';
247
+ if (/^\s*<!doctype|^\s*<html\b/i.test(src)) {
248
+ if (/<\/head>/i.test(src)) src = src.replace(/<\/head>/i, baseStyle + '</head>');
249
+ if (resizeHook && /<\/body>/i.test(src)) src = src.replace(/<\/body>/i, resizeHook + '</body>');
250
+ else if (resizeHook) src += resizeHook;
251
+ return src;
252
+ }
253
+ return '<!DOCTYPE html><html lang="zh-CN" data-theme="' + (dark ? 'dark' : 'light') + '"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">'
254
+ + baseStyle + '</head><body>' + src + resizeHook + '</body></html>';
255
+ }
256
+
257
+ function createUiPreviewBlock(rawCode, preEl) {
258
+ var wrap = document.createElement('div');
259
+ wrap.className = 'zoomable zui';
260
+ wrap.dataset.label = 'UI 原型预览';
261
+
262
+ var bar = document.createElement('div');
263
+ bar.className = 'zblock-bar';
264
+ bar.innerHTML =
265
+ '<span class="zblock-title">UI 原型预览</span>'
266
+ + '<span class="zblock-actions">'
267
+ + '<button type="button" class="zblock-btn active" data-ui-vp="desktop">桌面</button>'
268
+ + '<button type="button" class="zblock-btn" data-ui-vp="mobile">手机</button>'
269
+ + '<button type="button" class="zblock-btn" data-ui-zoom>放大</button>'
270
+ + '<button type="button" class="zblock-btn" data-ui-mode>代码</button>'
271
+ + '</span>';
272
+
273
+ var stage = document.createElement('div');
274
+ stage.className = 'zui-stage';
275
+ var frameId = 'ui_' + (++cladoUiFrameSeq);
276
+ var iframe = document.createElement('iframe');
277
+ iframe.className = 'zui-frame';
278
+ iframe.setAttribute('sandbox', 'allow-scripts');
279
+ iframe.setAttribute('loading', 'lazy');
280
+ iframe.setAttribute('title', 'UI 原型预览');
281
+ iframe.dataset.uiFrameId = frameId;
282
+ iframe._cladoUiSource = rawCode;
283
+ iframe.srcdoc = buildUiSandboxDoc(rawCode, frameId);
284
+ cladoUiFrames.set(frameId, iframe);
285
+ stage.appendChild(iframe);
286
+
287
+ preEl.classList.add('zui-code');
288
+ wrap.appendChild(bar);
289
+ wrap.appendChild(stage);
290
+ wrap.appendChild(preEl);
291
+
292
+ var deskBtn = bar.querySelector('[data-ui-vp="desktop"]');
293
+ var mobBtn = bar.querySelector('[data-ui-vp="mobile"]');
294
+ var zoomBtn = bar.querySelector('[data-ui-zoom]');
295
+ var modeBtn = bar.querySelector('[data-ui-mode]');
296
+
297
+ deskBtn.addEventListener('click', function (ev) {
298
+ ev.stopPropagation();
299
+ wrap.classList.remove('is-mobile');
300
+ deskBtn.classList.add('active');
301
+ mobBtn.classList.remove('active');
302
+ });
303
+ mobBtn.addEventListener('click', function (ev) {
304
+ ev.stopPropagation();
305
+ wrap.classList.add('is-mobile');
306
+ mobBtn.classList.add('active');
307
+ deskBtn.classList.remove('active');
308
+ });
309
+ modeBtn.addEventListener('click', function (ev) {
310
+ ev.stopPropagation();
311
+ var showCode = wrap.classList.toggle('is-code');
312
+ modeBtn.textContent = showCode ? '预览' : '代码';
313
+ modeBtn.classList.toggle('active', showCode);
314
+ if (document.body.dataset.mode === 'cards') { try { repinPorts(); drawWires(); } catch (e) {} }
315
+ });
316
+ zoomBtn.addEventListener('click', function (ev) {
317
+ ev.stopPropagation();
318
+ var lensHtml = '<iframe class="zui-lens-frame" sandbox="allow-scripts" title="UI 原型全屏预览"></iframe>';
319
+ openLens(lensHtml, 'UI 原型预览', 'ui');
320
+ var lensFr = $('#lensInner .zui-lens-frame');
321
+ if (lensFr) lensFr.srcdoc = buildUiSandboxDoc(rawCode, '');
322
+ });
323
+ return wrap;
324
+ }
325
+
326
+ CladoBlockRegistry.register('svg', function (rawCode, lang, ctx) {
327
+ var cleanSvg = sanitizeSvgMarkup(rawCode);
328
+ if (!cleanSvg) return null;
329
+ var wrap = document.createElement('div');
330
+ wrap.className = 'zoomable zsvg';
331
+ wrap.dataset.label = 'SVG 矢量图';
332
+
333
+ var bar = document.createElement('div');
334
+ bar.className = 'zblock-bar';
335
+ bar.innerHTML =
336
+ '<span class="zblock-title">SVG 矢量图</span>'
337
+ + '<span class="zblock-actions">'
338
+ + '<button type="button" class="zblock-btn" data-svg-zoom>放大</button>'
339
+ + '<button type="button" class="zblock-btn" data-svg-mode>源码</button>'
340
+ + '</span>';
341
+
342
+ var stage = document.createElement('div');
343
+ stage.className = 'zsvg-stage';
344
+ stage.title = '点一下放大';
345
+ stage.appendChild(cleanSvg);
346
+
347
+ var preEl = ctx.pre;
348
+ if (window.hljs && ctx.code) { try { hljs.highlightElement(ctx.code); } catch (e) {} }
349
+ preEl.classList.add('zsvg-code');
350
+
351
+ wrap.appendChild(bar);
352
+ wrap.appendChild(stage);
353
+ wrap.appendChild(preEl);
354
+
355
+ var modeBtn = bar.querySelector('[data-svg-mode]');
356
+ var zoomBtn = bar.querySelector('[data-svg-zoom]');
357
+ modeBtn.addEventListener('click', function (ev) {
358
+ ev.stopPropagation();
359
+ var showCode = wrap.classList.toggle('is-code');
360
+ modeBtn.textContent = showCode ? '视图' : '源码';
361
+ modeBtn.classList.toggle('active', showCode);
362
+ if (document.body.dataset.mode === 'cards') { try { repinPorts(); drawWires(); } catch (e) {} }
363
+ });
364
+ zoomBtn.addEventListener('click', function (ev) {
365
+ ev.stopPropagation();
366
+ var svgEl = $('.zsvg-stage svg', wrap);
367
+ if (svgEl) openLens(svgEl.outerHTML, 'SVG 矢量图', 'media');
368
+ });
369
+ return wrap;
370
+ });
371
+
372
+ CladoBlockRegistry.register(['ui', 'html-preview'], function (rawCode, lang, ctx) {
373
+ if (window.hljs && ctx.code) { try { hljs.highlightElement(ctx.code); } catch (e) {} }
374
+ return createUiPreviewBlock(rawCode, ctx.pre);
375
+ });
376
+
123
377
  /**
124
378
  * 一段原文 Markdown → 一个 `.markdown-body` 元素。
125
379
  *
@@ -145,6 +399,46 @@
145
399
  });
146
400
  box.appendChild(template.content);
147
401
 
402
+ // 媒体交付件升级:![说明](demo.mp4/.webm/.mov) → 原生视频播放器;.mp3/.wav/.m4a → 原生音频播放器
403
+ $$('img[src]', box).forEach(function (img) {
404
+ var src = img.getAttribute('src') || '';
405
+ var alt = (img.getAttribute('alt') || '').trim();
406
+ if (/\.(?:mp4|webm|mov|ogv)(?:[?#].*)?$/i.test(src)) {
407
+ var vw = document.createElement('div');
408
+ vw.className = 'zvideo';
409
+ var video = document.createElement('video');
410
+ video.setAttribute('controls', '');
411
+ video.setAttribute('playsinline', '');
412
+ video.setAttribute('preload', 'metadata');
413
+ video.setAttribute('src', src);
414
+ vw.appendChild(video);
415
+ if (alt) {
416
+ var vcap = document.createElement('div');
417
+ vcap.className = 'zmedia-cap';
418
+ vcap.textContent = alt;
419
+ vw.appendChild(vcap);
420
+ }
421
+ img.parentNode.replaceChild(vw, img);
422
+ return;
423
+ }
424
+ if (/\.(?:mp3|wav|m4a|ogg|flac)(?:[?#].*)?$/i.test(src)) {
425
+ var aw = document.createElement('div');
426
+ aw.className = 'zaudio';
427
+ if (alt) {
428
+ var acap = document.createElement('div');
429
+ acap.className = 'zmedia-cap';
430
+ acap.textContent = alt;
431
+ aw.appendChild(acap);
432
+ }
433
+ var audio = document.createElement('audio');
434
+ audio.setAttribute('controls', '');
435
+ audio.setAttribute('preload', 'metadata');
436
+ audio.setAttribute('src', src);
437
+ aw.appendChild(audio);
438
+ img.parentNode.replaceChild(aw, img);
439
+ }
440
+ });
441
+
148
442
  // 表格:左右到顶、列多了自己左右滑、点一下能放大
149
443
  $$('table', box).forEach(function (t) {
150
444
  var shell = document.createElement('div');
@@ -158,11 +452,12 @@
158
452
  shell.appendChild(sc);
159
453
  });
160
454
 
161
- // 代码:先把 mermaid 挑出来(它交给 mermaid 渲染),其余交给 hljs 上色,都点得开
455
+ // 代码:依次经过 mermaid / CladoBlockRegistry(svg、ui、扩展块)/ hljs 代码块
162
456
  $$('pre > code', box).forEach(function (code) {
163
457
  var pre = code.parentNode;
164
458
  var m = /(?:^|\s)language-([\w+#.-]+)/.exec(code.className || '');
165
459
  var lang = m ? m[1].toLowerCase() : '';
460
+ var rawCode = code.textContent || '';
166
461
  if (lang === 'mermaid') {
167
462
  var wrap = document.createElement('div');
168
463
  wrap.className = 'zoomable';
@@ -170,11 +465,22 @@
170
465
  wrap.title = '点一下放大';
171
466
  var d = document.createElement('div');
172
467
  d.className = 'mermaid';
173
- d.dataset.src = code.textContent; // 浏览器已经把实体解回来了
468
+ d.dataset.src = rawCode; // 浏览器已经把实体解回来了
174
469
  wrap.appendChild(d);
175
470
  pre.parentNode.replaceChild(wrap, pre);
176
471
  return;
177
472
  }
473
+ var effectiveLang = (lang === 'html' && /^\s*<!--\s*(?:preview|ui)\s*-->/i.test(rawCode)) ? 'ui' : lang;
474
+ var origParent = pre.parentNode;
475
+ var origNext = pre.nextSibling;
476
+ var customBlock = CladoBlockRegistry.resolve(effectiveLang, rawCode, { pre: pre, code: code });
477
+ if (customBlock) {
478
+ if (origParent) {
479
+ origParent.insertBefore(customBlock, origNext);
480
+ if (pre.parentNode === origParent) origParent.removeChild(pre);
481
+ }
482
+ return;
483
+ }
178
484
  if (window.hljs) { try { hljs.highlightElement(code); } catch (e) { /* 高亮挂了也得能看 */ } }
179
485
  var z = document.createElement('div');
180
486
  z.className = 'zoomable zcode';
@@ -182,6 +488,20 @@
182
488
  z.title = '点一下放大';
183
489
  pre.parentNode.insertBefore(z, pre);
184
490
  z.appendChild(pre);
491
+ if (lang === 'html') {
492
+ var prevBtn = document.createElement('button');
493
+ prevBtn.type = 'button';
494
+ prevBtn.className = 'zcode-preview-btn';
495
+ prevBtn.textContent = '预览';
496
+ prevBtn.title = '在隔离沙盒中实时预览此 HTML';
497
+ prevBtn.addEventListener('click', function (ev) {
498
+ ev.stopPropagation();
499
+ var uiBlock = createUiPreviewBlock(rawCode, pre);
500
+ if (z.parentNode) z.parentNode.replaceChild(uiBlock, z);
501
+ if (document.body.dataset.mode === 'cards') { try { repinPorts(); drawWires(); } catch (e) {} }
502
+ });
503
+ z.appendChild(prevBtn);
504
+ }
185
505
  });
186
506
 
187
507
  $$('pre code', box).forEach(linkifyCode);
@@ -359,6 +679,7 @@
359
679
  再点开放出来,里面还是你上次展开的样子。 */
360
680
 
361
681
  var openEdge = {};
682
+ var userClosedEdge = {};
362
683
  var VIS = {};
363
684
 
364
685
  function edgeKey(a, b) { return a + '\u0000' + b; }
@@ -429,7 +750,13 @@
429
750
  function toggleEdge(src, dst) {
430
751
  if (!src || !dst || canvasVisibleCards) return;
431
752
  var k = edgeKey(src, dst);
432
- if (openEdge[k]) delete openEdge[k]; else openEdge[k] = 1;
753
+ if (openEdge[k]) {
754
+ delete openEdge[k];
755
+ userClosedEdge[k] = 1;
756
+ } else {
757
+ openEdge[k] = 1;
758
+ delete userClosedEdge[k];
759
+ }
433
760
  afterToggle(src);
434
761
  }
435
762
 
@@ -523,26 +850,64 @@
523
850
  * —— 这条正好把「回边」挡掉,环不会把深度顶上去;
524
851
  * - 整张图全是环、一个根都没有时,所有卡都是 0,退化成一列,不会疯。
525
852
  */
526
- var depth = {}, indeg = {}, onStack = {}, walked = {};
527
- DOC.cards.forEach(function (c) { depth[c.id] = 0; indeg[c.id] = 0; });
528
- DOC.edges.forEach(function (e) { if (e[1] in indeg) indeg[e[1]] += 1; });
529
- var sink = function (id) {
530
- if (onStack[id]) return; // 回边:再往下走就会绕着环把自己的深度顶上去
531
- onStack[id] = 1;
532
- walked[id] = 1;
533
- DOC.edges.forEach(function (e) {
534
- if (e[0] !== id || !(e[1] in depth)) return;
535
- if (onStack[e[1]]) return; // ★ 这条就是环的入口,跳过
536
- if (depth[e[1]] < depth[id] + 1) {
537
- depth[e[1]] = depth[id] + 1;
538
- sink(e[1]);
853
+ // 严格 O(V + E) 线性时间防环拓扑分层:
854
+ // 1) 三色 DFS 去回边(每个节点和每条边仅访问一次,杜绝稠密/成环图上的 O(N!) 指数递归卡死)
855
+ // 2) Kahn 拓扑排序按最长正向路径计算列深
856
+ var depth = {}, rawInDeg = {}, outAdj = {}, color = {}, dagAdj = {}, dagInDeg = {};
857
+ DOC.cards.forEach(function (c) {
858
+ depth[c.id] = 0;
859
+ rawInDeg[c.id] = 0;
860
+ outAdj[c.id] = [];
861
+ color[c.id] = 0; // 0=未访问, 1=递归栈中, 2=已完成
862
+ dagAdj[c.id] = [];
863
+ dagInDeg[c.id] = 0;
864
+ });
865
+ DOC.edges.forEach(function (e) {
866
+ if (!e || e.length < 2) return;
867
+ var u = e[0], v = e[1];
868
+ if (u === v || !(u in depth) || !(v in depth)) return;
869
+ if (outAdj[u].indexOf(v) < 0) {
870
+ outAdj[u].push(v);
871
+ rawInDeg[v] += 1;
872
+ }
873
+ });
874
+ var breakCycles = function (startId) {
875
+ // 显式栈迭代 DFS,避免深链爆栈并保证 O(V + E)
876
+ var stack = [{ id: startId, idx: 0 }];
877
+ color[startId] = 1;
878
+ while (stack.length) {
879
+ var top = stack[stack.length - 1];
880
+ var nexts = outAdj[top.id];
881
+ if (top.idx < nexts.length) {
882
+ var nxt = nexts[top.idx++];
883
+ if (color[nxt] === 1) continue; // 回边:直接忽略,打破环
884
+ dagAdj[top.id].push(nxt);
885
+ dagInDeg[nxt] += 1;
886
+ if (color[nxt] === 0) {
887
+ color[nxt] = 1;
888
+ stack.push({ id: nxt, idx: 0 });
889
+ }
890
+ } else {
891
+ color[top.id] = 2;
892
+ stack.pop();
539
893
  }
540
- });
541
- delete onStack[id];
894
+ }
542
895
  };
543
- DOC.cards.forEach(function (c) { if (!indeg[c.id]) sink(c.id); });
544
- // 一个根都没有(整张图全是环)时,随便挑一张起头,剩下的照样能排开
545
- DOC.cards.forEach(function (c) { if (!walked[c.id]) sink(c.id); });
896
+ DOC.cards.forEach(function (c) { if (!rawInDeg[c.id] && color[c.id] === 0) breakCycles(c.id); });
897
+ DOC.cards.forEach(function (c) { if (color[c.id] === 0) breakCycles(c.id); });
898
+ var kahnQ = [];
899
+ DOC.cards.forEach(function (c) { if (dagInDeg[c.id] === 0) kahnQ.push(c.id); });
900
+ var qHead = 0;
901
+ while (qHead < kahnQ.length) {
902
+ var uId = kahnQ[qHead++];
903
+ var nextList = dagAdj[uId];
904
+ for (var ni = 0; ni < nextList.length; ni++) {
905
+ var vId = nextList[ni];
906
+ if (depth[vId] < depth[uId] + 1) depth[vId] = depth[uId] + 1;
907
+ dagInDeg[vId] -= 1;
908
+ if (dagInDeg[vId] === 0) kahnQ.push(vId);
909
+ }
910
+ }
546
911
  var kids = {}, incoming = {}, focused = typeof canvasVisibleCards !== 'undefined' && canvasVisibleCards;
547
912
  DOC.edges.forEach(function (e) {
548
913
  if (!seen[e[0]] || !seen[e[1]] || (!focused && !openEdge[edgeKey(e[0], e[1])])) return;
@@ -1315,7 +1680,18 @@
1315
1680
  var pointer = null, dragged = false, sx = 0, sy = 0, ox = 0, oy = 0;
1316
1681
  document.addEventListener('click', function (e) {
1317
1682
  var z = e.target.closest('.zoomable');
1318
- if (z && !e.target.closest('a.edge')) {
1683
+ if (z && !e.target.closest('a.edge,.zblock-bar,.zblock-btn,.zcode-preview-btn,iframe,video,audio')) {
1684
+ if (z.classList.contains('zui') && !z.classList.contains('is-code')) return;
1685
+ if (z.classList.contains('zsvg')) {
1686
+ if (z.classList.contains('is-code')) {
1687
+ var svgCode = $('pre', z);
1688
+ if (svgCode) openLens(svgCode.outerHTML, 'SVG 源码', 'code');
1689
+ } else {
1690
+ var svgNode = $('.zsvg-stage svg', z);
1691
+ if (svgNode) openLens(svgNode.outerHTML, z.dataset.label, 'media');
1692
+ }
1693
+ return;
1694
+ }
1319
1695
  var media = $('svg,img', z), code = $('pre', z), tbl = $('table', z);
1320
1696
  if (media) openLens(media.outerHTML, z.dataset.label, 'media');
1321
1697
  else if (code) openLens(code.outerHTML, z.dataset.label, 'code');
@@ -1433,6 +1809,11 @@
1433
1809
  function setTheme(t, save) {
1434
1810
  document.documentElement.dataset.theme = t;
1435
1811
  if (save !== false) { try { localStorage.setItem('clado-theme', t); } catch (e) {} }
1812
+ $$('.zui-frame').forEach(function (fr) {
1813
+ if (fr._cladoUiSource != null) {
1814
+ fr.srcdoc = buildUiSandboxDoc(fr._cladoUiSource, fr.dataset.uiFrameId || '');
1815
+ }
1816
+ });
1436
1817
  renderMermaid(true).then(afterMedia);
1437
1818
  document.dispatchEvent(new CustomEvent('clado:view-state'));
1438
1819
  }
@@ -1456,10 +1837,13 @@
1456
1837
  document.body.dataset.mode = 'cards';
1457
1838
  buildCards();
1458
1839
  renderDoc();
1840
+ if (window.__CLADO_WORKSPACE_CHROME__) {
1841
+ DOC.edges.forEach(function (e) { openEdge[edgeKey(e[0], e[1])] = 1; });
1842
+ }
1459
1843
  layout(); bindCards();
1460
1844
  DOC.cards.forEach(function (c) { placePorts(render.card[c.id]); });
1461
1845
  // 先把「谁看得见」算出来,fit() 才知道该按多大范围去缩 ——
1462
- // 默认只展开根,按整张图的尺寸缩的话,唯一那张卡会缩成角落里的一个小点。
1846
+ // 独立静态页默认只展开根;工作台模式下默认展开全部连线让整张网一览无余。
1463
1847
  VIS = visibleSet();
1464
1848
 
1465
1849
  // 主题三级兜底:本机上次切过的 > 编译时写进 <html data-theme> 的 > light。
@@ -1492,18 +1876,37 @@
1492
1876
  * 那一行一动,端口就错位了。ResizeObserver 只管尺寸,
1493
1877
  * 而 placePorts 只动端口(绝对定位),不会引起新的尺寸变化,所以不会打转。
1494
1878
  */
1879
+ if (typeof markIsolatedCards === 'function') markIsolatedCards();
1880
+
1495
1881
  if (window.ResizeObserver) {
1882
+ var roQueue = [];
1883
+ var roRaf = 0;
1496
1884
  var ro = new ResizeObserver(function (ents) {
1497
1885
  if (document.body.dataset.mode !== 'cards') return;
1498
- var hit = false;
1499
- ents.forEach(function (e) {
1500
- var c = e.target;
1501
- if (c.classList && c.classList.contains('card') && VIS[c.dataset.id]) {
1502
- placePorts(c); hit = true;
1886
+ for (var i = 0; i < ents.length; i++) {
1887
+ var c = ents[i].target;
1888
+ if (c && c.isConnected && c.classList && c.classList.contains('card') && VIS[c.dataset.id]) {
1889
+ if (roQueue.indexOf(c) < 0) roQueue.push(c);
1890
+ }
1891
+ }
1892
+ if (!roQueue.length || roRaf) return;
1893
+ roRaf = requestAnimationFrame(function () {
1894
+ roRaf = 0;
1895
+ if (document.body.dataset.mode !== 'cards') { roQueue = []; return; }
1896
+ var batch = roQueue;
1897
+ roQueue = [];
1898
+ var hit = false;
1899
+ for (var j = 0; j < batch.length; j++) {
1900
+ var card = batch[j];
1901
+ if (card && card.isConnected && VIS[card.dataset.id]) {
1902
+ placePorts(card);
1903
+ hit = true;
1904
+ }
1503
1905
  }
1906
+ if (hit) { applyVisibility(); drawWires(); }
1504
1907
  });
1505
- if (hit) { applyVisibility(); drawWires(); }
1506
1908
  });
1909
+ window.__cladoCardRO = ro;
1507
1910
  DOC.cards.forEach(function (c) { ro.observe(render.card[c.id]); });
1508
1911
  }
1509
1912