8000 Make “copy to clipboard” button Permissions API-aware by benjaminwil · Pull Request #1901 · gollum/gollum · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Make “copy to clipboard” button Permissions API-aware #1901

New issue 8000

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

Merged
merged 5 commits into from
Sep 8, 2023

Conversation

benjaminwil
Copy link
Member
@benjaminwil benjaminwil commented Dec 6, 2022

Fixes #1885.

This pull request makes the "copy to clipboard" button on wiki pages usable with modern browsers that implement the Permissions API for the "clipboard-write" permission as it exists today. And it generally improves the "copy to clipboard" functionality of code blocks on wiki pages:

  • Users who don't use pointing devices can use the "copy to clipboard" using the keyboard only.
    Note: Before this pull request, the feature was not keyboard-accessible, and it still isn't after this pull request. There is discussion about this in the pull request comments.
  • The button only uses Primer-provided CSS.

@benjaminwil
Copy link
Member Author

CI is exiting before the tests run... I am not sure why yet. Will check it out later this week.

@dometto
Copy link
Member
dometto commented Dec 6, 2022

Looks like the psych gem is failing to install: yaml.h not found. A new version of psych (5.0) has just been released, so that's probably causing it.

@benjaminwil
Copy link
Member Author

Ah, so locally I had to ensure libyaml is installed. I can try to add a commit to #1903 later today to remedy this.

@dometto
Copy link
Member
dometto commented Dec 6, 2022

Sounds good!

@benjaminwil benjaminwil force-pushed the copy-to-clipboard-keyboard-navigable branch 3 times, most recently from b9d90e6 to 429407b Compare December 9, 2022 17:15
@benjaminwil
Copy link
Member Author

I believe Selenium is failing because we are not accepting the clipboard permissions prompt that appears in Chromium-based browsers.

This failure is helpful, as it has made me realize that we need to handle the case where the the user has not provided clipboard permissions to Gollum from their browser.

I am a little bit confused, though, why my new test case passes and only the JS errors Capybara test fails. So I must still be missing something.

@dometto
Copy link
Member
dometto commented Dec 13, 2022

I am a little bit confused, though, why my new test case passes and only the JS errors Capybara test fails. So I must still be missing something.

That is quite odd. 😕

EDIT: sorry, the link I posted seems to be relevant only to a specif JS test API.

@bartkamphorst bartkamphorst added this to the 6.0 milestone Jan 6, 2023
@benjaminwil benjaminwil mentioned this pull request Jan 17, 2023
3 tasks
@benjaminwil benjaminwil force-pushed the copy-to-clipboard-keyboard-navigable branch from 429407b to 39898e5 Compare March 5, 2023 00:41
@benjaminwil
Copy link
Member Author

Just making a note that I'll be expanding the scope of this PR to include a change we talked about in #1937:

I really would favor a solution though that uses Primer utilities, rather than styling this button ourselves.

Here it is explained that the marketing layout utilities are built on top of the Primer core utilities so presumably we could position the button using only core utility classes?

@benjaminwil benjaminwil force-pushed the copy-to-clipboard-keyboard-navigable branch 7 times, most recently from 63a4d78 to ccd2215 Compare March 6, 2023 05:39
@bartkamphorst
Copy link
Member

@benjaminwil What's the current status of this PR?

@benjaminwil
Copy link
Member Author
benjaminwil commented Aug 1, 2023
  • It needs to be rebased against the main branch and assets must be recompiled.
  • The wip commit needs to be dropped or my issue with it needs to be resolved.
  • My issue in the wip commit is that I cannot figure out how to drive Chrome with the Permissions API enabled or disabled.

I was actually hoping to pick this up again and get it ready for review this weekend! So very timely of you to comment on it now.

@bartkamphorst
Copy link
Member

That's great. Thanks for the update!

@benjaminwil benjaminwil force-pushed the copy-to-clipboard-keyboard-navigable branch 2 times, most recently from bff1349 to cb93a2d Compare August 5, 2023 22:08
@benjaminwil benjaminwil marked this pull request as ready for review August 5, 2023 22:17
@benjaminwil benjaminwil force-pushed the copy-to-clipboard-keyboard-navigable branch from cb93a2d to 607dacd Compare August 6, 2023 20:32
@benjaminwil
Copy link
Member Author

@bartkamphorst Thanks for catching that error in Firefox.

This opened a small can of worms for me, and I'm just adding this comment to explain that can of worms so (hopefully) no one has to waste time digging through the MDN, Firefox bug reports, and ClipboardJS GitHub issues the next time we touch this feature.


It turns out that Firefox's support for the clipboard via the Permissions API is nonexistent, but we can still use Clipboard.writeText() as long as the user hasn't explicitly turned off clipboard access in their Firefox browser settings. For those users (like me, actually!), I don't know how to offer a good experience yet.

I also tried transitioning to use the ClipboardJS, but I ran into a different issue that stems from ClipboardJS using document.execCommand() in Firefox, which is deprecated anyway, to copy the text.

Since Clipboard.writeText() does actually work in Firefox, we can just return early from the permission checking function if we're confident the current browser is Firefox. This is definitely an unpleasant hack, but I don't see a clean way around it.

@benjaminwil benjaminwil force-pushed the copy-to-clipboard-keyboard-navigable branch from 607dacd to 038d2b0 Compare August 6, 2023 20:51
@benjaminwil
Copy link
Member Author

@bartkamphorst

Also I think the fade-in time is a little bit long for my taste, makes the UI feel a bit sluggish.

I agree. Just fixed up the Style 'copy to clipboard' button with Primer commit to use different animation classes and removed the delayed hiding of the button.

@benjaminwil
Copy link
Member Author

Will also add that I manually tested this in Chrome, Firefox, and Safari without issue.

Copy link
Member
@bartkamphorst bartkamphorst left a comment

Choose a reason for hiding this comment

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

Thanks @benjaminwil ! Sorry this was such a pain to resolve. I do wonder if we could do without an explicit check for userAgent == Firefox but see my remark about that in the review.

Could you explain how the button is now navigable with the keyboard? When I "tab through" the items, I go from the Search input, to Upload to Rename and then to some image in the page, but never to Overview, History, Edit or New or to any of the copy to clipboard buttons.

if (navigator.permissions && navigator.permissions.query) {
return await navigator.permissions.query({name: "clipboard-write"})
.then((permissionStatus) => permissionStatus.state === "denied")
.catch((error) => console.error(error));
Copy link
Member

Choose a reason for hiding this comment

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

Should we notify the user of the error in the UI?

Copy link
Member

Choose a reason for hiding this comment

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

My view would be that that's not necessary: if users are expecting copy/paste functionality on websites and not getting it, that won't be experienced as a problem with gollum specifically.

Copy link
Member
@dometto dometto left a comment

Choose a reason for hiding this comment

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

I have no comments in addition to those of @bartkamphorst. Thanks for the improvement and the extensive tests, @benjaminwil!

@benjaminwil
Copy link
Member Author

Wow, it’s been so long since I started this and it appears I’ve bit off more than I can easily chew for one PR. I don’t want to block any releases.

Perhaps I should close this and PR these fixes more one-at-a-time.

@benjaminwil
Copy link
Member Author

Weirdly, keyboard navigation is behaving much differently in Chrome versus Firefox for all menu items. Maybe this is worth looking into separately.

@dometto
Copy link
Member
dometto commented Aug 11, 2023

The fix for keyboard navigation could definitely be in a different PR, but otherwise it would be great to have the improved code buttons in the next release!

And please don't worry about holding up that release: I had an unexpectedly very busy last four months or so, so I didn't have to work on the new release anyway, until very recently!

@bartkamphorst
Copy link
Member

Weirdly, keyboard navigation is behaving much differently in Chrome versus Firefox for all menu items. Maybe this is worth looking into separately.

I agree. And this behavior is the same on master (on Firefox) so it's not introduced by your changes.

My only concern for this PR is whether we should be checking explicitly for whether userAgent is Firefox or whether we can check if (for example) Clipboard.writeText() is functional.

Modern browsers implement a permissions API that disallows websites from
writing to and reading the system clipboard without permission. To learn
more see the MDN's article about this[1].

This commit provides a basic check for clipboard access, so we don't
spam the browser or its console with errors accidentally.

We also don't want to run any of this code if no codeblocks are present.
So we can check for codeblocks, as well, and potentially exit early.

While I was refactoring, I also found it cumbersome to use a JS event
to find an element and its sibling element. So I've change the `event`
argument of the `copyToClipboard` function to be the codeblock ID we
generate, which lets us easily get the button for the codeblock as well
as the codeblock itself without a big event object.

[1]: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard
The button is now positioned, styled, and animated  using Primer
 utilties.
@benjaminwil benjaminwil force-pushed the copy-to-clipboard-keyboard-navigable branch from 038d2b0 to 0165758 Compare September 6, 2023 17:16
@benjaminwil benjaminwil changed the title Make “copy to clipboard” button keyboard navigable Make “copy to clipboard” button Permissions API-aware Sep 6, 2023
@benjaminwil
Copy link
Member Author

@dometto @bartkamphorst Thanks for your reviews so far. I've updated the PR description and title to reflect the reduced scope of functionality it provides. Let me know if you have further comments.

@dometto
Copy link
Member
dometto commented Sep 7, 2023

Thanks for working on the new approach to clipboard permissions @benjaminwil! 🎆 Happy we can still release this with 6.0, which is now really, really close. :)

See the code comment in the commit body.

I was able to manually test that Firefox emits no errors and copies to
clipboard from a Firefox profile with the default settings.
@benjaminwil benjaminwil force-pushed the copy-to-clipboard-keyboard-navigable branch from 0165758 to dff5f13 Compare September 7, 2023 23:44
@benjaminwil
Copy link
Member Author

Thanks again for the reviews. I've pushed up changes and recompiled the static assets. Should be ready to go. 👍

Copy link
Member
@bartkamphorst bartkamphorst left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for all your work on this!

@benjaminwil benjaminwil merged commit 3e50c1b into master Sep 8, 2023
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.

Add Capybara tests for codeblock "copy to clipboard" feature
3 participants
0