自定义剪切、复制和复制WKWebView 的粘贴操作 [英] Custom Cut, Copy & Paste operations for WKWebView

查看:87
本文介绍了自定义剪切、复制和复制WKWebView 的粘贴操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Cocoa 应用程序来包装我开发的 JavaScript Web 应用程序,该应用程序提供 SVG 图形的编辑.我有以下问题,这是出于自定义剪切、复制和粘贴操作及其相关菜单项的愿望.

I am developing a Cocoa application to wrap a JavaScript web application I have developed that provides editing of SVG graphics. I have the following question, which is motivated by a desire to customize the Cut, Copy and Paste operations and their associated menu items.

我该怎么做:

  • 用我自己的响应者代替 WKWebView 或
  • 自定义对 validateMenuItem:cut:copy:paste: 选择器的响应?
  • substitute my own responder in place of WKWebView or
  • customize its responses to validateMenuItem: and the cut:, copy: and paste: selectors?

我对保留有用的 WKWebView 功能(为文本字段提供剪切、复制和粘贴)的解决方案特别感兴趣.

I am particularly interested in solutions that retain the useful WKWebView functionality of providing Cut, Copy and Paste for text fields.

任何帮助将不胜感激.详情如下.

Any help would be much appreciated. Detail follows.

我的 Cocoa 包装器的目的有三个:

The purpose of my Cocoa wrapper is threefold:

  1. 提供持久性
  2. 提供剪贴板集成
  3. 提供印刷

我已经达到了 1,但是在 2 和 3 上被阻止了;这个问题是关于 2.

I've achieved 1, but am blocked on 2 and 3; this question is about 2.

具体来说,我无法让框架调用我提供的 validateMenuItem: 的任何实现 — 因此,我无法控制剪切、复制或粘贴菜单项何时可用于用户选择.

Specifically, I cannot get the framework to call any implementation of validateMenuItem: that I provide — as a result, I have no control over when the Cut, Copy or Paste menu items are available for the user to select.

我尝试了以下方法:

  1. 将我的 NSViewController 子类标记为接受第一响应者并在那里覆盖 validateMenyItem: — 我的实现从未被调用
  2. 子类化 WKWebView 并覆盖 validateMenuItem: — 我的实现从未被调用
  3. 子类 NSWindowController 并覆盖 validateMenuItem: — 我的实现从未被调用
  1. mark my NSViewController subclass as accepting first responder and override validateMenyItem: there — my implementation was never called
  2. subclass WKWebView and override validateMenuItem: — my implementation was never called
  3. subclass NSWindowController and override validateMenuItem: — my implementation was never called

现在,我打包的 Web 应用程序有一些文本字段.当我给出其中一个焦点并且在合适的时候(文本在剪贴板上;我突出显示一些文本),我的编辑"菜单中的剪切"、复制"和粘贴"选项会自动激活.他们甚至工作.(我喜欢这个功能,并希望保留它.)因此,我怀疑正在发生的事情是 WKWebView,作为我当前的第一响应者,正在拦截任何对 validateMenuItem: 沿响应者链的调用.但我还需要能够根据我封装的网络应用程序中的其他状态激活菜单项 - 例如,当用户选择 SVG 图形时.

Now, my wrapped web app has some text fields. When I give one of those focus and when it is appropriate (text is on the clipboard; I highlight some text), the Cut, Copy and Paste options in my Edit menu automagically activate. They even work. (I like this functionality, and would like to retain it.) I therefore suspect that what is happening is that the WKWebView, as my current first responder, is intercepting any call to validateMenuItem: along the responder chain. But I also need to be able to activate menu items according to other states in my wrapped web app — for example, when the user selects an SVG graphic.

更新

我进行了额外的挖掘,试图找出我的问题的解决方案.在我的 WKWebView 子类中,我添加了一个自定义 @IBAction.我在我的故事板上为它创建了一个菜单项,并将菜单项连接到 FirstResponder,选择我的新操作.

I have done additional digging, in trying to figure out a solution to my problem. In my WKWebView subclass, I added a custom @IBAction. I created a menu item for it on my story board and wired the menu item to FirstResponder, choosing my new action.

