Fork me on GitHub
#clojurescript
<
2022-03-10
>
Samuel McHugh16:03:16

I'm seeing some bizarre behavior with def regarding browser events in cljs. Try running a browser repl. (like the one at http://clojurescript.io) Run

(.addEventListener js/window
                   "paste"
                   (fn [event]
                     (def e event)
                     (js/console.log (.getData (.-clipboardData event) "text/plain"))))
Try pasting some text. Now you should have your last 'paste' event bound to the var e . And you will have seen your pasted text logged to the console. If you eval e in your repl, you do see a ClipboardEvent, however if you actually look into what is in that ClipboardEvent, it's essentially "blank". You can check this with
(.getData (.-clipboardData e) "text/plain")
Anyone have any idea what's going on here? Does it regard some subtle difference in how def works in cljs, or something about events?

1
lilactown16:03:14

the https://w3c.github.io/clipboard-apis/#clipboard-event-api has this to say about clipboard events > Access to the clipboard is performed using the standard DataTransfer methods to mutate the items on a ClipboardEvent's clipboardData attribute. One consequence of this is that these clipboard APIs can only access clipboard data in the context of a ClipboardEvent handler.

lilactown16:03:29

has nothing to do with def

lilactown16:03:54

it's just you're trying to access the data on a clipboard event outside of an event handler

🙌 1
Samuel McHugh08:03:15

thanks, good to know it's a consequence of the clipboard then. Thank you.