【发布时间】:2018-09-09 06:48:58
【问题描述】:
我正在考虑在 Angular 应用中实现撤消、重做功能的方法。
第一个也是最基本的想法是使用一个完全描述应用程序内部的模型,我们称之为AppModel。在每个值得记录的更改中,您只需创建一个 AppModel 的新对象并将其推送到堆栈并更新 currentIndex。
在绝对最坏的情况下,AppModel 的对象必须填写 500 个文本字段,平均长度为 20 个字符。每次更新 10000 个字符或 10kB。
这个数字有多糟糕?我不认为这会导致内存问题,但是每次推送到堆栈时都会使应用程序冻结吗?这是一个基本的实现:
historyStack: AppModel[];
currentIndex:number = -1;
push(newAppModel:AppModel){
//delete all after current index
this.historyStack.splice(++this.currentIndex, 0, newAppModel);
}
forward(){
if(this.currentIndex < this.historyStack.length-1){
this.currentIndex++;
return this.historyStack[this.currentIndex];
}
return this.historyStack[this.currentIndex];
}
back(){
return this.historyStack[this.currentIndex--];
}
我能想到的另一个选项是存储执行redo 和reverse 操作的函数调用。这种方法还需要我存储需要调用函数的对象。这些对象可能会被用户删除,因此还必须有一种方法来重新创建这些对象。当我打字时,这变得越来越痛苦:)
你推荐什么方式?
【问题讨论】:
-
我遇到过这种情况,我更喜欢 Mozilla 的 ExecCommands developer.mozilla.org/en-US/docs/Web/API/Document/execCommand