【问题标题】:Detect iOS 13 undo & redo检测 iOS 13 撤消和重做
【发布时间】:2020-01-08 17:19:41
【问题描述】:

我有一个自定义实现撤消和重做的 WKWebView。我希望能够知道何时触发系统撤消/重做(通过手势或通过点击 iPadOS 中的键盘助手按钮),以便我可以使用我的自定义实现。

是否有公共 API 可以做到这一点?

【问题讨论】:

    标签: ios ios13 ipados


    【解决方案1】:

    有多种方法。在 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

    【讨论】:

      猜你喜欢
      • 2020-06-06
      • 2023-03-02
      • 2020-02-21
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      相关资源
      最近更新 更多