tmagic-editor/docs/api/editor/historyServiceEvents.md
roymondchen 614f12adf3 feat(editor): 支持历史记录持久化
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-08 17:04:39 +08:00

109 lines
3.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# historyService事件
## page-change
- **详情:** 页面切换
- **事件回调函数:** `(undoRedo: UndoRedo) => void`
::: details 查看 UndoRedo 类定义
<<< @/../packages/editor/src/utils/undo-redo.ts#UndoRedo{ts}
:::
## change
- **详情** 历史记录发生变化
- **事件回调函数** `(state: StepValue | null) => void`
::: details 查看 StepValue 及关联类型定义
<<< @/../packages/editor/src/type.ts#StepValue{ts}
<<< @/../packages/editor/src/type.ts#HistoryOpType{ts}
<<< @/../packages/editor/src/type.ts#HistoryOpSource{ts}
<<< @/../packages/schema/src/index.ts#Id{ts}
<<< @/../packages/schema/src/index.ts#MNode{ts}
:::
:::tip
当游标处于历史栈边界已经无法继续撤销或重做`UndoRedo.undo()` / `redo()` 返回 `null`对应 `change` 回调收到的 `state` `null`
:::
## code-block-history-change
- **详情** 代码块历史记录发生变化`pushCodeBlock` / `undoCodeBlock` / `redoCodeBlock` 成功时触发
- **事件回调函数** `(codeBlockId: Id, step: CodeBlockStepValue) => void`
::: details 查看 CodeBlockStepValue 及关联类型定义
<<< @/../packages/editor/src/type.ts#CodeBlockStepValue{ts}
<<< @/../packages/editor/src/type.ts#HistoryOpSource{ts}
<<< @/../packages/schema/src/index.ts#CodeBlockContent{ts}
<<< @/../packages/schema/src/index.ts#Id{ts}
:::
:::tip
- 新增触发的 step `oldContent` `null`
- 删除触发的 step `newContent` `null`
- `undo` / `redo` 返回 `null`边界状态时不会触发该事件
:::
## data-source-history-change
- **详情** 数据源历史记录发生变化`pushDataSource` / `undoDataSource` / `redoDataSource` 成功时触发
- **事件回调函数** `(dataSourceId: Id, step: DataSourceStepValue) => void`
::: details 查看 DataSourceStepValue 及关联类型定义
<<< @/../packages/editor/src/type.ts#DataSourceStepValue{ts}
<<< @/../packages/editor/src/type.ts#HistoryOpSource{ts}
<<< @/../packages/schema/src/index.ts#Id{ts}
:::
:::tip
- 新增触发的 step `oldSchema` `null`
- 删除触发的 step `newSchema` `null`
- `undo` / `redo` 返回 `null`边界状态时不会触发该事件
:::
## mark-saved
- **详情** 调用 `markSaved` / `markPageSaved` / `markCodeBlockSaved` / `markDataSourceSaved` 标记已保存记录时触发
- **事件回调函数** `(payload: { kind: 'all' | 'page' | 'code-block' | 'data-source'; id?: Id }) => void`
::: tip
- `markSaved` 触发时 `kind` `all` `id`
- 细粒度方法触发时 `kind` 对应类别`id` 为目标页面 / 代码块 / 数据源 id
:::
## save-to-indexed-db
- **详情** `saveToIndexedDB` 把历史记录写入本地 IndexedDB 成功时触发
- **事件回调函数** `(snapshot: PersistedHistoryState) => void`
::: details 查看 PersistedHistoryState 类型定义
<<< @/../packages/editor/src/type.ts#PersistedHistoryState{ts}
<<< @/../packages/editor/src/utils/undo-redo.ts#SerializedUndoRedo{ts}
:::
## restore-from-indexed-db
- **详情** `restoreFromIndexedDB` 从本地 IndexedDB 读取并重建历史记录成功时触发找不到记录时不触发
- **事件回调函数** `(snapshot: PersistedHistoryState) => void`
::: details 查看 PersistedHistoryState 类型定义
<<< @/../packages/editor/src/type.ts#PersistedHistoryState{ts}
:::