dsh-mindmap 0.13.1 → 0.14.0

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 (2) hide show
  1. package/client.js +180 -39
  2. package/package.json +1 -1
package/client.js CHANGED
@@ -337,6 +337,94 @@ window.__ModuleLoader__.load({
337
337
  }
338
338
  return data.raw || node.topic || "";
339
339
  }
340
+
341
+ // 037 节点焦点上下文:空节点必须保留在路径里,不能被当成缺失节点。
342
+ const EMPTY_NODE_MARKER = "(这是一个空节点,节点客观存在,但节点容器内没有内容)";
343
+ const PATH_SEPARATOR = " >>> ";
344
+
345
+ /** 路径展示专用转义:不改节点原文,只避免分段包裹符破坏定位边界。 */
346
+ function escapePathSegment(text) {
347
+ return String(text ?? "")
348
+ .replace(/\r?\n/g, " ")
349
+ .replace(/【/g, "〔")
350
+ .replace(/】/g, "〕");
351
+ }
352
+
353
+ /** 节点在路径中的显示段;空 topic 用明确的空节点语义占位。 */
354
+ function nodePathLabel(node) {
355
+ const topic = String((node && node.topic) ?? "").trim();
356
+ return `【${escapePathSegment(topic || EMPTY_NODE_MARKER)}】`;
357
+ }
358
+
359
+ /** 找到根到目标节点的路径;目标不在当前树时退回目标自身,避免丢失焦点。 */
360
+ function nodePathTo(root, target) {
361
+ if (!target) return [];
362
+ const targetId = target.id;
363
+ const path = [];
364
+ const visit = (node) => {
365
+ if (!node) return false;
366
+ path.push(node);
367
+ if (node === target || (targetId != null && node.id === targetId)) return true;
368
+ for (const child of node.children || []) {
369
+ if (visit(child)) return true;
370
+ }
371
+ path.pop();
372
+ return false;
373
+ };
374
+ if (visit(root)) return path;
375
+ return [target];
376
+ }
377
+
378
+ /**
379
+ * 037 复制节点及子节点为文本:每个节点一行,每层 2 个半角空格。
380
+ * 多行节点内容的每一行都带同一层级前缀,空节点用明确标记保留其存在性。
381
+ */
382
+ function nodeTreeText(root) {
383
+ if (!root) return "";
384
+ const lines = [];
385
+ const visit = (node, depth) => {
386
+ const prefix = " ".repeat(Math.max(0, depth) * 2);
387
+ const own = String(nodeFullText(node) || "");
388
+ const body = own || EMPTY_NODE_MARKER;
389
+ for (const line of body.split(/\r?\n/)) lines.push(`${prefix}${line}`);
390
+ for (const child of node.children || []) visit(child, depth + 1);
391
+ };
392
+ visit(root, 0);
393
+ return lines.join("\n");
394
+ }
395
+
396
+ /**
397
+ * 037 围绕节点聊天:只发送当前节点自身与根→当前节点路径,绝不展开子树。
398
+ * 末尾协议要求模型完成一次短握手后等待用户继续,不让模型先复述上下文。
399
+ */
400
+ function nodeFocusPrompt(root, target) {
401
+ if (!target) return "";
402
+ const path = nodePathTo(root, target).map(nodePathLabel).join(PATH_SEPARATOR);
403
+ const own = String(nodeFullText(target) || "") || EMPTY_NODE_MARKER;
404
+ return [
405
+ "【插件功能声明】",
406
+ "你正在接收的是思维脑图插件发起的一次“节点焦点对齐”请求。",
407
+ "这是插件功能的一部分,不是用户普通聊天内容。",
408
+ "",
409
+ "【功能意图】",
410
+ "用户在思维脑图中选中了一个节点。插件现在要把你的注意力定位到这个节点上,后续对话将围绕这个节点继续进行。",
411
+ "",
412
+ "【定位路径】",
413
+ "以下路径表示从根节点到当前选中节点的完整父子关系;每个节点用【】包裹,使用“>>>”表示向下定位:",
414
+ path,
415
+ "",
416
+ "【当前选中节点内容】",
417
+ own,
418
+ "",
419
+ "【范围限制】",
420
+ "本次只提供当前选中节点及其定位路径,不展开当前节点的子节点、孙子节点或其他下级内容。",
421
+ "",
422
+ "【严格回复协议】",
423
+ "如果你已经理解上述插件功能意图和定位路径,你的下一条回复必须且只能是下面这一句话:",
424
+ "已理解和对齐您选中的节点,我们开始聊吧~",
425
+ "不得输出任何其他字符,不得添加解释、前缀、后缀、Markdown、表情或其他句子。回复完这句话后,等待用户继续提问。",
426
+ ].join("\n");
427
+ }
340
428
  //#endregion
