Skip to content

Commit bffa642

Browse files
authored
Add copy to clipboard for code snippets (#1548)
* Add copy to clipboard for code snippets
1 parent cc9dfb1 commit bffa642

File tree

10 files changed

+93
-4
lines changed

10 files changed

+93
-4
lines changed

assets/css/index.css

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,4 +1073,25 @@ a[href*="#no-click"], img[src*="#no-click"] {
10731073

10741074
.dd-item .highlight:hover {
10751075
color: #5961ff;
1076+
}
1077+
1078+
.copy-button {
1079+
background-color: #eee;
1080+
border: none;
1081+
padding: 4px;
1082+
cursor: pointer;
1083+
border-radius: 4px;
1084+
display: flex;
1085+
align-items: center;
1086+
justify-content: center;
1087+
}
1088+
1089+
.copy-button svg {
1090+
width: 16px;
1091+
height: 16px;
1092+
fill: #333;
1093+
}
1094+
1095+
.copy-button:hover {
1096+
background-color: #ddd;
10761097
}

layouts/commands/list.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ <h1 class="truncate font-mono font-medium text-lg mb-1.5 px-6">
132132
{{ end }}
133133
</div>
134134
</div>
135-
136135
{{ partial "commands-nav.html" . }}
136+
{{ partial "scripts.html" . }}
137137
</main>
138138
{{ end }}

layouts/commands/single.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,6 @@ <h2>History</h2>
130130
</dl>
131131
{{ end }}
132132
</aside>
133-
133+
{{ partial "scripts.html" . }}
134134
</main>
135135
{{ end }}

layouts/develop/list.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,6 @@ <h1>
9393
</section>
9494
</div>
9595
{{ partial "docs-toc.html" . }}
96+
{{ partial "scripts.html" . }}
9697
</main>
9798
{{ end }}

layouts/develop/single.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ <h1>
3737
</section>
3838
</div>
3939
{{ partial "docs-toc.html" . }}
40+
{{ partial "scripts.html" . }}
4041
</main>
4142
{{ end }}

layouts/integrate/list.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ <h1 class="text-xl leading-tight px-6 py-4">
7272
{{ end }}
7373
</div>
7474
</div>
75-
7675
{{ partial "commands-nav.html" . }}
76+
{{ partial "scripts.html" . }}
7777
</main>
7878
{{ end }}

layouts/integrate/single.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ <h1>{{ .Title }}</h1>
3636
</section>
3737
</div>
3838
{{ partial "docs-toc.html" . }}
39+
{{ partial "scripts.html" . }}
3940
</main>
4041
{{ end }}

layouts/integration/list.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ <h1>{{ .Title }}</h1>
4848
</section>
4949
</div>
5050
{{ partial "docs-toc.html" . }}
51+
{{ partial "scripts.html" . }}
5152
</main>
5253
{{ end }}

layouts/integration/single.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ <h1>{{ .Title }}</h1>
3636
</section>
3737
</div>
3838
{{ partial "docs-toc.html" . }}
39+
{{ partial "scripts.html" . }}
3940
</main>
4041
{{ end }}

layouts/partials/scripts.html

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,68 @@
1-
<!-- Code for version selectors -->
1+
<!-- Code for copy to clipboard button -->
2+
<script>
3+
document.addEventListener("DOMContentLoaded", function () {
4+
5+
// don't run script for tabbed code examples
6+
if (!document.querySelector('.codetabs.cli.group.flex.justify-start.items-center.flex-wrap.box-border.rounded-lg.mt-0.mb-0.mx-auto.bg-slate-900')) {
7+
document.querySelectorAll('div.highlight').forEach(block => {
8+
// Create a wrapper for the button and message
9+
const wrapper = document.createElement('div');
10+
11+
if (block.parentElement.classList.contains("expand-content")) {
12+
wrapper.style = 'position:absolute;top:10px;right:20px;z-index:1;display:flex;align-items:center;gap:6px;';
13+
} else {
14+
wrapper.style = 'position:absolute;top:10px;right:10px;z-index:1;display:flex;align-items:center;gap:6px;';
15+
}
16+
// Create the copy button
17+
const button = document.createElement('button');
18+
button.innerHTML = `
19+
<button class="clipboard-button text-neutral-400 hover:text-slate-100 bg-slate-600 relative h-7 w-7 p-1 rounded rounded-mx" title="Copy to clipboard">
20+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" fill="currentColor">
21+
<path d="M9 43.95q-1.2 0-2.1-.9-.9-.9-.9-2.1V10.8h3v30.15h23.7v3Zm6-6q-1.2 0-2.1-.9-.9-.9-.9-2.1v-28q0-1.2.9-2.1.9-.9 2.1-.9h22q1.2 0 2.1.9.9.9.9 2.1v28q0 1.2-.9 2.1-.9.9-2.1.9Zm0-3h22v-28H15v28Zm0 0v-28 28Z"></path>
22+
</svg>
23+
`;
24+
25+
// Create the tooltip container
26+
const tooltipContainer = document.createElement('div');
27+
tooltipContainer.className = 'tooltip relative inline-block';
28+
29+
// Create the tooltip span
30+
const tooltip = document.createElement('span');
31+
tooltip.className = 'tooltiptext hidden bg-slate-200 rounded rounded-mx text-slate-800 text-center text-xs p-1 absolute';
32+
tooltip.style.bottom = '-0.6rem';
33+
tooltip.style.left = '-5.5rem';
34+
tooltip.textContent = 'Copied!';
235

36+
tooltipContainer.appendChild(tooltip);
37+
button.appendChild(tooltipContainer);
38+
39+
// Handle copy logic
40+
button.addEventListener('click', () => {
41+
const lines = block.querySelectorAll('span.cl');
42+
const text = Array.from(lines).map(line => line.textContent).join('');
43+
44+
navigator.clipboard.writeText(text).then(() => {
45+
tooltip.style.display = 'block';
46+
setTimeout(() => {
47+
tooltip.style.display = 'none';
48+
}, 2000);
49+
}).catch(err => {
50+
console.error("Copy failed", err);
51+
});
52+
});
53+
54+
// Append button and message
55+
wrapper.appendChild(button);
56+
wrapper.appendChild(tooltipContainer);
57+
block.style.position = 'relative';
58+
block.appendChild(wrapper);
59+
});
60+
}
61+
});
62+
</script>
63+
64+
65+
<!-- Code for version selectors -->
366
<script>
467
window.onload = function(){
568
var currentUrl = window.location.href

0 commit comments

Comments
 (0)