如何设计撤消&在文本编辑器中重做? [英] How to design undo & redo in text editor?

查看:147
本文介绍了如何设计撤消&在文本编辑器中重做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目的一部分是写文本编辑器,用于键入某些规则,我的应用程序编译并运行它。编写编译器结束并发布测试版。在最终版本中,我们必须在文本编辑器中添加撤消和重做。我使用文件并定期保存文本编辑器。如何设计撤消和重做到我的文本编辑器?文件持久性结构的变化是什么?

Part of my project is write text editor that used for type some rule and my application compile and run it. Writing compiler was end and release beta version. In final version we must add undo and redo in text editor.I use file and save it periodically for text editor. How to design undo and redo to my text editor? What is change in structure of persistent of file?

推荐答案

您可以将操作建模为命令,保存在两个堆栈中。一个用于撤销,另一个用于重做。您可以撰写您的命令以创建更多高级命令,例如当您要撤消例如,宏的行为;或者,如果您想在一个操作中对单个单词或短语的单个击键进行分组。

You can model your actions as commands, that you keep in two stacks. One for undo, another for redo. You can compose your commands to create more high-level commands, like when you want to undo the actions of a macro, for example; or if you want to group individual keystrokes of a single word, or phrase, in one action.

编辑器中的每个操作(或重做操作)都会生成新的撤消操作进入撤消堆栈的命令(并清除重做堆栈)。每个撤消操作都会生成相应的重做命令,该命令将进入重做堆栈。

Each action in your editor (or a redo action) generates a new undo command that goes into the undo stack (and also clears the redo stack). Each undo action generates the corresponding redo command that goes into the redo stack.

您还可以按照 derekerdmann ,将undo和redo命令组合成一种命令,知道如何撤消和重做其动作。

You can also, as mentioned in the comments by derekerdmann, combine both undo and redo commands into one type of command, that knows how to undo and redo its action.

这篇关于如何设计撤消&在文本编辑器中重做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