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.
- package/CHANGELOG.md +29 -6
- package/README.md +1 -0
- package/package.json +2 -2
- package/src/core/attachmentdb/docs.js +104 -0
- package/src/core/attachmentdb/ids.js +96 -0
- package/src/core/attachmentdb/query.js +189 -0
- package/src/core/attachmentdb/relations.js +153 -0
- package/src/core/attachmentdb/resources.js +225 -0
- package/src/core/attachmentdb.js +42 -715
- package/src/core/db/import-json/data.js +140 -0
- package/src/core/db/import-json/extras.js +69 -0
- package/src/core/db/import-json/links.js +147 -0
- package/src/core/db/import-json/refs.js +183 -0
- package/src/core/db/import-json/repos.js +180 -0
- package/src/core/db/import-json/scan.js +35 -0
- package/src/core/db/import-json/tasks.js +317 -0
- package/src/core/db/import-json.js +31 -1061
- package/src/modules/docs/attach.js +181 -0
- package/src/modules/docs/index.js +20 -448
- package/src/modules/docs/knowledge.js +7 -2
- package/src/modules/docs/render.js +84 -0
- package/src/modules/docs/service.js +200 -0
- package/src/modules/localtask/action-attach.js +88 -0
- package/src/modules/localtask/action-flags.js +6 -0
- package/src/modules/localtask/action-lifecycle.js +190 -0
- package/src/modules/localtask/action-maintenance.js +130 -0
- package/src/modules/localtask/action-move.js +58 -0
- package/src/modules/localtask/action-query.js +46 -0
- package/src/modules/localtask/action-remove.js +116 -0
- package/src/modules/localtask/action-render.js +200 -0
- package/src/modules/localtask/action-submit.js +186 -0
- package/src/modules/localtask/action-topics.js +46 -0
- package/src/modules/localtask/action-waiting.js +95 -0
- package/src/modules/localtask/index.js +29 -1104
- package/src/modules/repos/boards/core.js +255 -0
- package/src/modules/repos/boards/docs.js +53 -0
- package/src/modules/repos/boards/members.js +228 -0
- package/src/modules/repos/boards/transfer.js +159 -0
- package/src/modules/repos/boards.js +25 -661
- package/src/modules/repos/cli/board-actions.js +177 -0
- package/src/modules/repos/cli/ref-actions.js +157 -0
- package/src/modules/repos/cli/remote-actions.js +141 -0
- package/src/modules/repos/cli/render.js +168 -0
- package/src/modules/repos/cli/repo-actions.js +199 -0
- package/src/modules/repos/cli/transfer-actions.js +105 -0
- package/src/modules/repos/index.js +18 -920
- package/src/modules/repos/service/bind-topics.js +97 -0
- package/src/modules/repos/service/context.js +189 -0
- package/src/modules/repos/service/crud.js +345 -0
- package/src/modules/repos/service/identity.js +188 -0
- package/src/modules/repos/service/store.js +274 -0
- package/src/modules/repos/service.js +41 -1061
- package/src/modules/repos/transfer/diff.js +126 -0
- package/src/modules/repos/transfer/export.js +223 -0
- package/src/modules/repos/transfer/import.js +377 -0
- package/src/modules/repos/transfer/layout.js +135 -0
- package/src/modules/repos/transfer/render.js +72 -0
- package/src/modules/repos/transfer.js +11 -922
- package/src/modules/tasktool/attach-resource.js +293 -0
- package/src/modules/tasktool/attach-template.js +266 -0
- package/src/modules/tasktool/attach.js +70 -558
- package/src/modules/tasktool/delivery-template.js +2 -1
- package/src/modules/tasktool/doc-inline.js +84 -0
- package/src/modules/tasktool/groupctx.js +3 -1
- package/src/modules/tasktool/plugins/timegoal-core-store.js +151 -0
- package/src/modules/tasktool/plugins/timegoal-cwd-store.js +441 -0
- package/src/modules/tasktool/plugins/timegoal-store.js +16 -842
- package/src/modules/tasktool/plugins/timegoal-task-store.js +289 -0
- package/src/modules/tasktool/wait.js +3 -1
- package/src/modules/todo/attachments.js +149 -0
- package/src/modules/todo/locate.js +82 -0
- package/src/modules/todo/service.js +42 -382
- package/src/modules/todo/store.js +158 -0
- package/src/shared/prompt-blocks.js +13 -4
- package/src/web/frontend/components/md/find-bar.jsx +38 -0
- package/src/web/frontend/components/md/live-block.jsx +129 -0
- package/src/web/frontend/components/md/md-panes-helpers.js +92 -0
- package/src/web/frontend/components/md/md-panes-keys.js +193 -0
- package/src/web/frontend/components/md/md-panes-root.jsx +158 -0
- package/src/web/frontend/components/md/md-panes.jsx +124 -1342
- package/src/web/frontend/components/md/slash-menu.jsx +83 -0
- package/src/web/frontend/components/md/status-bar.jsx +46 -0
- package/src/web/frontend/components/md/use-live-preview.js +279 -0
- package/src/web/frontend/components/md/use-slash-menu.js +80 -0
- package/src/web/frontend/components/md/use-source-editor.js +297 -0
- package/src/web/frontend/modules/md/md-blocks.js +248 -0
- package/src/web/frontend/modules/md/md-parse.js +23 -507
- package/src/web/frontend/modules/md/md-render.js +258 -0
- package/src/web/frontend/modules/md/style.css +7 -1605
- package/src/web/frontend/modules/md/styles/chrome.css +252 -0
- package/src/web/frontend/modules/md/styles/docs.css +185 -0
- package/src/web/frontend/modules/md/styles/editor.css +308 -0
- package/src/web/frontend/modules/md/styles/panels.css +372 -0
- package/src/web/frontend/modules/md/styles/shell.css +363 -0
- package/src/web/frontend/modules/md/styles/templates.css +125 -0
- package/src/web/frontend/page/dag/edges.js +55 -0
- package/src/web/frontend/page/dag/head-bar.jsx +46 -0
- package/src/web/frontend/page/dag/index.jsx +57 -420
- package/src/web/frontend/page/dag/layout.js +83 -0
- package/src/web/frontend/page/dag/model.js +47 -0
- package/src/web/frontend/page/dag/node-card.jsx +123 -0
- package/src/web/frontend/page/dag/use-interactions.js +135 -0
- package/src/web/frontend/page/dir/task-ack.js +38 -0
- package/src/web/frontend/page/dir/task-actions.js +379 -0
- package/src/web/frontend/page/dir/task-beside-header.jsx +18 -0
- package/src/web/frontend/page/dir/task-board-section.jsx +38 -0
- package/src/web/frontend/page/dir/task-chain.js +172 -0
- package/src/web/frontend/page/dir/task-derived-ctx.js +46 -0
- package/src/web/frontend/page/dir/task-dock-panel.jsx +227 -0
- package/src/web/frontend/page/dir/task-dock.js +79 -0
- package/src/web/frontend/page/dir/task-feedback.js +207 -0
- package/src/web/frontend/page/dir/task-goal-section.jsx +50 -0
- package/src/web/frontend/page/dir/task-goal.js +36 -0
- package/src/web/frontend/page/dir/task-history-view.jsx +44 -0
- package/src/web/frontend/page/dir/task-not-found.jsx +23 -0
- package/src/web/frontend/page/dir/task-preview-modal.jsx +82 -0
- package/src/web/frontend/page/dir/task-sel-menu.js +54 -0
- package/src/web/frontend/page/dir/task-status-bar.jsx +61 -0
- package/src/web/frontend/page/dir/task-utils.js +7 -0
- package/src/web/frontend/page/dir/task.jsx +117 -1532
- package/src/web/frontend/page/repos/bind-modals.jsx +118 -0
- package/src/web/frontend/page/repos/board-admin.jsx +226 -0
- package/src/web/frontend/page/repos/board-refs.jsx +97 -0
- package/src/web/frontend/page/repos/doc-search-modal.jsx +241 -0
- package/src/web/frontend/page/repos/filters-bar.jsx +44 -0
- package/src/web/frontend/page/repos/index.jsx +248 -1834
- package/src/web/frontend/page/repos/pending-section.jsx +163 -0
- package/src/web/frontend/page/repos/ref-panel.jsx +406 -0
- package/src/web/frontend/page/repos/remote-modal.jsx +112 -0
- package/src/web/frontend/page/repos/repo-form-modal.jsx +219 -0
- package/src/web/frontend/page/repos/repo-table.jsx +216 -0
- package/src/web/frontend/page/settings/prompt-template-params.js +214 -0
- package/src/web/frontend/page/settings/prompt-templates-section.jsx +1 -213
- package/src/web/frontend/style-v2.css +6 -1232
- package/src/web/frontend/style.css +10 -2434
- package/src/web/frontend/styles/base.css +139 -0
- package/src/web/frontend/styles/board.css +322 -0
- package/src/web/frontend/styles/buttons-cards.css +244 -0
- package/src/web/frontend/styles/modals.css +458 -0
- package/src/web/frontend/styles/panels.css +366 -0
- package/src/web/frontend/styles/repos-admin.css +207 -0
- package/src/web/frontend/styles/repos.css +330 -0
- package/src/web/frontend/styles/settings.css +244 -0
- package/src/web/frontend/styles/submit-bar.css +124 -0
- package/src/web/frontend/styles-v2/dag.css +285 -0
- package/src/web/frontend/styles-v2/dir.css +210 -0
- package/src/web/frontend/styles-v2/shell.css +251 -0
- package/src/web/frontend/styles-v2/sidebar.css +199 -0
- package/src/web/frontend/styles-v2/views.css +287 -0
- package/src/web/frontend/workspace/layout.js +399 -0
- package/src/web/frontend/workspace/open.js +246 -0
- package/src/web/frontend/workspace/panels.js +417 -0
- package/src/web/frontend/workspace.js +8 -1044
- package/src/web/public/assets/Introduction-Dnh0L_ss.js +1 -0
- package/src/web/public/assets/app-context-D7HAx67n.js +1 -0
- package/src/web/public/assets/bar-CgCAhY5x.js +1 -0
- package/src/web/public/assets/{board-view-t8CKtq17.js → board-view-CZKZsyk-.js} +1 -1
- package/src/web/public/assets/{button-M9TUn-AR.js → button-Cmo-6aoW.js} +1 -1
- package/src/web/public/assets/cloud-DMeqfWK9.js +3 -0
- package/src/web/public/assets/composer-launch-Bf4rXhLs.js +1 -0
- package/src/web/public/assets/composer-panel-C0qsxE7e.js +1 -0
- package/src/web/public/assets/dag-BBtSqd-X.js +1 -0
- package/src/web/public/assets/dir-UUWImKeX.js +5 -0
- package/src/web/public/assets/frontmatter-QvrjUNzF.js +2 -0
- package/src/web/public/assets/group-DFMGHQ-H.js +1 -0
- package/src/web/public/assets/index-CgEWvYQk.js +2 -0
- package/src/web/public/assets/md-history-CKumOs4q.js +39 -0
- package/src/web/public/assets/md-parse-awNLHAzO.js +13 -0
- package/src/web/public/assets/{member-picker--32VkTmv.js → member-picker-BAgkj36N.js} +1 -1
- package/src/web/public/assets/model-DzdWfp14.js +2 -0
- package/src/web/public/assets/{overview-B788GcJE.js → overview-BrcPfsJO.js} +1 -1
- package/src/web/public/assets/progress-DHDlz9O9.js +3 -0
- package/src/web/public/assets/refs-CqvRGv8w.js +1 -0
- package/src/web/public/assets/repos-DGrGWDnX.js +14 -0
- package/src/web/public/assets/route-crumb-yyPGKKKq.js +1 -0
- package/src/web/public/assets/settings-9zNucAPD.js +6 -0
- package/src/web/public/assets/{status-tag-BPbfa4zt.js → status-tag-bDdrFgpZ.js} +1 -1
- package/src/web/public/assets/task-CG4fzV6s.js +11 -0
- package/src/web/public/assets/{task-status-BP6y8MVu.js → task-status-BnymQMTw.js} +1 -1
- package/src/web/public/assets/templates-DdHWIxUE.js +1 -0
- package/src/web/public/assets/tool-bar-D8q3kOOl.js +1 -0
- package/src/web/public/assets/upload-BIMbGcCB.js +1 -0
- package/src/web/public/assets/workspace-C_5X2gIr.js +28 -0
- package/src/web/public/index.html +8 -4
- package/src/web/public/assets/Introduction-DKqRmABL.js +0 -1
- package/src/web/public/assets/bar-B8QJ4EnF.js +0 -1
- package/src/web/public/assets/cloud-C2O_1vT5.js +0 -3
- package/src/web/public/assets/composer-launch-BgMj0T3Y.js +0 -1
- package/src/web/public/assets/composer-panel-C5XUApWJ.js +0 -1
- package/src/web/public/assets/dag-CWAWk_gt.js +0 -1
- package/src/web/public/assets/dir-Cu5B5z6J.js +0 -5
- package/src/web/public/assets/group-YA-FgKS3.js +0 -1
- package/src/web/public/assets/index-DxX-ZzRP.js +0 -30
- package/src/web/public/assets/md-history-4sXoj4RH.js +0 -28
- package/src/web/public/assets/md-parse-C2bDc3IZ.js +0 -25
- package/src/web/public/assets/progress-CfzFVSDu.js +0 -3
- package/src/web/public/assets/refs-qtxw66hp.js +0 -1
- package/src/web/public/assets/repos-CpVuGaP2.js +0 -14
- package/src/web/public/assets/route-crumb-B8oEN8BB.js +0 -1
- package/src/web/public/assets/settings-BmWNBqcv.js +0 -6
- package/src/web/public/assets/task-DsFCNaOJ.js +0 -11
- package/src/web/public/assets/templates-4Xaj2m9L.js +0 -1
- package/src/web/public/assets/tool-bar-CfBWzhpV.js +0 -1
- package/src/web/public/assets/upload-L1g-pfvZ.js +0 -1
|
@@ -1,318 +1,53 @@
|
|
|
1
|
+
// MD 工作台的多面板组件入口(对外 import 面不变:MdPanes / ModeSeg 再导出)。
|
|
2
|
+
//
|
|
3
|
+
// 本文件只保留**结构钉测试锚定**的部分(tests/unit/md-editor-pro-gaps、
|
|
4
|
+
// md-view-selection-backspace、md-title-height 直接对本文件源码做断言):
|
|
5
|
+
// TitleArea 标题输入(ResizeObserver 跟宽重算,#94)
|
|
6
|
+
// LivePreview 块编辑器外壳:跨块选区删除 / Delete 并块 / IME 守卫 / 复选框点击
|
|
7
|
+
// SourceEditor 源码编辑器外壳:IME 守卫 / textarea 拖放(#118)
|
|
8
|
+
// 其余按职责拆到同目录兄弟文件:
|
|
9
|
+
// md-panes-root.jsx MdPanes(模式分发+文档级快捷键)与 ModeSeg
|
|
10
|
+
// live-block.jsx LiveBlock 单块渲染
|
|
11
|
+
// slash-menu.jsx 斜杠菜单组件与键盘助手
|
|
12
|
+
// use-slash-menu.js 两个编辑器共用的斜杠菜单状态 hook
|
|
13
|
+
// find-bar.jsx 查找条
|
|
14
|
+
// status-bar.jsx 源码底栏
|
|
15
|
+
// use-live-preview.js LivePreview 容器 hook(状态/查找/斜杠/@ 附件/编辑器 API)
|
|
16
|
+
// use-source-editor.js SourceEditor 容器 hook
|
|
17
|
+
// md-panes-keys.js 块编辑器按键子动作(Enter 拆块 / Backspace / Shift+Enter / ↑↓)
|
|
18
|
+
// md-panes-helpers.js 共享小助手(块渲染、快捷键表、拖放 hook)
|
|
1
19
|
import {
|
|
2
|
-
forwardRef,
|
|
3
20
|
useCallback,
|
|
4
21
|
useEffect,
|
|
5
22
|
useImperativeHandle,
|
|
6
23
|
useLayoutEffect,
|
|
7
|
-
useMemo,
|
|
8
24
|
useRef,
|
|
9
|
-
useState,
|
|
10
25
|
} from 'react';
|
|
11
26
|
import { createPortal } from 'react-dom';
|
|
12
|
-
import AttachPicker
|
|
13
|
-
import
|
|
27
|
+
import AttachPicker from './attach-picker.jsx';
|
|
28
|
+
import LiveBlock from './live-block.jsx';
|
|
29
|
+
import { EditorFindBar } from './find-bar.jsx';
|
|
30
|
+
import { handleSlashKey, SlashMenu } from './slash-menu.jsx';
|
|
31
|
+
import { StatusBar } from './status-bar.jsx';
|
|
32
|
+
import { useLivePreview } from './use-live-preview.js';
|
|
33
|
+
import { useSourceEditor } from './use-source-editor.js';
|
|
34
|
+
import { FMT_KEYS } from './md-panes-helpers.js';
|
|
14
35
|
import {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
} from '../../modules/md/md-parse.js';
|
|
36
|
+
handleArrowNav,
|
|
37
|
+
handleBackspaceStart,
|
|
38
|
+
handleBareListEscape,
|
|
39
|
+
handleEnterSplit,
|
|
40
|
+
handleSoftBreak,
|
|
41
|
+
} from './md-panes-keys.js';
|
|
22
42
|
import {
|
|
23
|
-
applySlashCommand,
|
|
24
|
-
caretLineCol,
|
|
25
|
-
continueListOnEnter,
|
|
26
|
-
countStats,
|
|
27
|
-
diffIndex,
|
|
28
|
-
filterSlashCmds,
|
|
29
|
-
findMatches,
|
|
30
43
|
filesFromDataTransfer,
|
|
31
44
|
indentLines,
|
|
32
|
-
insertTextAtCursor,
|
|
33
|
-
isBareListItemLine,
|
|
34
45
|
runFormat,
|
|
35
|
-
scrollTextareaToIndex,
|
|
36
|
-
slashQueryAt,
|
|
37
46
|
toggleNthTodo,
|
|
38
|
-
turnBlock,
|
|
39
|
-
turnLine,
|
|
40
47
|
} from '../../modules/md/md-edit-ops.js';
|
|
48
|
+
import { isFrontmatterBlock, normalizeBlockText } from '../../modules/md/md-parse.js';
|
|
41
49
|
|
|
42
|
-
|
|
43
|
-
const FMT_KEYS = { b: 'bold', i: 'italic', e: 'code', k: 'link' };
|
|
44
|
-
|
|
45
|
-
/** 正文里已经自带 `# 标题` 了吗(跳过 frontmatter)——阅读态据此决定要不要再画题头 */
|
|
46
|
-
function bodyHasH1(body) {
|
|
47
|
-
let t = String(body || '').replace(/^\uFEFF/, '').trimStart();
|
|
48
|
-
if (t.startsWith('---')) {
|
|
49
|
-
const end = t.indexOf('\n---', 3);
|
|
50
|
-
if (end > 0) t = t.slice(end + 4).trimStart();
|
|
51
|
-
}
|
|
52
|
-
return /^#[ \t]+\S/.test(t);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function autoSize(el) {
|
|
56
|
-
if (!el) return;
|
|
57
|
-
el.style.height = '0px';
|
|
58
|
-
// 高度交给内容本身(scrollHeight = 行数 × 行高)——原来写死最小 28px,
|
|
59
|
-
// 比一行标题(16px × 1.35 ≈ 21.6px)还高:点进标题块就凭空多出 6px,
|
|
60
|
-
// 下方内容跟着抖(2026-11-04 任务 #31)。
|
|
61
|
-
el.style.height = `${el.scrollHeight}px`;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function blockHtml(text, attachments) {
|
|
65
|
-
const raw = normalizeBlockText(text);
|
|
66
|
-
if (!raw.trim()) return '';
|
|
67
|
-
// 只传**图片**(v1 草稿按名字内嵌的 imgMap 只认图片):文件附件不能整表
|
|
68
|
-
// 下发——本函数是**逐块**调用的,renderMarkdownHtml 会把文件附件拼成 chip
|
|
69
|
-
// 附在每个块末尾,拖一个 md 进来就是「每一行都显示」(#118)。
|
|
70
|
-
const imgs = (attachments || []).filter((a) => a?.type === 'image');
|
|
71
|
-
return renderMarkdownHtml(raw, imgs).replace(
|
|
72
|
-
/<p class="mdw-empty">[\s\S]*?<\/p>/g,
|
|
73
|
-
''
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** 块类型 → 编辑态字号贴近渲染(避免「输入框感」) */
|
|
78
|
-
function blockKind(text) {
|
|
79
|
-
const t = String(text || '');
|
|
80
|
-
// frontmatter 块:预览态渲染成属性卡(renderMarkdownHtml),编辑态等宽小字
|
|
81
|
-
if (/^---\s*$/.test(t.split('\n', 1)[0] || '') && t.includes('\n')) return 'fm';
|
|
82
|
-
if (/^#\s/m.test(t) && !/^##/m.test(t)) return 'h1';
|
|
83
|
-
if (/^##\s/m.test(t) && !/^###/m.test(t)) return 'h2';
|
|
84
|
-
if (/^###\s/m.test(t)) return 'h3';
|
|
85
|
-
if (/^[-*]\s/m.test(t)) return 'li';
|
|
86
|
-
if (/^>\s/m.test(t)) return 'quote';
|
|
87
|
-
return 'p';
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** 单块:未聚焦渲染,聚焦时无描边源码;行首 `/` 出命令 */
|
|
91
|
-
function LiveBlock({
|
|
92
|
-
index,
|
|
93
|
-
text,
|
|
94
|
-
focused,
|
|
95
|
-
hit,
|
|
96
|
-
onFocus,
|
|
97
|
-
onToggleTodo,
|
|
98
|
-
onChange,
|
|
99
|
-
onBlur,
|
|
100
|
-
onKeyDown,
|
|
101
|
-
onPasteFiles,
|
|
102
|
-
onTaReady,
|
|
103
|
-
onTextSel,
|
|
104
|
-
attachments,
|
|
105
|
-
pendingSelRef,
|
|
106
|
-
pendingFormatRef,
|
|
107
|
-
}) {
|
|
108
|
-
const taRef = useRef(null);
|
|
109
|
-
const display = normalizeBlockText(text);
|
|
110
|
-
// frontmatter 块**整体不渲染**(#149):#113 起视图态已渲染为空,但块本身还在——
|
|
111
|
-
// 点它/聚焦它仍会开出裸 YAML 编辑框(组草稿首块的 `---\nboard:…\n---` 让人莫名,
|
|
112
|
-
// 用户原话「删除这个frontmatter」)。归属语义有页头「归属项目」下拉编辑,
|
|
113
|
-
// 源码模式仍可见可改;这里只动渲染层——块数组里 FM 必须保留(commit 按块列表
|
|
114
|
-
// 整表回写正文,滤掉会把组语义删没),data-i 坐标也不变(ack 定位轨/块选区共用)。
|
|
115
|
-
// ⚠️ 早退必须放在所有 hooks 之后(Rules of Hooks)。
|
|
116
|
-
const isFm = isFrontmatterBlock(display);
|
|
117
|
-
const html = isFm ? '' : blockHtml(display, attachments);
|
|
118
|
-
const kind = blockKind(display);
|
|
119
|
-
|
|
120
|
-
useLayoutEffect(() => {
|
|
121
|
-
if (!focused || !taRef.current) return;
|
|
122
|
-
const el = taRef.current;
|
|
123
|
-
onTaReady?.(el);
|
|
124
|
-
autoSize(el);
|
|
125
|
-
const pending = pendingSelRef?.current;
|
|
126
|
-
if (pending && pending.index === index) {
|
|
127
|
-
pendingSelRef.current = null;
|
|
128
|
-
el.focus();
|
|
129
|
-
try { el.setSelectionRange(pending.start, pending.end); } catch { /* ignore */ }
|
|
130
|
-
// 预览态划选后按格式键:选区已就位,直接套用格式(Notion/Typora 行为)
|
|
131
|
-
const op = pendingFormatRef?.current;
|
|
132
|
-
if (op) {
|
|
133
|
-
pendingFormatRef.current = null;
|
|
134
|
-
const res = runFormat(el, op);
|
|
135
|
-
if (res) onChange(res.value);
|
|
136
|
-
}
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
if (document.activeElement !== el) {
|
|
140
|
-
// @ 附件面板开着时焦点归它(#108):别把焦点抢回 textarea——
|
|
141
|
-
// 否则面板刚聚焦的搜索框立刻失焦,键盘输入落回正文。
|
|
142
|
-
if (document.activeElement?.closest?.('.mdw-atpick')) return;
|
|
143
|
-
el.focus();
|
|
144
|
-
const n = el.value.length;
|
|
145
|
-
try { el.setSelectionRange(n, n); } catch { /* ignore */ }
|
|
146
|
-
}
|
|
147
|
-
}, [focused, text, index, pendingSelRef, pendingFormatRef, onTaReady, onChange]);
|
|
148
|
-
|
|
149
|
-
if (isFm) return null;
|
|
150
|
-
|
|
151
|
-
if (focused) {
|
|
152
|
-
return (
|
|
153
|
-
<div className={`mdw-lp-block is-edit kind-${kind}`} data-i={index}>
|
|
154
|
-
<textarea
|
|
155
|
-
ref={taRef}
|
|
156
|
-
className="mdw-lp-src"
|
|
157
|
-
value={display}
|
|
158
|
-
rows={1}
|
|
159
|
-
spellCheck={false}
|
|
160
|
-
aria-label="正文"
|
|
161
|
-
placeholder="输入… Enter 新块 · 行首 / 命令"
|
|
162
|
-
onChange={(e) => {
|
|
163
|
-
onChange(e.target.value);
|
|
164
|
-
autoSize(e.target);
|
|
165
|
-
}}
|
|
166
|
-
onBlur={onBlur}
|
|
167
|
-
onKeyDown={onKeyDown}
|
|
168
|
-
onPaste={(e) => {
|
|
169
|
-
const files = filesFromDataTransfer(e.clipboardData);
|
|
170
|
-
if (!files.length) return;
|
|
171
|
-
e.preventDefault();
|
|
172
|
-
onPasteFiles?.(files, taRef.current);
|
|
173
|
-
}}
|
|
174
|
-
onMouseUp={(e) => {
|
|
175
|
-
const ta = taRef.current;
|
|
176
|
-
if (!ta || ta.selectionStart === ta.selectionEnd) return;
|
|
177
|
-
const sel = ta.value.slice(ta.selectionStart, ta.selectionEnd);
|
|
178
|
-
if (!sel) return;
|
|
179
|
-
onTextSel?.({
|
|
180
|
-
text: sel,
|
|
181
|
-
x: e.clientX - 80,
|
|
182
|
-
y: e.clientY - 44,
|
|
183
|
-
});
|
|
184
|
-
}}
|
|
185
|
-
/>
|
|
186
|
-
</div>
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return (
|
|
191
|
-
<div
|
|
192
|
-
className={`mdw-lp-block is-view kind-${kind}${isEmptyBlock(text) ? ' is-empty' : ''}${hit ? ' is-hit' : ''}`}
|
|
193
|
-
data-i={index}
|
|
194
|
-
role="button"
|
|
195
|
-
tabIndex={0}
|
|
196
|
-
onClick={(e) => {
|
|
197
|
-
// 复选框点击直接切换(#99:视图态 ☑/☐ 可点——专业编辑器基线行为)。
|
|
198
|
-
// 不进编辑态;开关走 updateAt=op 通道(可撤销)。DOM 里 .mdw-cb 的序号
|
|
199
|
-
// 与块源码里 todo 行的序号一一对应(按行序渲染)。
|
|
200
|
-
const cb = e.target.closest?.('.mdw-cb');
|
|
201
|
-
if (cb) {
|
|
202
|
-
e.stopPropagation();
|
|
203
|
-
const cbs = [...e.currentTarget.querySelectorAll('.mdw-cb')];
|
|
204
|
-
onToggleTodo?.(index, cbs.indexOf(cb));
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
// 划选评论:有选区时不进编辑,把选区留给上层 mouseup 浮层
|
|
208
|
-
const sel = window.getSelection();
|
|
209
|
-
if (sel && !sel.isCollapsed && String(sel).trim()) return;
|
|
210
|
-
onFocus(index);
|
|
211
|
-
}}
|
|
212
|
-
onKeyDown={(e) => {
|
|
213
|
-
if (e.key === 'Enter' || e.key === ' ') {
|
|
214
|
-
e.preventDefault();
|
|
215
|
-
onFocus(index);
|
|
216
|
-
}
|
|
217
|
-
}}
|
|
218
|
-
>
|
|
219
|
-
{html ? (
|
|
220
|
-
<div className="mdw-lp-view" dangerouslySetInnerHTML={{ __html: html }} />
|
|
221
|
-
) : (
|
|
222
|
-
<div className="mdw-lp-gap" />
|
|
223
|
-
)}
|
|
224
|
-
</div>
|
|
225
|
-
);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* 单栏实时预览:阅读态渲染 + 点击块就地改源码。
|
|
230
|
-
* 无双栏、无高亮描边块。
|
|
231
|
-
*/
|
|
232
|
-
/** 正文块:剥掉首个一级标题行(= 文档标题,由顶部标题栏单独编辑,提交时再写入) */
|
|
233
|
-
function bodyBlocksOnly(body) {
|
|
234
|
-
return liveBodyBlocks(body);
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
function isSlashArrow(e) {
|
|
238
|
-
return e.key === 'ArrowDown' || e.key === 'ArrowUp'
|
|
239
|
-
|| e.code === 'ArrowDown' || e.code === 'ArrowUp';
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
function slashActiveIndex(s) {
|
|
243
|
-
if (!s?.items?.length) return 0;
|
|
244
|
-
const a = s.active;
|
|
245
|
-
if (typeof a !== 'number' || Number.isNaN(a)) return 0;
|
|
246
|
-
return Math.min(Math.max(0, a), s.items.length - 1);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/** 斜杠菜单键盘:返回 true 表示已处理 */
|
|
250
|
-
function handleSlashKey(e, slashRef, { move, pick, close }) {
|
|
251
|
-
const cur = slashRef.current;
|
|
252
|
-
if (!cur?.items?.length) return false;
|
|
253
|
-
if (isSlashArrow(e)) {
|
|
254
|
-
e.preventDefault();
|
|
255
|
-
e.stopPropagation();
|
|
256
|
-
move(e.key === 'ArrowUp' || e.code === 'ArrowUp' ? -1 : 1);
|
|
257
|
-
return true;
|
|
258
|
-
}
|
|
259
|
-
if (e.key === 'Enter' || e.key === 'Tab') {
|
|
260
|
-
e.preventDefault();
|
|
261
|
-
e.stopPropagation();
|
|
262
|
-
pick(cur.items[slashActiveIndex(cur)]);
|
|
263
|
-
return true;
|
|
264
|
-
}
|
|
265
|
-
if (e.key === 'Escape') {
|
|
266
|
-
e.preventDefault();
|
|
267
|
-
e.stopPropagation();
|
|
268
|
-
close();
|
|
269
|
-
return true;
|
|
270
|
-
}
|
|
271
|
-
return false;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
function SlashMenu({ items, active, onPick, onHover, style }) {
|
|
275
|
-
const listRef = useRef(null);
|
|
276
|
-
const sel = slashActiveIndex({ items, active });
|
|
277
|
-
useEffect(() => {
|
|
278
|
-
const root = listRef.current;
|
|
279
|
-
if (!root) return;
|
|
280
|
-
const el = root.querySelector('.mdw-slash-item.on');
|
|
281
|
-
el?.scrollIntoView?.({ block: 'nearest' });
|
|
282
|
-
}, [sel, items]);
|
|
283
|
-
if (!items.length) return null;
|
|
284
|
-
return (
|
|
285
|
-
<div
|
|
286
|
-
ref={listRef}
|
|
287
|
-
className="mdw-slash"
|
|
288
|
-
role="listbox"
|
|
289
|
-
aria-activedescendant={`mdw-slash-opt-${sel}`}
|
|
290
|
-
style={style}
|
|
291
|
-
onMouseDown={(e) => e.preventDefault()}
|
|
292
|
-
>
|
|
293
|
-
<div className="mdw-slash-cap">插入 · ↑↓ 选择 · Enter 确认</div>
|
|
294
|
-
{items.map((c, i) => (
|
|
295
|
-
<button
|
|
296
|
-
key={c.id}
|
|
297
|
-
id={`mdw-slash-opt-${i}`}
|
|
298
|
-
type="button"
|
|
299
|
-
role="option"
|
|
300
|
-
tabIndex={-1}
|
|
301
|
-
aria-selected={i === sel}
|
|
302
|
-
className={`mdw-slash-item${i === sel ? ' on' : ''}`}
|
|
303
|
-
onMouseEnter={() => onHover(i)}
|
|
304
|
-
onMouseDown={(e) => {
|
|
305
|
-
e.preventDefault();
|
|
306
|
-
onPick(c);
|
|
307
|
-
}}
|
|
308
|
-
>
|
|
309
|
-
<span className="lab">{c.label}</span>
|
|
310
|
-
<span className="hint">{c.hint}</span>
|
|
311
|
-
</button>
|
|
312
|
-
))}
|
|
313
|
-
</div>
|
|
314
|
-
);
|
|
315
|
-
}
|
|
50
|
+
export { MdPanes, ModeSeg } from './md-panes-root.jsx';
|
|
316
51
|
|
|
317
52
|
// 标题输入:自动增高 textarea —— input 不能软换行,长标题会横向溢出(UIUX E1)。
|
|
318
53
|
// 标题在语义上仍是单条逻辑行:Enter 不产生换行,把焦点交给正文。
|
|
@@ -372,173 +107,23 @@ function TitleArea({ value, onTitle, placeholder, className, onFocus, onEnter })
|
|
|
372
107
|
);
|
|
373
108
|
}
|
|
374
109
|
|
|
110
|
+
/**
|
|
111
|
+
* 单栏实时预览:阅读态渲染 + 点击块就地改源码。
|
|
112
|
+
* 无双栏、无高亮描边块。
|
|
113
|
+
*/
|
|
375
114
|
function LivePreview({
|
|
376
115
|
title, body, onTitle, onBody, articleRef, attachments, docKey, onFiles, editorApiRef, onTextSel,
|
|
377
116
|
onCloseSelMenu, hideTitle = false, attachHost = null,
|
|
378
117
|
}) {
|
|
379
|
-
const
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
const blocks = bodyBlocksOnly(body);
|
|
389
|
-
// 实时预览查找:块级匹配 + 命中块高亮 + 滚动定位(阅读态此前无查找能力)
|
|
390
|
-
const [findOpen, setFindOpen] = useState(false);
|
|
391
|
-
const [findQuery, setFindQuery] = useState('');
|
|
392
|
-
const [findIdx, setFindIdx] = useState(0);
|
|
393
|
-
const findInputRef = useRef(null);
|
|
394
|
-
const liveMatches = useMemo(() => {
|
|
395
|
-
const q = findQuery.trim().toLowerCase();
|
|
396
|
-
if (!q) return [];
|
|
397
|
-
const out = [];
|
|
398
|
-
blocks.forEach((b, i) => {
|
|
399
|
-
const t = String(b ?? '').toLowerCase();
|
|
400
|
-
let pos = t.indexOf(q);
|
|
401
|
-
while (pos !== -1) {
|
|
402
|
-
out.push({ block: i, start: pos });
|
|
403
|
-
pos = t.indexOf(q, pos + q.length);
|
|
404
|
-
}
|
|
405
|
-
});
|
|
406
|
-
return out;
|
|
407
|
-
}, [blocks, findQuery]);
|
|
408
|
-
const jumpLive = (i) => {
|
|
409
|
-
const total = liveMatches.length;
|
|
410
|
-
if (!total) return;
|
|
411
|
-
const n = ((i % total) + total) % total;
|
|
412
|
-
setFindIdx(n);
|
|
413
|
-
const m = liveMatches[n];
|
|
414
|
-
const el = articleRef.current?.querySelector(`.mdw-lp-block[data-i="${m.block}"]`);
|
|
415
|
-
// 瞬时定位,不走 smooth:平滑滚动与行号槽的即时同步互相打架,命中行会
|
|
416
|
-
// 上下浮动、落点不稳(2026-11-03 任务 #7 移除)。
|
|
417
|
-
el?.scrollIntoView?.({ block: 'center' });
|
|
418
|
-
};
|
|
419
|
-
const hitBlock = liveMatches[findIdx]?.block ?? -1;
|
|
420
|
-
// 空文档:splitMdBlocks('') 会给 [''],视觉上是一片没有把手的空白——
|
|
421
|
-
// 用幽灵引导条接住第一次点击(聚焦首块),聚焦后让位给 textarea 占位符
|
|
422
|
-
const emptyDoc = blocks.length === 0 || blocks.every((b) => !String(b).trim());
|
|
423
|
-
const setSlashBoth = (next) => {
|
|
424
|
-
const v = typeof next === 'function' ? next(slashRef.current) : next;
|
|
425
|
-
slashRef.current = v;
|
|
426
|
-
setSlash(v);
|
|
427
|
-
};
|
|
428
|
-
|
|
429
|
-
useEffect(() => { setFocus(null); setSlashBoth(null); }, [docKey]);
|
|
430
|
-
useEffect(() => { slashRef.current = slash; }, [slash]);
|
|
431
|
-
|
|
432
|
-
// 实时预览不把标题写回正文;composeBody 提交时再拼 `# title`
|
|
433
|
-
// kind:'typing' 连续输入(历史合并成一条)· 'op' 格式/结构操作(各自成条)
|
|
434
|
-
const commit = (nextBlocks, kind = 'op') => onBody(joinMdBlocks(nextBlocks), kind);
|
|
435
|
-
|
|
436
|
-
const updateAt = (i, v, kind = 'op') => {
|
|
437
|
-
const next = blocks.slice();
|
|
438
|
-
next[i] = v;
|
|
439
|
-
commit(next, kind);
|
|
440
|
-
};
|
|
441
|
-
|
|
442
|
-
const applyBlockOp = (i, opResult) => {
|
|
443
|
-
if (!opResult) return;
|
|
444
|
-
pendingSelRef.current = { index: i, start: opResult.start, end: opResult.end };
|
|
445
|
-
updateAt(i, opResult.value);
|
|
446
|
-
};
|
|
447
|
-
|
|
448
|
-
const refreshSlash = (i, ta) => {
|
|
449
|
-
const hit = slashQueryAt(ta);
|
|
450
|
-
if (!hit) {
|
|
451
|
-
setSlashBoth(null);
|
|
452
|
-
return;
|
|
453
|
-
}
|
|
454
|
-
const items = filterSlashCmds(hit.query);
|
|
455
|
-
if (!items.length) {
|
|
456
|
-
setSlashBoth(null);
|
|
457
|
-
return;
|
|
458
|
-
}
|
|
459
|
-
const rect = ta.getBoundingClientRect();
|
|
460
|
-
const x = Math.min(window.innerWidth - 220, rect.left);
|
|
461
|
-
const y = Math.min(window.innerHeight - 220, rect.bottom + 4);
|
|
462
|
-
setSlashBoth((prev) => {
|
|
463
|
-
const keep = prev && prev.index === i && prev.query === hit.query;
|
|
464
|
-
const active = keep ? slashActiveIndex({ ...prev, items }) : 0;
|
|
465
|
-
return { index: i, query: hit.query, items, active, x, y };
|
|
466
|
-
});
|
|
467
|
-
};
|
|
468
|
-
|
|
469
|
-
const moveSlash = (delta) => {
|
|
470
|
-
setSlashBoth((s) => {
|
|
471
|
-
if (!s?.items?.length) return s;
|
|
472
|
-
const n = s.items.length;
|
|
473
|
-
const cur = slashActiveIndex(s);
|
|
474
|
-
return { ...s, active: (cur + delta + n) % n };
|
|
475
|
-
});
|
|
476
|
-
};
|
|
477
|
-
|
|
478
|
-
const pickSlash = (cmd) => {
|
|
479
|
-
const ta = liveTaRef.current;
|
|
480
|
-
const cur = slashRef.current;
|
|
481
|
-
const idx = cur?.index ?? focus;
|
|
482
|
-
if (!ta || idx == null || !cmd) return;
|
|
483
|
-
const next = applySlashCommand(ta, cmd);
|
|
484
|
-
setSlashBoth(null);
|
|
485
|
-
if (next) applyBlockOp(idx, next);
|
|
486
|
-
};
|
|
487
|
-
|
|
488
|
-
// ── @ 附件选择(#108/#111)──
|
|
489
|
-
// 草稿(无 v2 host)数据源 = 会话 doc 的附件(图片按名字内嵌,v1 渲染口径)
|
|
490
|
-
const draftItems = useMemo(
|
|
491
|
-
() => (attachHost ? null : (attachments || [])
|
|
492
|
-
.filter((a) => a.type === 'image')
|
|
493
|
-
// 映射成伪附件(kind/id 对齐 embedForAttachment):草稿的图片按**名字**内嵌
|
|
494
|
-
// (v1 渲染口径:imgMap[name] → dataURL),所以 id 就是名字。
|
|
495
|
-
.map((a) => ({ kind: 'image', id: a.name, name: a.name, sizeText: a.size }))),
|
|
496
|
-
[attachHost, attachments]
|
|
497
|
-
);
|
|
498
|
-
const picker = useAttachPicker({
|
|
499
|
-
host: attachHost,
|
|
500
|
-
itemsOverride: attachHost ? null : draftItems,
|
|
501
|
-
onInsert: ({ index, embed, at, caret }) => {
|
|
502
|
-
// 块内替换:把 '@' 那一段换成引用语法,光标落在引用之后;
|
|
503
|
-
// 引用与正文**前后隔一空**(#111:贴着中文会黏连,行内元素需要边界)
|
|
504
|
-
const val = normalizeBlockText(blocks[index] ?? '');
|
|
505
|
-
const padded = padEmbed(embed, at > 0 ? val[at - 1] : '', val[caret] || '');
|
|
506
|
-
const next = val.slice(0, at) + padded.text + val.slice(caret);
|
|
507
|
-
const caretAt = at + padded.caretAt;
|
|
508
|
-
updateAt(index, next);
|
|
509
|
-
pendingSelRef.current = { index, start: caretAt, end: caretAt };
|
|
510
|
-
setFocus(index);
|
|
511
|
-
},
|
|
512
|
-
});
|
|
513
|
-
// 草稿「+ 添加附件」:文件走页面附件通道(粘贴同款——传会话 doc 并在插入点
|
|
514
|
-
// 落片段);@ 占位字符先清掉,插入点用 '@' 原位置。
|
|
515
|
-
const onDraftAddFiles = (files) => {
|
|
516
|
-
const m = picker.menu;
|
|
517
|
-
const pos = m ? { index: m.index, at: m.at, caret: m.caret } : { index: focus ?? 0, at: null, caret: null };
|
|
518
|
-
if (m) {
|
|
519
|
-
const val = normalizeBlockText(blocks[m.index] ?? '');
|
|
520
|
-
updateAt(m.index, val.slice(0, m.at) + val.slice(m.caret));
|
|
521
|
-
}
|
|
522
|
-
requestAnimationFrame(() => {
|
|
523
|
-
onFiles?.(files, {
|
|
524
|
-
mode: 'live',
|
|
525
|
-
focusIndex: pos.index,
|
|
526
|
-
...(pos.at != null ? { caretInBlock: pos.at } : {}),
|
|
527
|
-
});
|
|
528
|
-
});
|
|
529
|
-
};
|
|
530
|
-
|
|
531
|
-
const applyTurn = (index, turnId) => {
|
|
532
|
-
const ta = liveTaRef.current;
|
|
533
|
-
const fake = ta && focus === index
|
|
534
|
-
? ta
|
|
535
|
-
: { value: normalizeBlockText(blocks[index] ?? ''), selectionStart: 0, selectionEnd: 0 };
|
|
536
|
-
const next = turnBlock(fake, turnId);
|
|
537
|
-
if (next) {
|
|
538
|
-
applyBlockOp(index, next);
|
|
539
|
-
setFocus(index);
|
|
540
|
-
}
|
|
541
|
-
};
|
|
118
|
+
const {
|
|
119
|
+
blocks, focus, setFocus, pendingSelRef, pendingFormatRef, liveTaRef,
|
|
120
|
+
commit, updateAt, applyBlockOp,
|
|
121
|
+
slash, slashRef, setSlashBoth, moveSlash, pickSlash, hoverSlash,
|
|
122
|
+
findOpen, setFindOpen, findQuery, setLiveQuery, findIdx, liveMatches, jumpLive,
|
|
123
|
+
hitBlock, findInputRef,
|
|
124
|
+
picker, onDraftAddFiles, apiBase, rootProps, emptyDoc,
|
|
125
|
+
ingestFiles, onBlockFocus, onTaReady, onBlockChange, onBlockBlur,
|
|
126
|
+
} = useLivePreview({ body, onBody, articleRef, attachments, docKey, onFiles, onCloseSelMenu, attachHost });
|
|
542
127
|
|
|
543
128
|
// ── 跨块选区(#124):块粒度的「统一删除」────────────────────────────
|
|
544
129
|
// 视图块是渲染好的 HTML,选区字符偏移无法精确映射回各块源码(markdown
|
|
@@ -559,92 +144,15 @@ function LivePreview({
|
|
|
559
144
|
pendingSelRef.current = { index: fi, start: 0, end: 0 };
|
|
560
145
|
setFocus(fi);
|
|
561
146
|
return true;
|
|
562
|
-
}, [blocks,
|
|
147
|
+
}, [blocks, commit]);
|
|
563
148
|
|
|
564
149
|
useImperativeHandle(editorApiRef, () => ({
|
|
565
|
-
|
|
566
|
-
const ta = liveTaRef.current;
|
|
567
|
-
if (!ta || focus == null) return;
|
|
568
|
-
ta.focus();
|
|
569
|
-
applyBlockOp(focus, runFormat(ta, op));
|
|
570
|
-
},
|
|
571
|
-
turn(turnId) {
|
|
572
|
-
const i = focus ?? 0;
|
|
573
|
-
applyTurn(i, turnId);
|
|
574
|
-
},
|
|
575
|
-
focus() {
|
|
576
|
-
liveTaRef.current?.focus();
|
|
577
|
-
},
|
|
578
|
-
openFind() {
|
|
579
|
-
setFindOpen(true);
|
|
580
|
-
requestAnimationFrame(() => findInputRef.current?.focus());
|
|
581
|
-
},
|
|
582
|
-
/** 预览态划选后按 Ctrl/⌘B/I/E/K:把该块切到编辑态并保留选区,再套用格式 */
|
|
583
|
-
formatSelection(op) {
|
|
584
|
-
const sel = window.getSelection();
|
|
585
|
-
if (!sel || sel.isCollapsed) return false;
|
|
586
|
-
const text = String(sel.toString() || '');
|
|
587
|
-
if (!text.trim()) return false;
|
|
588
|
-
const node = sel.anchorNode;
|
|
589
|
-
const el = node?.nodeType === 3 ? node.parentElement : node;
|
|
590
|
-
const blockEl = el?.closest?.('.mdw-lp-block');
|
|
591
|
-
if (!blockEl || !articleRef.current?.contains(blockEl)) return false;
|
|
592
|
-
const i = Number(blockEl.getAttribute('data-i'));
|
|
593
|
-
if (!Number.isInteger(i) || i < 0 || i >= blocks.length) return false;
|
|
594
|
-
const raw = normalizeBlockText(blocks[i] ?? '');
|
|
595
|
-
const pos = raw.indexOf(text);
|
|
596
|
-
if (pos === -1) return false; // 选区跨 markdown 标记时保守放弃,不做错位改写
|
|
597
|
-
pendingSelRef.current = { index: i, start: pos, end: pos + text.length };
|
|
598
|
-
pendingFormatRef.current = op;
|
|
599
|
-
onCloseSelMenu?.();
|
|
600
|
-
setFocus(i);
|
|
601
|
-
return true;
|
|
602
|
-
},
|
|
150
|
+
...apiBase,
|
|
603
151
|
/** 跨块选区的浮层「删除选区」(#124):整段移除 [lo, hi] 块(含端点) */
|
|
604
152
|
deleteBlockRange(lo, hi) {
|
|
605
153
|
return removeBlockRange(lo, hi);
|
|
606
154
|
},
|
|
607
|
-
|
|
608
|
-
caretFromDiff(prev, next) {
|
|
609
|
-
const prevJ = joinMdBlocks(bodyBlocksOnly(prev));
|
|
610
|
-
const nextJ = joinMdBlocks(bodyBlocksOnly(next));
|
|
611
|
-
const idx = diffIndex(prevJ, nextJ);
|
|
612
|
-
let start = 0;
|
|
613
|
-
for (let i = 0; i < blocks.length; i += 1) {
|
|
614
|
-
// 用真实 joiner 累加,块边界与正文偏移严格对齐(连接符 '\n\n')
|
|
615
|
-
const end = joinMdBlocks(blocks.slice(0, i + 1)).length;
|
|
616
|
-
if (idx <= end) {
|
|
617
|
-
const off = Math.max(0, Math.min(idx - start, normalizeBlockText(blocks[i] ?? '').length));
|
|
618
|
-
if (focus === i) {
|
|
619
|
-
const ta = liveTaRef.current;
|
|
620
|
-
if (ta) {
|
|
621
|
-
ta.focus();
|
|
622
|
-
ta.setSelectionRange(off, off);
|
|
623
|
-
}
|
|
624
|
-
} else {
|
|
625
|
-
pendingSelRef.current = { index: i, start: off, end: off };
|
|
626
|
-
setFocus(i);
|
|
627
|
-
}
|
|
628
|
-
return;
|
|
629
|
-
}
|
|
630
|
-
start = end + 2;
|
|
631
|
-
}
|
|
632
|
-
},
|
|
633
|
-
}), [focus, onBody, blocks, removeBlockRange]);
|
|
634
|
-
|
|
635
|
-
// document 捕获:即使焦点异常也能 ↑↓
|
|
636
|
-
useEffect(() => {
|
|
637
|
-
if (!slash) return undefined;
|
|
638
|
-
const onKey = (e) => {
|
|
639
|
-
handleSlashKey(e, slashRef, {
|
|
640
|
-
move: moveSlash,
|
|
641
|
-
pick: pickSlash,
|
|
642
|
-
close: () => setSlashBoth(null),
|
|
643
|
-
});
|
|
644
|
-
};
|
|
645
|
-
document.addEventListener('keydown', onKey, true);
|
|
646
|
-
return () => document.removeEventListener('keydown', onKey, true);
|
|
647
|
-
}, [slash]);
|
|
155
|
+
}), [apiBase, removeBlockRange]);
|
|
648
156
|
|
|
649
157
|
// document 捕获:预览态划选后按 Backspace/Delete —— 把选中文字删掉(#98)。
|
|
650
158
|
// 视图块是渲染好的 HTML,选区落在不可编辑内容上,浏览器默认对 Backspace
|
|
@@ -695,6 +203,29 @@ function LivePreview({
|
|
|
695
203
|
return () => document.removeEventListener('keydown', onKey, true);
|
|
696
204
|
}, [blocks, onBody, articleRef, onCloseSelMenu, removeBlockRange]);
|
|
697
205
|
|
|
206
|
+
// 复选框开关:DOM 序号 → 源码第 n 个 todo,走纯函数切换(updateAt=op 通道,可撤销)
|
|
207
|
+
const onToggleTodo = (idx, n) => {
|
|
208
|
+
const raw = normalizeBlockText(blocks[idx] ?? '');
|
|
209
|
+
const next = toggleNthTodo(raw, n);
|
|
210
|
+
if (next) updateAt(idx, next);
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// 视图块点击:复选框直接切换(#99:视图态 ☑/☐ 可点——专业编辑器基线行为)。
|
|
214
|
+
// 不进编辑态。DOM 里 .mdw-cb 的序号与块源码里 todo 行的序号一一对应(按行序渲染)。
|
|
215
|
+
const onViewClick = (e, index) => {
|
|
216
|
+
const cb = e.target.closest?.('.mdw-cb');
|
|
217
|
+
if (cb) {
|
|
218
|
+
e.stopPropagation();
|
|
219
|
+
const cbs = [...e.currentTarget.querySelectorAll('.mdw-cb')];
|
|
220
|
+
onToggleTodo?.(index, cbs.indexOf(cb));
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
// 划选评论:有选区时不进编辑,把选区留给上层 mouseup 浮层
|
|
224
|
+
const sel = window.getSelection();
|
|
225
|
+
if (sel && !sel.isCollapsed && String(sel).trim()) return;
|
|
226
|
+
onBlockFocus(index);
|
|
227
|
+
};
|
|
228
|
+
|
|
698
229
|
const onKeyDown = (i, e) => {
|
|
699
230
|
const val = normalizeBlockText(blocks[i] ?? '');
|
|
700
231
|
const ta = e.target;
|
|
@@ -721,11 +252,10 @@ function LivePreview({
|
|
|
721
252
|
}
|
|
722
253
|
const mod = e.metaKey || e.ctrlKey;
|
|
723
254
|
if (mod && !e.altKey) {
|
|
724
|
-
const
|
|
725
|
-
|
|
726
|
-
if (map[k]) {
|
|
255
|
+
const op = FMT_KEYS[e.key.toLowerCase()];
|
|
256
|
+
if (op) {
|
|
727
257
|
e.preventDefault();
|
|
728
|
-
applyBlockOp(i, runFormat(ta,
|
|
258
|
+
applyBlockOp(i, runFormat(ta, op));
|
|
729
259
|
return;
|
|
730
260
|
}
|
|
731
261
|
}
|
|
@@ -735,108 +265,15 @@ function LivePreview({
|
|
|
735
265
|
return;
|
|
736
266
|
}
|
|
737
267
|
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
e.preventDefault();
|
|
747
|
-
setSlashBoth(null);
|
|
748
|
-
const s = ta.selectionStart;
|
|
749
|
-
const en = ta.selectionEnd;
|
|
750
|
-
const before = val.slice(0, s).replace(/\n+$/, '');
|
|
751
|
-
const after = val.slice(en);
|
|
752
|
-
const next = blocks.slice();
|
|
753
|
-
next[i] = before;
|
|
754
|
-
next.splice(i + 1, 0, after);
|
|
755
|
-
pendingSelRef.current = { index: i + 1, start: 0, end: 0 };
|
|
756
|
-
commit(next);
|
|
757
|
-
setFocus(i + 1);
|
|
758
|
-
return;
|
|
759
|
-
}
|
|
268
|
+
const ctx = {
|
|
269
|
+
e, i, val, ta, blocks, commit, setFocus, pendingSelRef, slashRef, liveTaRef, setSlashBoth, applyBlockOp,
|
|
270
|
+
};
|
|
271
|
+
// Notion:Enter = 在光标处拆成新块(列表续行优先);Shift+Enter = 块内软换行
|
|
272
|
+
if (e.key === 'Enter' && !e.shiftKey && !mod && handleEnterSplit(ctx)) return;
|
|
273
|
+
// Backspace:空首块移除 / 块首并上块(FM 块除外)/ 裸列表项一键逃出(#100)
|
|
274
|
+
if (e.key === 'Backspace' && handleBackspaceStart(ctx)) return;
|
|
275
|
+
if (e.key === 'Backspace' && handleBareListEscape(ctx)) return;
|
|
760
276
|
|
|
761
|
-
if (e.key === 'Backspace' && isEmptyBlock(val) && i === 0 && blocks.length > 1) {
|
|
762
|
-
e.preventDefault();
|
|
763
|
-
setSlashBoth(null);
|
|
764
|
-
const next = blocks.slice();
|
|
765
|
-
next.splice(i, 1);
|
|
766
|
-
commit(next);
|
|
767
|
-
setFocus(0);
|
|
768
|
-
return;
|
|
769
|
-
}
|
|
770
|
-
// Backspace 在块首(无选区、非首块):并入上一块,光标落在接缝处(Notion 行为)。
|
|
771
|
-
// 这也是清掉「头上莫名多出的空块」的自然手段——空块并一次就消失。
|
|
772
|
-
// FM 块(属性卡)不许被并进正文:合并会把 `---` 撕碎成普通文本。
|
|
773
|
-
if (
|
|
774
|
-
e.key === 'Backspace'
|
|
775
|
-
&& ta.selectionStart === 0 && ta.selectionEnd === 0
|
|
776
|
-
&& i > 0 && !slashRef.current
|
|
777
|
-
&& !isFrontmatterBlock(normalizeBlockText(blocks[i - 1] ?? ''))
|
|
778
|
-
) {
|
|
779
|
-
e.preventDefault();
|
|
780
|
-
setSlashBoth(null);
|
|
781
|
-
const prev = normalizeBlockText(blocks[i - 1] ?? '');
|
|
782
|
-
const next = blocks.slice();
|
|
783
|
-
next[i - 1] = prev + val;
|
|
784
|
-
next.splice(i, 1);
|
|
785
|
-
pendingSelRef.current = { index: i - 1, start: prev.length, end: prev.length };
|
|
786
|
-
commit(next);
|
|
787
|
-
setFocus(i - 1);
|
|
788
|
-
return;
|
|
789
|
-
}
|
|
790
|
-
// Backspace 在**空的列表项行**上(光标前只剩 `- ` / `- [ ] ` / `1. ` 这类裸前缀,
|
|
791
|
-
// #100 验收反馈):一键剥掉前缀「逃出列表」——落点是**空白行**继续打字,
|
|
792
|
-
// 而不是被顶回上一行(此前:删完前缀再按一下,原生会把行首 \n 吃掉、光标
|
|
793
|
-
// 跳到上一行行尾)。与 continueListOnEnter 的「空项 Enter 收回」成对,
|
|
794
|
-
// 同为 Notion 语义。落点按行在块里的位置分三种(块内存不住空行——空行是
|
|
795
|
-
// 块分隔符,join/split 往返即裂):
|
|
796
|
-
// 独行块 → 本块原地变空,光标留在本块
|
|
797
|
-
// 块尾行 → 上文留下,新开一个空块(Shift+Enter 块尾同款约定)
|
|
798
|
-
// 块中行 → 拆成 [上文, '', 下文],光标落中间空块
|
|
799
|
-
if (
|
|
800
|
-
e.key === 'Backspace'
|
|
801
|
-
&& ta.selectionStart === ta.selectionEnd && !slashRef.current
|
|
802
|
-
) {
|
|
803
|
-
const s = ta.selectionStart;
|
|
804
|
-
const lineStart = val.lastIndexOf('\n', s - 1) + 1;
|
|
805
|
-
const lineEndIdx = val.indexOf('\n', s);
|
|
806
|
-
const lineEnd = lineEndIdx === -1 ? val.length : lineEndIdx;
|
|
807
|
-
// 整行必须只有裸前缀(光标夹在中间也一样)——否则 `- 待办|` 会整行被误删
|
|
808
|
-
const wholeLine = val.slice(lineStart, lineEnd);
|
|
809
|
-
const isBareItem = isBareListItemLine(wholeLine);
|
|
810
|
-
if (isBareItem) {
|
|
811
|
-
e.preventDefault();
|
|
812
|
-
setSlashBoth(null);
|
|
813
|
-
const above = val.slice(0, lineStart).replace(/\n$/, '');
|
|
814
|
-
const below = lineEndIdx === -1 ? '' : val.slice(lineEnd + 1);
|
|
815
|
-
const next = blocks.slice();
|
|
816
|
-
if (!above && !below) {
|
|
817
|
-
// 独行块:本块原地变空
|
|
818
|
-
next[i] = '';
|
|
819
|
-
pendingSelRef.current = { index: i, start: 0, end: 0 };
|
|
820
|
-
commit(next);
|
|
821
|
-
setFocus(i);
|
|
822
|
-
} else if (!below) {
|
|
823
|
-
// 块尾行:上文留下,新开一个空块
|
|
824
|
-
next[i] = above;
|
|
825
|
-
next.splice(i + 1, 0, '');
|
|
826
|
-
pendingSelRef.current = { index: i + 1, start: 0, end: 0 };
|
|
827
|
-
commit(next);
|
|
828
|
-
setFocus(i + 1);
|
|
829
|
-
} else {
|
|
830
|
-
// 块中行:上文 / 空块 / 下文,光标落中间空块
|
|
831
|
-
next[i] = above;
|
|
832
|
-
next.splice(i + 1, 0, '', below);
|
|
833
|
-
pendingSelRef.current = { index: i + 1, start: 0, end: 0 };
|
|
834
|
-
commit(next);
|
|
835
|
-
setFocus(i + 1);
|
|
836
|
-
}
|
|
837
|
-
return;
|
|
838
|
-
}
|
|
839
|
-
}
|
|
840
277
|
// Delete 在块尾(无选区、非末块):把下一块并进来,光标落接缝——
|
|
841
278
|
// Backspace 并上块的镜像(Notion 行为)。FM 块同理不许被并:
|
|
842
279
|
// 合并会把下一块的 `---` 撕碎成普通文本。
|
|
@@ -857,122 +294,21 @@ function LivePreview({
|
|
|
857
294
|
setFocus(i);
|
|
858
295
|
return;
|
|
859
296
|
}
|
|
860
|
-
// Shift+Enter
|
|
861
|
-
|
|
862
|
-
// 分隔符(2026-11-07 #92 实测换行被往返吃掉,表现为按键无响应)——
|
|
863
|
-
// 等效改写为「新开一个空块」:视觉同为多出一行,且模型存得住。
|
|
864
|
-
if (e.key === 'Enter' && e.shiftKey && !mod && !slashRef.current) {
|
|
865
|
-
const s = ta.selectionStart;
|
|
866
|
-
const en = ta.selectionEnd;
|
|
867
|
-
if (s === 0 && en === 0) {
|
|
868
|
-
e.preventDefault();
|
|
869
|
-
setSlashBoth(null);
|
|
870
|
-
const next = blocks.slice();
|
|
871
|
-
next.splice(i, 0, '');
|
|
872
|
-
pendingSelRef.current = { index: i, start: 0, end: 0 };
|
|
873
|
-
commit(next);
|
|
874
|
-
setFocus(i);
|
|
875
|
-
return;
|
|
876
|
-
}
|
|
877
|
-
if (s === val.length && en === val.length) {
|
|
878
|
-
e.preventDefault();
|
|
879
|
-
setSlashBoth(null);
|
|
880
|
-
const next = blocks.slice();
|
|
881
|
-
next.splice(i + 1, 0, '');
|
|
882
|
-
pendingSelRef.current = { index: i + 1, start: 0, end: 0 };
|
|
883
|
-
commit(next);
|
|
884
|
-
setFocus(i + 1);
|
|
885
|
-
return;
|
|
886
|
-
}
|
|
887
|
-
return; // 中段:原生插入
|
|
888
|
-
}
|
|
297
|
+
// Shift+Enter:块头/块尾新开空块(中段放行原生插入)
|
|
298
|
+
if (e.key === 'Enter' && e.shiftKey && !mod && !slashRef.current && handleSoftBreak(ctx)) return;
|
|
889
299
|
// 斜杠菜单打开时禁止块间跳转(必须用 ref,避免闭包过期)
|
|
890
300
|
if (slashRef.current) return;
|
|
891
|
-
// ↑↓ 跨块:块首 ↑
|
|
892
|
-
|
|
893
|
-
// 光标总在块的右下角、像跳过了整块开头,只能鼠标点回来。
|
|
894
|
-
const collapsed = ta.selectionStart === ta.selectionEnd;
|
|
895
|
-
if (collapsed && !e.shiftKey && !e.altKey) {
|
|
896
|
-
if (e.key === 'ArrowUp' && ta.selectionStart === 0 && i > 0) {
|
|
897
|
-
e.preventDefault();
|
|
898
|
-
const prevLen = normalizeBlockText(blocks[i - 1] ?? '').length;
|
|
899
|
-
pendingSelRef.current = { index: i - 1, start: prevLen, end: prevLen };
|
|
900
|
-
setFocus(i - 1);
|
|
901
|
-
return;
|
|
902
|
-
}
|
|
903
|
-
if (e.key === 'ArrowDown' && ta.selectionEnd === val.length && i < blocks.length - 1) {
|
|
904
|
-
e.preventDefault();
|
|
905
|
-
pendingSelRef.current = { index: i + 1, start: 0, end: 0 };
|
|
906
|
-
setFocus(i + 1);
|
|
907
|
-
return;
|
|
908
|
-
}
|
|
909
|
-
// 软换行块的**视觉行**兜底:光标不在块首/块尾时先放行原生移动,
|
|
910
|
-
// 一帧后没动 = 已在第一/最后视觉行——补一次跨块跳转(Notion 行为)
|
|
911
|
-
if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
|
|
912
|
-
const before = { start: ta.selectionStart, end: ta.selectionEnd };
|
|
913
|
-
const dir = e.key;
|
|
914
|
-
requestAnimationFrame(() => {
|
|
915
|
-
const el = liveTaRef.current;
|
|
916
|
-
if (!el || el !== document.activeElement) return;
|
|
917
|
-
if (el.selectionStart !== before.start || el.selectionEnd !== before.end) return;
|
|
918
|
-
const len = normalizeBlockText(blocks[i] ?? '').length;
|
|
919
|
-
if (dir === 'ArrowUp' && before.start > 0 && i > 0) {
|
|
920
|
-
// ↑ 到上一块**末尾**(跨块只动光标不改内容,FM 块也可进入)
|
|
921
|
-
const prevLen = normalizeBlockText(blocks[i - 1] ?? '').length;
|
|
922
|
-
pendingSelRef.current = { index: i - 1, start: prevLen, end: prevLen };
|
|
923
|
-
setFocus(i - 1);
|
|
924
|
-
} else if (dir === 'ArrowDown' && before.end < len && i < blocks.length - 1) {
|
|
925
|
-
pendingSelRef.current = { index: i + 1, start: 0, end: 0 };
|
|
926
|
-
setFocus(i + 1);
|
|
927
|
-
}
|
|
928
|
-
});
|
|
929
|
-
}
|
|
930
|
-
}
|
|
931
|
-
};
|
|
932
|
-
|
|
933
|
-
const ingestFiles = (files, ta) => {
|
|
934
|
-
if (!files?.length || !onFiles) return;
|
|
935
|
-
// 实时预览的光标是**块内**偏移,绝不能当全文 insertAt——否则会插进
|
|
936
|
-
// frontmatter(保存时被剥掉,附件仓留下孤儿文件;任务 #15 验收根因)。
|
|
937
|
-
onFiles(files, {
|
|
938
|
-
mode: 'live',
|
|
939
|
-
focusIndex: focus,
|
|
940
|
-
caretInBlock: ta ? ta.selectionStart : null,
|
|
941
|
-
});
|
|
301
|
+
// ↑↓ 跨块:块首 ↑ 落上一块末尾、块尾 ↓ 落下一块开头;软换行块走视觉行兜底(#92)
|
|
302
|
+
handleArrowNav(ctx);
|
|
942
303
|
};
|
|
943
304
|
|
|
944
305
|
return (
|
|
945
|
-
<div
|
|
946
|
-
className={`mdw-lp${dragging ? ' is-drop' : ''}`}
|
|
947
|
-
onDragEnter={(e) => {
|
|
948
|
-
if (![...e.dataTransfer.types].includes('Files')) return;
|
|
949
|
-
e.preventDefault();
|
|
950
|
-
setDragging(true);
|
|
951
|
-
}}
|
|
952
|
-
onDragOver={(e) => {
|
|
953
|
-
if (![...e.dataTransfer.types].includes('Files')) return;
|
|
954
|
-
e.preventDefault();
|
|
955
|
-
e.dataTransfer.dropEffect = 'copy';
|
|
956
|
-
}}
|
|
957
|
-
onDragLeave={(e) => {
|
|
958
|
-
if (!e.currentTarget.contains(e.relatedTarget)) setDragging(false);
|
|
959
|
-
}}
|
|
960
|
-
onDrop={(e) => {
|
|
961
|
-
setDragging(false);
|
|
962
|
-
const files = filesFromDataTransfer(e.dataTransfer);
|
|
963
|
-
if (!files.length) return;
|
|
964
|
-
e.preventDefault();
|
|
965
|
-
ingestFiles(files, null);
|
|
966
|
-
}}
|
|
967
|
-
>
|
|
306
|
+
<div {...rootProps}>
|
|
968
307
|
<div className="mdw-lp-sheet" ref={articleRef}>
|
|
969
308
|
{findOpen && (
|
|
970
309
|
<EditorFindBar
|
|
971
310
|
query={findQuery}
|
|
972
|
-
setQuery={
|
|
973
|
-
setFindQuery(q);
|
|
974
|
-
setFindIdx(0);
|
|
975
|
-
}}
|
|
311
|
+
setQuery={setLiveQuery}
|
|
976
312
|
index={findIdx}
|
|
977
313
|
total={liveMatches.length}
|
|
978
314
|
onNext={() => jumpLive(findIdx + 1)}
|
|
@@ -1003,39 +339,12 @@ function LivePreview({
|
|
|
1003
339
|
attachments={attachments}
|
|
1004
340
|
pendingSelRef={pendingSelRef}
|
|
1005
341
|
pendingFormatRef={pendingFormatRef}
|
|
1006
|
-
onFocus={
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
}
|
|
1010
|
-
|
|
1011
|
-
onChange={(v) => {
|
|
1012
|
-
updateAt(i, v, 'typing');
|
|
1013
|
-
cancelAnimationFrame(slashRafRef.current);
|
|
1014
|
-
slashRafRef.current = requestAnimationFrame(() => {
|
|
1015
|
-
const ta = liveTaRef.current;
|
|
1016
|
-
if (!ta) return;
|
|
1017
|
-
refreshSlash(i, ta);
|
|
1018
|
-
// @ → 唤起附件选择面板(#108);不在触发点就收掉
|
|
1019
|
-
const hit = atTriggerAt(ta);
|
|
1020
|
-
if (hit) picker.open({ index: i, ta, at: hit.at, caret: hit.caret });
|
|
1021
|
-
else picker.close();
|
|
1022
|
-
});
|
|
1023
|
-
}}
|
|
1024
|
-
onBlur={() => {
|
|
1025
|
-
requestAnimationFrame(() => {
|
|
1026
|
-
if (document.activeElement?.closest?.('.mdw-lp-block.is-edit')) return;
|
|
1027
|
-
if (document.activeElement?.closest?.('.mdw-slash')) return;
|
|
1028
|
-
if (document.activeElement?.closest?.('.mdw-atpick')) return; // @ 附件面板(#108)
|
|
1029
|
-
setFocus(null);
|
|
1030
|
-
setSlashBoth(null);
|
|
1031
|
-
});
|
|
1032
|
-
}}
|
|
342
|
+
onFocus={onBlockFocus}
|
|
343
|
+
onViewClick={onViewClick}
|
|
344
|
+
onTaReady={onTaReady}
|
|
345
|
+
onChange={(v) => onBlockChange(i, v)}
|
|
346
|
+
onBlur={onBlockBlur}
|
|
1033
347
|
onKeyDown={(e) => onKeyDown(i, e)}
|
|
1034
|
-
onToggleTodo={(idx, n) => {
|
|
1035
|
-
const raw = normalizeBlockText(blocks[idx] ?? '');
|
|
1036
|
-
const next = toggleNthTodo(raw, n);
|
|
1037
|
-
if (next) updateAt(idx, next);
|
|
1038
|
-
}}
|
|
1039
348
|
onPasteFiles={(files, ta) => ingestFiles(files, ta)}
|
|
1040
349
|
onTextSel={onTextSel}
|
|
1041
350
|
/>
|
|
@@ -1066,7 +375,7 @@ function LivePreview({
|
|
|
1066
375
|
<SlashMenu
|
|
1067
376
|
items={slash.items}
|
|
1068
377
|
active={slash.active}
|
|
1069
|
-
onHover={
|
|
378
|
+
onHover={hoverSlash}
|
|
1070
379
|
onPick={pickSlash}
|
|
1071
380
|
style={{ left: slash.x, top: slash.y }}
|
|
1072
381
|
/>,
|
|
@@ -1095,369 +404,50 @@ function LivePreview({
|
|
|
1095
404
|
);
|
|
1096
405
|
}
|
|
1097
406
|
|
|
1098
|
-
|
|
1099
|
-
return (
|
|
1100
|
-
<div className="mdw-find" role="search">
|
|
1101
|
-
<input
|
|
1102
|
-
ref={inputRef}
|
|
1103
|
-
className="mdw-find-input"
|
|
1104
|
-
value={query}
|
|
1105
|
-
placeholder="查找…"
|
|
1106
|
-
aria-label="查找"
|
|
1107
|
-
onChange={(e) => setQuery(e.target.value)}
|
|
1108
|
-
onKeyDown={(e) => {
|
|
1109
|
-
if (e.key === 'Escape') {
|
|
1110
|
-
e.preventDefault();
|
|
1111
|
-
onClose();
|
|
1112
|
-
return;
|
|
1113
|
-
}
|
|
1114
|
-
if (e.key === 'Enter') {
|
|
1115
|
-
e.preventDefault();
|
|
1116
|
-
if (e.shiftKey) onPrev();
|
|
1117
|
-
else onNext();
|
|
1118
|
-
}
|
|
1119
|
-
if (e.key === 'F3') {
|
|
1120
|
-
e.preventDefault();
|
|
1121
|
-
if (e.shiftKey) onPrev();
|
|
1122
|
-
else onNext();
|
|
1123
|
-
}
|
|
1124
|
-
}}
|
|
1125
|
-
/>
|
|
1126
|
-
<span className="mdw-find-count">
|
|
1127
|
-
{total ? `${index + 1}/${total}` : '无结果'}
|
|
1128
|
-
</span>
|
|
1129
|
-
<button type="button" className="mdw-find-nav" title="上一个" aria-label="上一个匹配" onClick={onPrev}>↑</button>
|
|
1130
|
-
<button type="button" className="mdw-find-nav" title="下一个" aria-label="下一个匹配" onClick={onNext}>↓</button>
|
|
1131
|
-
<button type="button" className="mdw-find-nav" title="关闭" aria-label="关闭查找" onClick={onClose}>×</button>
|
|
1132
|
-
</div>
|
|
1133
|
-
);
|
|
1134
|
-
}
|
|
1135
|
-
|
|
1136
|
-
/** 源码编辑器:soft-wrap、行号、查找、格式 API、粘贴/拖图、行首 / */
|
|
407
|
+
/** 源码编辑器:soft-wrap、行号、查找、格式 API、粘贴/拖图、行首 /(状态与逻辑见 use-source-editor.js) */
|
|
1137
408
|
function SourceEditor({ title, body, onTitle, onBody, editorApiRef, onFiles, onTextSel }) {
|
|
1138
|
-
const
|
|
1139
|
-
const
|
|
1140
|
-
|
|
1141
|
-
const pendingSel = useRef(null);
|
|
1142
|
-
const [wrap, setWrap] = useState(true);
|
|
1143
|
-
const [showLines, setShowLines] = useState(true);
|
|
1144
|
-
const [dragging, setDragging] = useState(false);
|
|
1145
|
-
const [findOpen, setFindOpen] = useState(false);
|
|
1146
|
-
const [findQuery, setFindQuery] = useState('');
|
|
1147
|
-
const [findIdx, setFindIdx] = useState(0);
|
|
1148
|
-
const [caret, setCaret] = useState({ line: 1, col: 1 });
|
|
1149
|
-
const [showRuler, setShowRuler] = useState(true);
|
|
1150
|
-
const [slash, setSlash] = useState(null);
|
|
1151
|
-
const slashRef = useRef(null);
|
|
1152
|
-
const slashRafRef = useRef(0);
|
|
1153
|
-
const stats = countStats(body);
|
|
1154
|
-
const matches = findMatches(body, findQuery);
|
|
1155
|
-
const setSlashBoth = (next) => {
|
|
1156
|
-
const v = typeof next === 'function' ? next(slashRef.current) : next;
|
|
1157
|
-
slashRef.current = v;
|
|
1158
|
-
setSlash(v);
|
|
1159
|
-
};
|
|
1160
|
-
useEffect(() => { slashRef.current = slash; }, [slash]);
|
|
1161
|
-
|
|
1162
|
-
const syncCaret = () => {
|
|
1163
|
-
const ta = taRef.current;
|
|
1164
|
-
if (!ta) return;
|
|
1165
|
-
setCaret(caretLineCol(ta.value, ta.selectionStart));
|
|
1166
|
-
};
|
|
1167
|
-
|
|
1168
|
-
const refreshSlash = (ta) => {
|
|
1169
|
-
const hit = slashQueryAt(ta);
|
|
1170
|
-
if (!hit) {
|
|
1171
|
-
setSlashBoth(null);
|
|
1172
|
-
return;
|
|
1173
|
-
}
|
|
1174
|
-
const items = filterSlashCmds(hit.query);
|
|
1175
|
-
if (!items.length) {
|
|
1176
|
-
setSlashBoth(null);
|
|
1177
|
-
return;
|
|
1178
|
-
}
|
|
1179
|
-
const rect = ta.getBoundingClientRect();
|
|
1180
|
-
const lineH = 20;
|
|
1181
|
-
const line = caretLineCol(ta.value, ta.selectionStart).line;
|
|
1182
|
-
const x = Math.min(window.innerWidth - 220, rect.left + 12);
|
|
1183
|
-
const y = Math.min(window.innerHeight - 220, rect.top + line * lineH - ta.scrollTop + 8);
|
|
1184
|
-
setSlashBoth((prev) => {
|
|
1185
|
-
const keep = prev && prev.query === hit.query;
|
|
1186
|
-
const active = keep ? slashActiveIndex({ ...prev, items }) : 0;
|
|
1187
|
-
return { query: hit.query, items, active, x, y };
|
|
1188
|
-
});
|
|
1189
|
-
};
|
|
1190
|
-
|
|
1191
|
-
const moveSlash = (delta) => {
|
|
1192
|
-
setSlashBoth((s) => {
|
|
1193
|
-
if (!s?.items?.length) return s;
|
|
1194
|
-
const n = s.items.length;
|
|
1195
|
-
const cur = slashActiveIndex(s);
|
|
1196
|
-
return { ...s, active: (cur + delta + n) % n };
|
|
1197
|
-
});
|
|
1198
|
-
};
|
|
1199
|
-
|
|
1200
|
-
const applyOp = (opResult) => {
|
|
1201
|
-
if (!opResult) return;
|
|
1202
|
-
pendingSel.current = { start: opResult.start, end: opResult.end };
|
|
1203
|
-
onBody(opResult.value);
|
|
1204
|
-
};
|
|
1205
|
-
|
|
1206
|
-
const pickSlash = (cmd) => {
|
|
1207
|
-
const ta = taRef.current;
|
|
1208
|
-
if (!ta || !cmd) return;
|
|
1209
|
-
const next = applySlashCommand(ta, cmd);
|
|
1210
|
-
setSlashBoth(null);
|
|
1211
|
-
if (next) applyOp(next);
|
|
1212
|
-
};
|
|
1213
|
-
|
|
1214
|
-
useEffect(() => {
|
|
1215
|
-
if (!slash) return undefined;
|
|
1216
|
-
const onKey = (e) => {
|
|
1217
|
-
handleSlashKey(e, slashRef, {
|
|
1218
|
-
move: moveSlash,
|
|
1219
|
-
pick: pickSlash,
|
|
1220
|
-
close: () => setSlashBoth(null),
|
|
1221
|
-
});
|
|
1222
|
-
};
|
|
1223
|
-
document.addEventListener('keydown', onKey, true);
|
|
1224
|
-
return () => document.removeEventListener('keydown', onKey, true);
|
|
1225
|
-
}, [slash]);
|
|
1226
|
-
|
|
1227
|
-
useLayoutEffect(() => {
|
|
1228
|
-
const sel = pendingSel.current;
|
|
1229
|
-
const ta = taRef.current;
|
|
1230
|
-
if (!sel || !ta) return;
|
|
1231
|
-
pendingSel.current = null;
|
|
1232
|
-
try { ta.setSelectionRange(sel.start, sel.end); } catch { /* ignore */ }
|
|
1233
|
-
}, [body]);
|
|
1234
|
-
|
|
1235
|
-
useEffect(() => {
|
|
1236
|
-
if (!findOpen) return;
|
|
1237
|
-
findInputRef.current?.focus();
|
|
1238
|
-
findInputRef.current?.select();
|
|
1239
|
-
}, [findOpen]);
|
|
1240
|
-
|
|
1241
|
-
useEffect(() => {
|
|
1242
|
-
setFindIdx(0);
|
|
1243
|
-
}, [findQuery, body]);
|
|
1244
|
-
|
|
1245
|
-
const jumpToMatch = (i) => {
|
|
1246
|
-
const ta = taRef.current;
|
|
1247
|
-
if (!ta || !matches.length) return;
|
|
1248
|
-
const idx = ((i % matches.length) + matches.length) % matches.length;
|
|
1249
|
-
setFindIdx(idx);
|
|
1250
|
-
const m = matches[idx];
|
|
1251
|
-
ta.focus();
|
|
1252
|
-
try { ta.setSelectionRange(m.start, m.end); } catch { /* ignore */ }
|
|
1253
|
-
scrollTextareaToIndex(ta, m.start);
|
|
1254
|
-
// 把焦点还给查找框,便于连续 Enter
|
|
1255
|
-
requestAnimationFrame(() => findInputRef.current?.focus());
|
|
1256
|
-
};
|
|
1257
|
-
|
|
1258
|
-
const openFind = () => {
|
|
1259
|
-
setFindOpen(true);
|
|
1260
|
-
const ta = taRef.current;
|
|
1261
|
-
if (ta && ta.selectionStart !== ta.selectionEnd) {
|
|
1262
|
-
const sel = ta.value.slice(ta.selectionStart, ta.selectionEnd);
|
|
1263
|
-
if (sel && !sel.includes('\n') && sel.length < 80) setFindQuery(sel);
|
|
1264
|
-
}
|
|
1265
|
-
};
|
|
1266
|
-
|
|
1267
|
-
useImperativeHandle(editorApiRef, () => ({
|
|
1268
|
-
format(op) {
|
|
1269
|
-
const ta = taRef.current;
|
|
1270
|
-
if (!ta) return;
|
|
1271
|
-
ta.focus();
|
|
1272
|
-
applyOp(runFormat(ta, op));
|
|
1273
|
-
},
|
|
1274
|
-
turn(turnId) {
|
|
1275
|
-
const ta = taRef.current;
|
|
1276
|
-
if (!ta) return;
|
|
1277
|
-
ta.focus();
|
|
1278
|
-
applyOp(turnLine(ta, turnId));
|
|
1279
|
-
},
|
|
1280
|
-
insertAtCursor(text) {
|
|
1281
|
-
const ta = taRef.current;
|
|
1282
|
-
if (!ta) return;
|
|
1283
|
-
applyOp(insertTextAtCursor(ta, text));
|
|
1284
|
-
},
|
|
1285
|
-
getCursor() {
|
|
1286
|
-
const ta = taRef.current;
|
|
1287
|
-
return ta ? ta.selectionStart : null;
|
|
1288
|
-
},
|
|
1289
|
-
/** 撤销/重做后把光标放回改动处(公共前缀即改动起点) */
|
|
1290
|
-
caretFromDiff(prev, next) {
|
|
1291
|
-
const ta = taRef.current;
|
|
1292
|
-
if (!ta) return;
|
|
1293
|
-
const p = Math.min(diffIndex(prev, next), ta.value.length);
|
|
1294
|
-
ta.focus();
|
|
1295
|
-
ta.setSelectionRange(p, p);
|
|
1296
|
-
},
|
|
1297
|
-
toggleWrap() {
|
|
1298
|
-
setWrap((v) => !v);
|
|
1299
|
-
},
|
|
1300
|
-
getWrap: () => wrap,
|
|
1301
|
-
openFind,
|
|
1302
|
-
focus() {
|
|
1303
|
-
taRef.current?.focus();
|
|
1304
|
-
},
|
|
1305
|
-
}), [onBody, wrap]);
|
|
1306
|
-
|
|
409
|
+
const ed = useSourceEditor({ body, onBody, editorApiRef, onFiles, onTextSel });
|
|
410
|
+
const { ingestImages } = ed;
|
|
411
|
+
// IME 组合中(拼音候选未上屏):按键归输入法,快捷键一概不接(块编辑器同款守卫)
|
|
1307
412
|
const onKeyDown = (e) => {
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
if (e.nativeEvent?.isComposing || e.keyCode === 229) return; // IME 组合中:按键归输入法
|
|
1311
|
-
if (handleSlashKey(e, slashRef, {
|
|
1312
|
-
move: moveSlash,
|
|
1313
|
-
pick: pickSlash,
|
|
1314
|
-
close: () => setSlashBoth(null),
|
|
1315
|
-
})) return;
|
|
1316
|
-
const mod = e.metaKey || e.ctrlKey;
|
|
1317
|
-
if (mod && !e.altKey && e.key.toLowerCase() === 'f') {
|
|
1318
|
-
e.preventDefault();
|
|
1319
|
-
openFind();
|
|
1320
|
-
return;
|
|
1321
|
-
}
|
|
1322
|
-
if (e.key === 'F3') {
|
|
1323
|
-
e.preventDefault();
|
|
1324
|
-
if (!findOpen) openFind();
|
|
1325
|
-
else if (e.shiftKey) jumpToMatch(findIdx - 1);
|
|
1326
|
-
else jumpToMatch(findIdx + 1);
|
|
1327
|
-
return;
|
|
1328
|
-
}
|
|
1329
|
-
if (mod && !e.altKey) {
|
|
1330
|
-
const k = e.key.toLowerCase();
|
|
1331
|
-
const map = { b: 'bold', i: 'italic', e: 'code', k: 'link' };
|
|
1332
|
-
if (map[k]) {
|
|
1333
|
-
e.preventDefault();
|
|
1334
|
-
applyOp(runFormat(ta, map[k]));
|
|
1335
|
-
return;
|
|
1336
|
-
}
|
|
1337
|
-
}
|
|
1338
|
-
if (e.key === 'Tab') {
|
|
1339
|
-
e.preventDefault();
|
|
1340
|
-
applyOp(indentLines(ta, e.shiftKey));
|
|
1341
|
-
return;
|
|
1342
|
-
}
|
|
1343
|
-
if (e.key === 'Enter' && !e.shiftKey && !mod) {
|
|
1344
|
-
const next = continueListOnEnter(ta);
|
|
1345
|
-
if (next) {
|
|
1346
|
-
e.preventDefault();
|
|
1347
|
-
applyOp(next);
|
|
1348
|
-
}
|
|
1349
|
-
setSlashBoth(null);
|
|
1350
|
-
}
|
|
1351
|
-
};
|
|
1352
|
-
|
|
1353
|
-
const ingestImages = (files) => {
|
|
1354
|
-
if (!files?.length || !onFiles) return;
|
|
1355
|
-
const ta = taRef.current;
|
|
1356
|
-
// 源码态 textarea 持有全文,selectionStart 才是合法的全文偏移。
|
|
1357
|
-
onFiles(files, { insertAt: ta ? ta.selectionStart : null, mode: 'edit' });
|
|
1358
|
-
};
|
|
1359
|
-
|
|
1360
|
-
const syncGutter = () => {
|
|
1361
|
-
if (gutterRef.current && taRef.current) {
|
|
1362
|
-
gutterRef.current.scrollTop = taRef.current.scrollTop;
|
|
1363
|
-
}
|
|
413
|
+
if (e.nativeEvent?.isComposing || e.keyCode === 229) return;
|
|
414
|
+
ed.onKeyDown(e);
|
|
1364
415
|
};
|
|
1365
|
-
|
|
1366
|
-
const lineNums = showLines
|
|
1367
|
-
? Array.from({ length: Math.max(1, stats.lines) }, (_, i) => i + 1)
|
|
1368
|
-
: null;
|
|
1369
|
-
|
|
1370
416
|
return (
|
|
1371
|
-
<div
|
|
1372
|
-
className={`mdw-pane mdw-pane-edit${dragging ? ' is-drop' : ''}`}
|
|
1373
|
-
onDragEnter={(e) => {
|
|
1374
|
-
if (![...e.dataTransfer.types].includes('Files')) return;
|
|
1375
|
-
e.preventDefault();
|
|
1376
|
-
setDragging(true);
|
|
1377
|
-
}}
|
|
1378
|
-
onDragOver={(e) => {
|
|
1379
|
-
if (![...e.dataTransfer.types].includes('Files')) return;
|
|
1380
|
-
e.preventDefault();
|
|
1381
|
-
e.dataTransfer.dropEffect = 'copy';
|
|
1382
|
-
}}
|
|
1383
|
-
onDragLeave={(e) => {
|
|
1384
|
-
if (!e.currentTarget.contains(e.relatedTarget)) setDragging(false);
|
|
1385
|
-
}}
|
|
1386
|
-
onDrop={(e) => {
|
|
1387
|
-
setDragging(false);
|
|
1388
|
-
// textarea 自己的 onDrop 已 ingest 过(preventDefault 后事件才冒泡到
|
|
1389
|
-
// 这里)——不查 defaultPrevented 一次拖放会传两遍、传两份附件(#118)。
|
|
1390
|
-
if (e.defaultPrevented) return;
|
|
1391
|
-
const files = filesFromDataTransfer(e.dataTransfer);
|
|
1392
|
-
if (!files.length) return;
|
|
1393
|
-
e.preventDefault();
|
|
1394
|
-
ingestImages(files);
|
|
1395
|
-
}}
|
|
1396
|
-
>
|
|
417
|
+
<div {...ed.rootProps}>
|
|
1397
418
|
<TitleArea
|
|
1398
419
|
className="mdw-title-input"
|
|
1399
420
|
value={title}
|
|
1400
421
|
placeholder="任务标题"
|
|
1401
422
|
onTitle={onTitle}
|
|
1402
|
-
|
|
1403
|
-
const ta = taRef.current;
|
|
1404
|
-
if (!ta) return;
|
|
1405
|
-
ta.focus();
|
|
1406
|
-
ta.setSelectionRange(0, 0);
|
|
1407
|
-
}}
|
|
423
|
+
onEnter={ed.focusBodyStart}
|
|
1408
424
|
/>
|
|
1409
|
-
{findOpen &&
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
total={matches.length}
|
|
1415
|
-
inputRef={findInputRef}
|
|
1416
|
-
onNext={() => jumpToMatch(findIdx + 1)}
|
|
1417
|
-
onPrev={() => jumpToMatch(findIdx - 1)}
|
|
1418
|
-
onClose={() => {
|
|
1419
|
-
setFindOpen(false);
|
|
1420
|
-
taRef.current?.focus();
|
|
1421
|
-
}}
|
|
1422
|
-
/>
|
|
1423
|
-
)}
|
|
1424
|
-
<div className={`mdw-editor-row${showLines ? ' has-gutter' : ''}${showRuler ? ' has-ruler' : ''}`}>
|
|
1425
|
-
{showLines && (
|
|
1426
|
-
<div className="mdw-gutter" ref={gutterRef} aria-hidden>
|
|
1427
|
-
{lineNums.map((n) => (
|
|
425
|
+
{ed.findOpen && <EditorFindBar {...ed.findBarProps} />}
|
|
426
|
+
<div className={`mdw-editor-row${ed.showLines ? ' has-gutter' : ''}${ed.showRuler ? ' has-ruler' : ''}`}>
|
|
427
|
+
{ed.showLines && (
|
|
428
|
+
<div className="mdw-gutter" ref={ed.gutterRef} aria-hidden>
|
|
429
|
+
{ed.lineNums.map((n) => (
|
|
1428
430
|
<div key={n} className="mdw-gutter-ln">{n}</div>
|
|
1429
431
|
))}
|
|
1430
432
|
</div>
|
|
1431
433
|
)}
|
|
1432
434
|
<div className="mdw-editor-stage">
|
|
1433
|
-
{showRuler && <div className="mdw-ruler" aria-hidden title="约 80 字符参考" />}
|
|
435
|
+
{ed.showRuler && <div className="mdw-ruler" aria-hidden title="约 80 字符参考" />}
|
|
1434
436
|
<textarea
|
|
1435
|
-
ref={taRef}
|
|
1436
|
-
className={`mdw-editor${wrap ? ' is-wrap' : ' is-nowrap'}`}
|
|
437
|
+
ref={ed.taRef}
|
|
438
|
+
className={`mdw-editor${ed.wrap ? ' is-wrap' : ' is-nowrap'}`}
|
|
1437
439
|
value={body}
|
|
1438
440
|
spellCheck={false}
|
|
1439
|
-
wrap={wrap ? 'soft' : 'off'}
|
|
441
|
+
wrap={ed.wrap ? 'soft' : 'off'}
|
|
1440
442
|
aria-label="正文源码"
|
|
1441
443
|
placeholder="用 Markdown 写任务——行首 / 出命令 · Ctrl/⌘F 查找 · Ctrl/⌘B 粗体"
|
|
1442
|
-
onChange={
|
|
1443
|
-
onBody(e.target.value, 'typing');
|
|
1444
|
-
syncCaret();
|
|
1445
|
-
cancelAnimationFrame(slashRafRef.current);
|
|
1446
|
-
slashRafRef.current = requestAnimationFrame(() => {
|
|
1447
|
-
if (taRef.current) refreshSlash(taRef.current);
|
|
1448
|
-
});
|
|
1449
|
-
}}
|
|
444
|
+
onChange={ed.onChange}
|
|
1450
445
|
onKeyDown={onKeyDown}
|
|
1451
|
-
onKeyUp={syncCaret}
|
|
1452
|
-
onClick={
|
|
1453
|
-
onSelect={syncCaret}
|
|
1454
|
-
onScroll={syncGutter}
|
|
1455
|
-
onPaste={
|
|
1456
|
-
const files = filesFromDataTransfer(e.clipboardData);
|
|
1457
|
-
if (!files.length) return;
|
|
1458
|
-
e.preventDefault();
|
|
1459
|
-
ingestImages(files);
|
|
1460
|
-
}}
|
|
446
|
+
onKeyUp={ed.syncCaret}
|
|
447
|
+
onClick={ed.onClick}
|
|
448
|
+
onSelect={ed.syncCaret}
|
|
449
|
+
onScroll={ed.syncGutter}
|
|
450
|
+
onPaste={ed.onPaste}
|
|
1461
451
|
onDrop={(e) => {
|
|
1462
452
|
// 拖文件进来 = 与粘贴同一通道(实时预览态已有拖放,源码态补齐对等能力)
|
|
1463
453
|
const files = filesFromDataTransfer(e.dataTransfer);
|
|
@@ -1465,225 +455,17 @@ function SourceEditor({ title, body, onTitle, onBody, editorApiRef, onFiles, onT
|
|
|
1465
455
|
e.preventDefault();
|
|
1466
456
|
ingestImages(files);
|
|
1467
457
|
}}
|
|
1468
|
-
onMouseUp={
|
|
1469
|
-
syncCaret();
|
|
1470
|
-
const ta = taRef.current;
|
|
1471
|
-
if (!ta || ta.selectionStart === ta.selectionEnd) {
|
|
1472
|
-
onTextSel?.(null);
|
|
1473
|
-
return;
|
|
1474
|
-
}
|
|
1475
|
-
const text = ta.value.slice(ta.selectionStart, ta.selectionEnd);
|
|
1476
|
-
if (!text) return;
|
|
1477
|
-
onTextSel?.({
|
|
1478
|
-
text,
|
|
1479
|
-
x: e.clientX - 80,
|
|
1480
|
-
y: e.clientY - 44,
|
|
1481
|
-
});
|
|
1482
|
-
}}
|
|
458
|
+
onMouseUp={ed.onMouseUp}
|
|
1483
459
|
/>
|
|
1484
460
|
</div>
|
|
1485
461
|
</div>
|
|
1486
|
-
<
|
|
1487
|
-
|
|
1488
|
-
<
|
|
1489
|
-
<span>{stats.words} 词</span>
|
|
1490
|
-
<span>{stats.chars} 字</span>
|
|
1491
|
-
<button
|
|
1492
|
-
type="button"
|
|
1493
|
-
className={`mdw-wrap-toggle${showLines ? ' on' : ''}`}
|
|
1494
|
-
title="显示/隐藏行号"
|
|
1495
|
-
onClick={() => setShowLines((v) => !v)}
|
|
1496
|
-
>
|
|
1497
|
-
行号
|
|
1498
|
-
</button>
|
|
1499
|
-
<button
|
|
1500
|
-
type="button"
|
|
1501
|
-
className={`mdw-wrap-toggle${showRuler ? ' on' : ''}`}
|
|
1502
|
-
title="80 字符参考线"
|
|
1503
|
-
onClick={() => setShowRuler((v) => !v)}
|
|
1504
|
-
>
|
|
1505
|
-
参考线
|
|
1506
|
-
</button>
|
|
1507
|
-
<button
|
|
1508
|
-
type="button"
|
|
1509
|
-
className={`mdw-wrap-toggle${findOpen ? ' on' : ''}`}
|
|
1510
|
-
title="查找 Ctrl/⌘F"
|
|
1511
|
-
onClick={() => (findOpen ? setFindOpen(false) : openFind())}
|
|
1512
|
-
>
|
|
1513
|
-
查找
|
|
1514
|
-
</button>
|
|
1515
|
-
<button
|
|
1516
|
-
type="button"
|
|
1517
|
-
className={`mdw-wrap-toggle${wrap ? ' on' : ''}`}
|
|
1518
|
-
title={wrap ? '自动换行开(点击关闭)' : '自动换行关(点击开启)'}
|
|
1519
|
-
onClick={() => setWrap((v) => !v)}
|
|
1520
|
-
>
|
|
1521
|
-
{wrap ? '换行' : '不换行'}
|
|
1522
|
-
</button>
|
|
1523
|
-
</div>
|
|
1524
|
-
{slash && createPortal(
|
|
1525
|
-
<SlashMenu
|
|
1526
|
-
items={slash.items}
|
|
1527
|
-
active={slash.active}
|
|
1528
|
-
onHover={(n) => setSlashBoth((s) => s && ({ ...s, active: n }))}
|
|
1529
|
-
onPick={pickSlash}
|
|
1530
|
-
style={{ left: slash.x, top: slash.y }}
|
|
1531
|
-
/>,
|
|
462
|
+
<StatusBar {...ed.statusProps} />
|
|
463
|
+
{ed.slash && createPortal(
|
|
464
|
+
<SlashMenu {...ed.slashMenuProps} />,
|
|
1532
465
|
document.body
|
|
1533
466
|
)}
|
|
1534
467
|
</div>
|
|
1535
468
|
);
|
|
1536
469
|
}
|
|
1537
470
|
|
|
1538
|
-
|
|
1539
|
-
export const MdPanes = forwardRef(function MdPanes(
|
|
1540
|
-
{
|
|
1541
|
-
mode,
|
|
1542
|
-
title,
|
|
1543
|
-
body,
|
|
1544
|
-
previewHtml,
|
|
1545
|
-
onTitle,
|
|
1546
|
-
onBody,
|
|
1547
|
-
attachments = [],
|
|
1548
|
-
docKey = 'draft',
|
|
1549
|
-
editorApiRef = null,
|
|
1550
|
-
onFiles = null,
|
|
1551
|
-
onTextSel = null,
|
|
1552
|
-
onSave = null,
|
|
1553
|
-
onUndo = null,
|
|
1554
|
-
onRedo = null,
|
|
1555
|
-
onCloseSelMenu = null,
|
|
1556
|
-
hideTitle = false,
|
|
1557
|
-
attachHost = null,
|
|
1558
|
-
},
|
|
1559
|
-
articleRef
|
|
1560
|
-
) {
|
|
1561
|
-
// 文档级快捷键集中接管(window 捕获阶段):
|
|
1562
|
-
// 受控 textarea 的原生 undo 栈会被程序化改写冲掉,且原生撤销与我们的
|
|
1563
|
-
// 历史栈会互相打架——统一在这里拦截,编辑器区的按键行为才稳定可预期。
|
|
1564
|
-
useEffect(() => {
|
|
1565
|
-
const onKey = (e) => {
|
|
1566
|
-
if (!(e.metaKey || e.ctrlKey) || e.altKey) return;
|
|
1567
|
-
const t = e.target;
|
|
1568
|
-
if (!t || typeof t.closest !== 'function') return;
|
|
1569
|
-
const k = e.key.toLowerCase();
|
|
1570
|
-
const inEditorText = t.closest('.mdw-editor,.mdw-lp-src,.mdw-lp-title,.mdw-title-input');
|
|
1571
|
-
if (k === 'f' && mode !== 'edit') {
|
|
1572
|
-
// 实时预览/阅读态的查找(源码态由编辑器自己接管);只要焦点在工作台内即可
|
|
1573
|
-
if (!t.closest('.mdw')) return;
|
|
1574
|
-
e.preventDefault();
|
|
1575
|
-
editorApiRef.current?.openFind?.();
|
|
1576
|
-
return;
|
|
1577
|
-
}
|
|
1578
|
-
if (!inEditorText) {
|
|
1579
|
-
// 预览态划选后按格式键:切编辑态并套格式(Notion/Typora 行为)
|
|
1580
|
-
if (mode === 'split' && FMT_KEYS[k]) {
|
|
1581
|
-
if (editorApiRef.current?.formatSelection?.(FMT_KEYS[k])) e.preventDefault();
|
|
1582
|
-
}
|
|
1583
|
-
return;
|
|
1584
|
-
}
|
|
1585
|
-
if (k === 's') {
|
|
1586
|
-
e.preventDefault();
|
|
1587
|
-
onSave?.();
|
|
1588
|
-
return;
|
|
1589
|
-
}
|
|
1590
|
-
if (k === 'z') {
|
|
1591
|
-
// 只在页面接了自定义历史时拦截(#156)——没接的页面(如外部只读视图)
|
|
1592
|
-
// 回落浏览器原生撤销,别把原生 textarea 撤销也一起 preventDefault 掉
|
|
1593
|
-
const h = e.shiftKey ? onRedo : onUndo;
|
|
1594
|
-
if (!h) return;
|
|
1595
|
-
e.preventDefault();
|
|
1596
|
-
h();
|
|
1597
|
-
return;
|
|
1598
|
-
}
|
|
1599
|
-
if (k === 'y') {
|
|
1600
|
-
if (!onRedo) return;
|
|
1601
|
-
e.preventDefault();
|
|
1602
|
-
onRedo();
|
|
1603
|
-
}
|
|
1604
|
-
};
|
|
1605
|
-
window.addEventListener('keydown', onKey, true);
|
|
1606
|
-
return () => window.removeEventListener('keydown', onKey, true);
|
|
1607
|
-
}, [onSave, onUndo, onRedo, mode, editorApiRef]);
|
|
1608
|
-
|
|
1609
|
-
// 源码态:偶尔切过去改标记(入口在「更多」菜单里,不再占页头)
|
|
1610
|
-
if (mode === 'edit') {
|
|
1611
|
-
return (
|
|
1612
|
-
<div className="mdw-split mode-edit">
|
|
1613
|
-
<SourceEditor
|
|
1614
|
-
title={title}
|
|
1615
|
-
body={body}
|
|
1616
|
-
onTitle={onTitle}
|
|
1617
|
-
onBody={onBody}
|
|
1618
|
-
editorApiRef={editorApiRef}
|
|
1619
|
-
onFiles={onFiles}
|
|
1620
|
-
onTextSel={onTextSel}
|
|
1621
|
-
/>
|
|
1622
|
-
</div>
|
|
1623
|
-
);
|
|
1624
|
-
}
|
|
1625
|
-
|
|
1626
|
-
// 只读文档(外部绑定):没有模式可切,直接渲染阅读视图。
|
|
1627
|
-
// 「阅读」不再是用户可选模式(#47 撤掉),只作为不可写文档的兜底呈现。
|
|
1628
|
-
if (!onBody && !onTitle) {
|
|
1629
|
-
return (
|
|
1630
|
-
<div className="mdw-split mode-read">
|
|
1631
|
-
<div className="mdw-pane mdw-preview">
|
|
1632
|
-
<div className="mdw-reader">
|
|
1633
|
-
{/* 阅读态此前不渲染标题,与 live/源码两态断裂;题头与正文同轴同宽。
|
|
1634
|
-
正文自带 `# 标题` 时不再画外部题头——文档的 body 本来就含首行标题,
|
|
1635
|
-
两个都画就是同一行字出现两次(阅读态看起来像渲染 bug)。 */}
|
|
1636
|
-
{title && !bodyHasH1(body) ? (
|
|
1637
|
-
<h1 className="mdw-reader-title">{title}</h1>
|
|
1638
|
-
) : null}
|
|
1639
|
-
<article ref={articleRef} dangerouslySetInnerHTML={{ __html: previewHtml }} />
|
|
1640
|
-
</div>
|
|
1641
|
-
</div>
|
|
1642
|
-
</div>
|
|
1643
|
-
);
|
|
1644
|
-
}
|
|
1645
|
-
|
|
1646
|
-
return (
|
|
1647
|
-
<div className="mdw-split mode-split live">
|
|
1648
|
-
<LivePreview
|
|
1649
|
-
title={title}
|
|
1650
|
-
body={body}
|
|
1651
|
-
onTitle={onTitle}
|
|
1652
|
-
onBody={onBody}
|
|
1653
|
-
articleRef={articleRef}
|
|
1654
|
-
attachments={attachments}
|
|
1655
|
-
docKey={docKey}
|
|
1656
|
-
onFiles={onFiles}
|
|
1657
|
-
editorApiRef={editorApiRef}
|
|
1658
|
-
onTextSel={onTextSel}
|
|
1659
|
-
onCloseSelMenu={onCloseSelMenu}
|
|
1660
|
-
hideTitle={hideTitle}
|
|
1661
|
-
attachHost={attachHost}
|
|
1662
|
-
/>
|
|
1663
|
-
</div>
|
|
1664
|
-
);
|
|
1665
|
-
});
|
|
1666
|
-
|
|
1667
|
-
const MODES = [
|
|
1668
|
-
{ id: 'split', label: '实时预览' },
|
|
1669
|
-
{ id: 'edit', label: '源码' },
|
|
1670
|
-
];
|
|
1671
|
-
|
|
1672
|
-
export function ModeSeg({ mode, onChange }) {
|
|
1673
|
-
return (
|
|
1674
|
-
<div className="seg" role="tablist" aria-label="编辑模式">
|
|
1675
|
-
{MODES.map((m) => (
|
|
1676
|
-
<button
|
|
1677
|
-
key={m.id}
|
|
1678
|
-
type="button"
|
|
1679
|
-
role="tab"
|
|
1680
|
-
aria-selected={mode === m.id}
|
|
1681
|
-
className={mode === m.id ? 'on' : ''}
|
|
1682
|
-
onClick={() => onChange(m.id)}
|
|
1683
|
-
>
|
|
1684
|
-
{m.label}
|
|
1685
|
-
</button>
|
|
1686
|
-
))}
|
|
1687
|
-
</div>
|
|
1688
|
-
);
|
|
1689
|
-
}
|
|
471
|
+
export { LivePreview, SourceEditor };
|