docs(tests): explain size history traversal and recovery capture semantics

This commit is contained in:
2026-11-12 17:57:51 +08:00
parent 93f14af667
commit 05a41813f3
2 changed files with 7 additions and 3 deletions
+3 -1
View File
@@ -22,6 +22,7 @@ function setup(options = {}) {
return { editor, entry, propose() { const { document, history } = editor.getSnapshot(); return planCreationSize(document, history, registry, entry, target, { width: 260, height: 130 }); } };
}
/** 从plan当前状态逆序撤销到空,再按undo顺序重做,对照最终图片;只适用于不含额外初始元素的调用。 */
// 直接应用历史 changes/invert 来检查候选前后值连续性;不走 editor.history,也不模拟其恢复对象时的 incarnation 分配。
function traverse(plan) {
let document = plan.document;
for (const id of [...plan.history.undo].reverse()) document = applyChanges(document, invert(plan.history.entries.get(id).changes));
@@ -82,7 +83,7 @@ test('an undone creation cannot be enriched', () => {
// 删除再恢复使生命周期变化,即使ID相同,旧创建尺寸任务也不能修改新对象。
test('a restored element has a new incarnation and rejects the old asynchronous task', () => {
const { editor, propose } = setup(); editor.dispatch({ type: 'element.remove', target }); editor.history.undo();
// Undo of removal creates a new incarnation: an old asynchronous task is stale.
// 删除撤销会恢复为新 incarnation,原 target 仍绑定第一代,因此旧尺寸任务必须失效。
assert.throws(propose, { code: 'creation_size_target_changed' });
});
// 用已有historyTransition格式描述plan,JSON往返后正反回放仍精确相等,entries不暴露set。
@@ -138,6 +139,7 @@ test('a future resize followed by movement retains the user size through both re
editor.dispatch({ type: 'element.geometry', target, patch: { height: 300 } });
editor.dispatch({ type: 'element.geometry', target, patch: { x: 75 } });
editor.history.undo(); editor.history.undo();
// redo 栈顶在尾端,故正向重做倒序取项;回卷则按原数组顺序应用逆变化。
const plan = propose(); let document = plan.document;
for (const id of [...plan.history.redo].reverse()) document = applyChanges(document, plan.history.entries.get(id).changes);
assert.equal(document.elements.get('image').geometry.height, 300);