《游戏编辑器技术原理与架构》10.3 跨文档/跨会话撤销

按文档独立撤销和重做,同时保留跨文档操作顺序,说明会话元数据的恢复范围。

10.3 跨文档/跨会话撤销

10.1/10.2 的撤销栈是单文档的。但多标签编辑器(Unity 多场景、helio 多关卡)里,撤销的真实语义是「编辑器的撤销」:命令在多个文档间交错发生,用户按 Cmd+Z 期望撤销「上一次任何地方的操作」,而不是「当前文档的撤销」——否则用户刚在场景 A 做了改动、切到场景 B 按撤销,却撤了场景 B 里更早的东西,是反直觉的。

每条命令记录所属文档和是否已应用。撤销从尾部查找该文档最后一条已应用命令,重做从头查找该文档第一条待重做命令。新命令成功后清理该文档的重做分支。

本章与站内既有内容的分工

主题站内既有内容本节做什么
单文档栈10.1 命令栈与事务单栈;本节升级为跨文档统一历史
异步命令10.2 异步撤销与内存异步栈管单文档;本节管全局导航
会话持久化11.1 项目与会话状态本节序列化历史元数据;下章管完整项目状态

每条命令记录应用状态

操作 A 后操作 B,再撤销 A。此时 A 可以重做,B 仍然生效并可撤销。如果再编辑 A,只清理 A 的待重做项。这套规则适用于相互独立的文档操作。一次修改多个文档的命令需要单独定义事务边界。

snapshotMeta() 输出文档 ID 和操作名称,可用于展示历史。恢复可执行历史还需保存正向与逆向数据、文档版本和应用状态。

对标跨文档撤销与会话记录

Godot 为已编辑场景分别维护历史,也为项目设置或外部资源等操作保留全局历史。公开的 Unity Undo API 提供撤销组及当前历史操作,本文以这些接口为依据讨论分组。 Godot EditorUndoRedoManager · Unity Undo

helio 保留跨文档记录的插入顺序,并按文档独立选择可撤销或重做的条目。

可运行产物:7 条用例

下面 src/cross-doc-undo.ts 层级 L1,完整可编译,命令 node --test test/10-3-cross-document-session-undo.test.ts:

// file: src/cross-doc-undo.ts
// tier: L1
// verify: node --test test/10-3-cross-document-session-undo.test.ts
//
// 撤销系统深化(第十章):跨文档/跨会话撤销。
// 多标签编辑器里撤销不是「当前文档的撤销」,而是「编辑器的撤销」——命令
// 跨文档交错发生,用户按 Cmd+Z 期望撤销「上一次任何地方的操作」。
// 按文档撤销与重做,保留跨文档记录顺序。

export interface SessionCommand {
  docId: string;
  name: string;
  do(): void;
  undo(): void;
}

export class GlobalUndoHistory {
  #entries: { command: SessionCommand; applied: boolean }[] = [];

  push(cmd: SessionCommand): void {
    cmd.do();
    this.#entries = this.#entries.filter((entry) =>
      entry.applied || entry.command.docId !== cmd.docId);
    this.#entries.push({ command: cmd, applied: true });
  }

  undoFor(docId: string): boolean {
    const entry = this.#entries.findLast((item) =>
      item.applied && item.command.docId === docId);
    if (!entry) return false;
    entry.command.undo();
    entry.applied = false;
    return true;
  }

  redoFor(docId: string): boolean {
    const entry = this.#entries.find((item) =>
      !item.applied && item.command.docId === docId);
    if (!entry) return false;
    entry.command.do();
    entry.applied = true;
    return true;
  }

  snapshotMeta(): { docId: string; name: string }[] {
    return this.#entries.map(({ command }) => ({ docId: command.docId, name: command.name }));
  }
}

只有命令成功返回后才更新应用状态。

可运行产物:7 条用例

下面是 test/10-3-cross-document-session-undo.test.ts 层级 L1,命令 node --test test/10-3-cross-document-session-undo.test.ts:

// file: test/10-3-cross-document-session-undo.test.ts
// tier: L1
// verify: node --test test/10-3-cross-document-session-undo.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { GlobalUndoHistory } from '../src/cross-doc-undo.ts';
import type { SessionCommand } from '../src/cross-doc-undo.ts';

function makeCmd(docId: string, name: string, state: Map<string, number[]>): SessionCommand {
  return {
    docId,
    name,
    do: () => { state.get(docId)![0] += 1; },
    undo: () => { state.get(docId)![0] -= 1; },
  };
}

