mirror of
https://github.com/welpo/tabi.git
synced 2025-12-17 04:48:44 +01:00
✨ feat: add copy button to code blocks
This commit is contained in:
parent
f710b62631
commit
2dec139e41
13 changed files with 190 additions and 112 deletions
1
static/js/copyCodeToClipboard_min.js
Normal file
1
static/js/copyCodeToClipboard_min.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
const changeIcon=(e,t)=>{e.classList.add(t),setTimeout(()=>e.classList.remove(t),2500)},addCopyEventListenerToDiv=(e,t)=>{e.addEventListener("click",()=>copyCodeAndChangeIcon(e,t))},copyCodeAndChangeIcon=async(e,t)=>{let o=t.querySelector("table")?getTableCode(t):getNonTableCode(t);try{await navigator.clipboard.writeText(o),changeIcon(e,"checked")}catch(c){changeIcon(e,"error")}},getNonTableCode=e=>[...e.querySelectorAll("code")].map(e=>e.textContent).join(""),getTableCode=e=>[...e.querySelectorAll("tr")].map(e=>e.querySelector("td:last-child")?.innerText??"").join("");document.querySelectorAll("pre").forEach(e=>{let t=document.createElement("div");t.className="copy-code",e.prepend(t),addCopyEventListenerToDiv(t,e)});
|
||||
Loading…
Add table
Add a link
Reference in a new issue