-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add copy button to markdown code blocks
Done mostly in JS because I think it's better not to try getting buttons past the markup sanitizer.
- Loading branch information
1 parent
bab95c3
commit 46b9a27
Showing
12 changed files
with
115 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {svgNode} from '../svg.js'; | ||
const {copied, copy_link_error} = window.i18n; | ||
|
||
// els refers to the code elements | ||
export function renderCodeCopy() { | ||
const els = document.querySelectorAll('.markup .code-block code'); | ||
if (!els?.length) return; | ||
|
||
const button = document.createElement('button'); | ||
button.classList.add('code-copy', 'ui', 'button'); | ||
button.setAttribute('data-success', copied); | ||
button.setAttribute('data-error', copy_link_error); | ||
button.setAttribute('data-variation', 'inverted tiny'); | ||
button.appendChild(svgNode('octicon-copy')); | ||
|
||
for (const el of els) { | ||
const btn = button.cloneNode(true); | ||
btn.setAttribute('data-clipboard-text', el.textContent); | ||
el.after(btn); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
@keyframes fadein { | ||
0% { | ||
opacity: 0; | ||
} | ||
100% { | ||
opacity: 1; | ||
} | ||
} | ||
|
||
@keyframes fadeout { | ||
0% { | ||
opacity: 1; | ||
} | ||
100% { | ||
opacity: 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
.markup .code-block { | ||
position: relative; | ||
} | ||
|
||
.markup .code-copy { | ||
position: absolute; | ||
top: .5rem; | ||
right: .5rem; | ||
padding: 10px; | ||
visibility: hidden; | ||
animation: fadeout .2s both; | ||
} | ||
|
||
.markup .code-copy:hover { | ||
background: var(--color-secondary) !important; | ||
} | ||
|
||
.markup .code-copy:active { | ||
background: var(--color-secondary-dark-1) !important; | ||
} | ||
|
||
.markup .code-block:hover .code-copy { | ||
visibility: visible; | ||
animation: fadein .2s both; | ||
} |