From 897ee8de9f67878ea59ae3bca1273b2dffb5feaf Mon Sep 17 00:00:00 2001 From: "mula.liu" Date: Wed, 11 Nov 2026 19:07:22 +0800 Subject: [PATCH] v 1.0.1 --- .../src/pages/Document/DocumentEditor.css | 93 +------------ .../src/pages/Document/DocumentEditor.tsx | 127 ++++++++++++------ 2 files changed, 87 insertions(+), 133 deletions(-) diff --git a/frontend/src/pages/Document/DocumentEditor.css b/frontend/src/pages/Document/DocumentEditor.css index 1b6994b..28e15b3 100644 --- a/frontend/src/pages/Document/DocumentEditor.css +++ b/frontend/src/pages/Document/DocumentEditor.css @@ -351,100 +351,11 @@ justify-content: flex-end; } -/* 保存按钮:自动保存开关融合在按钮内部,整体呈现为一个主色按钮 - 左半边「保存」执行保存,右半边「自动保存」开关切换自动保存 */ -.save-control { - display: inline-flex; - align-items: stretch; - height: 32px; - border-radius: var(--radius-sm); - background: var(--color-primary); - color: var(--text-color-inverse); - overflow: hidden; - transition: background var(--transition-fast); -} - -.save-control:hover { - background: var(--color-primary-hover); -} - -.save-control-main { +/* 保存分体按钮的标签:图标与文字对齐 */ +.save-button-label { display: inline-flex; align-items: center; gap: 6px; - padding: 0 12px; - border: 0; - background: transparent; - color: inherit; - font-size: 14px; - line-height: 1; - cursor: pointer; - transition: background var(--transition-fast); -} - -.save-control-main:hover:not(:disabled) { - background: rgba(255, 255, 255, 0.14); -} - -.save-control-main:disabled { - cursor: not-allowed; - opacity: 0.6; -} - -.save-control-sep { - align-self: center; - width: 1px; - height: 16px; - background: rgba(255, 255, 255, 0.35); -} - -.save-control-auto { - display: inline-flex; - align-items: center; - padding: 0 10px; - cursor: pointer; - user-select: none; - transition: background var(--transition-fast); -} - -.save-control-auto:hover { - background: rgba(255, 255, 255, 0.14); -} - -/* 开关本身不单独响应点击,由外层整块统一处理,避免重复触发 */ -.save-control-auto .ant-switch { - pointer-events: none; -} - -/* 主色按钮上的开关重新配色:开关内直接显示「自动 / 手动」,开 / 关都清晰可辨 */ -.save-control-auto .ant-switch { - background: rgba(0, 0, 0, 0.28); - /* antd 默认宽度放不下两个汉字,会折行/裁切,这里给足文本空间 */ - min-width: 58px; -} - -.save-control-auto .ant-switch .ant-switch-handle::before { - background: #fff; -} - -.save-control-auto .ant-switch.ant-switch-checked { - background: #fff; -} - -/* antd 把颜色写在子节点上,必须直接覆盖 checked / unchecked 文本,否则白字压白底看不见 */ -.save-control-auto .ant-switch .ant-switch-inner-checked, -.save-control-auto .ant-switch .ant-switch-inner-unchecked { - color: #fff; - font-size: 12px; - white-space: nowrap; -} - -.save-control-auto .ant-switch.ant-switch-checked .ant-switch-inner-checked { - color: var(--color-primary); -} - -.save-control-auto .ant-switch.ant-switch-checked .ant-switch-handle::before { - background: var(--color-primary); } /* 编辑器工具栏里的视图模式图标(文案放在 Tooltip 中) */ diff --git a/frontend/src/pages/Document/DocumentEditor.tsx b/frontend/src/pages/Document/DocumentEditor.tsx index e9090a1..f94403c 100644 --- a/frontend/src/pages/Document/DocumentEditor.tsx +++ b/frontend/src/pages/Document/DocumentEditor.tsx @@ -20,7 +20,7 @@ import { Segmented, Select, Space, - Switch, + Dropdown, Tabs, Tooltip, TreeSelect, @@ -50,7 +50,8 @@ import { EyeOutlined, FullscreenOutlined, FullscreenExitOutlined, - LoadingOutlined, + CheckOutlined, + DownOutlined, } from '@ant-design/icons' import { createPortal } from 'react-dom' import { Editor } from '@bytemd/react' @@ -61,6 +62,7 @@ import highlight from '@bytemd/plugin-highlight' import breaks from '@bytemd/plugin-breaks' import frontmatter from '@bytemd/plugin-frontmatter' import gemoji from '@bytemd/plugin-gemoji' +import rehypeSlug from 'rehype-slug' import 'bytemd/dist/index.css' import bytemdZhHans from 'bytemd/locales/zh_Hans.json' import 'highlight.js/styles/github.css' @@ -157,6 +159,57 @@ function getCmFromPane(root: Element | null | undefined): CodeMirrorEditor | nul return host?.CodeMirror || null } +/** 每个编辑器实例的动画序号:用于取消上一次未完成的滚动动画 */ +const editorScrollTokens = new WeakMap() + +/** + * 把指定行平滑滚动到编辑区顶部,观感对齐预览区的 scrollIntoView(smooth)。 + * + * 坑:CodeMirror 对尚未渲染的行使用估算高度,直接按估算值动画会滚错位置 + * (实测长文档里差约一个视口高度,目标停在视口底部)。 + * CodeMirror 的渲染是同步的,因此可以先瞬移到目标处强制渲染真实行高、 + * 测出真实位置,再在同一帧内滚回原位开始动画——浏览器一帧只绘制一次, + * 中间过程不会被看见。动画途中沿途行继续渲染、位置可能漂移, + * 所以每帧重新测量目标位置再插值,收尾时做一次精确对齐。 + */ +function scrollEditorLineToTop(cm: CodeMirrorEditor, line: number) { + const target = Math.max(0, line - 1) + const start = cm.getScrollInfo().top + + // 连续点击索引时取消上一次动画,避免两个 rAF 循环互相拉扯 + const token = (editorScrollTokens.get(cm) ?? 0) + 1 + editorScrollTokens.set(cm, token) + + // 瞬移 → 同步渲染 → 用真实行高测出目标位置 → 同帧回滚到起点 + cm.scrollIntoView({ line: target, ch: 0 }, 0) + const goal = cm.heightAtLine(target, 'local') + cm.scrollTo(null, start) + + if (Math.abs(goal - start) < 8) { + cm.scrollTo(null, goal) + return + } + + const easeInOutCubic = (t: number) => (t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2) + const { clientHeight } = cm.getScrollInfo() + const duration = Math.min(700, 220 + (Math.abs(goal - start) / Math.max(1, clientHeight)) * 140) + const startTime = performance.now() + const step = (now: number) => { + if (editorScrollTokens.get(cm) !== token) { + return + } + const progress = Math.min(1, (now - startTime) / duration) + const liveGoal = cm.heightAtLine(target, 'local') + cm.scrollTo(null, start + (liveGoal - start) * easeInOutCubic(progress)) + if (progress < 1) { + window.requestAnimationFrame(step) + } else if (Math.abs(cm.getScrollInfo().top - liveGoal) > 2) { + cm.scrollTo(null, liveGoal) + } + } + window.requestAnimationFrame(step) +} + function DocumentEditor() { const { projectId: projectIdParam } = useParams<{ projectId: string }>() // 路由参数是字符串,文件接口一律使用数值型项目 ID @@ -360,7 +413,7 @@ function DocumentEditor() { cm.focus() // 让目标标题落在编辑区顶部,和预览区跳转的阅读体验保持一致 // CodeMirror 的第二个参数是「额外留白像素」,原来的 'start' 参与算术会得到 NaN - cm.scrollIntoView({ line: Math.max(0, heading.line - 1), ch: 0 }, 0) + scrollEditorLineToTop(cm, heading.line) } return } @@ -1481,6 +1534,9 @@ function DocumentEditor() { breaks(), frontmatter(), gemoji(), + // 预览区标题加 id 锚点:大纲跳转与页内 # 链接依赖它, + // slug 规则与 utils/markdownToc(github-slugger)一致 + { rehype: (processor) => processor.use(rehypeSlug) }, uploadImagesPlugin, internalLinkPlugin, editorRefPlugin, @@ -1709,45 +1765,32 @@ function DocumentEditor() { 未保存 )} - {/* 保存按钮:自动保存开关融合在按钮内部,左半边点击保存,右半边开关自动保存 */} -
- - - -
+ {/* 保存:弱化为普通按钮,主色让给内容本身; + 自动保存是低频设置,收进分体按钮的下拉菜单,勾选项反映开关状态 */} + } + loading={saving} + disabled={!selectedFile} + title="保存(Ctrl/Cmd + S)" + onClick={() => handleSaveFile()} + menu={{ + items: [ + { + key: 'autosave', + label: '自动保存(每 30 秒)', + icon: , + }, + ], + onClick: ({ key }) => { + if (key === 'autosave') setAutoSave((value) => !value) + }, + }} + > + + {!saving && } + {saving ? '保存中' : '保存'} + +