mirror of
https://github.com/welpo/tabi.git
synced 2026-05-11 08:37:35 +02:00
🐛 fix: avoid copying code block line numbers (#647)
Co-authored-by: welpo <welpo@users.noreply.github.com>
This commit is contained in:
parent
17029f02b1
commit
35098abea3
2 changed files with 8 additions and 2 deletions
|
|
@ -27,7 +27,13 @@ const copyCodeAndChangeIcon = async (copyDiv, block) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const getNonTableCode = (block) => {
|
const getNonTableCode = (block) => {
|
||||||
return [...block.querySelectorAll('code')].map((code) => code.textContent).join('');
|
return [...block.querySelectorAll('code')]
|
||||||
|
.map((code) => {
|
||||||
|
const clone = code.cloneNode(true);
|
||||||
|
clone.querySelectorAll('.giallo-ln').forEach(el => el.remove());
|
||||||
|
return clone.textContent;
|
||||||
|
})
|
||||||
|
.join('');
|
||||||
};
|
};
|
||||||
|
|
||||||
const getTableCode = (block) => {
|
const getTableCode = (block) => {
|
||||||
|
|
|
||||||
2
static/js/copyCodeToClipboard.min.js
vendored
2
static/js/copyCodeToClipboard.min.js
vendored
|
|
@ -1 +1 @@
|
||||||
const copiedText=document.getElementById("copy-success").textContent,initCopyText=document.getElementById("copy-init").textContent,changeIcon=(e,t)=>{e.classList.add(t),e.setAttribute("aria-label",copiedText),setTimeout(()=>{e.classList.remove(t),e.setAttribute("aria-label",initCopyText)},2500)},addCopyEventListenerToDiv=(e,t)=>{e.addEventListener("click",()=>copyCodeAndChangeIcon(e,t))},copyCodeAndChangeIcon=async(t,e)=>{e=(e.querySelector("table")?getTableCode:getNonTableCode)(e);try{await navigator.clipboard.writeText(e),changeIcon(t,"checked")}catch(e){changeIcon(t,"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:not(.mermaid)").forEach(e=>{var t=document.createElement("div");t.setAttribute("role","button"),t.setAttribute("aria-label",initCopyText),t.setAttribute("title",initCopyText),t.className="copy-code",e.prepend(t),addCopyEventListenerToDiv(t,e)});
|
let copiedText=document.getElementById("copy-success").textContent,initCopyText=document.getElementById("copy-init").textContent,changeIcon=(e,t)=>{e.classList.add(t),e.setAttribute("aria-label",copiedText),setTimeout(()=>{e.classList.remove(t),e.setAttribute("aria-label",initCopyText)},2500)},addCopyEventListenerToDiv=(e,t)=>{e.addEventListener("click",()=>copyCodeAndChangeIcon(e,t))},copyCodeAndChangeIcon=async(t,e)=>{e=(e.querySelector("table")?getTableCode:getNonTableCode)(e);try{await navigator.clipboard.writeText(e),changeIcon(t,"checked")}catch(e){changeIcon(t,"error")}},getNonTableCode=e=>[...e.querySelectorAll("code")].map(e=>{e=e.cloneNode(!0);return e.querySelectorAll(".giallo-ln").forEach(e=>e.remove()),e.textContent}).join(""),getTableCode=e=>[...e.querySelectorAll("tr")].map(e=>e.querySelector("td:last-child")?.innerText??"").join("");document.querySelectorAll("pre:not(.mermaid)").forEach(e=>{var t=document.createElement("div");t.setAttribute("role","button"),t.setAttribute("aria-label",initCopyText),t.setAttribute("title",initCopyText),t.className="copy-code",e.prepend(t),addCopyEventListenerToDiv(t,e)});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue