Skip to content

Commit

Permalink
Create code-container element
Browse files Browse the repository at this point in the history
* See: #98
  • Loading branch information
stevenjoezhang committed Jul 17, 2023
1 parent 0624149 commit c596155
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
8 changes: 3 additions & 5 deletions source/css/_common/scaffolding/highlight/copy-code.styl
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
.highlight:hover .copy-btn, pre:hover .copy-btn {
.highlight:hover .copy-btn, .code-container:hover .copy-btn {
opacity: 1;
}

figure.highlight {
.table-container {
position: relative;
}
.code-container {
position: relative;
}

.copy-btn {
Expand Down
1 change: 0 additions & 1 deletion source/css/_common/scaffolding/highlight/index.styl
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ pre {
@extend $code-block;
overflow: auto;
padding: 10px;
position: relative;

code {
background: none;
Expand Down
15 changes: 12 additions & 3 deletions source/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ NexT.utils = {
*/
registerCopyCode: function() {
let figure = document.querySelectorAll('figure.highlight');
if (figure.length === 0) figure = document.querySelectorAll('pre:not(.mermaid)');
let needWrap = false;
if (figure.length === 0) {
figure = document.querySelectorAll('pre:not(.mermaid)');
needWrap = true;
}
figure.forEach(element => {
element.querySelectorAll('.code .line span').forEach(span => {
span.classList.forEach(name => {
Expand All @@ -53,9 +57,14 @@ NexT.utils = {
});
if (!CONFIG.copycode.enable) return;
let target = element;
if (CONFIG.copycode.style !== 'mac') target = element.querySelector('.table-container') || element;
if (needWrap) {
const box = document.createElement('div');
box.className = 'code-container';
element.wrap(box);
target = box;
}
target.insertAdjacentHTML('beforeend', '<div class="copy-btn"><i class="fa fa-copy fa-fw"></i></div>');
const button = element.querySelector('.copy-btn');
const button = target.querySelector('.copy-btn');
button.addEventListener('click', () => {
const lines = element.querySelector('.code') || element.querySelector('code');
const code = lines.innerText;
Expand Down

0 comments on commit c596155

Please sign in to comment.