如何实施撤消
这最初是一个问题,但在提问的过程中我找到了答案。然而,由于它没有很好的记录(无论如何我都能找到),我在这里为遇到同样问题的其他人发布信息。
我将 Autodesk Forge Viewer 与 Edit2D 工具结合使用。我看到在默认上下文中它有一个撤消堆栈。我可以订阅并在撤消堆栈发生如下变化时收到通知:
// @ts-ignore
e.defaultContext.undoStack.addEventListener(Autodesk.Edit2D.UndoStack.BEFORE_ACTION, s => {
console.log(s);
});
// @ts-ignore
e.defaultContext.undoStack.addEventListener(Autodesk.Edit2D.UndoStack.AFTER_ACTION, s => {
console.log(s);
});
但是,我想告诉查看者只需撤消最后一个操作,但我找不到任何有关如何执行此操作的文档。如何实施撤消?是否有文档文件或页面显示有关撤消堆栈系统的更多数据?
This started out as a question but in the process of asking it I figured out the answer. However since it's not well documented (that I could find anyway) I'm posting here for anyone else that comes along with the same issue.
I'm using the Autodesk Forge Viewer with the Edit2D tools. I see that in the default context it has an undo stack. I can subscribe and get a notification when the undo stack changes like this:
// @ts-ignore
e.defaultContext.undoStack.addEventListener(Autodesk.Edit2D.UndoStack.BEFORE_ACTION, s => {
console.log(s);
});
// @ts-ignore
e.defaultContext.undoStack.addEventListener(Autodesk.Edit2D.UndoStack.AFTER_ACTION, s => {
console.log(s);
});
However I want to tell the viewer to simply undo the last action and I can't find any documentation on how to do that. How do I implement an undo? If there is a documentation file or page somewhere that shows more data about the undo stack system?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
进一步查看事件,传递给撤消堆栈操作的参数有一个“action”属性以及与其关联的一些数据。这似乎是已完成操作的定义,并且可以传递回撤消堆栈以进行撤消。为了做到这一点,这对我有用。
首先如上所述订阅撤消堆栈事件(我使用了 after 操作,但两者都可能有效)。
另请注意,您必须使用打字稿忽略,因为上下文不会显示为 Autodesk.Viewing.Extension 的属性,我不确定为什么,但它确实在运行时显示。然后,如图所示,我将最后一个操作存储在变量中,以便我可以使用它。
接下来处理命令。当按下撤消命令时,将最后一个命令传递到撤消方法中:
请注意,此方法是一个有根据的猜测,因为我根本没有找到任何有关它的文档。我在此页面。这显示了这段代码的一部分:
我猜测如果有一个 run 方法,也许还有一个 undo 方法,所以我只是更改了名称,它就起作用了。
编辑
找到了一个稍微简单的方法。实际上,您可以避免存储最后一个操作,而直接从堆栈中引用它:
Looking into the events further, the argument passed to the undo stack action has an 'action' property with some data associated to it. This seems to be the definition of what was done and can be passed back into the undo stack to be undone. In order to do this, here is what worked for me.
First subscribe to the undo stack event as above (I used the after action but either would probably work).
Note also that you have to use typescript ignore since the context doesn't show up as a property of Autodesk.Viewing.Extension, I'm not sure why but it does show up at runtime. I then as shown store the last action in a variable so that I have it available.
Next handling the command. When the undo command is pressed, passed the last command into the undo method:
Note that this method was an educated guess as I found no documentation on it at all. I found some items on undo on this page. That shows part way down this code:
I made a guess that if there is a
run
method maybe there is anundo
method as well so I just changed the name and it worked.EDIT
Found a slightly simpler method. You can actually avoid storing the last action and just reference it directly from the stack:
撤消堆栈有一个简单的
undo
方法(不带任何参数),该方法将执行最后一个操作、恢复它并将其从堆栈中弹出。所以你应该可以打电话The undo stack has a simple
undo
method (without any parameters) that will take the last action, revert it, and pop it from the stack. So you should just be able to call