简体   繁体   English

用于在浏览器中撤消的事件侦听器

[英]Event listener for undo in browsers

To give context, my team is building a rich text editor in the browser that needs to persist state to a server between sessions.为了提供上下文,我的团队正在浏览中构建一个富文本编辑器,它需要在会话之间将状态持久化到服务器。 The editor's state can obviously change from updates to the document that include keydown events, but we should also account for updates to the editor's stage that are triggered by undo/redo events .编辑器的状态显然可以从更新到包含 keydown 事件的文档发生变化,但我们还应该考虑由 undo/redo 事件触发的编辑器阶段的更新

Unfortunately the browser has no native undo/redo events ( ref ).不幸的是,浏览器没有本机撤消/重做事件ref )。 One proposed hack floating around seems to be to stop propagation for keydown events that maps to undo's keyboard shortcut in addition to disabling the context menu.一个提议的hack似乎是停止传播除了禁用上下文菜单之外映射到撤消的键盘快捷键的 keydown 事件。 However , this still leaves open the ability for the user to navigate directly to Edit -> Undo in the application's menu bar , which will directly trigger a document.execCommand('undo') and doesn't fire an event.但是,这仍然为用户提供了直接导航到应用程序菜单栏中的Edit -> Undo的能力,这将直接触发document.execCommand('undo')并且不会触发事件。 As a result we wouldn't know to send an update to the server.因此,我们不知道向服务器发送更新。

Here's a W3C thread on this issue.这是有关此问题的 W3C主题 As of the writing of this, it appears that a solution is still in the works...在撰写本文时,似乎解决方案仍在制定中......

When Quill , another browser text editor, ran into this issue their team seemed to advise : "disable the native undo/stack", which is what Facebook's Draft.js actually does .当另一个浏览器文本编辑器Quill遇到这个问题时,他们的团队似乎建议:“禁用本机撤消/堆栈”,这正是 Facebook 的Draft.js实际所做的 Given that seems to be what we're working with, does anyone know of a way to disable/substitute the browser's native undo/redo stack ?鉴于这似乎是我们正在使用的,有没有人知道禁用/替换浏览器的本机撤消/重做堆栈的方法 Obviously, this is an aggressive solution, but, as of the writing of this, that seems to be the only option.显然,这是一个激进的解决方案,但是,在撰写本文时,这似乎是唯一的选择。

In the meantime, the answer to this question probably buried somewhere in the Draft's code.与此同时,这个问题的答案可能埋藏在草案代码的某个地方。 If no one beats my team to it, I'll report back on what Draft seems to be doing.如果没有人击败我的团队,我会报告草案似乎在做什么。 Thought it was at least worth documenting this issue.认为至少值得记录这个问题。

Likely the best solution is to listen for a debounced onChange event from the contentEditable element.可能最好的解决方案是监听来自contentEditable元素的去抖动onChange事件。 This doesn't allow for custom behavior regarding undo, but it should solve the main issue.这不允许关于撤消的自定义行为,但它应该解决主要问题。 Also adding a beforeunload event to alert the user to potential unsaved changes should handle additional edge cases.还添加一个beforeunload事件来提醒用户潜在的未保存更改应该处理额外的边缘情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM