diff options
-rw-r--r-- | assets/css/style.scss | 25 | ||||
-rw-r--r-- | assets/js/main.js | 33 |
2 files changed, 54 insertions, 4 deletions
diff --git a/assets/css/style.scss b/assets/css/style.scss index 7b3c924..b59d669 100644 --- a/assets/css/style.scss +++ b/assets/css/style.scss @@ -1,5 +1,5 @@ --- ---- +--- @import "{{ site.theme }}"; @@ -76,4 +76,27 @@ h6:hover .octicon { padding: 0; margin-left: -16px; vertical-align: middle; +} + +div.highlight { + position: relative; +} + +div.highlight:hover button { + opacity: 1 +} + +div.highlight button { + opacity: 0; + position: absolute; + top: 0.5rem; + right: 1rem; + height: 2em; + transition: all 0.2s ease-out +} + +div.highlight button:active, +div.highlight button:focus, +div.highlight button:hover { + opacity: 1 } \ No newline at end of file diff --git a/assets/js/main.js b/assets/js/main.js index f4b2a99..faac621 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -41,7 +41,7 @@ $(function () { } }); -$(function() { +$(function () { const urlParams = new URLSearchParams(window.location.search); const keyword = urlParams.get('kw')?.trim(); @@ -54,7 +54,7 @@ $(function() { // 递归遍历并高亮文本节点 function highlightTextNodes(element) { - $(element).contents().each(function() { + $(element).contents().each(function () { if (this.nodeType === Node.TEXT_NODE) { const $this = $(this); const text = $this.text(); @@ -72,11 +72,38 @@ $(function() { }); } - $('section').each(function() { + $('section').each(function () { highlightTextNodes(this); }); }); +var codeBlocks = document.querySelectorAll('div.highlight'); + +codeBlocks.forEach(function (codeBlock) { + var copyButton = document.createElement('button'); + copyButton.className = 'copy'; + copyButton.type = 'button'; + copyButton.innerText = '📋'; + + codeBlock.append(copyButton); + + copyButton.addEventListener('click', function () { + var code = codeBlock.querySelector('pre code').innerText.trim(); + window.navigator.clipboard.writeText(code) + .then(() => { + copyButton.innerText = '✅'; + }) + .catch(err => { + copyButton.innerText = '❌'; + console.error('Failed to copy:', err); + }); + + setTimeout(function () { + copyButton.innerText = '📋'; + }, 1500); + }); +}); + today = new Date(); timeold = (today.getTime() - lastUpdated.getTime()); secondsold = Math.floor(timeold / 1000); |