mirror of
https://github.com/ClovertaTheTrilobita/SanYeCao-blog.git
synced 2026-09-04 20:55:41 +00:00
added copy button
This commit is contained in:
parent
045b9f5740
commit
c07946bf95
6 changed files with 182 additions and 23 deletions
18
public/images/copy-svgrepo-com.svg
Normal file
18
public/images/copy-svgrepo-com.svg
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
<!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->
|
||||
<svg fill="#000000" height="800px" width="800px" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
|
||||
viewBox="0 0 460 460" xml:space="preserve">
|
||||
<g>
|
||||
<g>
|
||||
<g>
|
||||
<path d="M425.934,0H171.662c-18.122,0-32.864,14.743-32.864,32.864v77.134h30V32.864c0-1.579,1.285-2.864,2.864-2.864h254.272
|
||||
c1.579,0,2.864,1.285,2.864,2.864v254.272c0,1.58-1.285,2.865-2.864,2.865h-74.729v30h74.729
|
||||
c18.121,0,32.864-14.743,32.864-32.865V32.864C458.797,14.743,444.055,0,425.934,0z"/>
|
||||
<path d="M288.339,139.998H34.068c-18.122,0-32.865,14.743-32.865,32.865v254.272C1.204,445.257,15.946,460,34.068,460h254.272
|
||||
c18.122,0,32.865-14.743,32.865-32.864V172.863C321.206,154.741,306.461,139.998,288.339,139.998z M288.341,430H34.068
|
||||
c-1.58,0-2.865-1.285-2.865-2.864V172.863c0-1.58,1.285-2.865,2.865-2.865h254.272c1.58,0,2.865,1.285,2.865,2.865v254.273h0.001
|
||||
C291.206,428.715,289.92,430,288.341,430z"/>
|
||||
</g>
|
||||
</g>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 1.1 KiB |
12
public/images/correct-signal-svgrepo-com.svg
Normal file
12
public/images/correct-signal-svgrepo-com.svg
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
<!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
||||
<svg fill="#000000" version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
|
||||
width="800px" height="800px" viewBox="0 0 335.765 335.765"
|
||||
xml:space="preserve">
|
||||
<g>
|
||||
<g>
|
||||
<polygon points="311.757,41.803 107.573,245.96 23.986,162.364 0,186.393 107.573,293.962 335.765,65.795 "/>
|
||||
</g>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 571 B |
|
|
@ -64,6 +64,11 @@ avatar: 'https://s2.loli.net/2025/11/22/tiDKuzdqycx1v9B.png'
|
|||
border-radius: 3px;
|
||||
padding: 0rem;
|
||||
overflow-x: auto;
|
||||
/* color: var(--text-color); */
|
||||
}
|
||||
|
||||
:global(.dark) .friend-code-block {
|
||||
background: rgba(127, 127, 127, 0.12);
|
||||
}
|
||||
|
||||
.friend-code-block pre {
|
||||
|
|
|
|||
|
|
@ -42,6 +42,65 @@ const {
|
|||
<title>{pageTitle}</title>
|
||||
</head>
|
||||
<FloatingActions />
|
||||
<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;
|
||||
|
||||
const button = document.createElement("button");
|
||||
button.className = "code-copy-button";
|
||||
button.type = "button";
|
||||
button.setAttribute("aria-label", "复制代码");
|
||||
button.title = "复制代码";
|
||||
|
||||
const icon = document.createElement("img");
|
||||
icon.className = "code-copy-icon";
|
||||
icon.src = "/images/copy-svgrepo-com.svg";
|
||||
icon.alt = "";
|
||||
icon.setAttribute("aria-hidden", "true");
|
||||
|
||||
button.appendChild(icon);
|
||||
|
||||
button.addEventListener("click", async () => {
|
||||
const text = code.textContent ?? "";
|
||||
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
} catch {
|
||||
const textarea = document.createElement("textarea");
|
||||
textarea.value = text;
|
||||
textarea.style.position = "fixed";
|
||||
textarea.style.opacity = "0";
|
||||
|
||||
document.body.appendChild(textarea);
|
||||
textarea.select();
|
||||
document.execCommand("copy");
|
||||
textarea.remove();
|
||||
}
|
||||
|
||||
icon.src = "/images/correct-signal-svgrepo-com.svg";
|
||||
button.setAttribute("aria-label", "已复制");
|
||||
button.title = "已复制";
|
||||
button.classList.add("copied");
|
||||
|
||||
window.setTimeout(() => {
|
||||
icon.src = "/images/copy-svgrepo-com.svg";
|
||||
button.setAttribute("aria-label", "复制代码");
|
||||
button.title = "复制代码";
|
||||
button.classList.remove("copied");
|
||||
}, 1500);
|
||||
});
|
||||
|
||||
pre.appendChild(button);
|
||||
});
|
||||
}
|
||||
|
||||
addCodeCopyButtons();
|
||||
document.addEventListener("astro:page-load", addCodeCopyButtons);
|
||||
</script>
|
||||
|
||||
<body>
|
||||
<Header />
|
||||
|
|
|
|||
86
src/styles/code.css
Normal file
86
src/styles/code.css
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
pre {
|
||||
padding: 1rem;
|
||||
border-radius: 3px;
|
||||
overflow-x: auto;
|
||||
line-height: 1.6;
|
||||
margin: 1rem 0;
|
||||
position: relative;
|
||||
|
||||
}
|
||||
|
||||
/* pre:has(> .code-copy-button) {
|
||||
padding-top: 2.75rem;
|
||||
} */
|
||||
|
||||
code {
|
||||
font-family: "Maple Mono", monospace;
|
||||
font-size: 0.96rem;
|
||||
}
|
||||
|
||||
/* 行内代码 */
|
||||
p code,
|
||||
li code,
|
||||
blockquote code,
|
||||
td code {
|
||||
padding: 0.15em 0.4em;
|
||||
border-radius: 0.25rem;
|
||||
background: rgba(127, 127, 127, 0.12);
|
||||
}
|
||||
|
||||
.code-copy-button {
|
||||
position: absolute;
|
||||
top: 0.5rem;
|
||||
right: 0.5rem;
|
||||
z-index: 2;
|
||||
|
||||
display: grid;
|
||||
place-items: center;
|
||||
width: 2rem;
|
||||
height: 2rem;
|
||||
padding: 0.35rem;
|
||||
|
||||
border-top: 1px dotted var(--deep-blue);
|
||||
border-left: 1px dotted var(--deep-blue);
|
||||
border-right: 2px solid var(--deep-blue);
|
||||
border-bottom: 2px solid var(--deep-blue);
|
||||
border-radius: 3px;
|
||||
|
||||
background: rgba(250, 250, 250, 0.7);
|
||||
cursor: pointer;
|
||||
|
||||
transition:
|
||||
transform 0.15s ease,
|
||||
background-color 0.15s ease,
|
||||
box-shadow 0.15s ease;
|
||||
}
|
||||
|
||||
.code-copy-icon {
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: contain;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.code-copy-button:hover {
|
||||
background: rgb(250, 250, 250, 0.9);
|
||||
transform: translate(-1px, -1px);
|
||||
box-shadow: 3px 4px 8px rgb(0 0 0 / 18%);
|
||||
}
|
||||
|
||||
.code-copy-button:active {
|
||||
transform: translate(0, 0);
|
||||
box-shadow: none;
|
||||
}
|
||||
|
||||
.code-copy-button.copied {
|
||||
background: #dcebd5;
|
||||
}
|
||||
|
||||
/* .dark .code-copy-button {
|
||||
border-color: #9ecbff;
|
||||
}
|
||||
|
||||
.dark .code-copy-button:hover {
|
||||
box-shadow: 3px 4px 8px rgb(0 0 0 / 35%);
|
||||
} */
|
||||
|
|
@ -5,29 +5,8 @@
|
|||
@import "./variables.css";
|
||||
@import "./dialog.css";
|
||||
@import "./posts.css";
|
||||
@import "./code.css";
|
||||
|
||||
pre {
|
||||
padding: 1rem;
|
||||
border-radius: 0.5rem;
|
||||
overflow-x: auto;
|
||||
line-height: 1.6;
|
||||
margin: 1rem 0;
|
||||
}
|
||||
|
||||
code {
|
||||
font-family: "Maple Mono", monospace;
|
||||
font-size: 0.96rem;
|
||||
}
|
||||
|
||||
/* 行内代码 */
|
||||
p code,
|
||||
li code,
|
||||
blockquote code,
|
||||
td code {
|
||||
padding: 0.15em 0.4em;
|
||||
border-radius: 0.25rem;
|
||||
background: rgba(127, 127, 127, 0.12);
|
||||
}
|
||||
|
||||
article svg.flowchart,
|
||||
article svg[class*="flowchart"],
|
||||
|
|
@ -162,4 +141,4 @@ img {
|
|||
max-width: 100%;
|
||||
height: auto;
|
||||
display: block;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue