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

add navigator clipboard, ClipboardItem #18

Merged
merged 6 commits into from
Aug 25, 2022

Conversation

keithamus
Copy link
Member

@keithamus keithamus commented Apr 28, 2022

This implements navigator.clipboard.write and navigator.clipboard.read, which both work with ClipboardItems. It also adds a re-implementation of ClipboardItem which supports Promises, adhering to the spec.

write, per the spec takes an array of ClipboardItems and writes to the clipboard. We're limited in what we can do here, so we extract the text/plain content of the first one and call writeText. This is a pretty reasonable default behaviour.

read, per the spec should return an array of ClipboardItems representing the user's current clipboard. We're limited what we can do here, so we call readText and convert it into a ClipboardItem with just text/plain.

ClipboardItem has a presentationStyle attribute (defaulting to 'unspecified' unless it's passed as an option), and getType() always returns Blobs. In Chrome's buggy implementation, getType('text/plain') will return a String. We've accounted for both possibilities in navigator.clipboard.write just in case the polyfill isn't applied (e.g. Chrome adds promises but keeps their buggy getType() behaviour).

This means await navigator.clipboard.write(await navigator.clipboard.read()) should be a no-op (as long as the user only has plain-text data in their clipboard) which feels like a sensible thing to do.

Customer Support Data

Here are some tables representing the % of browsers we see that support (or don't support) these APIs.

Clipboard Write

Browser Supported Since Latest Version % Supported % Unsupported
chrome 66 101 73.565 0.007
edge 79 100 6.691 0.001
firefox Not Supported 100 0.000 12.669
firefox_android Not Supported 100 0.000 0.128
ie Not Supported 11 0.000 0.078
opera 63 86 1.267 0.000
safari 13.1 15.4 4.630 0.013
safari_ios 13.4 15.4 0.505 0.006
samsunginternet_android 12.0 16.0 0.020 0.000
webview_android 66 101 0.002 0.008
Total: 86.68 12.91

(Note: "Other" browsers make up 0.408%)

@pablonete
Copy link

This is great, thank you @keithamus for putting this together 🎉
I've read through it and I have no concerns, just a couple of questions:

  1. Is this going to provide Firefox support too?
  2. Will this be added to index.ts, so the apply method brings it to dotcom? I'm just trying to clarify the road ahead: once we merge this and upgrade this lib in dotcom, we'd got https://github.com/github/repos/issues/1073 fixed 🙏

@keithamus
Copy link
Member Author

keithamus commented Apr 29, 2022

  1. Yeah this should work with Firefox, so you can just use navigator.read/navigator.write and thing should mostly work.
  2. Yes, this should "just work" in dotcom. https://github.com/github/repos/issues/1073 can be closed once this is merged and upgraded into dotcom.

Copy link
Contributor

@theinterned theinterned left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super cool, just left my one drive-by question

src/navigator-clipboard.ts Outdated Show resolved Hide resolved
@keithamus keithamus force-pushed the add-navigator-clipboard-clipboarditem branch 2 times, most recently from 164abf9 to faa236c Compare May 18, 2022 14:44
@keithamus keithamus force-pushed the add-navigator-clipboard-clipboarditem branch from faa236c to f054df8 Compare May 18, 2022 15:01
@keithamus keithamus marked this pull request as ready for review May 18, 2022 15:34
@keithamus keithamus requested a review from a team as a code owner May 18, 2022 15:34
pablonete
pablonete previously approved these changes May 18, 2022
koddsson
koddsson previously approved these changes May 19, 2022
docs/index.html Outdated Show resolved Hide resolved
@Ballub17

This comment was marked as spam.

@pablonete
Copy link

Tested today on Firefox and confirmed that this fixes the Copy Remote functionality in the blob page for Firefox, see details on https://github.com/github/repos/issues/1073#issuecomment-1226971804.

@keithamus keithamus dismissed stale reviews from koddsson and pablonete via 6ff62ff August 25, 2022 10:21
Copy link

@JoseInTheArena JoseInTheArena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm 🚀

@dgreif dgreif merged commit 54a518a into main Aug 25, 2022
@dgreif dgreif deleted the add-navigator-clipboard-clipboarditem branch August 25, 2022 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants