diff options
| author | Francesco Tomaselli <tomaselli.fr@gmail.com> | 2025-04-01 11:22:55 +0200 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-01 11:22:55 +0200 | 
| commit | 133124affd2a9a2ef4c0d4a9867622b44b8153c6 (patch) | |
| tree | 7d479d4f9ad7b6560792a9f2b42c37e5772bd2dc /static | |
| parent | 0899dedab457e6f9ad2f56091d5f7f292019be26 (diff) | |
| parent | 0bfeae752f63dae4afa26fe587d4b628d2455d0e (diff) | |
Merge pull request #110 from Borber/main
fix: copy button position
Diffstat (limited to 'static')
| -rw-r--r-- | static/js/copy-code.js | 35 | 
1 files changed, 19 insertions, 16 deletions
| diff --git a/static/js/copy-code.js b/static/js/copy-code.js index 2de3cda..ee7cdc7 100644 --- a/static/js/copy-code.js +++ b/static/js/copy-code.js @@ -1,34 +1,37 @@  document.addEventListener("DOMContentLoaded", function () { -  const codeBlocks = document.querySelectorAll("pre"); +  const codeBlocks = document.querySelectorAll(".highlight")    codeBlocks.forEach((codeBlock) => { -    if (codeBlock.className == "mermaid") return; -    const copyButton = document.createElement("button"); -    copyButton.className = "copy-code-button"; -    copyButton.textContent = "copy"; +    if (codeBlock.className == "mermaid") return +    const copyButton = document.createElement("button") +    copyButton.className = "copy-code-button" +    copyButton.textContent = "copy" +    const copyButtonContainer = document.createElement("div") +    copyButtonContainer.className = "copy-code-container" +    copyButtonContainer.appendChild(copyButton)      // Insert the button inside the <pre> block -    codeBlock.appendChild(copyButton); +    codeBlock.appendChild(copyButton)      copyButton.addEventListener("click", function () { -      const code = codeBlock.querySelector("code"); +      const code = codeBlock.querySelector("code")        // Get the code content -      const textToCopy = code.textContent || code.innerText; +      const textToCopy = code.textContent || code.innerText        // Use the Clipboard API to copy the text        navigator.clipboard          .writeText(textToCopy)          .then(() => {            // Change button text to "Copied" -          copyButton.textContent = "copied"; +          copyButton.textContent = "copied"            setTimeout(() => { -            copyButton.textContent = "copy"; -          }, 2000); // Reset the button text after 2 seconds +            copyButton.textContent = "copy" +          }, 2000) // Reset the button text after 2 seconds          })          .catch((err) => { -          console.error("Unable to copy text:", err); -        }); -    }); -  }); -}); +          console.error("Unable to copy text:", err) +        }) +    }) +  }) +}) | 