function setup() {
  const state = new Map<string, number[]>([
    ['a', [0]],
    ['b', [0]],
  ]);
  const history = new GlobalUndoHistory();
  return { state, history };
}

test('跨文档命令交错,undoFor 只撤销指定文档的最新', () => {
  const { state, history } = setup();
  history.push(makeCmd('a', 'inc', state)); // A=1
  history.push(makeCmd('a', 'inc', state)); // A=2
  history.push(makeCmd('b', 'inc', state)); // B=1
  assert.equal(history.undoFor('a'), true);
  assert.equal(state.get('a')![0], 1); // 撤销 a 最新
  assert.equal(state.get('b')![0], 1); // b 不受影响
  assert.equal(history.undoFor('a'), true);
  assert.equal(state.get('a')![0], 0);
});

test('撤销 A 后 B 仍可撤销,新增 A 保留 B 的重做分支', () => {
  const { state, history } = setup();
  history.push(makeCmd('a', 'first-a', state));
  history.push(makeCmd('b', 'first-b', state));
  assert.equal(history.undoFor('a'), true);
  assert.equal(history.redoFor('b'), false);
  assert.equal(history.undoFor('b'), true);
  history.push(makeCmd('a', 'second-a', state));
  assert.equal(history.redoFor('a'), false);
  assert.equal(history.redoFor('b'), true);
  assert.equal(state.get('b')![0], 1);
});

test('undoFor 找不到该文档命令返回 false', () => {
  const { state, history } = setup();
  history.push(makeCmd('a', 'inc', state));
  assert.equal(history.undoFor('c'), false);
  assert.equal(history.undoFor('a'), true);
  assert.equal(history.undoFor('a'), false); // 已撤到底
});

test('redoFor 恢复被撤销的命令', () => {
  const { state, history } = setup();
  history.push(makeCmd('a', 'inc', state)); // A=1
  history.push(makeCmd('a', 'inc', state)); // A=2
  history.undoFor('a'); // A=1
  assert.equal(history.redoFor('a'), true);
  assert.equal(state.get('a')![0], 2);
});

test('新命令压栈截断重做分支', () => {
  const { state, history } = setup();
  history.push(makeCmd('a', 'inc', state));
  history.undoFor('a');            // A=0
  history.push(makeCmd('a', 'inc2', state));
  assert.equal(history.redoFor('a'), false); // 重做分支已无命令
  assert.equal(state.get('a')![0], 1);       // 只有新命令生效
});

test('snapshotMeta 序列化历史元数据', () => {
  const { state, history } = setup();
  history.push(makeCmd('a', 'inc', state));
  history.push(makeCmd('b', 'rename', state));
  assert.deepEqual(history.snapshotMeta(), [
    { docId: 'a', name: 'inc' },
    { docId: 'b', name: 'rename' },
  ]);
});

test('空历史 undoFor/redoFor 均返回 false', () => {
  const history = new GlobalUndoHistory();
  assert.equal(history.undoFor('a'), false);
  assert.equal(history.redoFor('a'), false);
});

本机实测(Node v26.5.0,node --test test/10-3-cross-document-session-undo.test.ts):tests 7、pass 7、fail 0。

交错操作测试覆盖了 B 保持生效、B 仍可撤销,以及编辑 A 后 B 仍可重做的路径。

小结

  • 按文档选取撤销与重做项,应用状态记录在各条历史上。
  • push 即执行并截断重做分支;undoFor 跳过其他文档命令、redoFor 对称恢复。
  • snapshotMeta 输出供展示使用的文档 ID 和命令名称。
  • 对标 Unity 的焦点文档栈与 Unreal 的全局事务:helio 取全局历史 + 按文档精确目标。
  • 第 10 章结束。下一章进入工作区:编辑会话与工作区——项目会话状态、窗口布局持久化、崩溃恢复自动保存。

阅读导航:上一节:10.2 异步撤销与内存 · 下一节:11.1 项目与会话状态 。

继续阅读

探索更多技术文章

浏览归档,发现更多关于系统设计、工具链和工程实践的内容。

全部文章 返回首页

「books」更多文章

  1. 《大规模分布式计算深入解析·向量数据库与 AI 检索内核》 · 目录与学习路径(45 篇)
  2. 《大规模分布式计算深入解析》15.3 自研向量引擎:可执行骨架与工程缺口
  3. 《大规模分布式计算深入解析》14.1 Milvus:组件架构、段管理与检索链路