并且我的 WKWebView 子类上的 validateMenuItem: 被调用.但是用于那个新选择器,而不用于与我之前添加到我的 WKWebView 子类中的 @IBAction 关联的 copy: 选择器.

And my validateMenuItem: on my WKWebView subclass was called. But only for that new selector, not for the copy: selector associated with the @IBAction that I had previously added to my WKWebView subclass.

现在,我的 copy: 方法,标有 @IBAction 并且具有 Interface Builder(或现在被称为的任何东西)能够使用的东西的方法签名,没有标记为 覆盖.当我尝试将其标记为 override 时,出现错误.显然,WKWebView 没有提供 copy:id 方法——即使它提供了复制功能并在选择文本字段中的文本时正确处理菜单项.

Now, my copy: method, marked with @IBAction and having the method signature for things that Interface Builder (or whatever it's called these days) is able to use, is not marked override. When I tried to mark it override, I got an error. Apparently, WKWebView does not provide a copy:id method — even though it provides Copy functionality and handles the menu item correctly, when text in a text field is selected.

因此,不知何故,WKWebView 会自动(并且总是)处理通常绑定到菜单的某些选择器的验证——特别是 cut:copy:粘贴:.此外,子类化 WKWebView 并覆盖 validateMenuItem: 不足以中断/控制此默认行为.这看起来很奇怪.

So it appears that, somehow, WKWebView automatically (and always) handles validation for certain selectors typically bound to menus — specifically, cut:, copy: and paste:. Moreover, subclassing WKWebView and overriding validateMenuItem: is insufficient to interrupt/take control over this default behavior. Which seems odd.

但是通过阅读 StackOverflow 上 WKWebView 上的其他帖子,我知道 WKWebView 实际上是一个相当复杂的功能.我特别感兴趣/担心的是 WKWebView 实际上在单独的进程中运行 Web 内容.因此,我想知道与那个单独的过程相关的东西是否负责菜单验证和处理 cut:copy:paste:——一些内部的东西我无权访问的框架.

But from reading other posts on WKWebView on StackOverflow, I am aware that WKWebView is actually quite a complex piece of functionality. Of particular interest/concern to me is that WKWebView actually runs the web content in a separate process. I therefore wonder if something related to that separate process is responsible for menu validation and handling cut:, copy: and paste: — something internal to the framework and to which I don't have access.

我仍然希望有一个解决方案.希望以上更新能证明有用.

I'm still hoping for a solution. Hopefully the above update will prove useful.

推荐答案

奇怪的是,我也在考虑通过 WKWebView 将矢量设计工具移植到 Mac 并遇到了同样的问题.看起来您实际上可以从 在 JavaScript 中.如果您添加 beforecopybeforecut 事件处理程序和 preventDefault() 它们,则将启用剪切和复制菜单项.我似乎无法启用粘贴菜单项.我认为这可能是由于 WebKit 中长期运行的错误.您仍然可以在键盘上按 ⌘-V,粘贴仍然会发生,因为在这种情况下,WebKit 将跳过调用 beforepaste 并直接触发 paste 事件.我只是想不出启用粘贴菜单项.作为一种解决方法,我创建了一个假"粘贴菜单选项,该选项不会转到普通的 paste: 选择器,而是转到我自己的 WKWebView 不会拦截的选择器.

Oddly enough, I was also looking at porting over a vector design tool to the Mac via WKWebView and encountered the same problem. It looks like you can actually control the cut/copy/paste menu items from inside JavaScript. If you add beforecopy and beforecut event handlers and preventDefault() them, then the cut and copy menu items will be enabled. I can't seem to enable the paste menu item though. I think that may be due to a long-running bug in WebKit. You can still ⌘-V on the keyboard, and the paste will still happen because in that case, WebKit will skip calling beforepaste and just trigger the paste event directly. I just can't figure out enabling the paste menu item. As a workaround, I've created a "fake" Paste menu option that doesn't go to the normal paste: selector but to my own selector that the WKWebView won't intercept.

这篇关于自定义剪切、复制和复制WKWebView 的粘贴操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