mirror of
https://github.com/ClovertaTheTrilobita/SanYeCao-blog.git
synced 2026-09-04 20:55:41 +00:00
fix copy button layout
This commit is contained in:
parent
cbad40b5cd
commit
08c4f13d54
2 changed files with 22 additions and 5 deletions
|
|
@ -45,11 +45,24 @@ const {
|
|||
<script is:inline>
|
||||
function addCodeCopyButtons() {
|
||||
document.querySelectorAll("pre").forEach((pre) => {
|
||||
if (pre.querySelector(":scope > .code-copy-button")) return;
|
||||
|
||||
const code = pre.querySelector("code");
|
||||
if (!code) return;
|
||||
|
||||
/* 已经处理过则跳过 */
|
||||
if (
|
||||
pre.parentElement?.classList.contains("code-block-wrapper")
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* 创建外层容器 */
|
||||
const wrapper = document.createElement("div");
|
||||
wrapper.className = "code-block-wrapper";
|
||||
|
||||
pre.parentNode?.insertBefore(wrapper, pre);
|
||||
wrapper.appendChild(pre);
|
||||
|
||||
/* 创建复制按钮 */
|
||||
const button = document.createElement("button");
|
||||
button.className = "code-copy-button";
|
||||
button.type = "button";
|
||||
|
|
@ -63,6 +76,7 @@ const {
|
|||
icon.setAttribute("aria-hidden", "true");
|
||||
|
||||
button.appendChild(icon);
|
||||
wrapper.appendChild(button);
|
||||
|
||||
button.addEventListener("click", async () => {
|
||||
const text = code.textContent ?? "";
|
||||
|
|
@ -74,6 +88,7 @@ const {
|
|||
textarea.value = text;
|
||||
textarea.style.position = "fixed";
|
||||
textarea.style.opacity = "0";
|
||||
textarea.style.pointerEvents = "none";
|
||||
|
||||
document.body.appendChild(textarea);
|
||||
textarea.select();
|
||||
|
|
@ -93,8 +108,6 @@ const {
|
|||
button.classList.remove("copied");
|
||||
}, 1500);
|
||||
});
|
||||
|
||||
pre.appendChild(button);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,8 @@
|
|||
pre {
|
||||
.code-block-wrapper {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.code-block-wrapper pre {
|
||||
padding: 1rem;
|
||||
border-radius: 3px;
|
||||
overflow-x: auto;
|
||||
|
|
|
|||
Loading…
Reference in a new issue