nx-kv 0.41.0 → 0.41.1

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 (204) hide show
  1. package/CHANGELOG.md +29 -6
  2. package/README.md +1 -0
  3. package/package.json +2 -2
  4. package/src/core/attachmentdb/docs.js +104 -0
  5. package/src/core/attachmentdb/ids.js +96 -0
  6. package/src/core/attachmentdb/query.js +189 -0
  7. package/src/core/attachmentdb/relations.js +153 -0
  8. package/src/core/attachmentdb/resources.js +225 -0
  9. package/src/core/attachmentdb.js +42 -715
  10. package/src/core/db/import-json/data.js +140 -0
  11. package/src/core/db/import-json/extras.js +69 -0
  12. package/src/core/db/import-json/links.js +147 -0
  13. package/src/core/db/import-json/refs.js +183 -0
  14. package/src/core/db/import-json/repos.js +180 -0
  15. package/src/core/db/import-json/scan.js +35 -0
  16. package/src/core/db/import-json/tasks.js +317 -0
  17. package/src/core/db/import-json.js +31 -1061
  18. package/src/modules/docs/attach.js +181 -0
  19. package/src/modules/docs/index.js +20 -448
  20. package/src/modules/docs/knowledge.js +7 -2
  21. package/src/modules/docs/render.js +84 -0
  22. package/src/modules/docs/service.js +200 -0
  23. package/src/modules/localtask/action-attach.js +88 -0
  24. package/src/modules/localtask/action-flags.js +6 -0
  25. package/src/modules/localtask/action-lifecycle.js +190 -0
  26. package/src/modules/localtask/action-maintenance.js +130 -0
  27. package/src/modules/localtask/action-move.js +58 -0
  28. package/src/modules/localtask/action-query.js +46 -0
  29. package/src/modules/localtask/action-remove.js +116 -0
  30. package/src/modules/localtask/action-render.js +200 -0
  31. package/src/modules/localtask/action-submit.js +186 -0
  32. package/src/modules/localtask/action-topics.js +46 -0
  33. package/src/modules/localtask/action-waiting.js +95 -0
  34. package/src/modules/localtask/index.js +29 -1104
  35. package/src/modules/repos/boards/core.js +255 -0
  36. package/src/modules/repos/boards/docs.js +53 -0
  37. package/src/modules/repos/boards/members.js +228 -0
  38. package/src/modules/repos/boards/transfer.js +159 -0
  39. package/src/modules/repos/boards.js +25 -661
  40. package/src/modules/repos/cli/board-actions.js +177 -0
  41. package/src/modules/repos/cli/ref-actions.js +157 -0
  42. package/src/modules/repos/cli/remote-actions.js +141 -0
  43. package/src/modules/repos/cli/render.js +168 -0
  44. package/src/modules/repos/cli/repo-actions.js +199 -0
  45. package/src/modules/repos/cli/transfer-actions.js +105 -0
  46. package/src/modules/repos/index.js +18 -920
  47. package/src/modules/repos/service/bind-topics.js +97 -0
  48. package/src/modules/repos/service/context.js +189 -0
  49. package/src/modules/repos/service/crud.js +345 -0
  50. package/src/modules/repos/service/identity.js +188 -0
  51. package/src/modules/repos/service/store.js +274 -0
  52. package/src/modules/repos/service.js +41 -1061
  53. package/src/modules/repos/transfer/diff.js +126 -0
  54. package/src/modules/repos/transfer/export.js +223 -0
  55. package/src/modules/repos/transfer/import.js +377 -0
  56. package/src/modules/repos/transfer/layout.js +135 -0
  57. package/src/modules/repos/transfer/render.js +72 -0
  58. package/src/modules/repos/transfer.js +11 -922
  59. package/src/modules/tasktool/attach-resource.js +293 -0
  60. package/src/modules/tasktool/attach-template.js +266 -0
  61. package/src/modules/tasktool/attach.js +70 -558
  62. package/src/modules/tasktool/delivery-template.js +2 -1
  63. package/src/modules/tasktool/doc-inline.js +84 -0
  64. package/src/modules/tasktool/groupctx.js +3 -1
  65. package/src/modules/tasktool/plugins/timegoal-core-store.js +151 -0
  66. package/src/modules/tasktool/plugins/timegoal-cwd-store.js +441 -0
  67. package/src/modules/tasktool/plugins/timegoal-store.js +16 -842
  68. package/src/modules/tasktool/plugins/timegoal-task-store.js +289 -0
  69. package/src/modules/tasktool/wait.js +3 -1
  70. package/src/modules/todo/attachments.js +149 -0
  71. package/src/modules/todo/locate.js +82 -0
  72. package/src/modules/todo/service.js +42 -382
  73. package/src/modules/todo/store.js +158 -0
  74. package/src/shared/prompt-blocks.js +13 -4
  75. package/src/web/frontend/components/md/find-bar.jsx +38 -0
  76. package/src/web/frontend/components/md/live-block.jsx +129 -0
  77. package/src/web/frontend/components/md/md-panes-helpers.js +92 -0
  78. package/src/web/frontend/components/md/md-panes-keys.js +193 -0
  79. package/src/web/frontend/components/md/md-panes-root.jsx +158 -0
  80. package/src/web/frontend/components/md/md-panes.jsx +124 -1342
  81. package/src/web/frontend/components/md/slash-menu.jsx +83 -0
  82. package/src/web/frontend/components/md/status-bar.jsx +46 -0
  83. package/src/web/frontend/components/md/use-live-preview.js +279 -0
  84. package/src/web/frontend/components/md/use-slash-menu.js +80 -0
  85. package/src/web/frontend/components/md/use-source-editor.js +297 -0
  86. package/src/web/frontend/modules/md/md-blocks.js +248 -0
  87. package/src/web/frontend/modules/md/md-parse.js +23 -507
  88. package/src/web/frontend/modules/md/md-render.js +258 -0
  89. package/src/web/frontend/modules/md/style.css +7 -1605
  90. package/src/web/frontend/modules/md/styles/chrome.css +252 -0
  91. package/src/web/frontend/modules/md/styles/docs.css +185 -0
  92. package/src/web/frontend/modules/md/styles/editor.css +308 -0
  93. package/src/web/frontend/modules/md/styles/panels.css +372 -0
  94. package/src/web/frontend/modules/md/styles/shell.css +363 -0
  95. package/src/web/frontend/modules/md/styles/templates.css +125 -0
  96. package/src/web/frontend/page/dag/edges.js +55 -0
  97. package/src/web/frontend/page/dag/head-bar.jsx +46 -0
  98. package/src/web/frontend/page/dag/index.jsx +57 -420
  99. package/src/web/frontend/page/dag/layout.js +83 -0
  100. package/src/web/frontend/page/dag/model.js +47 -0
  101. package/src/web/frontend/page/dag/node-card.jsx +123 -0
  102. package/src/web/frontend/page/dag/use-interactions.js +135 -0
  103. package/src/web/frontend/page/dir/task-ack.js +38 -0
  104. package/src/web/frontend/page/dir/task-actions.js +379 -0
  105. package/src/web/frontend/page/dir/task-beside-header.jsx +18 -0
  106. package/src/web/frontend/page/dir/task-board-section.jsx +38 -0
  107. package/src/web/frontend/page/dir/task-chain.js +172 -0
  108. package/src/web/frontend/page/dir/task-derived-ctx.js +46 -0
  109. package/src/web/frontend/page/dir/task-dock-panel.jsx +227 -0
  110. package/src/web/frontend/page/dir/task-dock.js +79 -0
  111. package/src/web/frontend/page/dir/task-feedback.js +207 -0
  112. package/src/web/frontend/page/dir/task-goal-section.jsx +50 -0
  113. package/src/web/frontend/page/dir/task-goal.js +36 -0
  114. package/src/web/frontend/page/dir/task-history-view.jsx +44 -0
  115. package/src/web/frontend/page/dir/task-not-found.jsx +23 -0
  116. package/src/web/frontend/page/dir/task-preview-modal.jsx +82 -0
  117. package/src/web/frontend/page/dir/task-sel-menu.js +54 -0
  118. package/src/web/frontend/page/dir/task-status-bar.jsx +61 -0
  119. package/src/web/frontend/page/dir/task-utils.js +7 -0
  120. package/src/web/frontend/page/dir/task.jsx +117 -1532
  121. package/src/web/frontend/page/repos/bind-modals.jsx +118 -0
  122. package/src/web/frontend/page/repos/board-admin.jsx +226 -0
  123. package/src/web/frontend/page/repos/board-refs.jsx +97 -0
  124. package/src/web/frontend/page/repos/doc-search-modal.jsx +241 -0
  125. package/src/web/frontend/page/repos/filters-bar.jsx +44 -0
  126. package/src/web/frontend/page/repos/index.jsx +248 -1834
  127. package/src/web/frontend/page/repos/pending-section.jsx +163 -0
  128. package/src/web/frontend/page/repos/ref-panel.jsx +406 -0
  129. package/src/web/frontend/page/repos/remote-modal.jsx +112 -0
  130. package/src/web/frontend/page/repos/repo-form-modal.jsx +219 -0
  131. package/src/web/frontend/page/repos/repo-table.jsx +216 -0
  132. package/src/web/frontend/page/settings/prompt-template-params.js +214 -0
  133. package/src/web/frontend/page/settings/prompt-templates-section.jsx +1 -213
  134. package/src/web/frontend/style-v2.css +6 -1232
  135. package/src/web/frontend/style.css +10 -2434
  136. package/src/web/frontend/styles/base.css +139 -0
  137. package/src/web/frontend/styles/board.css +322 -0
  138. package/src/web/frontend/styles/buttons-cards.css +244 -0
  139. package/src/web/frontend/styles/modals.css +458 -0
  140. package/src/web/frontend/styles/panels.css +366 -0
  141. package/src/web/frontend/styles/repos-admin.css +207 -0
  142. package/src/web/frontend/styles/repos.css +330 -0
  143. package/src/web/frontend/styles/settings.css +244 -0
  144. package/src/web/frontend/styles/submit-bar.css +124 -0
  145. package/src/web/frontend/styles-v2/dag.css +285 -0
  146. package/src/web/frontend/styles-v2/dir.css +210 -0
  147. package/src/web/frontend/styles-v2/shell.css +251 -0
  148. package/src/web/frontend/styles-v2/sidebar.css +199 -0
  149. package/src/web/frontend/styles-v2/views.css +287 -0
  150. package/src/web/frontend/workspace/layout.js +399 -0
  151. package/src/web/frontend/workspace/open.js +246 -0
  152. package/src/web/frontend/workspace/panels.js +417 -0
  153. package/src/web/frontend/workspace.js +8 -1044
  154. package/src/web/public/assets/Introduction-Dnh0L_ss.js +1 -0
  155. package/src/web/public/assets/app-context-D7HAx67n.js +1 -0
  156. package/src/web/public/assets/bar-CgCAhY5x.js +1 -0
  157. package/src/web/public/assets/{board-view-t8CKtq17.js → board-view-CZKZsyk-.js} +1 -1
  158. package/src/web/public/assets/{button-M9TUn-AR.js → button-Cmo-6aoW.js} +1 -1
  159. package/src/web/public/assets/cloud-DMeqfWK9.js +3 -0
  160. package/src/web/public/assets/composer-launch-Bf4rXhLs.js +1 -0
  161. package/src/web/public/assets/composer-panel-C0qsxE7e.js +1 -0
  162. package/src/web/public/assets/dag-BBtSqd-X.js +1 -0
  163. package/src/web/public/assets/dir-UUWImKeX.js +5 -0
  164. package/src/web/public/assets/frontmatter-QvrjUNzF.js +2 -0
  165. package/src/web/public/assets/group-DFMGHQ-H.js +1 -0
  166. package/src/web/public/assets/index-CgEWvYQk.js +2 -0
  167. package/src/web/public/assets/md-history-CKumOs4q.js +39 -0
  168. package/src/web/public/assets/md-parse-awNLHAzO.js +13 -0
  169. package/src/web/public/assets/{member-picker--32VkTmv.js → member-picker-BAgkj36N.js} +1 -1
  170. package/src/web/public/assets/model-DzdWfp14.js +2 -0
  171. package/src/web/public/assets/{overview-B788GcJE.js → overview-BrcPfsJO.js} +1 -1
  172. package/src/web/public/assets/progress-DHDlz9O9.js +3 -0
  173. package/src/web/public/assets/refs-CqvRGv8w.js +1 -0
  174. package/src/web/public/assets/repos-DGrGWDnX.js +14 -0
  175. package/src/web/public/assets/route-crumb-yyPGKKKq.js +1 -0
  176. package/src/web/public/assets/settings-9zNucAPD.js +6 -0
  177. package/src/web/public/assets/{status-tag-BPbfa4zt.js → status-tag-bDdrFgpZ.js} +1 -1
  178. package/src/web/public/assets/task-CG4fzV6s.js +11 -0
  179. package/src/web/public/assets/{task-status-BP6y8MVu.js → task-status-BnymQMTw.js} +1 -1
  180. package/src/web/public/assets/templates-DdHWIxUE.js +1 -0
  181. package/src/web/public/assets/tool-bar-D8q3kOOl.js +1 -0
  182. package/src/web/public/assets/upload-BIMbGcCB.js +1 -0
  183. package/src/web/public/assets/workspace-C_5X2gIr.js +28 -0
  184. package/src/web/public/index.html +8 -4
  185. package/src/web/public/assets/Introduction-DKqRmABL.js +0 -1
  186. package/src/web/public/assets/bar-B8QJ4EnF.js +0 -1
  187. package/src/web/public/assets/cloud-C2O_1vT5.js +0 -3
  188. package/src/web/public/assets/composer-launch-BgMj0T3Y.js +0 -1
  189. package/src/web/public/assets/composer-panel-C5XUApWJ.js +0 -1
  190. package/src/web/public/assets/dag-CWAWk_gt.js +0 -1
  191. package/src/web/public/assets/dir-Cu5B5z6J.js +0 -5
  192. package/src/web/public/assets/group-YA-FgKS3.js +0 -1
  193. package/src/web/public/assets/index-DxX-ZzRP.js +0 -30
  194. package/src/web/public/assets/md-history-4sXoj4RH.js +0 -28
  195. package/src/web/public/assets/md-parse-C2bDc3IZ.js +0 -25
  196. package/src/web/public/assets/progress-CfzFVSDu.js +0 -3
  197. package/src/web/public/assets/refs-qtxw66hp.js +0 -1
  198. package/src/web/public/assets/repos-CpVuGaP2.js +0 -14
  199. package/src/web/public/assets/route-crumb-B8oEN8BB.js +0 -1
  200. package/src/web/public/assets/settings-BmWNBqcv.js +0 -6
  201. package/src/web/public/assets/task-DsFCNaOJ.js +0 -11
  202. package/src/web/public/assets/templates-4Xaj2m9L.js +0 -1
  203. package/src/web/public/assets/tool-bar-CfBWzhpV.js +0 -1
  204. package/src/web/public/assets/upload-L1g-pfvZ.js +0 -1
@@ -0,0 +1,83 @@
1
+ // 行首 `/` 斜杠命令菜单:键盘交互(↑↓/Enter/Tab/Esc)与浮层组件。
2
+ // 实时预览与源码两个编辑器共用(菜单位置算法不同,由调用方算好 x/y 传入)。
3
+ import { useEffect, useRef } from 'react';
4
+
5
+ function isSlashArrow(e) {
6
+ return e.key === 'ArrowDown' || e.key === 'ArrowUp'
7
+ || e.code === 'ArrowDown' || e.code === 'ArrowUp';
8
+ }
9
+
10
+ export function slashActiveIndex(s) {
11
+ if (!s?.items?.length) return 0;
12
+ const a = s.active;
13
+ if (typeof a !== 'number' || Number.isNaN(a)) return 0;
14
+ return Math.min(Math.max(0, a), s.items.length - 1);
15
+ }
16
+
17
+ /** 斜杠菜单键盘:返回 true 表示已处理 */
18
+ export function handleSlashKey(e, slashRef, { move, pick, close }) {
19
+ const cur = slashRef.current;
20
+ if (!cur?.items?.length) return false;
21
+ if (isSlashArrow(e)) {
22
+ e.preventDefault();
23
+ e.stopPropagation();
24
+ move(e.key === 'ArrowUp' || e.code === 'ArrowUp' ? -1 : 1);
25
+ return true;
26
+ }
27
+ if (e.key === 'Enter' || e.key === 'Tab') {
28
+ e.preventDefault();
29
+ e.stopPropagation();
30
+ pick(cur.items[slashActiveIndex(cur)]);
31
+ return true;
32
+ }
33
+ if (e.key === 'Escape') {
34
+ e.preventDefault();
35
+ e.stopPropagation();
36
+ close();
37
+ return true;
38
+ }
39
+ return false;
40
+ }
41
+
42
+ export function SlashMenu({ items, active, onPick, onHover, style }) {
43
+ const listRef = useRef(null);
44
+ const sel = slashActiveIndex({ items, active });
45
+ useEffect(() => {
46
+ const root = listRef.current;
47
+ if (!root) return;
48
+ const el = root.querySelector('.mdw-slash-item.on');
49
+ el?.scrollIntoView?.({ block: 'nearest' });
50
+ }, [sel, items]);
51
+ if (!items.length) return null;
52
+ return (
53
+ <div
54
+ ref={listRef}
55
+ className="mdw-slash"
56
+ role="listbox"
57
+ aria-activedescendant={`mdw-slash-opt-${sel}`}
58
+ style={style}
59
+ onMouseDown={(e) => e.preventDefault()}
60
+ >
61
+ <div className="mdw-slash-cap">插入 · ↑↓ 选择 · Enter 确认</div>
62
+ {items.map((c, i) => (
63
+ <button
64
+ key={c.id}
65
+ id={`mdw-slash-opt-${i}`}
66
+ type="button"
67
+ role="option"
68
+ tabIndex={-1}
69
+ aria-selected={i === sel}
70
+ className={`mdw-slash-item${i === sel ? ' on' : ''}`}
71
+ onMouseEnter={() => onHover(i)}
72
+ onMouseDown={(e) => {
73
+ e.preventDefault();
74
+ onPick(c);
75
+ }}
76
+ >
77
+ <span className="lab">{c.label}</span>
78
+ <span className="hint">{c.hint}</span>
79
+ </button>
80
+ ))}
81
+ </div>
82
+ );
83
+ }
@@ -0,0 +1,46 @@
1
+ // 源码编辑器底栏:光标位置 / 统计 / 行号·参考线·查找·换行开关。
2
+ export function StatusBar({
3
+ caret, stats, showLines, showRuler, findOpen, wrap,
4
+ onToggleLines, onToggleRuler, onToggleFind, onToggleWrap,
5
+ }) {
6
+ return (
7
+ <div className="mdw-editor-status" role="status">
8
+ <span>Ln {caret.line}, Col {caret.col}</span>
9
+ <span>{stats.lines} 行</span>
10
+ <span>{stats.words} 词</span>
11
+ <span>{stats.chars} 字</span>
12
+ <button
13
+ type="button"
14
+ className={`mdw-wrap-toggle${showLines ? ' on' : ''}`}
15
+ title="显示/隐藏行号"
16
+ onClick={onToggleLines}
17
+ >
18
+ 行号
19
+ </button>
20
+ <button
21
+ type="button"
22
+ className={`mdw-wrap-toggle${showRuler ? ' on' : ''}`}
23
+ title="80 字符参考线"
24
+ onClick={onToggleRuler}
25
+ >
26
+ 参考线
27
+ </button>
28
+ <button
29
+ type="button"
30
+ className={`mdw-wrap-toggle${findOpen ? ' on' : ''}`}
31
+ title="查找 Ctrl/⌘F"
32
+ onClick={onToggleFind}
33
+ >
34
+ 查找
35
+ </button>
36
+ <button
37
+ type="button"
38
+ className={`mdw-wrap-toggle${wrap ? ' on' : ''}`}
39
+ title={wrap ? '自动换行开(点击关闭)' : '自动换行关(点击开启)'}
40
+ onClick={onToggleWrap}
41
+ >
42
+ {wrap ? '换行' : '不换行'}
43
+ </button>
44
+ </div>
45
+ );
46
+ }
@@ -0,0 +1,279 @@
1
+ // LivePreview 的容器 hook:状态与副作用都收在这里,组件(md-panes.jsx)只留
2
+ // 结构钉测试锚定的交互本体(跨块选区删除、Delete 并块、IME 守卫、复选框点击)
3
+ // 与 JSX 装配。返回值即组件所需的全部上下文。
4
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
5
+ import { useAttachPicker } from './attach-picker.jsx';
6
+ import { atTriggerAt, padEmbed } from '../../modules/md/attach-mention.js';
7
+ import { diffIndex, runFormat, turnBlock } from '../../modules/md/md-edit-ops.js';
8
+ import { joinMdBlocks, normalizeBlockText } from '../../modules/md/md-parse.js';
9
+ import { bodyBlocksOnly, useDropFiles } from './md-panes-helpers.js';
10
+ import { useSlashMenu } from './use-slash-menu.js';
11
+
12
+ export function useLivePreview({
13
+ body, onBody, articleRef, attachments, docKey, onFiles, onCloseSelMenu, attachHost,
14
+ }) {
15
+ const [focus, setFocus] = useState(null);
16
+ const pendingSelRef = useRef(null);
17
+ // 预览态划选后按格式键:先记下待套用的操作,块进入编辑态并恢复选区后再执行
18
+ const pendingFormatRef = useRef(null);
19
+ const liveTaRef = useRef(null); // 当前聚焦块的 textarea
20
+ const blocks = bodyBlocksOnly(body);
21
+ // 空文档:splitMdBlocks('') 会给 [''],视觉上是一片没有把手的空白——
22
+ // 用幽灵引导条接住第一次点击(聚焦首块),聚焦后让位给 textarea 占位符
23
+ const emptyDoc = blocks.length === 0 || blocks.every((b) => !String(b).trim());
24
+
25
+ // ── 查找:块级匹配 + 命中块高亮 + 滚动定位(阅读态此前无查找能力)──
26
+ const [findOpen, setFindOpen] = useState(false);
27
+ const [findQuery, setFindQuery] = useState('');
28
+ const [findIdx, setFindIdx] = useState(0);
29
+ const findInputRef = useRef(null);
30
+ const liveMatches = useMemo(() => {
31
+ const q = findQuery.trim().toLowerCase();
32
+ if (!q) return [];
33
+ const out = [];
34
+ blocks.forEach((b, i) => {
35
+ const t = String(b ?? '').toLowerCase();
36
+ let pos = t.indexOf(q);
37
+ while (pos !== -1) {
38
+ out.push({ block: i, start: pos });
39
+ pos = t.indexOf(q, pos + q.length);
40
+ }
41
+ });
42
+ return out;
43
+ }, [blocks, findQuery]);
44
+ const jumpLive = (i) => {
45
+ const total = liveMatches.length;
46
+ if (!total) return;
47
+ const n = ((i % total) + total) % total;
48
+ setFindIdx(n);
49
+ const m = liveMatches[n];
50
+ const el = articleRef.current?.querySelector(`.mdw-lp-block[data-i="${m.block}"]`);
51
+ // 瞬时定位,不走 smooth:平滑滚动与行号槽的即时同步互相打架,命中行会
52
+ // 上下浮动、落点不稳(2026-11-03 任务 #7 移除)。
53
+ el?.scrollIntoView?.({ block: 'center' });
54
+ };
55
+ const hitBlock = liveMatches[findIdx]?.block ?? -1;
56
+ const setLiveQuery = (q) => {
57
+ setFindQuery(q);
58
+ setFindIdx(0);
59
+ };
60
+
61
+ // 实时预览不把标题写回正文;composeBody 提交时再拼 `# title`
62
+ // kind:'typing' 连续输入(历史合并成一条)· 'op' 格式/结构操作(各自成条)
63
+ const commit = useCallback(
64
+ (nextBlocks, kind = 'op') => onBody(joinMdBlocks(nextBlocks), kind),
65
+ [onBody]
66
+ );
67
+
68
+ const updateAt = (i, v, kind = 'op') => {
69
+ const next = blocks.slice();
70
+ next[i] = v;
71
+ commit(next, kind);
72
+ };
73
+
74
+ const applyBlockOp = (i, opResult) => {
75
+ if (!opResult) return;
76
+ pendingSelRef.current = { index: i, start: opResult.start, end: opResult.end };
77
+ updateAt(i, opResult.value);
78
+ };
79
+
80
+ // ── 斜杠命令:浮层坐标按块矩形算 ──
81
+ const slashPositionOf = (ta) => {
82
+ const rect = ta.getBoundingClientRect();
83
+ return {
84
+ x: Math.min(window.innerWidth - 220, rect.left),
85
+ y: Math.min(window.innerHeight - 220, rect.bottom + 4),
86
+ };
87
+ };
88
+ const {
89
+ slash, slashRef, slashRafRef, setSlashBoth, refreshSlash, moveSlash, pickSlash, hoverSlash,
90
+ } = useSlashMenu({
91
+ getTa: () => liveTaRef.current,
92
+ positionOf: slashPositionOf,
93
+ onApply: (next, cur) => {
94
+ const idx = cur?.index ?? focus;
95
+ if (idx == null) return;
96
+ applyBlockOp(idx, next);
97
+ },
98
+ });
99
+
100
+ useEffect(() => { setFocus(null); setSlashBoth(null); }, [docKey]);
101
+
102
+ // ── @ 附件选择(#108/#111)──
103
+ // 草稿(无 v2 host)数据源 = 会话 doc 的附件(图片按名字内嵌,v1 渲染口径)
104
+ const draftItems = useMemo(
105
+ () => (attachHost ? null : (attachments || [])
106
+ .filter((a) => a.type === 'image')
107
+ // 映射成伪附件(kind/id 对齐 embedForAttachment):草稿的图片按**名字**内嵌
108
+ // (v1 渲染口径:imgMap[name] → dataURL),所以 id 就是名字。
109
+ .map((a) => ({ kind: 'image', id: a.name, name: a.name, sizeText: a.size }))),
110
+ [attachHost, attachments]
111
+ );
112
+ const picker = useAttachPicker({
113
+ host: attachHost,
114
+ itemsOverride: attachHost ? null : draftItems,
115
+ onInsert: ({ index, embed, at, caret }) => {
116
+ // 块内替换:把 '@' 那一段换成引用语法,光标落在引用之后;
117
+ // 引用与正文**前后隔一空**(#111:贴着中文会黏连,行内元素需要边界)
118
+ const val = normalizeBlockText(blocks[index] ?? '');
119
+ const padded = padEmbed(embed, at > 0 ? val[at - 1] : '', val[caret] || '');
120
+ const next = val.slice(0, at) + padded.text + val.slice(caret);
121
+ const caretAt = at + padded.caretAt;
122
+ updateAt(index, next);
123
+ pendingSelRef.current = { index, start: caretAt, end: caretAt };
124
+ setFocus(index);
125
+ },
126
+ });
127
+ // 草稿「+ 添加附件」:文件走页面附件通道(粘贴同款——传会话 doc 并在插入点
128
+ // 落片段);@ 占位字符先清掉,插入点用 '@' 原位置。
129
+ const onDraftAddFiles = (files) => {
130
+ const m = picker.menu;
131
+ const pos = m ? { index: m.index, at: m.at, caret: m.caret } : { index: focus ?? 0, at: null, caret: null };
132
+ if (m) {
133
+ const val = normalizeBlockText(blocks[m.index] ?? '');
134
+ updateAt(m.index, val.slice(0, m.at) + val.slice(m.caret));
135
+ }
136
+ requestAnimationFrame(() => {
137
+ onFiles?.(files, {
138
+ mode: 'live',
139
+ focusIndex: pos.index,
140
+ ...(pos.at != null ? { caretInBlock: pos.at } : {}),
141
+ });
142
+ });
143
+ };
144
+
145
+ const applyTurn = (index, turnId) => {
146
+ const ta = liveTaRef.current;
147
+ const fake = ta && focus === index
148
+ ? ta
149
+ : { value: normalizeBlockText(blocks[index] ?? ''), selectionStart: 0, selectionEnd: 0 };
150
+ const next = turnBlock(fake, turnId);
151
+ if (next) {
152
+ applyBlockOp(index, next);
153
+ setFocus(index);
154
+ }
155
+ };
156
+
157
+ const ingestFiles = (files, ta) => {
158
+ if (!files?.length || !onFiles) return;
159
+ // 实时预览的光标是**块内**偏移,绝不能当全文 insertAt——否则会插进
160
+ // frontmatter(保存时被剥掉,附件仓留下孤儿文件;任务 #15 验收根因)。
161
+ onFiles(files, {
162
+ mode: 'live',
163
+ focusIndex: focus,
164
+ caretInBlock: ta ? ta.selectionStart : null,
165
+ });
166
+ };
167
+
168
+ const { rootProps } = useDropFiles({
169
+ baseClass: 'mdw-lp',
170
+ onDropFiles: (files) => ingestFiles(files, null),
171
+ });
172
+
173
+ // ── 编辑器命令 API:deleteBlockRange 由组件补上(结构钉锚定实现本体)──
174
+ const apiBase = useMemo(() => ({
175
+ format(op) {
176
+ const ta = liveTaRef.current;
177
+ if (!ta || focus == null) return;
178
+ ta.focus();
179
+ applyBlockOp(focus, runFormat(ta, op));
180
+ },
181
+ turn(turnId) {
182
+ const i = focus ?? 0;
183
+ applyTurn(i, turnId);
184
+ },
185
+ focus() {
186
+ liveTaRef.current?.focus();
187
+ },
188
+ openFind() {
189
+ setFindOpen(true);
190
+ requestAnimationFrame(() => findInputRef.current?.focus());
191
+ },
192
+ /** 预览态划选后按 Ctrl/⌘B/I/E/K:把该块切到编辑态并保留选区,再套用格式 */
193
+ formatSelection(op) {
194
+ const sel = window.getSelection();
195
+ if (!sel || sel.isCollapsed) return false;
196
+ const text = String(sel.toString() || '');
197
+ if (!text.trim()) return false;
198
+ const node = sel.anchorNode;
199
+ const el = node?.nodeType === 3 ? node.parentElement : node;
200
+ const blockEl = el?.closest?.('.mdw-lp-block');
201
+ if (!blockEl || !articleRef.current?.contains(blockEl)) return false;
202
+ const i = Number(blockEl.getAttribute('data-i'));
203
+ if (!Number.isInteger(i) || i < 0 || i >= blocks.length) return false;
204
+ const raw = normalizeBlockText(blocks[i] ?? '');
205
+ const pos = raw.indexOf(text);
206
+ if (pos === -1) return false; // 选区跨 markdown 标记时保守放弃,不做错位改写
207
+ pendingSelRef.current = { index: i, start: pos, end: pos + text.length };
208
+ pendingFormatRef.current = op;
209
+ onCloseSelMenu?.();
210
+ setFocus(i);
211
+ return true;
212
+ },
213
+ /** 撤销/重做后把光标放回改动处:把正文偏移换算到具体块与块内偏移 */
214
+ caretFromDiff(prev, next) {
215
+ const prevJ = joinMdBlocks(bodyBlocksOnly(prev));
216
+ const nextJ = joinMdBlocks(bodyBlocksOnly(next));
217
+ const idx = diffIndex(prevJ, nextJ);
218
+ let start = 0;
219
+ for (let i = 0; i < blocks.length; i += 1) {
220
+ // 用真实 joiner 累加,块边界与正文偏移严格对齐(连接符 '\n\n')
221
+ const end = joinMdBlocks(blocks.slice(0, i + 1)).length;
222
+ if (idx <= end) {
223
+ const off = Math.max(0, Math.min(idx - start, normalizeBlockText(blocks[i] ?? '').length));
224
+ if (focus === i) {
225
+ const ta = liveTaRef.current;
226
+ if (ta) {
227
+ ta.focus();
228
+ ta.setSelectionRange(off, off);
229
+ }
230
+ } else {
231
+ pendingSelRef.current = { index: i, start: off, end: off };
232
+ setFocus(i);
233
+ }
234
+ return;
235
+ }
236
+ start = end + 2;
237
+ }
238
+ },
239
+ // eslint 无 exhaustive-deps;闭包只依赖 blocks/focus/onBody/onCloseSelMenu 这轮的快照
240
+ }), [focus, blocks, onBody, onCloseSelMenu]);
241
+
242
+ const onBlockFocus = (idx) => {
243
+ setFocus(idx);
244
+ setSlashBoth(null);
245
+ };
246
+ const onTaReady = (el) => { liveTaRef.current = el; };
247
+ const onBlockChange = (i, v) => {
248
+ updateAt(i, v, 'typing');
249
+ cancelAnimationFrame(slashRafRef.current);
250
+ slashRafRef.current = requestAnimationFrame(() => {
251
+ const ta = liveTaRef.current;
252
+ if (!ta) return;
253
+ refreshSlash(ta, { index: i });
254
+ // @ → 唤起附件选择面板(#108);不在触发点就收掉
255
+ const hit = atTriggerAt(ta);
256
+ if (hit) picker.open({ index: i, ta, at: hit.at, caret: hit.caret });
257
+ else picker.close();
258
+ });
259
+ };
260
+ const onBlockBlur = () => {
261
+ requestAnimationFrame(() => {
262
+ if (document.activeElement?.closest?.('.mdw-lp-block.is-edit')) return;
263
+ if (document.activeElement?.closest?.('.mdw-slash')) return;
264
+ if (document.activeElement?.closest?.('.mdw-atpick')) return; // @ 附件面板(#108)
265
+ setFocus(null);
266
+ setSlashBoth(null);
267
+ });
268
+ };
269
+
270
+ return {
271
+ blocks, focus, setFocus, pendingSelRef, pendingFormatRef, liveTaRef,
272
+ commit, updateAt, applyBlockOp,
273
+ slash, slashRef, setSlashBoth, moveSlash, pickSlash, hoverSlash,
274
+ findOpen, setFindOpen, findQuery, setLiveQuery, findIdx, liveMatches, jumpLive,
275
+ hitBlock, findInputRef,
276
+ picker, onDraftAddFiles, apiBase, rootProps, emptyDoc,
277
+ ingestFiles, onBlockFocus, onTaReady, onBlockChange, onBlockBlur,
278
+ };
279
+ }
@@ -0,0 +1,80 @@
1
+ // 斜杠菜单状态 hook:实时预览与源码两个编辑器共用。
2
+ // 差异点全部由参数注入:getTa 取当前 textarea、positionOf 算浮层坐标
3
+ // (预览按块矩形、源码按行列推算行高)、onApply 把选中的命令结果写回
4
+ // (预览走块级 applyBlockOp,源码走全文 applyOp)。
5
+ import { useEffect, useRef, useState } from 'react';
6
+ import { applySlashCommand, filterSlashCmds, slashQueryAt } from '../../modules/md/md-edit-ops.js';
7
+ import { handleSlashKey, slashActiveIndex } from './slash-menu.jsx';
8
+
9
+ export function useSlashMenu({ getTa, positionOf, onApply }) {
10
+ const [slash, setSlash] = useState(null); // { index?, query, items, active, x, y }
11
+ const slashRef = useRef(null);
12
+ const slashRafRef = useRef(0);
13
+ const setSlashBoth = (next) => {
14
+ const v = typeof next === 'function' ? next(slashRef.current) : next;
15
+ slashRef.current = v;
16
+ setSlash(v);
17
+ };
18
+ useEffect(() => { slashRef.current = slash; }, [slash]);
19
+
20
+ // extra:调用方附加上下文(预览态是块序号 index);extra 变化时活动项归零重算
21
+ const refreshSlash = (ta, extra = null) => {
22
+ const hit = slashQueryAt(ta);
23
+ if (!hit) {
24
+ setSlashBoth(null);
25
+ return;
26
+ }
27
+ const items = filterSlashCmds(hit.query);
28
+ if (!items.length) {
29
+ setSlashBoth(null);
30
+ return;
31
+ }
32
+ const { x, y } = positionOf(ta);
33
+ setSlashBoth((prev) => {
34
+ const sameExtra = Object.keys(extra || {}).every((k) => prev?.[k] === extra[k]);
35
+ const keep = prev && prev.query === hit.query && sameExtra;
36
+ const active = keep ? slashActiveIndex({ ...prev, items }) : 0;
37
+ return { ...(extra || {}), query: hit.query, items, active, x, y };
38
+ });
39
+ };
40
+
41
+ const moveSlash = (delta) => {
42
+ setSlashBoth((s) => {
43
+ if (!s?.items?.length) return s;
44
+ const n = s.items.length;
45
+ const cur = slashActiveIndex(s);
46
+ return { ...s, active: (cur + delta + n) % n };
47
+ });
48
+ };
49
+
50
+ const pickSlash = (cmd) => {
51
+ const ta = getTa();
52
+ const cur = slashRef.current;
53
+ if (!ta || !cmd) return;
54
+ const next = applySlashCommand(ta, cmd);
55
+ setSlashBoth(null);
56
+ if (next) onApply(next, cur);
57
+ };
58
+
59
+ const hoverSlash = (n) => setSlashBoth((s) => s && ({ ...s, active: n }));
60
+
61
+ // document 捕获:即使焦点异常也能 ↑↓
62
+ useEffect(() => {
63
+ if (!slash) return undefined;
64
+ const onKey = (e) => {
65
+ handleSlashKey(e, slashRef, {
66
+ move: moveSlash,
67
+ pick: pickSlash,
68
+ close: () => setSlashBoth(null),
69
+ });
70
+ };
71
+ document.addEventListener('keydown', onKey, true);
72
+ return () => document.removeEventListener('keydown', onKey, true);
73
+ // eslint 无 exhaustive-deps:moveSlash/pickSlash 走 ref,slash 变化才需要重挂
74
+ }, [slash]);
75
+
76
+ return {
77
+ slash, slashRef, slashRafRef, setSlashBoth,
78
+ refreshSlash, moveSlash, pickSlash, hoverSlash,
79
+ };
80
+ }