Appearance
操作历史 History
基于官方 @tiptap/extension-history 的本地包装:保留官方的 undo / redo 命令、depth / newGroupDelay 配置与键盘快捷键,并新增 clearHistory 命令(prosemirror-history 未暴露 clear API,以当前 doc/selection 重建 EditorState,全部插件状态重置,含 history)。
扩展名取 history(官方为 undoRedo):官方名与 Commands 声明键冲突,改名避免合并冲突。
用法
ts
import { History } from '@qingpai/core'
const editor = new Editor({
extensions: [History],
})History 是 StartKit 成员(history: false 可关闭),一般用 StartKit 时已默认注册。
Settings
HistoryOptions 与官方一致(自 @tiptap/extension-history 重新导出):
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
depth | number | 100 | 保留的历史条目数 |
newGroupDelay | number | 500 | 在此间隔(毫秒)内的连续输入合并为一个撤销组 |
命令
undo
撤销上一步操作。
ts
editor.chain().focus().undo().run()redo
重做被撤销的操作。
ts
editor.chain().focus().redo().run()clearHistory
清空撤销/重做历史。以当前 doc/selection 重建 EditorState:doc 引用不变,EditorView 不重绘、nodeView 保持;重建后经 beforeTransaction 事件同步封装层的缓存 state(如 @tiptap/vue-3 的 debounced reactiveState),避免 undo 读到旧历史。典型场景:文件导入整份替换内容后清理历史,防止撤销回替换前的文档。
ts
editor.commands.clearHistory()键盘快捷键
| 快捷键 | 命令 |
|---|---|
Mod-Z | undo |
Shift-Mod-Z | redo |
Mod-Y | redo |
Source code
packages/core/src/extensions/history/