osx - Custom Cut, Copy & Paste operations for WKWebView -
i developing cocoa application wrap javascript web application have developed provides editing of svg graphics. have following question, motivated desire customize cut, copy , paste operations , associated menu items.
how can either:
- substitute own responder in place of wkwebview or
- customize responses
validatemenuitem:,cut:,copy:,paste:selectors?
i particularly interested in solutions retain useful wkwebview functionality of providing cut, copy , paste text fields.
any appreciated. detail follows.
the purpose of cocoa wrapper threefold:
- to provide persistence
- to provide clipboard integration
- to provide printing
i've achieved 1, blocked on 2 , 3; question 2.
specifically, cannot framework call implementation of validatemenuitem: provide — result, have no control on when cut, copy or paste menu items available user select.
i have tried following:
- mark nsviewcontroller subclass accepting first responder , override
validatemenyitem:there — implementation never called - subclass wkwebview , override
validatemenuitem:— implementation never called - subclass nswindowcontroller , override
validatemenuitem:— implementation never called
now, wrapped web app has text fields. when give 1 of focus , when appropriate (text on clipboard; highlight text), cut, copy , paste options in edit menu automagically activate. work. (i functionality, , retain it.) therefore suspect happening wkwebview, current first responder, intercepting call validatemenuitem: along responder chain. need able activate menu items according other states in wrapped web app — for example, when user selects svg graphic.
update
i have done additional digging, in trying figure out solution problem. in wkwebview subclass, added custom @ibaction. created menu item on story board , wired menu item firstresponder, choosing new action.
and validatemenuitem: on wkwebview subclass called. only new selector, not copy: selector associated @ibaction had added wkwebview subclass.
now, copy: method, marked @ibaction , having method signature things interface builder (or whatever it's called these days) able use, not marked override. when tried mark override, got error. apparently, wkwebview not provide copy:id method — though provides copy functionality , handles menu item correctly, when text in text field selected.
so appears that, somehow, wkwebview automatically (and always) handles validation selectors typically bound menus — specifically, cut:, copy: , paste:. moreover, subclassing wkwebview , overriding validatemenuitem: insufficient interrupt/take control on default behavior. seems odd.
but reading other posts on wkwebview on stackoverflow, aware wkwebview quite complex piece of functionality. of particular interest/concern me wkwebview runs web content in separate process. therefore wonder if related separate process responsible menu validation , handling cut:, copy: , paste: — internal framework , don't have access.
i'm still hoping solution. above update prove useful.
oddly enough, looking @ porting on vector design tool mac via wkwebview , encountered same problem. looks can control cut/copy/paste menu items inside javascript. if add beforecopy , beforecut event handlers , preventdefault() them, cut , copy menu items enabled. can't seem enable paste menu item though. think may due long-running bug in webkit. can still ⌘-v on keyboard, , paste still happen because in case, webkit skip calling beforepaste , trigger paste event directly. can't figure out enabling paste menu item. workaround, i've created "fake" paste menu option doesn't go normal paste: selector own selector wkwebview won't intercept.
Comments
Post a Comment