我有一个WKWebView,它具有撤消和重做的自定义实现。我想知道何时触发系统撤消/重做(通过手势或通过点击iPadOS中的键盘助手按钮),以便我可以使用自定义实现。
是否有公共API可以做到这一点?
答案 0 :(得分:0)
有多种方法。在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