-
-
Notifications
You must be signed in to change notification settings - Fork 71
Fix copy code button size when over oneline code blocks #2007
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,29 @@ | ||
| $(() => { | ||
| $(".post--content pre > code") | ||
| const buttonTemplate = `<button class="copy-button button is-muted is-outlined has-margin-2">Copy</button>`; | ||
|
|
||
| $('.post--content pre > code') | ||
| .parent() | ||
| .each(function() { | ||
| const content = $(this).text() | ||
| .each(function () { | ||
| const $button = $(buttonTemplate); | ||
| const $content = $(this).text(); | ||
| const numLines = $content.trim().split(/\r?\n/).length | ||
|
|
||
| if (numLines <= 1) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using This seems a very rare case. The only example I can think of where avoiding having the Copy button might be a problem is Code Golf. In the esolang (esoteric programming language) Whitespace it's possible to have a valid program composed entirely of newlines, tabs, and spaces, which would all be removed by the If we wanted to cover both of these rare cases we could also test the length of the content:
Is there a simpler approach where we find the height of the code block without trimming? Since both
Maybe something like (untested): numLines = $content.length == 0 ? 0 : $content.match(/\n/g).length + 1 |
||
| $button.addClass('is-small'); | ||
| } | ||
|
|
||
| $(this) | ||
| .wrap('<div style="position:relative;"></div>') | ||
| .parent() | ||
| .prepend($('<button class="copy-button button is-muted is-outlined has-margin-2">Copy</button>') | ||
| .click(function () { | ||
| navigator.clipboard.writeText(content); | ||
| .prepend( | ||
| $button.click(function () { | ||
| navigator.clipboard.writeText($content); | ||
| $(this).text('Copied!'); | ||
| setTimeout(() => { $(this).text('Copy'); }, 2000); | ||
| })) | ||
| }); | ||
| }); | ||
| setTimeout(() => { | ||
| $(this).text('Copy'); | ||
| }, 2000); | ||
| }), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
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.
Non-blocking: would this be a good opportunity to phase out jQuery for this file?