Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Programmatic" copy to clipboard don't work in Jupyter 5 #2975

Closed
Madhu94 opened this issue Oct 25, 2017 · 10 comments · Fixed by #3088
Closed

"Programmatic" copy to clipboard don't work in Jupyter 5 #2975

Madhu94 opened this issue Oct 25, 2017 · 10 comments · Fixed by #3088

Comments

@Madhu94
Copy link
Contributor

Madhu94 commented Oct 25, 2017

Because of #1286, any extension that relies on document.execCommand('copy') for copying to clipboard don't work, as the notebook captures all copy events triggered.

For instance, if a notebook extension used clipboard.js - an invisible textarea is created whose value is set to whatever is supposed to go on the clipboard. In that case window.getSelection().isCollapsed is set to True and the contents of the cell (not the value of the invisible textarea) is stored in the system clipboard.

In this case, Ctrl+C would never be pressed and it is surprising for the extension users to click a button on the toolbar or access a menu-item and find that the current cell's contents are copied over.

A workaround might be to disable Jupyter.keyboard_manager and re-enable it once the extension's work is done.

Do you think the notebook can somehow try to detect explicit requests to copy (when the user hits Ctrl+C) as opposed to capturing all copy events that are fired ?

Thanks!

@Madhu94
Copy link
Contributor Author

Madhu94 commented Oct 25, 2017

A solution (that I haven't really thought through) is to re-implement a little of what clipboard.js does, something like -

  1. Detect keypress of Ctrl+C or Cmd+C
  2. Create a temporary, invisible text area and set its value to the contents of the cells.
  3. Run document.execCommand to get access to the system clipboard.

( There is also the question of handling the plain text + json + html, also how it could affect #1321, etc)

@takluyver
Copy link
Member

My understanding is that we can only interact with the clipboard by capturing clipboard events, so capturing keyboard shortcuts like Ctrl+C won't work.

The MDN docs for document.execCommand say that it's only available in 'design mode', which I know ~nothing about.

@Madhu94
Copy link
Contributor Author

Madhu94 commented Nov 15, 2017

@takluyver I'll try to make a WIP PR ?

@takluyver
Copy link
Member

OK

@Madhu94
Copy link
Contributor Author

Madhu94 commented Nov 18, 2017

My understanding is that we can only interact with the clipboard by capturing clipboard events, so capturing keyboard shortcuts like Ctrl+C won't work.

It is possible to fire off a copy event by creating a temporary invisible textarea, set its value to what you want to copy and focussing it. Then, when the user hits Ctrl+C, what is in the invisible, focussed textarea will be copied to the clipboard. Or running document.execCommand('copy') will fire a copy event. This is what libraries like clipboard.js do. Look here.

Another instance of this issue popped up recently - twosigma/beakerx#6227
You can see that they worked around it the way I had suggested in the first update - by disabling and enabling the keyboard manager.

Do you think we can have an "escape hatch" for the notebook's copy (It is a really good feature btw) feature? Similar to how MathJax provides a tex2jax_ignore class. Notebook widgets,extensions and js libraries sometimes do need a copy to clipboard feature. Maybe a notebook-ignore-copy class like this.

@Madhu94 Madhu94 changed the title Extensions which use "programmatic" copy to clipboard don't work in Jupyter 5 "Programmatic" copy to clipboard don't work in Jupyter 5 Nov 18, 2017
@Madhu94
Copy link
Contributor Author

Madhu94 commented Nov 21, 2017

@takluyver @gnestor Please let me know what you think of the last part of my previous comment -

Another instance of this issue popped up recently - twosigma/beakerx#6227
You can see that they worked around it the way I had suggested in the first update - by disabling and enabling the keyboard manager.

Do you think we can have an "escape hatch" for the notebook's copy (It is a really good feature btw) feature? Similar to how MathJax provides a tex2jax_ignore class. Notebook widgets,extensions and js libraries sometimes do need a copy to clipboard feature. Maybe a notebook-ignore-copy class like this.

@takluyver
Copy link
Member

If you can see how to implement it, PRs are welcome! I'm a bit hazy on what we can do with the clipboard - I thought we could only respond to clipboard events generated by the browser, but it sounds like document.execCommand can get around that.

@blink1073
Copy link
Contributor

We have a function for this in @jupyterlab/apputils.

@Madhu94
Copy link
Contributor Author

Madhu94 commented Nov 22, 2017

@takluyver Thanks, I'll give it a shot. But do you see any issue with adding a workaround - something like Madhu94@083f2c8 ?

@takluyver
Copy link
Member

If that works, it looks fine. I don't know how event.target works. Have you tried it on a few different browsers?

@minrk minrk added this to the Reference milestone Sep 13, 2018
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants