mirror of
https://github.com/ClovertaTheTrilobita/SanYeCao-blog.git
synced 2026-07-03 15:41:26 +00:00
commit
c69e72cd2f
4 changed files with 218 additions and 0 deletions
118
src/components/Posts/Dialog.astro
Normal file
118
src/components/Posts/Dialog.astro
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
<dialog id="image-preview-dialog" class="image-preview-dialog">
|
||||
<button class="image-preview-close" aria-label="关闭图片预览">×</button>
|
||||
|
||||
<img id="image-preview-img" alt="" />
|
||||
|
||||
<a
|
||||
id="image-preview-download"
|
||||
class="image-preview-download"
|
||||
href="#"
|
||||
aria-label="下载图片"
|
||||
title="下载图片"
|
||||
>
|
||||
<svg
|
||||
width="26"
|
||||
height="26"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path
|
||||
d="M12 3v12m0 0 5-5m-5 5-5-5M5 21h14"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"></path>
|
||||
</svg>
|
||||
</a>
|
||||
</dialog>
|
||||
<script is:inline>
|
||||
function initImagePreview() {
|
||||
const dialog = document.getElementById("image-preview-dialog");
|
||||
const previewImg = document.getElementById("image-preview-img");
|
||||
const closeBtn = dialog?.querySelector(".image-preview-close");
|
||||
const downloadBtn = document.getElementById("image-preview-download");
|
||||
|
||||
if (!dialog || !previewImg || !closeBtn || !downloadBtn) return;
|
||||
|
||||
let currentImageUrl = "";
|
||||
|
||||
function getFileNameFromUrl(url) {
|
||||
try {
|
||||
const pathname = new URL(url, window.location.href).pathname;
|
||||
return pathname.split("/").pop() || "image";
|
||||
} catch {
|
||||
return "image";
|
||||
}
|
||||
}
|
||||
|
||||
async function downloadImage(url) {
|
||||
const response = await fetch(url, {
|
||||
mode: "cors",
|
||||
credentials: "omit",
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to fetch image");
|
||||
}
|
||||
|
||||
const blob = await response.blob();
|
||||
const blobUrl = URL.createObjectURL(blob);
|
||||
|
||||
const a = document.createElement("a");
|
||||
a.href = blobUrl;
|
||||
a.download = getFileNameFromUrl(url);
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
|
||||
a.remove();
|
||||
URL.revokeObjectURL(blobUrl);
|
||||
}
|
||||
|
||||
document.querySelectorAll("article img").forEach((img) => {
|
||||
if (img.dataset.previewBound === "true") return;
|
||||
img.dataset.previewBound = "true";
|
||||
|
||||
img.addEventListener("click", () => {
|
||||
const imageUrl = img.currentSrc || img.src;
|
||||
|
||||
currentImageUrl = imageUrl;
|
||||
previewImg.src = imageUrl;
|
||||
previewImg.alt = img.alt || "";
|
||||
|
||||
dialog.showModal();
|
||||
});
|
||||
});
|
||||
|
||||
downloadBtn.addEventListener("click", async (event) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
|
||||
if (!currentImageUrl) return;
|
||||
|
||||
try {
|
||||
await downloadImage(currentImageUrl);
|
||||
} catch (error) {
|
||||
// 如果 fetch 因为跨域失败,就退回到直接打开图片
|
||||
window.open(currentImageUrl, "_blank", "noopener,noreferrer");
|
||||
}
|
||||
});
|
||||
|
||||
function closePreview() {
|
||||
dialog.close();
|
||||
previewImg.removeAttribute("src");
|
||||
currentImageUrl = "";
|
||||
}
|
||||
|
||||
closeBtn.addEventListener("click", closePreview);
|
||||
|
||||
dialog.addEventListener("click", (event) => {
|
||||
if (event.target === dialog) {
|
||||
closePreview();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", initImagePreview);
|
||||
document.addEventListener("astro:page-load", initImagePreview);
|
||||
</script>
|
||||
|
|
@ -3,6 +3,7 @@ import BaseLayout from "./BaseLayout.astro";
|
|||
import Remark42Embed from "@/components/Remark42Embed.astro";
|
||||
import PostNav from "@/components/Posts/PostNav.astro";
|
||||
import PostMenu from "@/components/Posts/PostMenu.astro";
|
||||
import Dialog from "@/components/Posts/Dialog.astro";
|
||||
import { fade } from "astro:transitions";
|
||||
import { getTranslations } from "@/i18n";
|
||||
import "@/styles/global.css";
|
||||
|
|
@ -81,6 +82,8 @@ const t = getTranslations(lang);
|
|||
<slot />
|
||||
</div>
|
||||
|
||||
<Dialog />
|
||||
|
||||
<PostNav post={post} lang={lang} />
|
||||
|
||||
<h2>{comments}</h2>
|
||||
|
|
|
|||
96
src/styles/dialog.css
Normal file
96
src/styles/dialog.css
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
.image-preview-dialog {
|
||||
padding: 0;
|
||||
border: none;
|
||||
background: transparent;
|
||||
max-width: min(96vw, 1200px);
|
||||
max-height: 96vh;
|
||||
}
|
||||
|
||||
.image-preview-dialog::backdrop {
|
||||
background: rgba(0, 0, 0, 0.72);
|
||||
backdrop-filter: blur(6px);
|
||||
}
|
||||
|
||||
.image-preview-dialog img {
|
||||
display: block;
|
||||
max-width: 96vw;
|
||||
max-height: 90vh;
|
||||
object-fit: contain;
|
||||
border-radius: 16px;
|
||||
box-shadow: 0 20px 80px rgba(0, 0, 0, 0.45);
|
||||
}
|
||||
|
||||
.image-preview-close:focus {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.image-preview-close:focus-visible {
|
||||
outline: 2px solid rgba(125, 162, 255, 0.9);
|
||||
outline-offset: 4px;
|
||||
}
|
||||
|
||||
.image-preview-close {
|
||||
position: fixed;
|
||||
top: 1.25rem;
|
||||
right: 1.25rem;
|
||||
z-index: 1;
|
||||
width: 2.4rem;
|
||||
height: 2.4rem;
|
||||
border: none;
|
||||
border-radius: 999px;
|
||||
background: transparent;
|
||||
color: var(--text-color-dark);
|
||||
font-size: 2.6rem;
|
||||
line-height: 1;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
/* .image-preview-close:hover {
|
||||
background: rgba(0, 0, 0, 0.75);
|
||||
} */
|
||||
|
||||
/* 给文章图片一个可点击提示 */
|
||||
.prose img,
|
||||
.markdown-body img,
|
||||
article img {
|
||||
cursor: zoom-in;
|
||||
}
|
||||
|
||||
.image-preview-download {
|
||||
position: fixed;
|
||||
left: 50%;
|
||||
bottom: max(1.5rem, env(safe-area-inset-bottom));
|
||||
transform: translateX(-50%);
|
||||
|
||||
z-index: 2;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
|
||||
color: white;
|
||||
background: transparent;
|
||||
border: none;
|
||||
padding: 0.4rem;
|
||||
cursor: pointer;
|
||||
text-decoration: none;
|
||||
opacity: 0.88;
|
||||
|
||||
transition:
|
||||
opacity 0.2s ease,
|
||||
transform 0.2s ease;
|
||||
}
|
||||
|
||||
.image-preview-download:hover {
|
||||
opacity: 1;
|
||||
transform: translateX(-50%) translateY(-2px);
|
||||
}
|
||||
|
||||
.image-preview-download:focus {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.image-preview-download:focus-visible {
|
||||
outline: 2px solid rgba(125, 162, 255, 0.9);
|
||||
outline-offset: 4px;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@
|
|||
@import url("https://unpkg.com/@fontsource/maple-mono@5.2.6/700.css");
|
||||
@import "./latest-comments.css";
|
||||
@import "./variables.css";
|
||||
@import "./dialog.css";
|
||||
|
||||
pre {
|
||||
padding: 1rem;
|
||||
|
|
|
|||
Loading…
Reference in a new issue