-
Notifications
You must be signed in to change notification settings - Fork 25.8k
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
Copy code to clipboard #2812
Copy code to clipboard #2812
Conversation
@iBug really wants a place here in the Credits section :P
Add code based on mmistakes/minimal-mistakes#2812 with some custom changes: Add the option to not creating the copy button by using a “nocopy” class Remove bash prompt symbol when copying instructions, so they can be directly pasted into a terminal Update javascript packages Tested on Chrome, Safari, Opera, Firefox Change some three back-ticks by one when inlining
Hi @iBug, I've been playing with this code and works nice. I have some humble (and surely naive) suggestions: 1.- Sometimes, it makes no sense to copy the code block. For instance, the output of a program in a Terminal. So I would like to be able to shut down the "Copy to clipboard" button in certain code blocks. For instance, adding something like: So I can do:
... but I'm sure there are more elegant ways to do that. 2.- The implementation throws an
3.- Since
It seems that |
|
Hey!!! Really great!!! I really wanted to add something like this to my site!! Thanks a lot! |
ummm… the feature is neat, but only works once in a page, after that you have to reload to copy again form another codeblock. |
Question… why don't you use this? |
It's more likely there's something wrong with your copy of the code. The behavior in the demo links on
Quoting @mmistakes here:
This is a similarly simple feature that simpler code would be easier to maintain. |
I swear over ms-dos and unix that even your demo is behaving like that. In safari. On Firefox I can't even make it work. On macOS Big Sur and last versions of all the apps involved. I even disabled content blockers and so just in case.
I totally understand and see you there. That extension is used for several sites like github, or for example for R documentation sites like this one. I much prefer your solution to be honest, if finally works. Specially because I like how you implemented the CSS and it's already a complete solution.
I really don't know about that, but wouldn't be easier just to update the extension if it continue to be maintained? |
This comment has been minimized.
This comment has been minimized.
@luispuerto It's a stupid typo from somewhere I couldn't recall. Can you test the demo pages now on Safari? It's working in Firefox for me now. Make sure to clear your browser's cache for the updated JavaScript. |
@iBug now works! 👍🏻 thanks a lot! 🚀 |
This feature comes from mmistakes/minimal-mistakes#2812 mmistakes/minimal-mistakes#2812
@mmistakes Given the discussions here and on #2795, #2508 and #2338, as well as that (at least) two people also ported code from this PR into their own repositories, I think there's enough popularity for this feature to get included. Any ideas? |
I have usability and accessibility concerns with how it's currently implimented.
Then there's the concern of turning this on by default. I could see users not wanting it at all and now we've made it that they need to add a class to every code block to disable. I'd prefer it be optin globally rather than optout. This would also prevent loading more JavaScript for a feature not used by someone. Though it might be more of a pain to try and conditionally add those scripts to the page(s) which gives me pause on this PR as well. |
Addressing one concern quickly:
With the current implementation it's not. There's a rule for Making this an opt-in feature is similarly easy: Require a "copy-enabled" class for a parent element, then there could be a switch to add that class to Plus, I'm a bit busy with RL stuff recently, and I'll get back to this PR in maybe June (half a month or so later). |
Still hesitant on this one. I can already see the feature requests asking for a config flag to turn it on/off globally. |
OK, I'll implement that global toggle flag with two modes: Global enable + individual page or block opt-out / global disable + individual page or block opt-in. One technical question: Since the elements and all their HTML content are produced from JS, how should I integrate i18n into that? There's no direct way to transfer data from |
Another global flag is exactly what I’m trying to avoid. The point I was trying to make was I’m hesitant on adding this feature. Not because it doesn’t add value, but because it’s the sort of thing I expect users to not be happy with the implementation and want ever more customization.
I’d rather leave it to the user to override the theme if they want this feature vs the theme trying to do something core Jekyll or a plugin should do. Even better this probably should be an enhancement to Jekyll’s |
Jekyll's
Users have always been wanting ever more customization as is evident by the amount of Issues you're receiving, not to mention some of them are fantastical. I propose this feature for integration into the theme most simply because it's been requested multiple times and has the popularity in the user base. There are ones who have already gone one step sooner and customized on their own. And there certainly are more who want it badly but don't have the skill to push that. 99% of the users will have the attitude that "I'll happily enable it (or stay with it) if it's ready and easily reachable". And there will be users content or discontent for every single feature, so that argument pretty much applies to everything. IMHO, the rule of thumb should be "if the majority goes fine with it, then it's fine". I believe it's courteous enough to provide a way to turn it off (and it's easy and documented). |
Another option would be to go the "helper" I like the following solution where you drop in an include before a code block you want to add copy/paste support. I think this would address most of my concerns with the current implimentation. Instead of Emoji icons I'd leverage Font Awesome, make sure the text strings are done in a way that they can be customized via the UI text data file, add a parameter for filepath (optional), and maybe come up with a better include name than code header. |
Is there a good way to cover both "enable on individual code blocks" and "enable globally"? I'm still thinking of a global "enable" flag (unset by default so the current behavior remains). |
Can't think of anything that would make a global enable doable. This is the sort of thing I think should be a plugin/enhancement to the https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-remark-prismjs I could see the
|
This pull request has been automatically marked as stale because it has not had recent activity. This pull request will automatically be closed in 7 days if no further activity occurs. Thank you for all your contributions. |
As of the last commit Michael's questions are addresses as such:
The theme already has CSS for I've tested with both latest Edge and Firefox and can confirm that it works with keyboard navigation.
Added
There are design and implementation concerns so not easy to address quickly... so maybe later? Again, Material for MkDocs is a really good reference design for all these kinds of features. I may come up with something sensible in the future.
The only UI text is the Most importantly, I would directly dismiss Michael's primary concern of whether users want this feature, as pretty much every, single, theme Correction: Just The Docs has a site-wide switch, which is enabled by default though. The current approach (using uglify-js to squeeze everything into a single Now that only point 3 is left completely unaddressed, I'm going to green-light this PR so as not to let this nice feature be blocked for too long. |
This reverts commit 0527e17.
Add "copy to clipboard" button for code blocks (mmistakes#2812) * Add copy-to-clipboard button and JS * Ignore line numbers if present * Rewrite heading permalink code to use vanilla JS * README: Add credits to zenorocha/clipboard.js (MIT License) @iBug really wants a place here in the Credits section :P * Add .no-copy for hiding the button, update docs * Add td.rouge-code to selectors * Fix navigator.clipboard branch * Add screenreader text for copy button * Restore focus to the button after copying * Add site-wide enable switch
* Add copy-to-clipboard button and JS * Ignore line numbers if present * Rewrite heading permalink code to use vanilla JS * README: Add credits to zenorocha/clipboard.js (MIT License) @iBug really wants a place here in the Credits section :P * Add .no-copy for hiding the button, update docs * Add td.rouge-code to selectors * Fix navigator.clipboard branch * Add screenreader text for copy button * Restore focus to the button after copying * Add site-wide enable switch
Add a "copy to clipboard" button for each
<pre>
block in main body. Code from my website repo with small non-functional adjustments.Live preview available:
```
code blocks) (Wayback Machine){% highlight %}
code blocks) (Wayback Machine)Screenshot:
Caveat: There's only one single color written for the button because I assumed that all skin variants use "dark mode" for blocks of code (
<pre>
blocks).More testing and appearance tweaking is recommended before merging.
Contexts
This is a very popular feature and requested multiple times.
#2338 #2508 #2766 #2795
Some technical notes
My approach does not depend on code blocks having IDs, but instead searches for the containing code block starting from the button. This is not resistant to structural changes, e.g. when an adjacent HTML tag is changed or the button is wrapped in another layer of
<span>
or whatever. (but who does that, really?)The CSS selector I used is
.page__content pre > code
(and the go back a level to locate the<pre>
element), which should be safe from unexpected HTML structures while covering most regular code blocks.The actual "copy" action is done via
document.execCommand("copy")
, which should have the best compatibility according to this answer (quoted below), yet it's deprecated in 2020.Credits