cladogram 0.5.4 → 0.5.5-beta.2

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.
Files changed (54) hide show
  1. package/README.md +8 -6
  2. package/bin/clado.js +5 -5
  3. package/docs/release.md +13 -0
  4. package/package.json +1 -1
  5. package/plugins/agent/index.js +458 -25
  6. package/plugins/agent/style.css +299 -8
  7. package/plugins/directory/capabilities.json +1 -1
  8. package/plugins/directory/index.js +57 -2
  9. package/plugins/directory/style.css +11 -0
  10. package/plugins/view-controls/index.js +1 -1
  11. package/plugins/view-controls/style.css +30 -10
  12. package/skills/clado/SKILL.md +17 -16
  13. package/src/agent/executors.js +213 -0
  14. package/src/agent/probe.js +176 -0
  15. package/src/agent/providers/anthropic.js +220 -0
  16. package/src/agent/providers/index.js +52 -0
  17. package/src/agent/providers/openai.js +146 -0
  18. package/src/agent/runtime.js +557 -0
  19. package/src/agent/sessions.js +129 -0
  20. package/src/html/app-css.js +62 -9
  21. package/src/html/canvas-chain.js +2 -1
  22. package/src/html/canvas-ui.css +63 -0
  23. package/src/html/page.js +4 -0
  24. package/src/html/plugin-ui.css +12 -0
  25. package/src/html/plugin-ui.js +8 -2
  26. package/src/html/runtime-api.js +136 -5
  27. package/src/html/runtime.js +160 -290
  28. package/src/html/view-registry.js +95 -0
  29. package/src/html/views/view-cards.js +28 -0
  30. package/src/html/views/view-doc.js +51 -0
  31. package/src/html/views/view-mindmap.js +173 -0
  32. package/src/server/agent.js +335 -17
  33. package/src/server/assets/library-canvas.js +13 -3
  34. package/src/server/assets/library-plugin-bridge.js +2 -2
  35. package/src/server/assets/library-plugins.js +2 -2
  36. package/src/server/assets/library.css +5 -0
  37. package/src/server/assets/library.html +1 -1
  38. package/src/server/assets/library.js +157 -20
  39. package/src/server/events.js +189 -0
  40. package/src/server/export.js +1 -1
  41. package/src/server/mcp.js +16 -16
  42. package/src/server/plugin-capabilities.js +38 -22
  43. package/src/server/plugin-settings.js +27 -1
  44. package/src/server/plugins.js +17 -4
  45. package/src/server/workspace.js +171 -56
  46. package/src/skill/install.js +1 -1
  47. package/src/skill/targets.js +3 -3
  48. package/src/util/update.js +3 -2
  49. package/src/workspace/service.js +23 -15
  50. package/src/workspace/storage.js +88 -20
  51. package/templates/business-flow.mdc +1 -1
  52. package/templates/call-chain.mdc +1 -1
  53. package/templates/data-design.mdc +1 -1
  54. package/templates/test-cases.mdc +47 -0
@@ -529,35 +529,59 @@
529
529
  DOC.cards.forEach(function (c) { if (!indeg[c.id]) sink(c.id); });
530
530
  // 一个根都没有(整张图全是环)时,随便挑一张起头,剩下的照样能排开
531
531
  DOC.cards.forEach(function (c) { if (!walked[c.id]) sink(c.id); });
532
- var kids = {}, focused = typeof canvasVisibleCards !== 'undefined' && canvasVisibleCards;
532
+ var kids = {}, incoming = {}, focused = typeof canvasVisibleCards !== 'undefined' && canvasVisibleCards;
533
533
  DOC.edges.forEach(function (e) {
534
534
  if (!seen[e[0]] || !seen[e[1]] || (!focused && !openEdge[edgeKey(e[0], e[1])])) return;
535
- (kids[e[0]] = kids[e[0]] || []).push(e[1]);
536
- });
537
- /*
538
- * ★ 同一列里的上下顺序,按**边出现的先后**排,不按卡片在文档里的先后。
539
- * 原来直接 `vis.forEach` 走卡片顺序:`订单明细表` 那一段写在文档最前面,
540
- * 它就先被塞进第 2 列、排在最上面 —— 可它明明是从「明细」那一节连出去的,
541
- * 应该排在 `用户表`、`商品表` 后面。用户看到的「订单明细表跑到最上面去了」
542
- * 就是这个。改成先按 DOC.edges 的顺序收,剩下的(没有入边的根)再补。
543
- */
544
- var cols = {}, inCol = {};
545
- DOC.edges.forEach(function (e) {
546
- if (!seen[e[0]] || !seen[e[1]] || (!focused && !openEdge[edgeKey(e[0], e[1])]) || inCol[e[1]]) return;
547
- inCol[e[1]] = 1;
548
- (cols[depth[e[1]]] = cols[depth[e[1]]] || []).push(e[1]);
535
+ var list = kids[e[0]] || (kids[e[0]] = []);
536
+ if (list.indexOf(e[1]) < 0) list.push(e[1]);
537
+ if (depth[e[0]] < depth[e[1]]) {
538
+ var parents = incoming[e[1]] || (incoming[e[1]] = []);
539
+ if (parents.indexOf(e[0]) < 0) parents.push(e[0]);
540
+ }
549
541
  });
550
- vis.forEach(function (id) {
551
- if (inCol[id]) return;
542
+ // 按列自左向右排定列内初始顺序:遵循左侧已排好父卡的上下顺序及父卡内首次引用顺序,避免深层列上下颠倒。
543
+ var cols = {}, docIndex = {};
544
+ vis.forEach(function (id, index) {
545
+ docIndex[id] = index;
552
546
  (cols[depth[id]] = cols[depth[id]] || []).push(id);
553
547
  });
554
- pos = {};
555
548
  var keys = Object.keys(cols).map(Number).sort(function (a, b) { return a - b; });
549
+ var colRank = {};
550
+ keys.forEach(function (d, ki) {
551
+ if (ki > 0) {
552
+ var meta = {};
553
+ cols[d].forEach(function (id) {
554
+ var parents = (incoming[id] || []).filter(function (p) { return p in colRank; });
555
+ if (parents.length) {
556
+ var sum = 0, minRank = Infinity, minRef = Infinity;
557
+ parents.forEach(function (p) {
558
+ sum += colRank[p];
559
+ if (colRank[p] < minRank) {
560
+ minRank = colRank[p];
561
+ minRef = (kids[p] || []).indexOf(id);
562
+ }
563
+ });
564
+ meta[id] = { hasParent: 1, avg: sum / parents.length, minRank: minRank, minRef: minRef < 0 ? 1e9 : minRef };
565
+ } else {
566
+ meta[id] = { hasParent: 0, avg: 1e9, minRank: 1e9, minRef: 1e9 };
567
+ }
568
+ });
569
+ cols[d].sort(function (a, b) {
570
+ var ma = meta[a], mb = meta[b];
571
+ if (ma.hasParent !== mb.hasParent) return mb.hasParent - ma.hasParent;
572
+ if (ma.avg !== mb.avg) return ma.avg - mb.avg;
573
+ if (ma.minRank !== mb.minRank) return ma.minRank - mb.minRank;
574
+ if (ma.minRef !== mb.minRef) return ma.minRef - mb.minRef;
575
+ return docIndex[a] - docIndex[b];
576
+ });
577
+ }
578
+ cols[d].forEach(function (id, idx) { colRank[id] = idx; });
579
+ });
580
+ pos = {};
556
581
  /*
557
582
  * ★ 每一列的 x 得按**左边那一列最宽的那张卡**让位。
558
583
  * 卡片会被 Mermaid 图撑宽(最宽到 1500px),原来写死 `PAD + d * (W + COL)` ——
559
- * 一张被撑到 1200 的卡就直接压在右边那一列的卡身上了。这就是「缩放的时候
560
- * 卡片会重叠」的来源(不是只算了一层,是压根没算宽度)。
584
+ * 一张被撑到 1200 的卡就直接压在右边那一列的卡身上了。
561
585
  */
562
586
  var colX = {}, cx = PAD, naturalY = {};
563
587
  keys.forEach(function (d) {
@@ -566,50 +590,83 @@
566
590
  cols[d].forEach(function (id) { wmax = Math.max(wmax, render.card[id].offsetWidth || W); });
567
591
  cx += wmax + COL;
568
592
  });
593
+ // 统一按左上角蓝色竖条中心(y + 47px)对齐:
594
+ // 因为每张卡的蓝色竖条距离卡片顶部都是固定偏移,父卡蓝条对齐子卡蓝条中点即 parent.y = (minChildY + maxChildY) / 2。
595
+ // 无子卡的同列兄弟不再写死到 PAD,而是就近紧贴相邻已定位兄弟,避免长分支把上方叶卡甩飞。
569
596
  keys.slice().reverse().forEach(function (d) {
570
597
  var col = cols[d].map(function (id) {
571
598
  var h = render.card[id].offsetHeight;
572
- var ks = kids[id] || [], top = Infinity, bot = -Infinity;
599
+ var ks = kids[id] || [], topY = Infinity, botY = -Infinity;
573
600
  ks.forEach(function (q) {
574
- if (!pos[q]) return;
575
- top = Math.min(top, pos[q].y);
576
- bot = Math.max(bot, pos[q].y + render.card[q].offsetHeight);
601
+ if (!pos[q] || depth[q] <= d) return;
602
+ topY = Math.min(topY, pos[q].y);
603
+ botY = Math.max(botY, pos[q].y);
577
604
  });
578
- var want = isFinite(top) ? (top + bot) / 2 - h / 2 : PAD;
579
- naturalY[id] = want;
580
- return { id: id, h: h, want: want };
605
+ var hasWant = isFinite(topY);
606
+ var want = hasWant ? (topY + botY) / 2 : null;
607
+ if (hasWant) naturalY[id] = want;
608
+ return { id: id, h: h, hasWant: hasWant, want: want, y: 0 };
581
609
  });
582
- // 后代的位置只影响纵向对齐。同一父卡的子卡始终按正文首次引用的顺序排列。
583
- var prev = null;
610
+ var firstAnchored = col.findIndex(function (it) { return it.hasWant; });
611
+ if (firstAnchored < 0) {
612
+ var cursor = PAD;
613
+ col.forEach(function (it) {
614
+ it.y = cursor;
615
+ cursor += it.h + ROW;
616
+ });
617
+ } else {
618
+ // 先为有子卡的节点从上到下分配不重叠位置(为中间夹着的无子卡兄弟预留足够高度),
619
+ // 再把首段无子卡兄弟贴紧在首个有子卡节点上方、中间与尾段无子卡兄弟紧贴在上方兄弟下方。
620
+ var prevAnchor = -1;
621
+ for (var i = 0; i < col.length; i++) {
622
+ if (!col[i].hasWant) continue;
623
+ var y = col[i].want;
624
+ if (prevAnchor >= 0) {
625
+ var minY = col[prevAnchor].y + col[prevAnchor].h + ROW;
626
+ for (var m = prevAnchor + 1; m < i; m++) minY += col[m].h + ROW;
627
+ if (y < minY) y = minY;
628
+ }
629
+ col[i].y = y;
630
+ if (prevAnchor >= 0) {
631
+ var cur = col[prevAnchor].y + col[prevAnchor].h + ROW;
632
+ for (var m2 = prevAnchor + 1; m2 < i; m2++) {
633
+ col[m2].y = cur;
634
+ cur += col[m2].h + ROW;
635
+ }
636
+ }
637
+ prevAnchor = i;
638
+ }
639
+ // 首段无子卡节点向上紧贴首个已定位兄弟,不再飞到顶部 PAD。
640
+ for (var k = firstAnchored - 1; k >= 0; k--) {
641
+ col[k].y = col[k + 1].y - col[k].h - ROW;
642
+ }
643
+ // 尾段无子卡节点向下紧贴最后一个已定位兄弟。
644
+ for (var t = prevAnchor + 1; t < col.length; t++) {
645
+ col[t].y = col[t - 1].y + col[t - 1].h + ROW;
646
+ }
647
+ }
584
648
  col.forEach(function (it) {
585
- // 先不夹到 PAD:父亲比儿子们高一大截时「居中」是要往负数走的,
586
- // 夹住就退化成「跟第一个儿子齐平」——正好是这次要修的那个毛病。
587
- // 越界统一留到最后整体下移。
588
- var y = it.want;
589
- if (prev && y < prev.y + prev.h + ROW) y = prev.y + prev.h + ROW;
590
- pos[it.id] = { x: colX[d], y: y };
591
- prev = { y: y, h: it.h };
649
+ pos[it.id] = { x: colX[d], y: it.y };
592
650
  });
593
651
  });
594
- // 左列卡片互相让位后的位移要传给右列分支;共享子卡取所有父卡位移的平均值。
652
+ // 左列卡片互相让位后的位移传给右列分支,保序且用同列等距分组消解碰撞。
595
653
  if (!anchor || !prevPos) {
596
- var incoming = {};
597
- Object.keys(kids).forEach(function (parent) {
598
- kids[parent].forEach(function (child) {
599
- if (depth[parent] < depth[child]) (incoming[child] || (incoming[child] = [])).push(parent);
600
- });
601
- });
602
654
  keys.slice(1).forEach(function (d) {
603
- var parentY = {}, desiredY = {};
604
- cols[d].forEach(function (id) {
655
+ var parentY = {}, desiredY = {}, colOrder = {};
656
+ cols[d].forEach(function (id, idx) {
657
+ colOrder[id] = idx;
605
658
  var parents = incoming[id] || [];
606
659
  parentY[id] = parents.length ? parents.reduce(function (sum, parent) { return sum + pos[parent].y; }, 0) / parents.length : pos[id].y;
607
- var shift = parents.length ? parents.reduce(function (sum, parent) { return sum + pos[parent].y - naturalY[parent]; }, 0) / parents.length : 0;
660
+ var shifted = parents.filter(function (parent) { return naturalY[parent] !== undefined; });
661
+ var shift = shifted.length ? shifted.reduce(function (sum, parent) { return sum + (pos[parent].y - naturalY[parent]); }, 0) / shifted.length : 0;
608
662
  desiredY[id] = pos[id].y + shift;
609
663
  });
610
- // 同列先按父卡顺序分组。同组保留引用顺序,相撞时把间距分摊给两边。
664
+ var ordered = cols[d].slice().sort(function (a, b) {
665
+ if (parentY[a] !== parentY[b]) return parentY[a] - parentY[b];
666
+ return colOrder[a] - colOrder[b];
667
+ });
611
668
  var groups = [], offset = 0;
612
- cols[d].slice().sort(function (a, b) { return parentY[a] - parentY[b]; }).forEach(function (id) {
669
+ ordered.forEach(function (id) {
613
670
  groups.push({ cards: [{ id: id, offset: offset }], y: desiredY[id] - offset });
614
671
  offset += render.card[id].offsetHeight + ROW;
615
672
  while (groups.length > 1 && groups[groups.length - 2].y > groups[groups.length - 1].y) {
@@ -625,9 +682,8 @@
625
682
  }
626
683
  /*
627
684
  * ★ 收起 / 展开只动「被点的那张 + 它的**所有后代**」,别的卡一个像素都不挪。
628
- * 做法:先照常全局摆一遍,然后把不在 moved 里的卡片 y 换回快照里的旧值,
629
- * 最后按列把 moved 的卡片往下推开,避开这些钉住的卡。
630
- * (用户的原话:「除了当前节点以及它自己的儿子,其他的节点位置都不动」。)
685
+ * 避让同列固定卡时尊重父卡上下顺序:上方父卡的子卡优先往上让位,下方父卡的子卡优先往下让位,
686
+ * 避免上面的子卡被推到下面调用卡片的下方错位。
631
687
  */
632
688
  var moved = null;
633
689
  if (anchor && prevPos) {
@@ -639,7 +695,7 @@
639
695
  moved[walkId] = 1;
640
696
  (kids[walkId] || []).forEach(function (q) { stack.push(q); });
641
697
  }
642
- // 钉回父卡时整支一起平移,保留可见孩子围绕父卡的居中关系。
698
+ // 钉回父卡时整支一起平移,保留可见孩子围绕父卡蓝条的对齐关系。
643
699
  if (pos[anchor] && prevPos[anchor]) {
644
700
  var dx = prevPos[anchor].x - pos[anchor].x, dy = prevPos[anchor].y - pos[anchor].y;
645
701
  Object.keys(moved).forEach(function (id) {
@@ -650,23 +706,25 @@
650
706
  if (pos[c.id] && !moved[c.id] && prevPos[c.id]) pos[c.id].y = prevPos[c.id].y;
651
707
  if (pos[c.id] && !moved[c.id] && prevPos[c.id]) pos[c.id].x = prevPos[c.id].x;
652
708
  });
653
- /*
654
- * ★ 被点的这张**自己也不许动**。用户原话:「我点的这个标题的这个卡片,它能不动吗?」
655
- * 它虽然在 moved 里(要跟着走一遍好收放后代),但位置得钉死在原地 ——
656
- * 手指点哪儿,那张卡就待在哪儿,只有它右边那一串后代会重摆。
657
- */
658
709
  if (pos[anchor] && prevPos[anchor]) pos[anchor].y = prevPos[anchor].y;
710
+ var anchorY = pos[anchor] ? pos[anchor].y : 0;
659
711
  keys.forEach(function (d) {
660
- var moving = [], pinned = [], blocked = [];
712
+ var moving = [], pinned = [], blocked = [], collidingParents = [];
661
713
  cols[d].forEach(function (id) {
662
714
  if (CARD_PLACEMENTS[id]) pos[id] = Object.assign({}, CARD_PLACEMENTS[id]);
663
715
  (moved[id] && id !== anchor && !CARD_PLACEMENTS[id] ? moving : pinned).push(id);
664
716
  });
665
- // 整组让开固定卡,保持孩子间距;选择离父卡最近的上下空位。
717
+ if (!moving.length) return;
666
718
  moving.forEach(function (id) {
667
719
  pinned.forEach(function (fixed) {
668
- blocked.push({ lo: pos[fixed].y - ROW - pos[id].y - render.card[id].offsetHeight,
669
- hi: pos[fixed].y + render.card[fixed].offsetHeight + ROW - pos[id].y });
720
+ var lo = pos[fixed].y - ROW - pos[id].y - render.card[id].offsetHeight;
721
+ var hi = pos[fixed].y + render.card[fixed].offsetHeight + ROW - pos[id].y;
722
+ blocked.push({ lo: lo, hi: hi });
723
+ if (lo < 0 && hi > 0) {
724
+ var fp = (incoming[fixed] || []).filter(function (p) { return pos[p]; });
725
+ var py = fp.length ? fp.reduce(function (s, p) { return s + pos[p].y; }, 0) / fp.length : pos[fixed].y;
726
+ collidingParents.push(py);
727
+ }
670
728
  });
671
729
  });
672
730
  blocked.sort(function (a, b) { return a.lo - b.lo; });
@@ -677,11 +735,31 @@
677
735
  else intervals.push(range);
678
736
  });
679
737
  var shift = 0;
738
+ var avgPinnedParentY = collidingParents.length
739
+ ? collidingParents.reduce(function (s, y) { return s + y; }, 0) / collidingParents.length
740
+ : null;
680
741
  intervals.forEach(function (range) {
681
- if (range.lo < 0 && range.hi > 0) shift = Math.abs(range.lo) < Math.abs(range.hi) ? range.lo : range.hi;
742
+ if (range.lo < 0 && range.hi > 0) {
743
+ if (avgPinnedParentY !== null && Math.abs(anchorY - avgPinnedParentY) > 1) {
744
+ shift = anchorY < avgPinnedParentY ? range.lo : range.hi;
745
+ } else {
746
+ shift = Math.abs(range.lo) < Math.abs(range.hi) ? range.lo : range.hi;
747
+ }
748
+ }
682
749
  });
683
- // 无限画布允许负坐标,不能把上半组逐张夹到PAD而破坏居中。
684
- moving.forEach(function (id) { pos[id].y += shift; });
750
+ if (shift) {
751
+ // 同列移动组连同其后续列子孙一起平移,保持整支对齐不撕裂。
752
+ var sub = {}, q = moving.slice();
753
+ while (q.length) {
754
+ var cur = q.pop();
755
+ if (sub[cur]) continue;
756
+ sub[cur] = 1;
757
+ (kids[cur] || []).forEach(function (child) {
758
+ if (moved[child] && !CARD_PLACEMENTS[child] && depth[child] > d) q.push(child);
759
+ });
760
+ }
761
+ Object.keys(sub).forEach(function (id) { if (pos[id]) pos[id].y += shift; });
762
+ }
685
763
  });
686
764
  } else {
687
765
  // 整体下移,保证最上面那张卡正好落在 PAD 上(相对关系一个没动)
@@ -690,14 +768,6 @@
690
768
  var shift = isFinite(minY) ? PAD - minY : 0;
691
769
  DOC.cards.forEach(function (c) { if (pos[c.id]) pos[c.id].y += shift; });
692
770
  }
693
- /*
694
- * ★ 最后兜一次底:上面两个分支(整体摆 / 只动那一支)都是**摆的时候**量高度的,
695
- * 而卡片的高度会被异步的东西改(mermaid 渲染、字体加载、图片解码)。
696
- * 实测就是这么重叠的:摆的时候「库存服务」只有 151 高,之后长到 240,
697
- * 同一列下面的「通知服务」就被压住了。
698
- * 这里按**当前实测高度**把每一列从上往下再扫一遍,谁压住谁就往下推。
699
- * 只往下推、不往上提,也不改 x —— 列位置和镜头都不受影响。
700
- */
701
771
  keys.forEach(function (d) {
702
772
  var col = cols[d].map(function (id) { return { id: id, h: render.card[id].offsetHeight }; });
703
773
  col.sort(function (a, b) { return pos[a.id].y - pos[b.id].y; });
@@ -705,8 +775,6 @@
705
775
  col.forEach(function (it) {
706
776
  var y = pos[it.id].y;
707
777
  if (prev && y < prev.y + prev.h + ROW) y = prev.y + prev.h + ROW;
708
- // ★ 只推「这一支」里的卡。钉住的卡一个像素都不许动 ——
709
- // 用户报的「每次点标题,父节点就往下挪一点」就是这里把它推走的。
710
778
  if ((!moved || moved[it.id]) && it.id !== anchor) pos[it.id].y = y;
711
779
  prev = { y: pos[it.id].y, h: it.h };
712
780
  });
@@ -1110,193 +1178,9 @@
1110
1178
  });
1111
1179
  }
1112
1180
 
1113
- /* ══ ⑤ 文档 / 脑图 ══════════════════════════════════════════════ */
1114
-
1115
- function renderDoc() {
1116
- var art = $('#article'), toc = $('#toc');
1117
- art.innerHTML = ''; toc.innerHTML = '<h4>大纲</h4>';
1118
- if (DOC.preamble) art.appendChild(mdToBody(DOC.preamble));
1119
- var addToc = function (label, d, path) {
1120
- var a = document.createElement('a');
1121
- a.className = 'd' + Math.min(d, 4);
1122
- a.href = '#';
1123
- a.dataset.path = path;
1124
- a.textContent = label;
1125
- toc.appendChild(a);
1126
- };
1127
- DOC.cards.forEach(function (c, ci) {
1128
- var sec = document.createElement('section');
1129
- sec.className = 'slice';
1130
- sec.style.setProperty('--d', 1);
1131
- sec.dataset.path = String(ci); sec.dataset.level = "1";
1132
- sec.innerHTML = '<div class="slice-head"><div class="h">' + esc(c.title) + '</div></div>';
1133
- if (c.md) sec.appendChild(mdToBody(c.md));
1134
- art.appendChild(sec);
1135
- addToc(c.title, 1, String(ci));
1136
- (function walk(nodes, prefix) {
1137
- (nodes || []).forEach(function (n, i) {
1138
- var path = prefix + '.' + i;
1139
- var s = document.createElement('section');
1140
- s.className = 'slice';
1141
- s.style.setProperty('--d', n.d);
1142
- s.dataset.path = path; s.dataset.level = String(n.d);
1143
- s.innerHTML = '<div class="slice-head"><div class="h">' + esc(n.title) + '</div></div>';
1144
- if (n.md) s.appendChild(mdToBody(n.md));
1145
- art.appendChild(s);
1146
- addToc(n.title, n.d, path);
1147
- walk(n.kids, path);
1148
- });
1149
- })(c.nodes, String(ci));
1150
- });
1151
- }
1152
-
1153
- var MIND_CARD_STATE = {};
1154
- function applyMindCardPlacement(el) {
1155
- el.dataset.layoutX = el.style.left; el.dataset.layoutY = el.style.top;
1156
- var state = MIND_CARD_STATE[el.dataset.contentId];
1157
- if (state) {
1158
- el.style.left = (parseFloat(el.style.left) + (state.dx || 0)) + 'px';
1159
- el.style.top = (parseFloat(el.style.top) + (state.dy || 0)) + 'px';
1160
- }
1161
- }
1162
- function bindMindCard(el) {
1163
- function state() { return MIND_CARD_STATE[el.dataset.contentId] || (MIND_CARD_STATE[el.dataset.contentId] = {}); }
1164
- bindCanvasCard(el, {
1165
- move: function (x, y) { var s = state(); s.dx = x - parseFloat(el.dataset.layoutX); s.dy = y - parseFloat(el.dataset.layoutY); },
1166
- resize: function (width, height) { var s = state(); s.width = width; s.height = height; },
1167
- redraw: function () { drawMindWires(); }
1168
- });
1169
- }
1170
- function renderMind() {
1171
- var mind = $('#mind');
1172
- $$('.mnode', mind).forEach(function (n) { n.remove(); });
1173
- var GX = 64, GY = 12, X0 = 40, Y0 = 40;
1174
- var cursor = Y0;
1175
- var ALL = []; // 所有节点,等宽度都量完了再统一算 left
1176
- // 先摆子、再回头把父摆在子的垂直中线上 —— 父先占位的话,父会挂在子树的头顶上,不是树
1177
- function place(nodes, depth, prefix) {
1178
- var out = [];
1179
- nodes.forEach(function (n, i) {
1180
- var path = prefix ? prefix + '.' + i : String(i);
1181
- var hasKids = !!(n.kids && n.kids.length);
1182
- var el = createCanvasCard(n.title, 'mnode');
1183
- el.dataset.path = path; el.dataset.contentId = n.card || n.id || '__document';
1184
- // 记下这张小卡片对应文档里的哪张卡 —— 画关系边的时候要靠它对上号
1185
- if (n.card) el.dataset.card = n.card;
1186
- if (n.md && n.md.trim()) el.classList.add('has-body');
1187
- $('.body', el).appendChild(mdToBody(n.md));
1188
- var savedSize = MIND_CARD_STATE[el.dataset.contentId];
1189
- if (savedSize && savedSize.width) el.style.width = savedSize.width + 'px';
1190
- if (savedSize && savedSize.height) el.style.height = savedSize.height + 'px';
1191
- mind.appendChild(el); bindMindCard(el);
1192
- ALL.push({ el: el, depth: depth });
1193
- var b = { el: el, kids: [] };
1194
- var from = cursor;
1195
- if (hasKids) {
1196
- b.kids = place(n.kids, depth + 1, path);
1197
- var f = b.kids[0].el, l = b.kids[b.kids.length - 1].el;
1198
- var mid = (f.offsetTop + l.offsetTop + l.offsetHeight) / 2;
1199
- var top = mid - el.offsetHeight / 2;
1200
- if (top < from) {
1201
- var shift = from - top;
1202
- (function shiftChildren(branches) {
1203
- branches.forEach(function (branch) {
1204
- branch.el.style.top = (parseFloat(branch.el.style.top) + shift) + 'px';
1205
- shiftChildren(branch.kids);
1206
- });
1207
- })(b.kids);
1208
- cursor += shift; top = from;
1209
- }
1210
- el.style.top = top + 'px';
1211
- } else {
1212
- el.style.top = cursor + 'px';
1213
- cursor += el.offsetHeight + GY;
1214
- }
1215
- cursor = Math.max(cursor, el.offsetTop + el.offsetHeight + GY);
1216
- out.push(b);
1217
- });
1218
- return out;
1219
- }
1220
- // 一棵树,不是好几棵:先立一个「文档」根,每张卡是它的第一层分支
1221
- var root = {
1222
- title: DOC.title || '文档', md: DOC.preamble,
1223
- kids: DOC.cards.map(function (c) { return { title: c.title, md: c.md, kids: c.nodes || [], card: c.id }; }),
1224
- };
1225
- var all = place([root], 0, '');
1226
- all.forEach(function (r) { r.el.classList.add('root'); });
1227
- /*
1228
- * 每一列取这一列最宽的那个节点当列宽,再按列累加出 x —— 宽度是内容说了算,
1229
- * 列里最宽的那张决定下一列从哪开始,列与列之间才不会叠在一起。
1230
- */
1231
- var colW = [];
1232
- ALL.forEach(function (x) {
1233
- colW[x.depth] = Math.max(colW[x.depth] || 0, x.el.offsetWidth);
1234
- });
1235
- var colX = [], acc = X0;
1236
- for (var ci = 0; ci < colW.length; ci++) {
1237
- colX[ci] = acc;
1238
- acc += (colW[ci] || 0) + GX;
1239
- }
1240
- ALL.forEach(function (x) { x.el.style.left = colX[x.depth] + 'px'; applyMindCardPlacement(x.el); });
1241
- var w = 0, h = 0;
1242
- $$('.mnode', mind).forEach(function (n) {
1243
- w = Math.max(w, n.offsetLeft + n.offsetWidth);
1244
- h = Math.max(h, n.offsetTop + n.offsetHeight);
1245
- });
1246
- box = { w: w + X0, h: h + Y0 };
1247
- drawMindWires();
1248
- }
1249
-
1250
- function drawMindWires() {
1251
- var mind = $('#mind');
1252
- /*
1253
- * ★ 文档里的关系边(正文里 [文字](目标) 连出来的那些)也要画在脑图上。
1254
- * 原来脑图只有「树」的线 —— 那是标题嵌套长出来的父子线,文档里真正
1255
- * 声明的边一条都没有,看着就像「脑图上没连线」。
1256
- * 只连线,不碰布局:脑图的坐标是 place() 摆好的,这里纯粹是照着画。
1257
- * 脑图里每张卡都摊成了小卡片,没有「一二三级标题」之分,所以一律浅色虚线。
1258
- */
1259
- var nodeOf = {};
1260
- $$('.mnode[data-card]', mind).forEach(function (n) { nodeOf[n.dataset.card] = n; });
1261
- var svg = $('#mwires');
1262
- svg.setAttribute('width', box.w); svg.setAttribute('height', box.h);
1263
- svg.setAttribute('viewBox', '0 0 ' + box.w + ' ' + box.h);
1264
- svg.style.width = box.w + 'px'; svg.style.height = box.h + 'px';
1265
- var d = '';
1266
- DOC.edges.forEach(function (e) {
1267
- var A = nodeOf[e[0]], B = nodeOf[e[1]];
1268
- if (!A || !B) return;
1269
- var ax = A.offsetLeft, aw = A.offsetWidth, bx = B.offsetLeft, bw = B.offsetWidth;
1270
- var y1 = A.offsetTop + A.offsetHeight / 2, y2 = B.offsetTop + B.offsetHeight / 2;
1271
- var x1, x2, c1, c2;
1272
- if (Math.abs(ax - bx) < 1) {
1273
- /*
1274
- * ★ 同一列的边:卡片节点全在第一列,左右边完全重合。原来 x1 取源卡右边、
1275
- * x2 取目标卡左边,等于横穿整列卡片画一道弧 —— 看着就像「所有线都是从
1276
- * 一级那一列拉出来的」,而不是从某张卡出来。改成两头都钉在右边、向右
1277
- * 鼓出去再绕回来:线从哪张卡出、进哪张卡,一眼能对上。
1278
- */
1279
- x1 = ax + aw; x2 = bx + bw;
1280
- var bulge = Math.max(x1, x2) + 44;
1281
- c1 = bulge; c2 = bulge;
1282
- } else if (bx < ax) { x1 = ax; x2 = bx + bw; c1 = (x1 + x2) / 2; c2 = c1; }
1283
- else { x1 = ax + aw; x2 = bx; c1 = (x1 + x2) / 2; c2 = c1; }
1284
- if (x2 === x1 && c1 === x1) return;
1285
- d += '<path class="edge" d="M' + x1 + ' ' + y1 + ' C' + c1 + ' ' + y1 + ' ' + c2 + ' ' + y2 + ' ' + x2 + ' ' + y2 + '"/>';
1286
- });
1287
- var byPath = {};
1288
- $$('.mnode', mind).forEach(function (el) { byPath[el.dataset.path] = el; });
1289
- Object.keys(byPath).forEach(function (path) {
1290
- var child = byPath[path], parent = byPath[path.slice(0, path.lastIndexOf('.'))];
1291
- if (!parent || !path.includes('.')) return;
1292
- var x1 = parent.offsetLeft + parent.offsetWidth, x2 = child.offsetLeft;
1293
- var input1 = $('.hbar', parent), input2 = $('.hbar', child);
1294
- var y1 = parent.offsetTop + input1.offsetTop + input1.offsetHeight / 2;
1295
- var y2 = child.offsetTop + input2.offsetTop + input2.offsetHeight / 2, mx = (x1 + x2) / 2;
1296
- d += '<path d="M' + x1 + ' ' + y1 + ' C' + mx + ' ' + y1 + ' ' + mx + ' ' + y2 + ' ' + x2 + ' ' + y2 + '"/>';
1297
- });
1298
- svg.innerHTML = d;
1299
- }
1181
+ /* ══ ⑤ 视图插件注册表与内置视图(卡片 / 脑图 / 文档) ══════════ */
1182
+ /*__VIEW_REGISTRY__*/
1183
+ /*__VIEW_PLUGINS__*/
1300
1184
 
1301
1185
  /* ══ ⑥ 放大镜 ═══════════════════════════════════════════════════ */
1302
1186
 
@@ -1316,10 +1200,13 @@
1316
1200
  lx = (st.width - w * ls) / 2; ly = (st.height - h * ls) / 2;
1317
1201
  lensApply();
1318
1202
  }
1203
+ var lensSeq = 0;
1319
1204
  function openLens(html, label, kind) {
1205
+ lensSeq++;
1320
1206
  var inn = $('#lensInner');
1321
1207
  inn.innerHTML = kind ? '<div class="markdown-body">' + html + '</div>' : html;
1322
- if (kind) inn.dataset.cap = kind === 'code' ? '2.2' : '3'; else delete inn.dataset.cap;
1208
+ if (kind) { inn.dataset.cap = kind === 'code' ? '2.2' : '3'; inn.dataset.kind = kind; }
1209
+ else { delete inn.dataset.cap; delete inn.dataset.kind; }
1323
1210
  $$('svg', inn).forEach(function (s) {
1324
1211
  var vb = s.viewBox && s.viewBox.baseVal;
1325
1212
  s.removeAttribute('width'); s.removeAttribute('height');
@@ -1336,9 +1223,12 @@
1336
1223
  }
1337
1224
  function closeLens() {
1338
1225
  if (!$('#lens').classList.contains('open')) return;
1226
+ var seq = ++lensSeq;
1339
1227
  $('#lens').classList.remove('open');
1340
- $('#lensInner').innerHTML = '';
1341
1228
  document.dispatchEvent(new CustomEvent('clado:lens-close'));
1229
+ setTimeout(function () {
1230
+ if (seq === lensSeq && !$('#lens').classList.contains('open')) $('#lensInner').innerHTML = '';
1231
+ }, 180);
1342
1232
  }
1343
1233
 
1344
1234
  function flash(id) {
@@ -1418,10 +1308,8 @@
1418
1308
 
1419
1309
  /** 只渲「当前看得见」的那一块 —— mermaid 要量文字,display:none 的地方量出来是 0 */
1420
1310
  function visibleScope() {
1421
- var m = document.body.dataset.mode;
1422
- if (m === 'doc') return '#article';
1423
- if (m === 'mindmap') return '#mind';
1424
- return '#world .card';
1311
+ var view = CladoViewRegistry.get(document.body.dataset.mode);
1312
+ return view ? view.scope : '#world .card';
1425
1313
  }
1426
1314
 
1427
1315
  function renderMermaid(force) {
@@ -1478,28 +1366,10 @@
1478
1366
  }
1479
1367
 
1480
1368
  function setMode(m) {
1481
- document.body.dataset.mode = m;
1482
- if (m === 'cards') {
1483
- requestAnimationFrame(function () {
1484
- applyVisibility();
1485
- layout();
1486
- // ★ 从脑图切回来必须重新钉端口:脑图模式下卡片是 display:none,
1487
- // 端口的高度是量不出来的(见 placePorts 开头那段)。不重钉的话,
1488
- // 端口会一直赖在卡片右上角,直到你去点一次标题才归位。
1489
- fit(); repinPorts();
1490
- keepWiresAlive(1200); // 卡片和镜头都在过渡里,等它们停稳再定稿
1491
- });
1492
- } else if (m === 'mindmap') {
1493
- // 脑图的节点位置是按「当前高度」摆的,所以图渲完必须重摆一次
1494
- requestAnimationFrame(function () {
1495
- renderMind(); fit();
1496
- var mm = $$('#mind .mermaid');
1497
- mm.forEach(function (d) { delete d.dataset.mmd; });
1498
- renderMermaid().then(function () { renderMind(); fit(); });
1499
- });
1500
- } else {
1501
- renderMermaid().then(function () { canvasPlaceMediaJumps($('#article')); });
1502
- }
1369
+ var view = CladoViewRegistry.get(m) || CladoViewRegistry.get('cards');
1370
+ var mode = view ? view.mode : 'cards';
1371
+ document.body.dataset.mode = mode;
1372
+ if (view && typeof view.activate === 'function') view.activate();
1503
1373
  document.dispatchEvent(new CustomEvent('clado:view-state'));
1504
1374
  }
1505
1375
 
@@ -1510,7 +1380,7 @@
1510
1380
  // ★ 这里原来把模式写死成卡片,覆盖掉了模板里的 data-mode,
1511
1381
  // 于是 `--mode=doc` / `--mode=mindmap` 编出来的页面永远开在卡片模式。
1512
1382
  var startMode = document.body.dataset.mode;
1513
- if (['doc', 'cards', 'mindmap'].indexOf(startMode) < 0) startMode = 'cards';
1383
+ if (!CladoViewRegistry.has(startMode)) startMode = 'cards';
1514
1384
  document.body.dataset.mode = 'cards';
1515
1385
  buildCards();
1516
1386
  renderDoc();