Skip to content

Commit a02db13

Browse files
committed
fix: prevent copy text being copied in code samples fixes #2366 [deploy site]
1 parent 0125c11 commit a02db13

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

website_and_docs/layouts/shortcodes/gh-codeblock.html

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@
5151

5252
{{ highlight $codeSnippet $language }}
5353

54+
<style>
55+
div.code-toolbar > .toolbar {
56+
display: none !important;
57+
}
58+
</style>
59+
5460
{{ if $hasFragment }}
5561
{{ $uniqueId := md5 $path }}
5662
<div class="mt-4 pb-2">
@@ -70,10 +76,10 @@
7076
<div style="position: sticky; top: 0; z-index: 10; background: white; box-shadow: 0 2px 2px rgba(0,0,0,0.1); padding: 10px 15px; border-bottom: 1px solid #dee2e6; display: flex; justify-content: space-between; align-items: center; height: 50px;">
7177
<h5 style="margin: 0;">{{ $path }}</h5>
7278
<div>
73-
<button onclick="copyCode('{{ $uniqueId }}', event)" style="margin-right: 10px; padding: 5px 10px; border: 1px solid #007bff; background: #007bff; color: white; border-radius: 4px; cursor: pointer;">
79+
<button onclick="copyCode('{{ $uniqueId }}', event)" style="margin-right: 10px; padding: 5px 10px; border: 1px solid #007bff; background: #007bff; color: white; border-radius: 4px; cursor: pointer; user-select: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none;">
7480
Copy
7581
</button>
76-
<button onclick="closeCodeModal('{{ $uniqueId }}')" style="padding: 5px 10px; border: 1px solid #6c757d; background: #6c757d; color: white; border-radius: 4px; cursor: pointer;">
82+
<button onclick="closeCodeModal('{{ $uniqueId }}')" style="padding: 5px 10px; border: 1px solid #6c757d; background: #6c757d; color: white; border-radius: 4px; cursor: pointer; user-select: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none;">
7783
Close
7884
</button>
7985
</div>
@@ -85,6 +91,11 @@ <h5 style="margin: 0;">{{ $path }}</h5>
8591
</div>
8692

8793
<script>
94+
document.addEventListener('DOMContentLoaded', function() {
95+
const toolbars = document.querySelectorAll('div.code-toolbar > .toolbar');
96+
toolbars.forEach(toolbar => toolbar.remove());
97+
});
98+
8899
window.showCodeModal = window.showCodeModal || function(id) {
89100
document.getElementById('codeModal_' + id).style.display = 'block';
90101
document.body.style.overflow = 'hidden';
@@ -97,7 +108,8 @@ <h5 style="margin: 0;">{{ $path }}</h5>
97108

98109
window.copyCode = window.copyCode || function(id, event) {
99110
const codeElement = document.getElementById('codeContent_' + id);
100-
const code = codeElement.textContent;
111+
const preElement = codeElement.querySelector('pre');
112+
const code = preElement ? preElement.textContent : codeElement.textContent;
101113

102114
if (navigator.clipboard) {
103115
navigator.clipboard.writeText(code).then(() => {

0 commit comments

Comments
 (0)