cladogram 0.6.0-beta.4 → 0.6.0-beta.6

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.
@@ -155,11 +155,23 @@
155
155
 
156
156
  function sanitizeSvgMarkup(raw) {
157
157
  var src = String(raw || '').trim();
158
- if (!src || src.indexOf('<svg') < 0) return null;
158
+ if (!src || !/<svg\b/i.test(src)) return null;
159
+ if (!/\bxmlns\s*=/i.test(src)) {
160
+ src = src.replace(/<svg\b/i, '<svg xmlns="http://www.w3.org/2000/svg"');
161
+ }
162
+ if (/\bxlink:/i.test(src) && !/\bxmlns:xlink\s*=/i.test(src)) {
163
+ src = src.replace(/<svg\b/i, '<svg xmlns:xlink="http://www.w3.org/1999/xlink"');
164
+ }
159
165
  var svg = null;
160
166
  try {
161
167
  var parsed = new DOMParser().parseFromString(src, 'image/svg+xml');
162
- if (parsed && !parsed.querySelector('parsererror') && parsed.documentElement && parsed.documentElement.localName === 'svg') {
168
+ if (
169
+ parsed
170
+ && !parsed.querySelector('parsererror')
171
+ && parsed.documentElement
172
+ && parsed.documentElement.localName === 'svg'
173
+ && parsed.documentElement.namespaceURI === 'http://www.w3.org/2000/svg'
174
+ ) {
163
175
  svg = document.importNode(parsed.documentElement, true);
164
176
  }
165
177
  } catch (e) {}
@@ -258,6 +270,7 @@
258
270
  var wrap = document.createElement('div');
259
271
  wrap.className = 'zoomable zui';
260
272
  wrap.dataset.label = 'UI 原型预览';
273
+ wrap.dataset.rawSource = rawCode;
261
274
 
262
275
  var bar = document.createElement('div');
263
276
  bar.className = 'zblock-bar';
@@ -288,47 +301,14 @@
288
301
  wrap.appendChild(bar);
289
302
  wrap.appendChild(stage);
290
303
  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
304
  return wrap;
324
305
  }
325
306
 
326
- CladoBlockRegistry.register('svg', function (rawCode, lang, ctx) {
327
- var cleanSvg = sanitizeSvgMarkup(rawCode);
328
- if (!cleanSvg) return null;
307
+ function createSvgBlock(rawCode, cleanSvg, preEl) {
329
308
  var wrap = document.createElement('div');
330
309
  wrap.className = 'zoomable zsvg';
331
310
  wrap.dataset.label = 'SVG 矢量图';
311
+ wrap.dataset.rawSource = rawCode;
332
312
 
333
313
  var bar = document.createElement('div');
334
314
  bar.className = 'zblock-bar';
@@ -344,29 +324,18 @@
344
324
  stage.title = '点一下放大';
345
325
  stage.appendChild(cleanSvg);
346
326
 
347
- var preEl = ctx.pre;
348
- if (window.hljs && ctx.code) { try { hljs.highlightElement(ctx.code); } catch (e) {} }
349
327
  preEl.classList.add('zsvg-code');
350
-
351
328
  wrap.appendChild(bar);
352
329
  wrap.appendChild(stage);
353
330
  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
331
  return wrap;
332
+ }
333
+
334
+ CladoBlockRegistry.register('svg', function (rawCode, lang, ctx) {
335
+ var cleanSvg = sanitizeSvgMarkup(rawCode);
336
+ if (!cleanSvg) return null;
337
+ if (window.hljs && ctx.code) { try { hljs.highlightElement(ctx.code); } catch (e) {} }
338
+ return createSvgBlock(rawCode, cleanSvg, ctx.pre);
370
339
  });
371
340
 
372
341
  CladoBlockRegistry.register(['ui', 'html-preview'], function (rawCode, lang, ctx) {
@@ -385,12 +354,24 @@
385
354
  box.className = 'markdown-body';
386
355
  if (!md) return box;
387
356
  var template = document.createElement('template'); template.innerHTML = marked.parse(md);
357
+ // 兼容 AI 直接在正文中裸写 <svg>...</svg>(不带 ```svg 围栏)时同样安全清洗并支持放大
358
+ var inlineSvgs = [];
359
+ Array.from(template.content.querySelectorAll('svg')).forEach(function (svgNode) {
360
+ if (svgNode.parentNode && svgNode.parentNode.closest && svgNode.parentNode.closest('svg')) return;
361
+ var rawSvg = svgNode.outerHTML || '';
362
+ var cleaned = sanitizeSvgMarkup(rawSvg);
363
+ if (!cleaned) { svgNode.remove(); return; }
364
+ var ph = document.createElement('div');
365
+ ph.dataset.cladoInlineSvg = String(inlineSvgs.length);
366
+ inlineSvgs.push({ raw: rawSvg, node: cleaned });
367
+ svgNode.parentNode.replaceChild(ph, svgNode);
368
+ });
388
369
  var tags = new Set('a p br hr em strong del code pre blockquote ul ol li table thead tbody tr th td img h1 h2 h3 h4 h5 h6 details summary sub sup kbd span div'.split(' '));
389
370
  Array.from(template.content.querySelectorAll('*')).forEach(function (element) {
390
371
  var tag = element.localName;
391
372
  if (!tags.has(tag)) { element.remove(); return; }
392
373
  Array.from(element.attributes).forEach(function (attribute) {
393
- var name=attribute.name, value=attribute.value, allow=['title','alt','width','height','start','align'].includes(name);
374
+ var name=attribute.name, value=attribute.value, allow=['title','alt','width','height','start','align','data-clado-inline-svg'].includes(name);
394
375
  if (name === 'class' && tag === 'code') allow = /^language-[\w+#.-]+$/.test(value);
395
376
  if (name === 'href' && tag === 'a') allow = !/^[a-z][a-z0-9+.-]*:/i.test(value.replace(/[\x00-\x20]/g,'')) || /^(https?:|mailto:|tel:)/i.test(value);
396
377
  if (name === 'src' && tag === 'img') allow = !/^[a-z][a-z0-9+.-]*:/i.test(value.replace(/[\x00-\x20]/g,'')) || /^(https?:|data:image\/(png|jpeg|gif|webp);base64,)/i.test(value);
@@ -399,7 +380,21 @@
399
380
  });
400
381
  box.appendChild(template.content);
401
382
 
402
- // 媒体交付件升级:![说明](demo.mp4/.webm/.mov) → 原生视频播放器;.mp3/.wav/.m4a → 原生音频播放器
383
+ // 还原裸写 <svg> 为带「放大 / 源码」的 .zoomable.zsvg
384
+ $$('[data-clado-inline-svg]', box).forEach(function (ph) {
385
+ var entry = inlineSvgs[Number(ph.dataset.cladoInlineSvg)];
386
+ if (!entry) { ph.remove(); return; }
387
+ var pre = document.createElement('pre');
388
+ var code = document.createElement('code');
389
+ code.className = 'language-svg';
390
+ code.textContent = entry.raw;
391
+ pre.appendChild(code);
392
+ if (window.hljs) { try { hljs.highlightElement(code); } catch (e) {} }
393
+ var svgBlock = createSvgBlock(entry.raw, entry.node, pre);
394
+ ph.parentNode.replaceChild(svgBlock, ph);
395
+ });
396
+
397
+ // 媒体交付件升级:视频 / 音频 / 可放大普通图片(含 .png/.jpg/.webp/.gif/.svg)
403
398
  $$('img[src]', box).forEach(function (img) {
404
399
  var src = img.getAttribute('src') || '';
405
400
  var alt = (img.getAttribute('alt') || '').trim();
@@ -436,6 +431,22 @@
436
431
  audio.setAttribute('src', src);
437
432
  aw.appendChild(audio);
438
433
  img.parentNode.replaceChild(aw, img);
434
+ return;
435
+ }
436
+ // 普通图片同样包进 .zoomable.zimg,点击即弹出放大镜全屏缩放查看
437
+ if (!img.closest('.zoomable')) {
438
+ var iw = document.createElement('div');
439
+ iw.className = 'zoomable zimg';
440
+ iw.dataset.label = alt || '图片';
441
+ iw.title = '点一下放大';
442
+ img.parentNode.insertBefore(iw, img);
443
+ iw.appendChild(img);
444
+ if (alt) {
445
+ var icap = document.createElement('div');
446
+ icap.className = 'zmedia-cap';
447
+ icap.textContent = alt;
448
+ iw.appendChild(icap);
449
+ }
439
450
  }
440
451
  });
441
452
 
@@ -486,6 +497,10 @@
486
497
  z.className = 'zoomable zcode';
487
498
  z.dataset.label = (lang || '代码') + ' 代码';
488
499
  z.title = '点一下放大';
500
+ if (lang === 'html') {
501
+ z.dataset.lang = 'html';
502
+ z.dataset.rawSource = rawCode;
503
+ }
489
504
  pre.parentNode.insertBefore(z, pre);
490
505
  z.appendChild(pre);
491
506
  if (lang === 'html') {
@@ -494,12 +509,6 @@
494
509
  prevBtn.className = 'zcode-preview-btn';
495
510
  prevBtn.textContent = '预览';
496
511
  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
512
  z.appendChild(prevBtn);
504
513
  }
505
514
  });
@@ -1631,9 +1640,11 @@
1631
1640
  function lensFit() {
1632
1641
  var st = $('#lensStage').getBoundingClientRect(), inn = $('#lensInner');
1633
1642
  var w = inn.offsetWidth, h = inn.offsetHeight;
1643
+ if (!w || !h || !st.width || !st.height) return;
1634
1644
  // 一行代码也撑到 400% 就太夸张了,留个上限
1635
1645
  var cap = Number(inn.dataset.cap || 4);
1636
- ls = Math.min(cap, Math.min((st.width - 90) / w, (st.height - 140) / h));
1646
+ var fitScale = Math.min((st.width - 90) / w, (st.height - 140) / h);
1647
+ ls = Math.max(0.2, Math.min(cap, isFinite(fitScale) && fitScale > 0 ? fitScale : 1));
1637
1648
  lx = (st.width - w * ls) / 2; ly = (st.height - h * ls) / 2;
1638
1649
  lensApply();
1639
1650
  }
@@ -1642,15 +1653,33 @@
1642
1653
  lensSeq++;
1643
1654
  var inn = $('#lensInner');
1644
1655
  inn.innerHTML = kind ? '<div class="markdown-body">' + html + '</div>' : html;
1645
- if (kind) { inn.dataset.cap = kind === 'code' ? '2.2' : '3'; inn.dataset.kind = kind; }
1646
- else { delete inn.dataset.cap; delete inn.dataset.kind; }
1656
+ if (kind) {
1657
+ inn.dataset.cap = kind === 'code' ? '2.2' : kind === 'ui' ? '1.5' : '3';
1658
+ inn.dataset.kind = kind;
1659
+ } else {
1660
+ delete inn.dataset.cap; delete inn.dataset.kind;
1661
+ }
1647
1662
  $$('svg', inn).forEach(function (s) {
1648
1663
  var vb = s.viewBox && s.viewBox.baseVal;
1664
+ var vbW = (vb && vb.width > 0) ? vb.width : 0;
1665
+ var vbH = (vb && vb.height > 0) ? vb.height : 0;
1666
+ if (!vbW || !vbH) {
1667
+ var vbAttr = String(s.getAttribute('viewBox') || '').trim().split(/[\s,]+/);
1668
+ if (vbAttr.length === 4) {
1669
+ vbW = parseFloat(vbAttr[2]) || vbW;
1670
+ vbH = parseFloat(vbAttr[3]) || vbH;
1671
+ }
1672
+ }
1673
+ var targetW = vbW || 640;
1674
+ var targetH = vbH || Math.round(targetW * 0.625);
1649
1675
  s.removeAttribute('width'); s.removeAttribute('height');
1650
- s.style.width = ((vb && vb.width) || 640) + 'px';
1651
- s.style.height = 'auto';
1676
+ s.style.width = targetW + 'px';
1677
+ s.style.height = targetH + 'px';
1678
+ });
1679
+ $$('img', inn).forEach(function (i) {
1680
+ i.style.maxWidth = 'none';
1681
+ if (!i.complete) i.addEventListener('load', function () { requestAnimationFrame(lensFit); }, { once: true });
1652
1682
  });
1653
- $$('img', inn).forEach(function (i) { i.style.maxWidth = 'none'; });
1654
1683
  $('#lensLabel').textContent = label || '图';
1655
1684
  if (!$('#lens').classList.contains('open')) {
1656
1685
  $('#lens').classList.add('open');
@@ -1679,16 +1708,93 @@
1679
1708
  var st = $('#lensStage');
1680
1709
  var pointer = null, dragged = false, sx = 0, sy = 0, ox = 0, oy = 0;
1681
1710
  document.addEventListener('click', function (e) {
1711
+ // 1. 富媒体块工具栏按钮统一委托(兼容卡片/脑图/文档与克隆链路弹窗)
1712
+ var svgZoomBtn = e.target.closest('[data-svg-zoom]');
1713
+ if (svgZoomBtn) {
1714
+ e.stopPropagation();
1715
+ var svgWrap = svgZoomBtn.closest('.zsvg');
1716
+ var svgNode = svgWrap && $('.zsvg-stage svg', svgWrap);
1717
+ if (svgNode) openLens(svgNode.outerHTML, svgWrap.dataset.label || 'SVG 矢量图', 'media');
1718
+ return;
1719
+ }
1720
+ var svgModeBtn = e.target.closest('[data-svg-mode]');
1721
+ if (svgModeBtn) {
1722
+ e.stopPropagation();
1723
+ var svgW = svgModeBtn.closest('.zsvg');
1724
+ if (!svgW) return;
1725
+ var showSvgCode = svgW.classList.toggle('is-code');
1726
+ svgModeBtn.textContent = showSvgCode ? '视图' : '源码';
1727
+ svgModeBtn.classList.toggle('active', showSvgCode);
1728
+ if (document.body.dataset.mode === 'cards') { try { repinPorts(); drawWires(); } catch (err) {} }
1729
+ return;
1730
+ }
1731
+ var uiVpBtn = e.target.closest('[data-ui-vp]');
1732
+ if (uiVpBtn) {
1733
+ e.stopPropagation();
1734
+ var uiW = uiVpBtn.closest('.zui');
1735
+ if (!uiW) return;
1736
+ var isMob = uiVpBtn.dataset.uiVp === 'mobile';
1737
+ uiW.classList.toggle('is-mobile', isMob);
1738
+ $$('[data-ui-vp]', uiW).forEach(function (b) { b.classList.toggle('active', b === uiVpBtn); });
1739
+ return;
1740
+ }
1741
+ var uiModeBtn = e.target.closest('[data-ui-mode]');
1742
+ if (uiModeBtn) {
1743
+ e.stopPropagation();
1744
+ var uiM = uiModeBtn.closest('.zui');
1745
+ if (!uiM) return;
1746
+ var showUiCode = uiM.classList.toggle('is-code');
1747
+ uiModeBtn.textContent = showUiCode ? '预览' : '代码';
1748
+ uiModeBtn.classList.toggle('active', showUiCode);
1749
+ if (document.body.dataset.mode === 'cards') { try { repinPorts(); drawWires(); } catch (err) {} }
1750
+ return;
1751
+ }
1752
+ var uiZoomBtn = e.target.closest('[data-ui-zoom]');
1753
+ if (uiZoomBtn) {
1754
+ e.stopPropagation();
1755
+ var uiZ = uiZoomBtn.closest('.zui');
1756
+ var uiRaw = uiZ ? (uiZ.dataset.rawSource || ($('pre code', uiZ)?.textContent || '')) : '';
1757
+ var lensHtml = '<iframe class="zui-lens-frame" sandbox="allow-scripts" title="UI 原型全屏预览"></iframe>';
1758
+ openLens(lensHtml, 'UI 原型预览', 'ui');
1759
+ var lensFr = $('#lensInner .zui-lens-frame');
1760
+ if (lensFr) lensFr.srcdoc = buildUiSandboxDoc(uiRaw, '');
1761
+ return;
1762
+ }
1763
+ var htmlPrevBtn = e.target.closest('.zcode-preview-btn');
1764
+ if (htmlPrevBtn) {
1765
+ e.stopPropagation();
1766
+ var zc = htmlPrevBtn.closest('.zcode');
1767
+ var preEl = zc && $('pre', zc);
1768
+ if (!zc || !preEl) return;
1769
+ var rawHtml = zc.dataset.rawSource || ($('code', preEl)?.textContent || '');
1770
+ var uiBlock = createUiPreviewBlock(rawHtml, preEl);
1771
+ if (zc.parentNode) zc.parentNode.replaceChild(uiBlock, zc);
1772
+ if (document.body.dataset.mode === 'cards') { try { repinPorts(); drawWires(); } catch (err) {} }
1773
+ return;
1774
+ }
1775
+
1776
+ // 2. 点击任意 .zoomable 内容区(图片 / SVG / Mermaid / 代码 / 表格)直接弹出放大镜
1682
1777
  var z = e.target.closest('.zoomable');
1683
1778
  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;
1779
+ if (z.classList.contains('zui')) {
1780
+ if (z.classList.contains('is-code')) {
1781
+ var uiCode = $('pre', z);
1782
+ if (uiCode) openLens(uiCode.outerHTML, 'UI 源码', 'code');
1783
+ } else {
1784
+ var uiSrc = z.dataset.rawSource || ($('pre code', z)?.textContent || '');
1785
+ openLens('<iframe class="zui-lens-frame" sandbox="allow-scripts" title="UI 原型全屏预览"></iframe>', 'UI 原型预览', 'ui');
1786
+ var lFr = $('#lensInner .zui-lens-frame');
1787
+ if (lFr) lFr.srcdoc = buildUiSandboxDoc(uiSrc, '');
1788
+ }
1789
+ return;
1790
+ }
1685
1791
  if (z.classList.contains('zsvg')) {
1686
1792
  if (z.classList.contains('is-code')) {
1687
1793
  var svgCode = $('pre', z);
1688
1794
  if (svgCode) openLens(svgCode.outerHTML, 'SVG 源码', 'code');
1689
1795
  } else {
1690
- var svgNode = $('.zsvg-stage svg', z);
1691
- if (svgNode) openLens(svgNode.outerHTML, z.dataset.label, 'media');
1796
+ var svgEl = $('.zsvg-stage svg', z);
1797
+ if (svgEl) openLens(svgEl.outerHTML, z.dataset.label || 'SVG 矢量图', 'media');
1692
1798
  }
1693
1799
  return;
1694
1800
  }
@@ -1721,7 +1827,7 @@
1721
1827
  st.addEventListener('pointerdown', function (e) {
1722
1828
  if (pointer !== null) return;
1723
1829
  dragged = false;
1724
- if (e.button !== 0 || e.target.closest('pre,table,svg text,svg foreignObject,a')) return;
1830
+ if (e.button !== 0 || e.target.closest('pre,table,svg text,svg foreignObject,a,iframe')) return;
1725
1831
  pointer = e.pointerId;
1726
1832
  sx = e.clientX; sy = e.clientY; ox = lx; oy = ly;
1727
1833
  });