v 1.0.1
parent
550797c35a
commit
897ee8de9f
|
|
@ -351,100 +351,11 @@
|
||||||
justify-content: flex-end;
|
justify-content: flex-end;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 保存按钮:自动保存开关融合在按钮内部,整体呈现为一个主色按钮
|
/* 保存分体按钮的标签:图标与文字对齐 */
|
||||||
左半边「保存」执行保存,右半边「自动保存」开关切换自动保存 */
|
.save-button-label {
|
||||||
.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 {
|
|
||||||
display: inline-flex;
|
display: inline-flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 6px;
|
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 中) */
|
/* 编辑器工具栏里的视图模式图标(文案放在 Tooltip 中) */
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ import {
|
||||||
Segmented,
|
Segmented,
|
||||||
Select,
|
Select,
|
||||||
Space,
|
Space,
|
||||||
Switch,
|
Dropdown,
|
||||||
Tabs,
|
Tabs,
|
||||||
Tooltip,
|
Tooltip,
|
||||||
TreeSelect,
|
TreeSelect,
|
||||||
|
|
@ -50,7 +50,8 @@ import {
|
||||||
EyeOutlined,
|
EyeOutlined,
|
||||||
FullscreenOutlined,
|
FullscreenOutlined,
|
||||||
FullscreenExitOutlined,
|
FullscreenExitOutlined,
|
||||||
LoadingOutlined,
|
CheckOutlined,
|
||||||
|
DownOutlined,
|
||||||
} from '@ant-design/icons'
|
} from '@ant-design/icons'
|
||||||
import { createPortal } from 'react-dom'
|
import { createPortal } from 'react-dom'
|
||||||
import { Editor } from '@bytemd/react'
|
import { Editor } from '@bytemd/react'
|
||||||
|
|
@ -61,6 +62,7 @@ import highlight from '@bytemd/plugin-highlight'
|
||||||
import breaks from '@bytemd/plugin-breaks'
|
import breaks from '@bytemd/plugin-breaks'
|
||||||
import frontmatter from '@bytemd/plugin-frontmatter'
|
import frontmatter from '@bytemd/plugin-frontmatter'
|
||||||
import gemoji from '@bytemd/plugin-gemoji'
|
import gemoji from '@bytemd/plugin-gemoji'
|
||||||
|
import rehypeSlug from 'rehype-slug'
|
||||||
import 'bytemd/dist/index.css'
|
import 'bytemd/dist/index.css'
|
||||||
import bytemdZhHans from 'bytemd/locales/zh_Hans.json'
|
import bytemdZhHans from 'bytemd/locales/zh_Hans.json'
|
||||||
import 'highlight.js/styles/github.css'
|
import 'highlight.js/styles/github.css'
|
||||||
|
|
@ -157,6 +159,57 @@ function getCmFromPane(root: Element | null | undefined): CodeMirrorEditor | nul
|
||||||
return host?.CodeMirror || null
|
return host?.CodeMirror || null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 每个编辑器实例的动画序号:用于取消上一次未完成的滚动动画 */
|
||||||
|
const editorScrollTokens = new WeakMap<CodeMirrorEditor, number>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 把指定行平滑滚动到编辑区顶部,观感对齐预览区的 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() {
|
function DocumentEditor() {
|
||||||
const { projectId: projectIdParam } = useParams<{ projectId: string }>()
|
const { projectId: projectIdParam } = useParams<{ projectId: string }>()
|
||||||
// 路由参数是字符串,文件接口一律使用数值型项目 ID
|
// 路由参数是字符串,文件接口一律使用数值型项目 ID
|
||||||
|
|
@ -360,7 +413,7 @@ function DocumentEditor() {
|
||||||
cm.focus()
|
cm.focus()
|
||||||
// 让目标标题落在编辑区顶部,和预览区跳转的阅读体验保持一致
|
// 让目标标题落在编辑区顶部,和预览区跳转的阅读体验保持一致
|
||||||
// CodeMirror 的第二个参数是「额外留白像素」,原来的 'start' 参与算术会得到 NaN
|
// CodeMirror 的第二个参数是「额外留白像素」,原来的 'start' 参与算术会得到 NaN
|
||||||
cm.scrollIntoView({ line: Math.max(0, heading.line - 1), ch: 0 }, 0)
|
scrollEditorLineToTop(cm, heading.line)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -1481,6 +1534,9 @@ function DocumentEditor() {
|
||||||
breaks(),
|
breaks(),
|
||||||
frontmatter(),
|
frontmatter(),
|
||||||
gemoji(),
|
gemoji(),
|
||||||
|
// 预览区标题加 id 锚点:大纲跳转与页内 # 链接依赖它,
|
||||||
|
// slug 规则与 utils/markdownToc(github-slugger)一致
|
||||||
|
{ rehype: (processor) => processor.use(rehypeSlug) },
|
||||||
uploadImagesPlugin,
|
uploadImagesPlugin,
|
||||||
internalLinkPlugin,
|
internalLinkPlugin,
|
||||||
editorRefPlugin,
|
editorRefPlugin,
|
||||||
|
|
@ -1709,45 +1765,32 @@ function DocumentEditor() {
|
||||||
未保存
|
未保存
|
||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
{/* 保存按钮:自动保存开关融合在按钮内部,左半边点击保存,右半边开关自动保存 */}
|
{/* 保存:弱化为普通按钮,主色让给内容本身;
|
||||||
<div className="save-control">
|
自动保存是低频设置,收进分体按钮的下拉菜单,勾选项反映开关状态 */}
|
||||||
<Tooltip title={autoSave ? '立即保存(Ctrl/Cmd + S)' : '保存(Ctrl/Cmd + S)'}>
|
<Dropdown.Button
|
||||||
<button
|
icon={<DownOutlined aria-hidden style={{ fontSize: 10 }} />}
|
||||||
type="button"
|
loading={saving}
|
||||||
className="save-control-main"
|
disabled={!selectedFile}
|
||||||
onClick={() => handleSaveFile()}
|
title="保存(Ctrl/Cmd + S)"
|
||||||
disabled={!selectedFile || saving}
|
onClick={() => handleSaveFile()}
|
||||||
>
|
menu={{
|
||||||
{saving ? <LoadingOutlined spin aria-hidden /> : <SaveOutlined aria-hidden />}
|
items: [
|
||||||
<span>{saving ? '保存中' : '保存'}</span>
|
{
|
||||||
</button>
|
key: 'autosave',
|
||||||
</Tooltip>
|
label: '自动保存(每 30 秒)',
|
||||||
<span className="save-control-sep" aria-hidden="true" />
|
icon: <CheckOutlined aria-hidden style={{ opacity: autoSave ? 1 : 0 }} />,
|
||||||
<Tooltip title={autoSave ? '关闭自动保存,改为手动保存' : '开启自动保存(每 30 秒自动保存)'}>
|
},
|
||||||
<span
|
],
|
||||||
className="save-control-auto"
|
onClick: ({ key }) => {
|
||||||
role="switch"
|
if (key === 'autosave') setAutoSave((value) => !value)
|
||||||
aria-checked={autoSave}
|
},
|
||||||
aria-label="自动保存"
|
}}
|
||||||
tabIndex={0}
|
>
|
||||||
onClick={() => setAutoSave((value) => !value)}
|
<span className="save-button-label">
|
||||||
onKeyDown={(event) => {
|
{!saving && <SaveOutlined aria-hidden />}
|
||||||
if (event.key === 'Enter' || event.key === ' ') {
|
{saving ? '保存中' : '保存'}
|
||||||
event.preventDefault()
|
</span>
|
||||||
setAutoSave((value) => !value)
|
</Dropdown.Button>
|
||||||
}
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Switch
|
|
||||||
checked={autoSave}
|
|
||||||
checkedChildren="自动"
|
|
||||||
unCheckedChildren="手动"
|
|
||||||
tabIndex={-1}
|
|
||||||
aria-hidden="true"
|
|
||||||
/>
|
|
||||||
</span>
|
|
||||||
</Tooltip>
|
|
||||||
</div>
|
|
||||||
<Button
|
<Button
|
||||||
icon={<UndoOutlined aria-hidden />}
|
icon={<UndoOutlined aria-hidden />}
|
||||||
onClick={handleReset}
|
onClick={handleReset}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue