Skip to content

Commit 305ac42

Browse files
authored
feat: webui add copy btn (#42)
1 parent e1f8181 commit 305ac42

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

assets/index.html

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@
265265
.code-view {
266266
display: flex;
267267
align-items: center;
268+
position: relative;
268269
}
269270

270271
.code-view pre {
@@ -283,6 +284,24 @@
283284
width: 100%;
284285
}
285286

287+
.code-view .copy-btn {
288+
cursor: pointer;
289+
position: absolute;
290+
display: inline-flex;
291+
top: 4px;
292+
right: 4px;
293+
padding: 4px;
294+
width: 24px;
295+
height: 24px;
296+
border-radius: 50%;
297+
align-items: center;
298+
justify-content: center;
299+
}
300+
301+
.code-view .copy-btn:hover {
302+
background-color: #e4e4e4;
303+
}
304+
286305
.media-view {
287306
display: block;
288307
margin: 0 0 10px;
@@ -660,10 +679,25 @@
660679
});
661680

662681
document.addEventListener("click", function (e) {
663-
if (!e.target.closest(".dropdown")) {
682+
const $dropdown = e.target.closest(".dropdown");
683+
if (!$dropdown) {
664684
$exportDropdownContent.style.display = "none";
665685
$copyDropdownContent.style.display = "none";
666686
}
687+
const $codeCopyBtn = e.target.closest(".code-view .copy-btn");
688+
if ($codeCopyBtn) {
689+
ClipboardJS.copy($codeCopyBtn.parentElement.querySelector(".code").textContent);
690+
691+
const oldBtnHtml = $codeCopyBtn.innerHTML;
692+
$codeCopyBtn.innerHTML = `
693+
<svg width="16" height="16" viewBox="0 0 16 16">
694+
<path d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0"/>
695+
</svg>
696+
`;
697+
setTimeout(() => {
698+
$codeCopyBtn.innerHTML = oldBtnHtml;
699+
}, 2000);
700+
}
667701
});
668702
}
669703

@@ -947,7 +981,14 @@
947981
const highlightedCode = hljs.getLanguage(language)
948982
? hljs.highlight(value, { language }).value
949983
: hljs.highlightAuto(value).value;
950-
return `<div class="code-view"><pre><code>${highlightedCode}</code></pre></div>`;
984+
return `<div class="code-view">
985+
<pre><code class="code">${highlightedCode}</code></pre>
986+
<span class="copy-btn" title="Copy">
987+
<svg width="16" height="16" viewBox="0 0 16 16">
988+
<path fill-rule="evenodd" d="M4 2a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2zm2-1a1 1 0 0 0-1 1v8a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1zM2 5a1 1 0 0 0-1 1v8a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1v-1h1v1a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h1v1z"/>
989+
</svg>
990+
</span>
991+
</div>`;
951992
}
952993
if (encode == "base64") {
953994
if (
@@ -968,7 +1009,7 @@
9681009
return `<div class="media-view"><a href="${downloadUrl}" download="${filename}">Download the binary data</a></div>`;
9691010
}
9701011
}
971-
return `<div class="code-view">${value}</div>`;
1012+
return `<div class="code-view"><pre><code>${value}</code></pre></div>`;
9721013
}
9731014

9741015
function tmplError(error) {
@@ -1073,15 +1114,15 @@
10731114
* @param {string} contentType
10741115
*/
10751116
function base64ToBlob(base64String, contentType) {
1076-
const byteArrays= base64ToUint8Array(base64String);
1117+
const byteArrays = base64ToUint8Array(base64String);
10771118
return new Blob([byteArrays], { type: contentType });
10781119
}
10791120

10801121
/**
10811122
* @param {string} base64String
10821123
*/
10831124
function base64ToText(base64String) {
1084-
const byteArrays= base64ToUint8Array(base64String);
1125+
const byteArrays = base64ToUint8Array(base64String);
10851126
const decoder = new TextDecoder('utf-8');
10861127
return decoder.decode(byteArrays);
10871128
}

0 commit comments

Comments
 (0)