-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add copy support Note that I explicitly trim the text otherwise Firefox adds a trailing EOF line feed. * Implement the tooltip for clipboard feature * Lint * Make it work with dark/light mode --------- Co-authored-by: Julien Déramond <[email protected]>
- Loading branch information
1 parent
44f22fc
commit b5475d7
Showing
8 changed files
with
154 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,59 @@ | ||
/* global bootstrap:false */ | ||
|
||
import ClipboardJS from 'clipboard' | ||
|
||
const btnTitle = 'Copy to clipboard' | ||
|
||
const btnHtml = [ | ||
'<div class="bd-clipboard">', | ||
`<button type="button" class="btn-clipboard" title="${btnTitle}">`, | ||
'<svg class="bi" role="img" aria-label="Copy"><use xlink:href="#clipboard"/></svg>', | ||
'</button>', | ||
'</div>'].join('') | ||
|
||
document.querySelectorAll('div.highlight') | ||
.forEach((element) => { | ||
element.insertAdjacentHTML('beforebegin', btnHtml) | ||
}) | ||
|
||
window.addEventListener('load', () => { | ||
document.querySelectorAll('.btn-clipboard').forEach(btn => { | ||
bootstrap.Tooltip.getOrCreateInstance(btn, { btnTitle }) | ||
}) | ||
}) | ||
|
||
const clipboard = new ClipboardJS('.btn-clipboard', { | ||
target: trigger => trigger.parentNode.nextElementSibling, | ||
text: trigger => trigger.parentNode.nextElementSibling.textContent.trimEnd() | ||
}) | ||
|
||
clipboard.on('success', (event) => { | ||
const iconFirstChild = event.trigger.querySelector('.bi').firstElementChild | ||
const tooltipBtn = bootstrap.Tooltip.getInstance(event.trigger) | ||
const namespace = 'http://www.w3.org/1999/xlink' | ||
const originalXhref = iconFirstChild.getAttributeNS(namespace, 'href') | ||
const originalTitle = event.trigger.title | ||
|
||
tooltipBtn.setContent({ '.tooltip-inner': 'Copied!' }) | ||
event.trigger.addEventListener('hidden.bs.tooltip', () => { | ||
tooltipBtn.setContent({ '.tooltip-inner': btnTitle }) | ||
}, { once: true }) | ||
event.clearSelection() | ||
iconFirstChild.setAttributeNS(namespace, 'href', originalXhref.replace('clipboard', 'check2')) | ||
|
||
setTimeout(() => { | ||
iconFirstChild.setAttributeNS(namespace, 'href', originalXhref) | ||
event.trigger.title = originalTitle | ||
}, 2000) | ||
}) | ||
|
||
clipboard.on('error', event => { | ||
const modifierKey = /mac/i.test(navigator.userAgent) ? '\u2318' : 'Ctrl-' | ||
const fallbackMsg = `Press ${modifierKey}C to copy` | ||
const tooltipBtn = bootstrap.Tooltip.getInstance(event.trigger) | ||
|
||
tooltipBtn.setContent({ '.tooltip-inner': fallbackMsg }) | ||
event.trigger.addEventListener('hidden.bs.tooltip', () => { | ||
tooltipBtn.setContent({ '.tooltip-inner': btnTitle }) | ||
}, { once: true }) | ||
}) |
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,36 @@ | ||
// clipboard.js | ||
// | ||
// JS-based `Copy` buttons for code snippets. | ||
|
||
.bd-clipboard { | ||
position: relative; | ||
display: none; | ||
float: right; | ||
|
||
+ .highlight { | ||
margin-top: 0; | ||
} | ||
|
||
@media (min-width: 768px) { | ||
display: block; | ||
} | ||
} | ||
|
||
.btn-clipboard { | ||
position: absolute; | ||
top: .75em; | ||
right: .5em; | ||
z-index: 10; | ||
display: block; | ||
padding: .5em .75em .625em; | ||
line-height: 1; | ||
color: var(--bs-body-color); | ||
background-color: var(--bd-pre-bg); | ||
|
||
border: 0; | ||
border-radius: .25rem; | ||
|
||
&:hover { | ||
color: var(--bs-link-hover-color); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,18 @@ | ||
{{- $bootstrapJs := resources.Get "/js/bootstrap.bundle.min.js" | resources.Copy "/assets/js/vendor/bootstrap.bundle.min.js" -}} | ||
<script async src="{{ $bootstrapJs.Permalink | relURL }}"></script> | ||
|
||
{{- $esbuildOptions := dict "targetPath" "/assets/js/lazyload.js" "target" "es2019" -}} | ||
{{- $esbuildOptions := dict "target" "es2019" -}} | ||
{{- $lazyloadOptions := dict "targetPath" "/assets/js/lazyload.js" -}} | ||
{{- $appOptions := dict "targetPath" "/assets/js/application.js" -}} | ||
|
||
{{- if eq hugo.Environment "production" -}} | ||
{{- $esbuildOptions = merge $esbuildOptions (dict "minify" "true") -}} | ||
{{- end -}} | ||
|
||
{{- $lazyload := resources.Get "js/lazyload.js" | js.Build $esbuildOptions }} | ||
{{- $lazyloadOptions = merge $esbuildOptions $lazyloadOptions -}} | ||
{{- $appOptions = merge $esbuildOptions $appOptions -}} | ||
{{- $lazyload := resources.Get "js/lazyload.js" | js.Build $lazyloadOptions -}} | ||
{{- $application := resources.Get "js/application.js" | js.Build $appOptions -}} | ||
|
||
<script async src="{{ $lazyload.RelPermalink | relURL }}"></script> | ||
<script async src="{{ $application.RelPermalink | relURL }}"></script> |