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

3.4 KiB

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 回调收到的 statenull :::

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 中 oldContentnull
    • 删除触发的 step 中 newContentnull
    • 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 中 oldSchemanull
    • 删除触发的 step 中 newSchemanull
    • undo / redo 返回 null(边界状态)时不会触发该事件 :::

mark-saved

  • 详情: 调用 markSaved / markPageSaved / markCodeBlockSaved / markDataSourceSaved 标记「已保存」记录时触发

  • 事件回调函数: (payload: { kind: 'all' | 'page' | 'code-block' | 'data-source'; id?: Id }) => void

    ::: tip

    • markSaved 触发时 kindall,无 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} :::