-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support copying code from codeblocks (#260)
- Loading branch information
Showing
5 changed files
with
106 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
article figure.highlight { | ||
position: relative; | ||
|
||
& > .codeblock-copy-wrapper { | ||
position: absolute; | ||
width: 30px; | ||
height: 30px; | ||
right: 12px; | ||
top: 12px; | ||
border-radius: 4px; | ||
z-index: 10; | ||
background-color: #1e1e20; | ||
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' height='20' width='20' stroke='rgba(128,128,128,1)' stroke-width='2' viewBox='0 0 24 24'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M9 5H7a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V7a2 2 0 0 0-2-2h-2M9 5a2 2 0 0 0 2 2h2a2 2 0 0 0 2-2M9 5a2 2 0 0 1 2-2h2a2 2 0 0 1 2 2'/%3E%3C/svg%3E"); | ||
background-position: 50%; | ||
background-size: 20px; | ||
background-repeat: no-repeat; | ||
cursor: pointer; | ||
opacity: 0; | ||
transition: opacity 0.25s; | ||
|
||
&-copied { | ||
opacity: 1; | ||
border-radius: 0 4px 4px 0; | ||
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' height='20' width='20' stroke='rgba(128,128,128,1)' stroke-width='2' viewBox='0 0 24 24'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M9 5H7a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V7a2 2 0 0 0-2-2h-2M9 5a2 2 0 0 0 2 2h2a2 2 0 0 0 2-2M9 5a2 2 0 0 1 2-2h2a2 2 0 0 1 2 2m-6 9 2 2 4-4'/%3E%3C/svg%3E"); | ||
|
||
&::before { | ||
position: relative; | ||
left: -64px; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
border-radius: 4px 0 0 4px; | ||
width: 64px; | ||
height: 30px; | ||
padding-right: -15px; | ||
text-align: center; | ||
font-size: 12px; | ||
font-weight: 500; | ||
color: rgba(235, 235, 245, 0.6); | ||
background-color: #1e1e20; | ||
white-space: nowrap; | ||
content: 'Copied'; | ||
} | ||
} | ||
} | ||
|
||
&:hover > .codeblock-copy-wrapper { | ||
opacity: 1; | ||
} | ||
} |
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,50 @@ | ||
window.onload = () => { | ||
const codeBlocks = document.querySelectorAll('figure.highlight'); | ||
if (!codeBlocks.length) return; | ||
|
||
const addCopyButton = (codeBlock) => { | ||
const copyWrapper = document.createElement('div'); | ||
copyWrapper.setAttribute('class', 'codeblock-copy-wrapper'); | ||
|
||
let copiedTimeout = null; | ||
|
||
copyWrapper.addEventListener('click', (ev) => { | ||
const highlightDom = ev.target.parentElement; | ||
const code = highlightDom.querySelector('code'); | ||
|
||
let copiedCode = ''; | ||
|
||
(function traverseChildNodes(node) { | ||
const childNodes = node.childNodes; | ||
childNodes.forEach((child) => { | ||
switch (child.nodeName) { | ||
case '#text': // 文本节点 | ||
copiedCode += child.nodeValue; | ||
break; | ||
case 'BR': // <br /> | ||
copiedCode += '\n'; | ||
break; | ||
default: | ||
traverseChildNodes(child); | ||
} | ||
}); | ||
})(code); | ||
|
||
navigator.clipboard.writeText( | ||
/* 去掉最后的换行 */ | ||
copiedCode.slice(0, -1) | ||
).then(() => { | ||
if (!!copiedTimeout) clearTimeout(copiedTimeout); | ||
|
||
copyWrapper.classList.add('codeblock-copy-wrapper-copied'); | ||
copiedTimeout = setTimeout(() => { | ||
copyWrapper.classList.remove('codeblock-copy-wrapper-copied'); | ||
copiedTimeout = null; | ||
}, 1500); | ||
}); | ||
}); | ||
codeBlock.appendChild(copyWrapper); | ||
}; | ||
|
||
codeBlocks.forEach(addCopyButton); | ||
}; |