实施撤消
我正在创建一个地图编辑 web 应用程序,我们可以在其中创建和编辑折线、多边形等。我在网上查找有关撤消实现的信息时遇到了一些麻烦,我发现抱怨“我们需要撤消”和“这是我的命令模式使用闭包”,但我认为在它和完整的撤消/重做界面之间还有相当长的路要走。
所以,这是我的问题(我认为是 wiki 的良好候选者):
- 我应该管理堆栈,还是有办法将我的命令发送到浏览器的堆栈? (以及如何处理本机命令,例如本例中文本字段中的文本编辑)
- 当某些命令是浏览器本机命令时,如何处理“命令压缩”(命令分组)
- 如何检测撤消(ctrl+z) 击键?
- 如果我注册了一个 keyup 事件,我如何决定是否阻止默认?
- 如果没有,我可以在某处注册一些撤消事件处理程序吗?
- 用户不习惯在网络上撤消,我如何“训练”他们在我的应用程序上探索/撤消?
I'm creating a map editing webapp where we can create and edit polylines, polygons etc. I've some trouble finding informations on undo implementation on the web, I find whining about "we need undo" and "here is my Command pattern using closures" but I think between that and a full undo/redo interface there is quite some road.
So, here are my questions (good candidate for wiki I think):
- Should I manage the stack, or is there a way to send my commands to the browser's stack ? (and how do I handle native commands, like text edits in textifields in this case)
- how do I handle "command compression" (command grouping) when some commands are browser native
- How do I detect the undo (ctrl+z) keystroke?
- If I register a keyup event, how do I decide if I prevent default or not?
- If not, can I register some undoevent handler somewhere ?
- Users are not used to undo on the web, how can I "train" them to explore/undo on my application ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要具有用于创建和删除对象的函数。然后将这些函数传递给撤消管理器。请参阅我的 javascript 撤消管理器的演示文件: https://github.com/ArthurClemens/Javascript-Undo -Manager
演示代码显示画布,但代码是不可知的。
它不包含按键绑定,但可能会帮助您完成第一步。
我自己在一个网络应用程序中使用了它,该应用程序带有撤消和重做按钮,然后是保存按钮。
You need to have functions for object creation and deletion. Then pass those functions to the undo manager. See the demo file of my javascript undo manager: https://github.com/ArthurClemens/Javascript-Undo-Manager
The demo code shows canvas, but the code is agnostic.
It doesn't contain key bindings, but may help you with the first steps.
Myself I have used this in a web application with buttons for undo and redo, next to save.
以下是使用 Knockout JS 进行 N 级撤消的示例:
它使用 MVVM 模型,因此您的页面状态以 JavaScript 对象表示,并为其维护历史记录。
Here is a sample of N-Level undo using Knockout JS:
It uses an MVVM model so your page state is represented in a javascript object that it maintains a history for.
Cappuccino 的自动撤消支持的工作方式是告诉撤消管理器哪些属性应该是可撤消的。例如,假设您正在管理学生记录,您可能会执行以下操作:
现在,无论学生姓名在 UI 中如何更改,点击“撤消”都会自动将其恢复回来。 Cappuccino 还会自动处理同一运行循环中的合并更改,当撤消堆栈上有项目等时,将文档标记为“脏”(需要保存)(换句话说,以上应该是您需要做的所有支持)撤消)。
再举一个例子,如果您想要对学生进行不可撤销的添加和删除操作,您需要执行以下操作:
由于“students”是 theClass 中学生的数组,因此将跟踪该数组中的添加和删除操作。
The way Cappuccino's automatic undo support works is by telling the undo manager what properties should be undoable. For example, pretend you are managing records of students, you might do something like:
Now regardless of how the students name is changed in the UI, hitting undo will automatically revert it back. Cappuccino also automatically handles coalescing changes in the same run loop, marking the document as "dirty" (needing save) when there are items on the undo stack, etc etc (in other words, the above should be ALL you need to do to support undo).
As another example, if you wanted to make additions and deletions of students undoable, you'd do the following:
Since "students" is an array of students in theClass, then additions and deletions from this array will be tracked.