Skip to content

Commit

Permalink
Add copy code button (#963)
Browse files Browse the repository at this point in the history
* Add copy code button

* External package not needed

* Improve alignement

* Update button style + add feedback on click

Signed-off-by: hossainemruz <[email protected]>

* Update dependencies

Signed-off-by: hossainemruz <[email protected]>

---------

Signed-off-by: hossainemruz <[email protected]>
Co-authored-by: Emruz Hossain <[email protected]>
  • Loading branch information
BernatBC and hossainemruz authored Sep 20, 2024
1 parent 9da3e34 commit 0d4d8a8
Show file tree
Hide file tree
Showing 8 changed files with 582 additions and 99 deletions.
22 changes: 22 additions & 0 deletions assets/scripts/features/copyCode/copyCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
addCopyButtons(navigator.clipboard)

function addCopyButtons(clipboard) {
document.querySelectorAll('pre > code').forEach(function (codeBlock) {
const button = document.createElement('button')
button.title = "Copy"
button.className = 'copy-code-button btn btn-sm'
button.innerHTML = "<i class='fa-regular fa-copy'></i>"

button.addEventListener('click', function () {
clipboard.writeText(codeBlock.innerText)
})

const pre = codeBlock.parentNode
if (pre.parentNode.classList.contains('highlight')) {
const highlight = pre.parentNode
highlight.parentNode.insertBefore(button, highlight)
} else {
pre.parentNode.insertBefore(button, pre)
}
})
}
1 change: 1 addition & 0 deletions assets/scripts/features/copyCode/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './copyCode'
4 changes: 4 additions & 0 deletions assets/scripts/features/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ if (process.env.FEATURE_MATH === '1') {
if (process.env.FEATURE_EMBEDPDF === '1') {
import('./embedpdf')
}

if (process.env.FEATURE_COPYCODEBUTTON === '1') {
import('./copyCode')
}
45 changes: 45 additions & 0 deletions assets/styles/components/buttons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,40 @@
}
}

.copy-code-button {
float: right;
margin-top: 0.5em;
margin-left: -2.6em;
margin-right: 3em;

background-color: get-light-color('text-color') !important;
color: get-light-color('inverse-text-color') !important;
padding: 0.25rem 0.5rem;
line-height: 1.5;
border-radius: 0.2rem;
border: none;

&:hover,
&:focus {
background-color: get-light-color('accent-color') !important;
color: get-light-color('text-over-accent-color') !important;
@include transition();
}
&:focus {
&::before {
content: 'Copied!';
position: absolute;
padding: 0.3em;
border-radius: 0.2em;
margin-left: -5em;
margin-top: -0.2em;
background-color: get-light-color('accent-color') !important;
color: get-light-color('text-over-accent-color') !important;
@include transition();
}
}
}

html[data-theme='dark'] {
.btn-dark {
background-color: get-dark-color('accent-color') !important;
Expand Down Expand Up @@ -169,4 +203,15 @@ html[data-theme='dark'] {
background-color: get-dark-color('hover-over-accent-color') !important;
}
}
.copy-code-button {
background-color: get-dark-color('bg-primary') !important;
color: get-dark-color('muted-text-color') !important;

&:hover,
&:focus,
&::before {
background-color: get-dark-color('accent-color') !important;
color: get-dark-color('text-over-accent-color') !important;
}
}
}
7 changes: 7 additions & 0 deletions exampleSite/hugo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ params:
# Configure various features of this theme
features:

# Enable dark theme
darkMode:

# [Deprecated] Enable dark theme
# This is a deprecated setting, but has not been removed to maintain backward compatibility
# If `theme` is set, the `darkMode` setting will be discarded.
Expand Down Expand Up @@ -285,6 +288,10 @@ params:
# options doc: https://github.com/sampotts/plyr#options
# fullscreen: true

# Enables copy code button
copyCodeButton:
enable: true

# Enable reading time support in post cards and in post pages
readingTime:
enable: true
Expand Down
Loading

0 comments on commit 0d4d8a8

Please sign in to comment.