|
1 | 1 | <script setup lang="ts" name="ArticleImagePreview">
|
2 | 2 | import { useDesign } from "../hooks";
|
| 3 | +import { onMounted, onUnmounted, reactive, ref } from "vue"; |
| 4 | +import { createImageViewer } from "./ImageViewer"; |
3 | 5 |
|
4 | 6 | const { getPrefixClass } = useDesign();
|
5 | 7 | const prefixClass = getPrefixClass("articleImagePreview");
|
6 |
| -</script> |
7 | 8 |
|
8 |
| -<template> |
9 |
| - <div :class="prefixClass"></div> |
10 |
| -</template> |
| 9 | +const previewImage = (e: Event) => { |
| 10 | + const target = e.target as HTMLElement; |
| 11 | + const currentTarget = e.currentTarget as HTMLElement; |
| 12 | +
|
| 13 | + if (target.tagName.toLowerCase() === "img") { |
| 14 | + const imgDoms = currentTarget.querySelectorAll<HTMLImageElement>(".content-container .main img"); |
| 15 | + const imgs = Array.from(imgDoms); |
| 16 | +
|
| 17 | + const urlList = imgs.map(el => el.src); |
| 18 | + const initialIndex = imgs.findIndex(el => el === target); |
| 19 | + const url = target.getAttribute("src"); |
| 20 | +
|
| 21 | + // 兼容点击文档之外的图片 |
| 22 | + if (initialIndex === -1 && url) { |
| 23 | + urlList.push(url); |
| 24 | + initialIndex = urlList.length - 1; |
| 25 | + } |
11 | 26 |
|
12 |
| -<style lang="scss" scoped> |
13 |
| -@use "../styles/components/articleImagePreview.scss"; |
14 |
| -</style> |
| 27 | + createImageViewer({ urlList, initialIndex, infinite: false }); |
| 28 | + } |
| 29 | +}; |
| 30 | +
|
| 31 | +onMounted(() => { |
| 32 | + const docDomContainer = document.querySelector("#VPContent"); |
| 33 | + docDomContainer?.addEventListener("click", previewImage); |
| 34 | +}); |
| 35 | +
|
| 36 | +onUnmounted(() => { |
| 37 | + const docDomContainer = document.querySelector("#VPContent"); |
| 38 | + docDomContainer?.removeEventListener("click", previewImage); |
| 39 | +}); |
| 40 | +</script> |
0 commit comments