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

Projects
None yet
3 participants
@Madhu94
Copy link
Contributor

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

This comment has been minimized.

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

This comment has been minimized.

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 gnestor added this to the 5.3 milestone Dec 1, 2017

@gnestor

This comment has been minimized.

Copy link
Contributor

gnestor commented Dec 1, 2017

I tested and it works! Looks good to me.

@takluyver Care to merge?

@takluyver

This comment has been minimized.

Copy link
Member

takluyver commented Dec 1, 2017

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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Member

takluyver commented Dec 1, 2017

Fair enough. Browsers are strange.

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

4 checks passed

codecov/patch Coverage not affected when comparing ee419c0...4455bcf
Details
codecov/project 78.57% remains the same compared to ee419c0
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@takluyver

This comment has been minimized.

Copy link
Member

takluyver commented Dec 1, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.