341
429
 
342
430
  /**
@@ -1025,6 +1113,19 @@ window.__ModuleLoader__.load({
1025
1113
  const draft = readDraftText(inputActions);
1026
1114
  return typeof draft === "string" && draft.trim() !== "";
1027
1115
  }
1116
+
1117
+ /** 037 节点焦点消息:保护现有草稿后,原子地写入并提交到当前 DSH 对话。 */
1118
+ function submitNodeFocusMessage(inputActions, text) {
1119
+ if (draftBlocksAutoSend(inputActions)) {
1120
+ throw new Error("当前聊天框已有未发送内容,请先处理后再围绕节点聊天");
1121
+ }
1122
+ if (!inputActions || typeof inputActions.setDraft !== "function" || typeof inputActions.submit !== "function") {
1123
+ throw new Error("当前对话不支持自动发送节点焦点消息");
1124
+ }
1125
+ inputActions.setDraft(String(text ?? ""));
1126
+ inputActions.submit();
1127
+ return true;
1128
+ }
1028
1129
  //#endregion
1029
1130
 
1030
1131
  //#region 025 子树折叠:画布视图态纯函数(不进 markdown 资产,只影响呈现)
@@ -1469,7 +1570,8 @@ window.__ModuleLoader__.load({
1469
1570
  treeRefreshHover: { background: "var(--dsw-alias-interactive-bg-hover)", color: "var(--dsw-alias-label-primary)" },
1470
1571
  treeError: { color: "var(--dsw-alias-label-error)", fontSize: "12px", lineHeight: 1.6, margin: "0" },
1471
1572
  treeMenu: { position: "fixed", zIndex: 60, minWidth: "210px", background: "var(--dsw-alias-bg-layer-3)", border: "1px solid var(--dsw-alias-border-l2)", borderRadius: "10px", padding: "6px", boxShadow: "var(--dsw-shadow-lv2)" },
1472
- treeMenuItem: { display: "block", width: "100%", boxSizing: "border-box", textAlign: "left", border: "none", background: "none", cursor: "pointer", padding: "7px 12px", borderRadius: "8px", font: "inherit", fontSize: "13px", color: "var(--dsw-alias-label-primary)" },
1573
+ treeMenuItem: { display: "block", width: "100%", boxSizing: "border-box", textAlign: "left", border: "none", background: "none", cursor: "pointer", padding: "7px 12px", borderRadius: "8px", font: "inherit", fontSize: "13px", color: "var(--dsw-alias-label-primary)", transition: "background 0.08s ease, color 0.08s ease" },
1574
+ treeMenuItemHover: { background: "var(--dsw-alias-interactive-bg-hover)", color: "var(--dsw-alias-label-primary)" },
1473
1575
  // 017 画布节点右键菜单:标题行(节点主题)+ 错误行 + 菜单项禁用态。
1474
1576
  nodeMenuHeader: { padding: "4px 12px 6px", fontSize: "12px", color: "var(--dsw-alias-label-tertiary)", whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis", maxWidth: "220px", boxSizing: "border-box" },
1475
1577
  // 020:标题(节点主题)与动作项之间的分隔线,拉开层次。
@@ -2280,7 +2382,7 @@ window.__ModuleLoader__.load({
2280
2382
  * 修正 scroll,视图不跳变(内容回到视口内时浏览器会自动钳制回 0)。
2281
2383
  */
2282
2384
  function MindmapCanvas(props) {
2283
- const { node, theme, fitKey, reveal } = props;
2385
+ const { node, theme, fitKey, reveal, inputActions } = props;
2284
2386
  const scrollRef = react.useRef(null);
2285
2387
  const contentRef = react.useRef(null);
2286
2388
  const zoomRef = react.useRef(1);
@@ -2304,11 +2406,12 @@ window.__ModuleLoader__.load({
2304
2406
  const fitStampRef = react.useRef([]);
2305
2407
  const [zoom, setZoomState] = react.useState(1);
2306
2408
  const [hover, setHover] = react.useState(null);
2307
- // 017 节点右键菜单:{x, y, node};null = 关闭。busy = "copy" |
2308
- // "export" 表示对应动作进行中(两项都禁用),error 展示失败原因。
2409
+ // 037 节点右键菜单:{x, y, node};null = 关闭。busy 标记 chat/text/
2410
+ // copy/export 中的当前动作;任一动作在途时整张菜单禁用。
2309
2411
  const [nodeMenu, setNodeMenu] = react.useState(null);
2310
2412
  const [nodeMenuBusy, setNodeMenuBusy] = react.useState(null);
2311
2413
  const [nodeMenuError, setNodeMenuError] = react.useState("");
2414
+ const [nodeMenuHover, setNodeMenuHover] = react.useState(null);
2312
2415
  const nodeMenuRef = react.useRef(null);
2313
2416
  // 019 选中态:点击聚焦的节点下选选中环(002 §6 状态体系)。
2314
2417
  const [selectedId, setSelectedId] = react.useState(null);
@@ -2729,36 +2832,19 @@ window.__ModuleLoader__.load({
2729
2832
  // 递归 TreeRow 传回调。聚焦视为用户手动缩放(停自动再适配)。
2730
2833
  // 033:目标比例经 clampFocusJump 单次最多 ×2/÷2(巨图点叶子不再一步
2731
2834
  // 怼到 100%,连点渐进 drill);zoom 变化经 animateZoomTo 平滑过渡。
2732
- function onCanvasClick(e) {
2733
- // 021:刚拖过画布(平移)的这次 click 不是点击,直接吞掉——
2734
- // 否则每次平移松手都会顺手把选中环清掉。
2735
- if (suppressClickRef.current) {
2736
- suppressClickRef.current = false;
2737
- return;
2738
- }
2739
- const target = e.target;
2740
- if (!target || typeof target.closest !== "function") return;
2741
- const boxEl = target.closest("[data-mindmap-node]");
2742
- if (!boxEl || !boxEl.isConnected) {
2743
- // 019 空白处点击:取消选中环(链接点击已 stopPropagation,不走这里)。
2744
- setSelectedId(null);
2745
- return;
2746
- }
2747
- // 019:聚焦同时记选中态(盒包裹上挂了 data-mindmap-node-id)。
2835
+ function focusNodeBox(boxEl) {
2836
+ if (!boxEl || !boxEl.isConnected) return false;
2748
2837
  setSelectedId(boxEl.getAttribute("data-mindmap-node-id"));
2749
2838
  const rowEl = boxEl.closest("[data-mindmap-row]");
2750
- if (!rowEl) return;
2839
+ if (!rowEl) return false;
2751
2840
  const scroller = scrollRef.current;
2752
- if (!scroller) return;
2841
+ if (!scroller) return false;
2753
2842
  const current = zoomRef.current;
2754
2843
  const rowRect = rowEl.getBoundingClientRect();
2755
- // 033 跳变钳制:相对当前比例单次最多 ×2 / ÷2。
2756
2844
  const focus = clampFocusJump(
2757
2845
  focusZoom(rowRect.width / current, rowRect.height / current, scroller.clientWidth, scroller.clientHeight),
2758
2846
  current,
2759
2847
  );
2760
- // 034:记录点击时节点中心的视口比例位置——动画锚位插值起点
2761
- //(第一帧期望位置 = 点击时位置,消除首帧滚动瞬移)。
2762
2848
  const boxRect = boxEl.getBoundingClientRect();
2763
2849
  const scrollerRect = scroller.getBoundingClientRect();
2764
2850
  const startAnchor = {
@@ -2768,8 +2854,6 @@ window.__ModuleLoader__.load({
2768
2854
  userZoomedRef.current = true;
2769
2855
  if (focus !== current) {
2770
2856
  anchorRef.current = null;
2771
- // 034:先完整截停旧动画(清过期 focusRef + 同步中间值),
2772
- // 再登记本次聚焦——顺序不能反(cancel 会清 focusRef)。
2773
2857
  cancelZoomAnim();
2774
2858
  focusRef.current = { boxEl, startAnchor };
2775
2859
  animateZoomTo(focus);
@@ -2778,6 +2862,26 @@ window.__ModuleLoader__.load({
2778
2862
  focusRef.current = { boxEl, startAnchor };
2779
2863
  positionFocus();
2780
2864
  }
2865
+ return true;
2866
+ }
2867
+
2868
+ function onCanvasClick(e) {
2869
+ // 021:刚拖过画布(平移)的这次 click 不是点击,直接吞掉——
2870
+ // 否则每次平移松手都会顺手把选中环清掉。
2871
+ if (suppressClickRef.current) {
2872
+ suppressClickRef.current = false;
2873
+ return;
2874
+ }
2875
+ const target = e.target;
2876
+ if (!target || typeof target.closest !== "function") return;
2877
+ const boxEl = target.closest("[data-mindmap-node]");
2878
+ if (!boxEl || !boxEl.isConnected) {
2879
+ // 019 空白处点击:取消选中环(链接点击已 stopPropagation,不走这里)。
2880
+ setSelectedId(null);
2881
+ return;
2882
+ }
2883
+ // 019:聚焦同时记选中态(盒包裹上挂了 data-mindmap-node-id)。
2884
+ focusNodeBox(boxEl);
2781
2885
  }
2782
2886
 
2783
2887
  // 034 锚位应用:把节点盒中心滚到指定视口比例锚位(滚动增量与视口
@@ -2976,14 +3080,24 @@ window.__ModuleLoader__.load({
2976
3080
  setCollapsed((prev) => toggleCollapsed(prev, id));
2977
3081
  }
2978
3082
 
2979
- // 017 右键节点:记录菜单锚点与目标子树(清掉上次的忙碌/错误态)。
3083
+ // 037 节点焦点聊天:沿用目录树的自动发送能力,但不覆盖用户已有草稿。
3084
+ function submitNodeChat(text) {
3085
+ return submitNodeFocusMessage(inputActions, text);
3086
+ }
3087
+
3088
+ // 037 右键节点:先完成与左键相同的注意力聚焦,再记录菜单锚点。
2980
3089
  function onNodeContextMenu(e, target) {
3090
+ const boxEl = e && e.currentTarget && typeof e.currentTarget.closest === "function"
3091
+ ? e.currentTarget
3092
+ : (e && e.target && typeof e.target.closest === "function" ? e.target.closest("[data-mindmap-node]") : null);
3093
+ focusNodeBox(boxEl);
2981
3094
  setNodeMenuBusy(null);
2982
3095
  setNodeMenuError("");
3096
+ setNodeMenuHover(null);
2983
3097
  setNodeMenu({ x: e.clientX, y: e.clientY, node: target });
2984
3098
  }
2985
3099
 
2986
- // 017 菜单动作:text = 节点全文写剪贴板(020);copy = PNG 写系统剪贴板
3100
+ // 037 菜单动作:chat = 节点焦点上下文自动发送;text = 子树文本写剪贴板;copy = PNG 写系统剪贴板
2987
3101
  // (可粘贴到聊天/文档);export = PNG 下载为文件。图片范围 = 该节点
2988
3102
  // 及其全部子孙(buildExportSvg 以任意节点为根重排布局,根样式随深度判定)。
2989
3103
  async function onNodeMenuAction(mode) {
@@ -2992,7 +3106,8 @@ window.__ModuleLoader__.load({
2992
3106
  setNodeMenuBusy(mode);
2993
3107
  setNodeMenuError("");
2994
3108
  try {
2995
- if (mode === "text") await copyPlainText(nodeFullText(target));
3109
+ if (mode === "chat") submitNodeChat(nodeFocusPrompt(node, target));
3110
+ else if (mode === "text") await copyPlainText(nodeTreeText(target));
2996
3111
  else if (mode === "copy") await copyPng(target, theme && theme.colorTheme);
2997
3112
  else await exportPng(target, target.topic, theme && theme.colorTheme);
2998
3113
  setNodeMenu(null);
@@ -3009,6 +3124,13 @@ window.__ModuleLoader__.load({
3009
3124
  const zoomBtnStyle = (key, disabled) => (disabled
3010
3125
  ? { ...S.zoomBtn, ...S.zoomBtnDisabled }
3011
3126
  : (hover === key ? { ...S.zoomBtn, ...S.zoomBtnHover } : S.zoomBtn));
3127
+ const nodeMenuItemStyle = (key) => (nodeMenuBusy
3128
+ ? { ...S.treeMenuItem, ...S.treeMenuItemDisabled }
3129
+ : (nodeMenuHover === key ? { ...S.treeMenuItem, ...S.treeMenuItemHover } : S.treeMenuItem));
3130
+ const nodeMenuHoverProps = (key) => ({
3131
+ onMouseEnter: () => setNodeMenuHover(key),
3132
+ onMouseLeave: () => setNodeMenuHover((current) => (current === key ? null : current)),
3133
+ });
3012
3134
 
3013
3135
  return (0, react_jsx_runtime.jsxs)("div", {
3014
3136
  style: S.canvasWrap,
@@ -3170,8 +3292,7 @@ window.__ModuleLoader__.load({
3170
3292
  children: "✕",
3171
3293
  }),
3172
3294
  ] }) : null,
3173
- // 017 节点右键菜单:标题行(节点主题)+ 复制全文/复制为图片/导出为
3174
- // 图片三动作 + 错误行。复用目录树菜单容器样式(fixed 定位,left/top = 视口坐标)。
3295
+ // 037 节点右键菜单:标题 + 核心聊天入口 + 分隔线 + 文本/图片动作。
3175
3296
  nodeMenu ? (0, react_jsx_runtime.jsxs)("div", {
3176
3297
  ref: nodeMenuRef,
3177
3298
  style: { ...S.treeMenu, left: nodeMenu.x, top: nodeMenu.y },
@@ -3182,28 +3303,40 @@ window.__ModuleLoader__.load({
3182
3303
  title: nodeMenu.node.topic || "待填写",
3183
3304
  children: truncateForExport(nodeMenu.node.topic, 18) || "待填写",
3184
3305
  }),
3306
+ (0, react_jsx_runtime.jsx)("button", {
3307
+ type: "button",
3308
+ style: nodeMenuItemStyle("chat"),
3309
+ disabled: Boolean(nodeMenuBusy),
3310
+ ...nodeMenuHoverProps("chat"),
3311
+ title: "把当前节点的定位路径与自身内容发送到当前对话,围绕该节点继续聊天",
3312
+ onClick: () => onNodeMenuAction("chat"),
3313
+ children: nodeMenuBusy === "chat" ? "发送中…" : "围绕该节点与 AI 聊天",
3314
+ }),
3185
3315
  // 020:标题与动作项之间拉一根分隔线。
3186
3316
  (0, react_jsx_runtime.jsx)("div", { style: S.nodeMenuDivider }),
3187
3317
  (0, react_jsx_runtime.jsx)("button", {
3188
3318
  type: "button",
3189
- style: nodeMenuBusy ? { ...S.treeMenuItem, ...S.treeMenuItemDisabled } : S.treeMenuItem,
3319
+ style: nodeMenuItemStyle("text"),
3190
3320
  disabled: Boolean(nodeMenuBusy),
3191
- title: "把该节点自身内容的完整文本复制到剪贴板(截断块/代码块/表格块均取全文)",
3321
+ ...nodeMenuHoverProps("text"),
3322
+ title: "把该节点及其全部子节点按层级缩进复制为文本",
3192
3323
  onClick: () => onNodeMenuAction("text"),
3193
- children: nodeMenuBusy === "text" ? "复制中…" : "复制全文",
3324
+ children: nodeMenuBusy === "text" ? "复制中…" : "复制节点及子节点为文本",
3194
3325
  }),
3195
3326
  (0, react_jsx_runtime.jsx)("button", {
3196
3327
  type: "button",
3197
- style: nodeMenuBusy ? { ...S.treeMenuItem, ...S.treeMenuItemDisabled } : S.treeMenuItem,
3328
+ style: nodeMenuItemStyle("copy"),
3198
3329
  disabled: Boolean(nodeMenuBusy),
3330
+ ...nodeMenuHoverProps("copy"),
3199
3331
  title: "把该节点及其全部子孙渲染为 PNG 并复制到剪贴板",
3200
3332
  onClick: () => onNodeMenuAction("copy"),
3201
3333
  children: nodeMenuBusy === "copy" ? "复制中…" : "复制为图片",
3202
3334
  }),
3203
3335
  (0, react_jsx_runtime.jsx)("button", {
3204
3336
  type: "button",
3205
- style: nodeMenuBusy ? { ...S.treeMenuItem, ...S.treeMenuItemDisabled } : S.treeMenuItem,
3337
+ style: nodeMenuItemStyle("export"),
3206
3338
  disabled: Boolean(nodeMenuBusy),
3339
+ ...nodeMenuHoverProps("export"),
3207
3340
  title: "把该节点及其全部子孙渲染为 PNG 并下载为文件",
3208
3341
  onClick: () => onNodeMenuAction("export"),
3209
3342
  children: nodeMenuBusy === "export" ? "导出中…" : "导出为图片",
@@ -4031,7 +4164,7 @@ window.__ModuleLoader__.load({
4031
4164
  : (doc && doc.op === "local")
4032
4165
  ? renderLoading()
4033
4166
  : renderTree() })
4034
- : (0, react_jsx_runtime.jsx)(MindmapCanvas, { node: tree, theme, fitKey: doc && doc.path, reveal }),
4167
+ : (0, react_jsx_runtime.jsx)(MindmapCanvas, { node: tree, theme, fitKey: doc && doc.path, reveal, inputActions }),
4035
4168
  tabMenu ? (0, react_jsx_runtime.jsxs)("div", {
4036
4169
  style: { ...S.treeMenu, left: tabMenu.x, top: tabMenu.y },
4037
4170
  onContextMenu: (e) => e.preventDefault(),
@@ -4122,7 +4255,7 @@ window.__ModuleLoader__.load({
4122
4255
  : (doc && doc.op === "local")
4123
4256
  ? renderLoading()
4124
4257
  : renderTree() })
4125
- : (0, react_jsx_runtime.jsx)(MindmapCanvas, { node: tree, theme, fitKey: doc && doc.path, reveal }),
4258
+ : (0, react_jsx_runtime.jsx)(MindmapCanvas, { node: tree, theme, fitKey: doc && doc.path, reveal, inputActions }),
4126
4259
  tabMenu ? (0, react_jsx_runtime.jsxs)("div", {
4127
4260
  style: { ...S.treeMenu, left: tabMenu.x, top: tabMenu.y },
4128
4261
  onContextMenu: (e) => e.preventDefault(),
@@ -4916,6 +5049,7 @@ window.__ModuleLoader__.load({
4916
5049
  // 025 草稿保护:宿主草稿读取面的能力探测(供测试)。
4917
5050
  readDraftText,
4918
5051
  draftBlocksAutoSend,
5052
+ submitNodeFocusMessage,
4919
5053
  // 025 子树折叠:视图态纯函数 + 节点行组件(供测试)。
4920
5054
  toggleCollapsed,
4921
5055
  countDescendants,
@@ -4934,6 +5068,13 @@ window.__ModuleLoader__.load({
4934
5068
  isTableSeparator,
4935
5069
  parseTableRow,
4936
5070
  nodeFullText,
5071
+ nodeTreeText,
5072
+ nodeFocusPrompt,
5073
+ EMPTY_NODE_MARKER,
5074
+ PATH_SEPARATOR,
5075
+ escapePathSegment,
5076
+ nodePathTo,
5077
+ nodePathLabel,
4937
5078
  renderInline,
4938
5079
  // 链接点击:供测试验证开窗成功才拦默认行为(宿主拦截时退回原生导航)。
4939
5080
  openLink,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mindmap",
3
- "version": "0.13.1",
3
+ "version": "0.14.0",
4
4
  "description": "Mindmap plugin for DeepSeek Harness: a plain markdown file in the working directory IS the mindmap; the chat edits it step by step and the right-side panel follows live.",
5
5
  "repository": {
6
6
  "type": "git",