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

Allow programmatic copy to clipboard #3088

Merged
merged 1 commit into from
Dec 1, 2017

Conversation

Madhu94
Copy link
Contributor

@Madhu94 Madhu94 commented Nov 27, 2017

Trying to close #2975.

@takluyver I did not go with document.execCommand because it does not allow you to set a mime-types like html, json, etc. (or I couldn't find a way to do that anyway) and I see you need that.

Let me know if these changes are okay.

@Madhu94
Copy link
Contributor Author

Madhu94 commented Nov 27, 2017

This works for my usecase, in both Chrome and FF. But I see that you can't actually test this...I'll come up with a reproducible test case.

@Madhu94
Copy link
Contributor Author

Madhu94 commented Nov 29, 2017

@takluyver Here is an example you can paste into a notebook cell to reproduce this bug and see what this fix does.

class copytoclipboard(object):
    def _repr_html_(self):
        return """
            <button class="btn">Copy to clipboard</button>
            <script>
                function copyToClipboard(copytext) {
                    var ta = document.createElement('textarea');
                    ta.value = copytext;
                    ta.style.position = 'absolute';
                    ta.style.left = '-9999px';
                    document.body.append(ta);
                    ta.select()
                    ta.focus();
                    console.log("Attempting to copy" + copytext);
                    var success=document.execCommand('copy');
                    console.log(success);
                    
                }
                $('.btn').click(function(){
                  copyToClipboard("Ho ho");
                })
            </script>
        """
copytoclipboard()

@gnestor
Copy link
Contributor

gnestor commented Dec 1, 2017

I tested and it works! Looks good to me.

@takluyver Care to merge?

@takluyver
Copy link
Member

Thanks, I've also tested this, and it seems to be fine.

For my own understanding, why does the existing window.getSelection().isCollapsed check not do the same? Is the invisible text field ignored when finding window.getSelection()?

@Madhu94
Copy link
Contributor Author

Madhu94 commented Dec 1, 2017

@takluyver window.getSelection().isCollapsed would not return True if, in the above example, you were to set ta.innerHTML to the copytext and not ta.value.

I don't find it intuitive either.

@takluyver
Copy link
Member

Fair enough. Browsers are strange.

@takluyver takluyver merged commit b1e5f72 into jupyter:master Dec 1, 2017
@takluyver
Copy link
Member

By the way, thanks for providing the example so we could easily test what you meant!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"Programmatic" copy to clipboard don't work in Jupyter 5
3 participants