构建 Logo 解释器:实现撤销、重做、本地存储与焦点控制
1. 撤销和重做功能的实现
在开发过程中,撤销(Undo)和重做(Redo)功能是提升用户体验的重要部分。下面详细介绍如何实现这两个功能。
1.1 撤销功能测试与实现
- canRedo 测试 :最后一个测试是关于
canRedo的,测试在撤销操作后canRedo是否被设置为true。
it('sets canRedo to true after undoing', () => {
const updated = reducer(newState, undoAction);
expect(updated.canRedo).toBeTruthy();
});
- 实现撤销逻辑 :通过返回包含
lastEntry和新的canRedo属性的新对象来使测试通过。
case 'UNDO':
const lastEntry = past[past.length - 1];
past = past.slice(0, -1);
return {
...lastEntry,
canRedo: t
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



