我有一个带有撤销和重做自定义实现的 WKWebView。我希望能够知道何时触发系统撤消/重做(通过手势或通过点击 iPadOS 中的键盘助手按钮),以便我可以使用我的自定义实现。
是否有公共 API 可以做到这一点?
有多种方法。在 iOS 原生端,没有可用的公共 API 来接管完全控制 AFAIK,但您可以收听这样的 UNDO 通知以了解它们:
[NSNotificationCenter.defaultCenter addObserverForName:NSUndoManagerWillUndoChangeNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"Undo Notification: %@", note);
}];
然后,您将看到负责该操作的 NSUndoManager 位于WKContentView
. 要到达那里,只有调酒有助于解决已知的风险......
但是还有另一种方法可以在基于 WebKit 的浏览器视图中工作(按原样WKWebView
),那就是监听beforeinput
事件。例如,对于一个contenteditable
元素,您可以添加以下侦听器:
editor.addEventListener('beforeinput', event => {
if (event.inputType === 'historyUndo') {
console.log('undo')
event.preventDefault()
}
if (event.inputType === 'historyRedo') {
console.log('redo')
event.preventDefault()
}
console.log('some other inputType', event.inputType)
})
这个想法来自这个讨论:https ://discuss.prosemirror.net/t/native-undo-history/1823/3?u=holtwick
这是一个要测试的 JSFiddle:https ://jsfiddle.net/perenzo/bhztrgw3/4/
另见相应的 TipTap 插件:https ://github.com/scrumpy/tiptap/issues/468