Skip to content

Commit 1f11d00

Browse files
committed
解决代码块仅复制头一行的问题
1 parent b24b81a commit 1f11d00

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

resources/views/documents/articles/show.blade.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,14 +497,23 @@ function initLightbox() {
497497
498498
function addCopyButtons() {
499499
document.querySelectorAll('pre').forEach(function(pre) {
500-
var codeBlock = pre.querySelector('code');
501500
var button = document.createElement('button');
502501
button.className = 'copy-btn';
503502
button.textContent = '复制';
504503
505504
button.addEventListener('click', function() {
505+
var codeLines = pre.querySelectorAll('ol.linenums li');
506506
var textArea = document.createElement('textarea');
507-
textArea.value = codeBlock.textContent;
507+
var codeText = '';
508+
509+
codeLines.forEach(function(line) {
510+
var codeElement = line.querySelector('code');
511+
if (codeElement) {
512+
codeText += codeElement.innerText + '\n';
513+
}
514+
});
515+
516+
textArea.value = codeText.trim();
508517
document.body.appendChild(textArea);
509518
textArea.select();
510519
try {
@@ -526,7 +535,7 @@ function addCopyButtons() {
526535
pre.appendChild(button);
527536
});
528537
}
529-
538+
530539
function getCurrentArticleIndex() {
531540
var currentArticleId = {{ $article->id }};
532541
var articles = @json($articles);

0 commit comments

Comments
 (0)