Skip to content

Commit

Permalink
feat: add copy-code-btn #360
Browse files Browse the repository at this point in the history
  • Loading branch information
xianmin committed Oct 2, 2024
1 parent d6995a3 commit 743d55f
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 38 deletions.
26 changes: 26 additions & 0 deletions assets/js/initCopyCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const initCopyCode = () => {
const containers = document.querySelectorAll('.highlight-container');

containers.forEach(container => {
const copyBtn = container.querySelector('.copy-code-btn');
const codeElement = container.querySelector('.highlight code[data-lang]');

copyBtn.addEventListener('click', function () {
navigator.clipboard.writeText(codeElement.textContent).then(function () {
/* Chrome doesn't seem to blur automatically,
leaving the button in a focused state. */
copyBtn.blur();

copyBtn.innerText = 'Copied!';

setTimeout(function () {
copyBtn.innerText = 'Copy';
}, 2000);
}, function (error) {
copyBtn.innerText = 'Error';
});
});
})
}

export default initCopyCode;
4 changes: 3 additions & 1 deletion assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import initMobileNavbar from './initMobileNavbar.js';
import initToc from './initToc.js';
import initHeaderAnchor from './initHeaderAnchor.js';
import initToggleTheme from './initToggleTheme.js';
import initCopyCode from './initCopyCode.js';

// Use an async function to handle asynchronous initialization
async function initApp() {
Expand All @@ -13,7 +14,8 @@ async function initApp() {
await Promise.all([
initMobileNavbar(),
initToc(),
initHeaderAnchor()
initHeaderAnchor(),
initCopyCode()
]);

console.log('All modules initialized successfully');
Expand Down
36 changes: 0 additions & 36 deletions assets/sass/_partial/_post/_content/_highlight.scss

This file was deleted.

50 changes: 50 additions & 0 deletions assets/sass/_shortcodes/_highlight.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.highlight-container {
margin: 1em 0;
}

.highlight {
position: relative;
overflow-x: auto;
border: 2px solid #dddddd;
line-height: 1.6;

& > div {
padding: 1em 0.3em;
}

code {
all: unset;
border-radius: 0;
padding: 0 !important;
}

pre {
margin: 0; /* remove normal pre margin */
border-radius: 0;
}

table {
padding: 1em 0.3em !important;

// td:nth-child(2) code {
// &::after {
// position: absolute;
// top: 0;
// left: 0.5em;
// font-weight: bold;
// content: attr(data-lang);
// }
// }
}
}

button.copy-code-btn {
/* right-align */
display: block;
margin-left: auto;
margin-right: 0;

margin-bottom: -2px;
font-size: 0.6em;
padding: 0.1em 0.3em;
}
2 changes: 1 addition & 1 deletion assets/sass/jane.scss
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ $mobile-breakpoint: 768px !default;
// @import "_partial/404";
// @import "_partial/author_info";
// @import "_partial/search";
@import "_partial/_post/_content/highlight"; // global highlight style

/**------------------------------------------------------------------------
* page styles
Expand All @@ -207,6 +206,7 @@ $mobile-breakpoint: 768px !default;
/**------------------------------------------------------------------------
* shortcodes
*------------------------------------------------------------------------**/
@import "_shortcodes/highlight";
@import "_shortcodes/notice";

/**------------------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions layouts/_default/_markup/render-codeblock.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="highlight-container">

<button class="copy-code-btn outline">Copy</button>

{{ $result := transform.HighlightCodeBlock . }}
{{ $result.Wrapped }}
</div>
8 changes: 8 additions & 0 deletions layouts/shortcodes/highlight.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="highlight-container">
<button class="copy-code-btn outline">Copy</button>
{{- if len .Params | eq 2 }}
{{ highlight (trim .InnerDeindent "\n\r") (.Get 0) (.Get 1) }}
{{- else }}
{{ highlight (trim .InnerDeindent "\n\r") (.Get 0) "" }}
{{- end }}
</div>

0 comments on commit 743d55f

Please sign in to comment.