-
Notifications
You must be signed in to change notification settings - Fork 14k
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
fix: Support the Clipboard API in modern browsers #20058
fix: Support the Clipboard API in modern browsers #20058
Conversation
a4fd00f
to
d321279
Compare
Codecov Report
@@ Coverage Diff @@
## master #20058 +/- ##
==========================================
- Coverage 66.50% 66.50% -0.01%
==========================================
Files 1726 1726
Lines 64789 64803 +14
Branches 6829 6830 +1
==========================================
+ Hits 43090 43097 +7
- Misses 19967 19974 +7
Partials 1732 1732
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
/testenv up |
@stephenLYZ Ephemeral environment spinning up at http://34.220.180.16:8080. Credentials are |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix! Left a comment and let me know if it makes sense.
superset-frontend/src/utils/copy.ts
Outdated
if (!document.execCommand('copy')) { | ||
// Use the new Clipboard API if the browser supports it | ||
const copyTextWithClipboardApi = async (getText: () => Promise<string>) => { | ||
try { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually It is not recommended to use nested try catches from async/await, which cause errors to bubble up, meaning that all functions under the catch scope are called (You can see copy is called twice in the chrome). I think It is okay to guarantee top-level try catches, maybe more appropriate to use promise here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for that! The change in the parent function (copyTextToClipboard
) should do it.
@diegomedina248 Hi, It looks like the copy will still be called twice (send the request twice). can you check it again? |
Yes, that seems to be a limitation on the API, for anything other than Safari, the promise inside We could check for the browser engine and try to assign the proper clipboard api execution: That has the drawback of browser engine & version miss match, aka, not all webkit browsers behave the way Safari does (in fact, I think only Safari has this weird bug). |
2bbb585
to
25672fc
Compare
@diegomedina248 So we just need to do some checks for safari? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with a minor cleanup suggestion. Tested to work as expected with both Safari and Chrome (also verified SQL Lab which uses the same utils).
selection.addRange(range); | ||
const copyTextToClipboard = (getText: () => Promise<string>) => | ||
copyTextWithClipboardApi(getText) | ||
// If the Clipboard API is not supported, fallback to the older method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: it could clean up the code by breaking the old logic into a separate function
We could dedupe the permalink request by saving the request payload and response of the previous permalink request, and if the old payload is identical to the new one, we just return the original response without calling the API again. This would have the benefit of also not creating new permalinks if the user clicks one the link button multiple times in a row. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be nice to get that duplicated request fixed before merging
25672fc
to
f02bf0b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, verified that it works as expected on Firefox, Chrome and Safari, and that there are no longer duplicated requests to the permalink endpoint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!
Ephemeral environment shutdown and build artifacts deleted. |
* fix: Support the Clipboard API in modern browsers * fix tests * PR comment * Improvements
SUMMARY
The copy command, though largely still supported, is deprecated.
In Safari, latest versions, the copy to clipboard of a permalink no longer works because of it.
This PR adds support for the Clipboard API, with all the nuances and small differences between the different browser engines. Be sure to read the comments & associated discussions linked for more information.
The new API is not yet fully supported by all browsers, so the current copy code was left as a fallback.
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
Before:
Screen.Recording.2022-05-13.at.10.19.43.mov
After:
new.mov
TESTING INSTRUCTIONS
Go to any chart/dashboard and ensure the copy to permalink works across browsers.
ADDITIONAL INFORMATION
Fixes #19994