Skip to content

Commit 873a2bd

Browse files
committed
feat: 🚀 文章页新增多图片预览功能,部分配置项命名修改
1 parent 6d5d689 commit 873a2bd

File tree

9 files changed

+56
-28
lines changed

9 files changed

+56
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
11
<script setup lang="ts" name="ArticleImagePreview">
22
import { useDesign } from "../hooks";
3+
import { onMounted, onUnmounted, reactive, ref } from "vue";
4+
import { createImageViewer } from "./ImageViewer";
35
46
const { getPrefixClass } = useDesign();
57
const prefixClass = getPrefixClass("articleImagePreview");
6-
</script>
78
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+
}
1126
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>

vitepress-theme-tk/src/components/FriendLinkCard.vue

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ const {
1515
limit = 4,
1616
title = `${friendLinkSvg}友情链接`,
1717
autoScroll = false,
18-
scrollTimeOut = 2500,
18+
scrollSpeed = 2500,
1919
autoPage = false,
20-
pageTimeOut = 4000,
20+
pageSpeed = 4000,
2121
} = { ...theme.friendLink, ...frontmatter.tk?.friendLink };
2222
2323
// 使用上下滚动功能
24-
const { visibleData, startAutoScroll, stopAutoScroll } = useScrollData(list, 5, scrollTimeOut);
24+
const { visibleData, startAutoScroll, stopAutoScroll } = useScrollData(list, 5, scrollSpeed);
2525
2626
const pageNum = ref(1);
2727
// 友情链接渲染数据
@@ -64,7 +64,7 @@ const getLiStyle = (index: number) => {
6464
:total="list.length"
6565
:title
6666
:autoPage
67-
:pageTimeOut
67+
:pageSpeed
6868
:class="prefixClass"
6969
>
7070
<template #default="{ transitionName, startAutoPage, closeAutoPage }">

vitepress-theme-tk/src/components/HomeCard.vue

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface HomeCardProps {
1414
pageSize?: number;
1515
total?: number;
1616
autoPage?: boolean;
17-
pageTimeOut?: number;
17+
pageSpeed?: number;
1818
}
1919
2020
const {
@@ -24,7 +24,7 @@ const {
2424
pageSize = 4,
2525
total,
2626
autoPage = false,
27-
pageTimeOut = 4000,
27+
pageSpeed = 4000,
2828
} = defineProps<HomeCardProps>();
2929
3030
const emit = defineEmits<{ pagination: [to: number, "pre" | "next"] }>();
@@ -63,10 +63,10 @@ let timer: NodeJS.Timeout;
6363
const startAutoPage = () => {
6464
// 先关闭自动翻页
6565
closeAutoPage();
66-
if (pageTimeOut > 0) {
66+
if (pageSpeed > 0) {
6767
timer = setTimeout(() => {
6868
pagination(1, "next");
69-
}, pageTimeOut);
69+
}, pageSpeed);
7070
}
7171
};
7272
/**

vitepress-theme-tk/src/components/HomeCategoryCard.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const {
2323
homeTitle = `${categorySvg}文章分类`,
2424
limit = 5,
2525
autoPage = false,
26-
pageTimeOut = 4000,
26+
pageSpeed = 4000,
2727
} = { ...theme.category, ...frontmatter.tk?.category };
2828
2929
// 当前显示的分类,如果是在分类页,则显示所有分类,如果在首页,则分页显示
@@ -57,7 +57,7 @@ const itemRefs = ref<HTMLLIElement[]>([]);
5757
:title="categoriesPage ? pageTitle : homeTitle"
5858
title-link="/categories"
5959
:autoPage
60-
:pageTimeOut
60+
:pageSpeed
6161
:class="prefixClass"
6262
>
6363
<template #default="{ transitionName }">

vitepress-theme-tk/src/components/HomePostItem.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script setup lang="ts" name="HomePostItem">
22
import { computed } from "vue";
33
import { useDesign } from "../hooks";
4+
import { withBase } from "vitepress";
45
import { KtContentData } from "../data/types";
56
import { createImageViewer } from "./ImageViewer";
67
import { formatDate, isArray } from "../helper";
@@ -17,8 +18,9 @@ const { frontmatter } = useUnrefData();
1718
1819
const postFrontmatter = computed(() => props.post.frontmatter);
1920
const getImgUrl = (imgUrl: string | string[]) => {
20-
if (isArray(imgUrl)) return imgUrl[0];
21-
return imgUrl;
21+
// 页面只展示一个图片
22+
if (isArray(imgUrl)) return withBase(imgUrl[0]);
23+
return withBase(imgUrl);
2224
};
2325
2426
/**

vitepress-theme-tk/src/components/HomeTagCard.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const {
2323
homeTitle = `${tagSvg}热门标签`,
2424
limit = 21,
2525
autoPage = false,
26-
pageTimeOut = 4000,
26+
pageSpeed = 4000,
2727
} = { ...theme.tag, ...frontmatter.tk?.tag };
2828
2929
// 当前显示的标签,如果是在标签页,则显示所有标签,如果在首页,则显示前 limit 个标签
@@ -64,7 +64,7 @@ const itemRefs = ref<HTMLLIElement[]>([]);
6464
:title="tagsPage ? pageTitle : homeTitle"
6565
title-link="/tags"
6666
:autoPage
67-
:pageTimeOut
67+
:pageSpeed
6868
:class="prefixClass"
6969
>
7070
<template #default="{ transitionName }">

vitepress-theme-tk/src/components/TopArticleCard.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const {
1616
limit = 4,
1717
title = `${TopArticleSvg}精选文章`,
1818
autoPage = false,
19-
pageTimeOut = 4000,
19+
pageSpeed = 4000,
2020
} = { ...theme.topArticle, ...frontmatter.tk?.topArticle };
2121
2222
const TopArticleList =
@@ -48,7 +48,7 @@ const getStyle = (num: number, index: number) => {
4848
:total="TopArticleList.length"
4949
:title
5050
:autoPage
51-
:pageTimeOut
51+
:pageSpeed
5252
:class="prefixClass"
5353
>
5454
<template #default="{ transitionName }">

vitepress-theme-tk/src/config/types.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export interface KtThemeConfig {
199199
* 翻页间隔时间,单位:毫秒。autoPage 为 true 时生效
200200
* @default 4000 (4秒)
201201
*/
202-
pageTimeOut?: number;
202+
pageSpeed?: number;
203203
};
204204
tag?: {
205205
/**
@@ -231,7 +231,7 @@ export interface KtThemeConfig {
231231
* 翻页间隔时间,单位:毫秒。autoPage 为 true 时生效
232232
* @default 4000 (4秒)
233233
*/
234-
pageTimeOut?: number;
234+
pageSpeed?: number;
235235
};
236236
topArticle?: {
237237
/**
@@ -258,7 +258,7 @@ export interface KtThemeConfig {
258258
* 翻页间隔时间,单位:毫秒。autoPage 为 true 时生效
259259
* @default 4000 (4秒)
260260
*/
261-
pageTimeOut?: number;
261+
pageSpeed?: number;
262262
};
263263
friendLink?: {
264264
/**
@@ -306,7 +306,7 @@ export interface KtThemeConfig {
306306
* 滚动间隔时间,单位:毫秒。autoScroll 为 true 时生效
307307
* @default 2500 (2.5秒)
308308
*/
309-
scrollTimeOut?: number;
309+
scrollSpeed?: number;
310310
/**
311311
* 是否自动翻页
312312
* @default false
@@ -316,7 +316,7 @@ export interface KtThemeConfig {
316316
* 翻页间隔时间,单位:毫秒。autoPage 为 true 时生效
317317
* @default 4000 (4秒)
318318
*/
319-
pageTimeOut?: number;
319+
pageSpeed?: number;
320320
};
321321
docAnalysis?: {
322322
/**

vitepress-theme-tk/src/styles/components/articleImagePreview.scss

Whitespace-only changes.

0 commit comments

Comments
 (0)