像素画板的撤销重做:相同操作为什么不该清空 redo
今天阅读华为云鸿蒙开发实践中心的“像素画板”案例时,我重点看了它对修改历史的定义:一段笔画算一次修改;吸管取色和重复同色填充不占用历史;真正的新修改才清空重做记录。把这几个规则落到代码中,比单纯准备两个数组更重要。
用一次用户操作定义历史边界
在 32×32 画布上滑动画笔,一次手势可能触发很多 Move 事件。如果每个 Move 都保存快照,用户点击撤销就只退回一小段,而且很快耗尽历史容量。合理的边界是 Down 时记录起点,Move 持续更新临时画布,Up 或 Cancel 时按产品约定提交这一段操作。填充则可以把一次区域替换作为一次提交。
下面用 2×2 数字矩阵模拟画布,只研究快照与历史,不涉及触摸、Canvas 或设备权限。数字代表像素颜色,0 表示空白。实际应用可以把矩阵换成完整的像素数据。
三个状态,各有一个职责
current 是当前画面,past 保存可撤销的旧画面,future 保存撤销后可以重做的画面。提交真正的新修改时,把 current 放进 past,再设置新画面,清空 future。撤销和重做则在两个栈之间移动快照。
这里有个容易漏掉的细节:如果用户撤销了一次,随后用当前颜色重复填充一个同色区域,画面没有变化。这种操作应当直接返回,保留 future。否则用户会发现,自己看起来什么都没改,重做按钮却失效了。
一个可以直接运行的最小实现
将下面内容保存为 pixel-history.cjs,用 node pixel-history.cjs 运行。它把历史上限缩到 3,方便验证淘汰规则。
const assert = require('node:assert/strict');
const clone = value => value.map(row => row.slice());
class History {
constructor(initial, limit = 3) {
this.current = clone(initial);
this.past = [];
this.future = [];
this.limit = limit;
}
commit(next) {
if (JSON.stringify(next) === JSON.stringify(this.current)) return false;
this.past.push(clone(this.current));
if (this.past.length > this.limit) this.past.shift();
this.current = clone(next);
this.future = [];
return true;
}
undo() {
if (!this.past.length) return false;
this.future.push(clone(this.current));
this.current = this.past.pop();
return true;
}
redo() {
if (!this.future.length) return false;
this.past.push(clone(this.current));
if (this.past.length > this.limit) this.past.shift();
this.current = this.future.pop();
return true;
}
}
const grid = n => [[n, 0], [0, 0]];
const h = new History(grid(0));
assert.equal(h.undo(), false);
assert.equal(h.redo(), false);
h.commit(grid(1)); h.commit(grid(2)); h.undo();
assert.deepEqual(h.current, grid(1));
assert.equal(h.commit(grid(1)), false);
assert.equal(h.future.length, 1);
h.redo(); assert.deepEqual(h.current, grid(2));
console.log('PASS 1: undo and redo restore snapshots');
console.log('PASS 2: unchanged commit preserves redo');
h.undo(); h.commit(grid(3));
assert.equal(h.redo(), false);
console.log('PASS 3: new change clears redo');
const input = grid(4); h.commit(input); input[0][0] = 999;
assert.equal(h.current[0][0], 4);
h.current[1][1] = 8; h.undo();
assert.deepEqual(h.current, grid(3));
console.log('PASS 4: caller and history snapshots are independent');
for (let i = 5; i <= 9; i++) h.commit(grid(i));
assert.equal(h.past.length, 3);
assert.equal(h.undo(), true); assert.equal(h.undo(), true);
assert.equal(h.undo(), true); assert.equal(h.undo(), false);
assert.equal(h.current[0][0], 6);
console.log('PASS 5: at most three changes can be undone');
console.log('ALL FIVE GROUPS PASSED');
检查点比按钮能点击更具体
这个演示把五组规则写成断言:撤销重做能恢复画面、相同提交保留重做、新修改清空重做、快照之间相互独立、最多撤销三次。执行成功时会打印 ALL FIVE GROUPS PASSED。其中深复制很重要:二维数组只复制外层,内部行仍可能共享引用,后续修改就会污染过去的画面。
演示为了直观直接公开了 current;产品代码应封装修改入口,通过返回副本或只读接口限制外部直接改动。32×32 的小画布可以先使用完整快照;画布变大以后,再根据内存和操作频率考虑差量记录。不同颜色表达和序列化结构下,也应重新选择判等方法。
实际接入 ArkUI 时,还需要处理手势结束与取消、界面刷新、打开已保存作品后的历史基线,以及“清空”和“新建”各自是否保留作品 ID。这些属于产品行为,应该先约定再写测试。本示例的五组断言验证的是历史模型,设备端绘画和保存流程需要另外运行检查。
参考案例:华为云鸿蒙开发实践中心《像素画板》,仓库 harmony-practice-center/HarmonyOS-AtomSer-pixel-paint(MIT)。本文的 2×2 JavaScript 模型用于说明历史边界,是单独编写的教学演示。
- 点赞
- 收藏
- 关注作者
评论(0)