Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Fix: Copy code button actually do copy the code block content (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
aridder authored Mar 19, 2024
1 parent 0509e3b commit e4752fa
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions theme/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ function playground_text(playground, hidden = true) {
}
}

function copy_to_clipboard(pre_block) {
var text = playground_text(pre_block);
navigator.clipboard.writeText(text)
}

function run_rust_code(code_block) {
var result_block = code_block.querySelector(".result");
if (!result_block) {
Expand Down Expand Up @@ -231,6 +236,9 @@ function playground_text(playground, hidden = true) {
clipButton.innerHTML = '<i class=\"tooltiptext\"></i>';

buttons.insertBefore(clipButton, buttons.firstChild);
clipButton.addEventListener('click', function (e) {
copy_to_clipboard(pre_block);
})
}
});
}
Expand Down Expand Up @@ -264,6 +272,10 @@ function playground_text(playground, hidden = true) {
copyCodeClipboardButton.setAttribute('aria-label', copyCodeClipboardButton.title);

buttons.insertBefore(copyCodeClipboardButton, buttons.firstChild);

copyCodeClipboardButton.addEventListener('click', function (e) {
copy_to_clipboard(pre_block);
});
}

let code_block = pre_block.querySelector("code");
Expand Down

0 comments on commit e4752fa

Please sign in to comment.