Skip to content

Commit f5bda00

Browse files
committed
FaImagePreview 组件函数式调用增加多图支持
1 parent 60a3c4d commit f5bda00

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

src/ui/components/FaImagePreview/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import Preview from './preview.vue'
22

33
export function useFaImagePreview() {
4-
function open(src: string) {
4+
function open(src: string | string[], index = 0) {
55
const container = document.createElement('div')
66
const app = createApp({
77
render() {
88
return h(Preview, {
99
modelValue: true,
1010
src,
11+
index,
1112
})
1213
},
1314
})

src/ui/components/FaImagePreview/preview.vue

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
<script setup lang="ts">
22
import { Dialog, DialogContent } from './dialog'
33
4-
defineProps<{
5-
src: string
4+
const props = defineProps<{
5+
src: string | string[]
6+
index?: number
67
}>()
78
89
const isOpen = defineModel<boolean>()
910
11+
// 多图操作
12+
const index = ref(props.index ?? 0)
13+
const srcList = computed(() => {
14+
if (Array.isArray(props.src)) {
15+
return props.src
16+
}
17+
return [props.src]
18+
})
19+
function handlePrev() {
20+
index.value = index.value - 1
21+
if (index.value < 0) {
22+
index.value = srcList.value.length - 1
23+
}
24+
}
25+
function handleNext() {
26+
index.value = index.value + 1
27+
if (index.value >= srcList.value.length) {
28+
index.value = 0
29+
}
30+
}
31+
watch(index, resetImageState)
32+
1033
// 图片操作相关状态
1134
const scale = ref(1)
1235
const rotate = ref(0)
@@ -101,7 +124,7 @@ function handleAnimationEnd() {
101124
<DialogContent class="size-full" @animation-end="handleAnimationEnd">
102125
<div class="relative size-full flex-center" @wheel="handleWheel">
103126
<img
104-
:src="src"
127+
:src="srcList[index]"
105128
class="mx-auto max-h-full max-w-full object-contain"
106129
:class="{
107130
'transition-all duration-300': !isDragging,
@@ -112,6 +135,17 @@ function handleAnimationEnd() {
112135
}"
113136
@mousedown.prevent="handleMouseDown"
114137
>
138+
<template v-if="srcList.length > 1">
139+
<div class="absolute bottom-20 left-1/2 text-sm text-muted-foreground -translate-x-1/2">
140+
{{ index + 1 }} / {{ srcList.length }}
141+
</div>
142+
<FaButton variant="ghost" size="icon" class="absolute left-4 top-1/2 rounded-full bg-muted/50 -translate-y-1/2" @click="handlePrev">
143+
<FaIcon name="i-lucide:chevron-left" class="size-6" />
144+
</FaButton>
145+
<FaButton variant="ghost" size="icon" class="absolute right-4 top-1/2 rounded-full bg-muted/50 -translate-y-1/2" @click="handleNext">
146+
<FaIcon name="i-lucide:chevron-right" class="size-6" />
147+
</FaButton>
148+
</template>
115149
<FaButtonGroup class="absolute bottom-4 left-1/2 scale-125 -translate-x-1/2">
116150
<FaButton variant="outline" size="icon" class="border-none bg-muted/50" @click="handleZoomIn">
117151
<FaIcon name="i-carbon:zoom-in" class="size-5" />

src/views/component_built_in_example/imagepreview.vue

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,24 @@ meta:
66
<script setup lang="ts">
77
import { useFaImagePreview } from '@/ui/components/FaImagePreview'
88
9-
function open() {
10-
useFaImagePreview().open('https://fantastic-admin.hurui.me/logo.svg')
9+
const { open } = useFaImagePreview()
10+
11+
function openSingle() {
12+
open('https://fantastic-admin.hurui.me/logo.svg')
13+
}
14+
15+
function openMulti() {
16+
open([
17+
'https://fantastic-admin.hurui.me/logo.svg',
18+
'https://fantastic-mobile.hurui.me/logo.png',
19+
])
20+
}
21+
22+
function openMulti2() {
23+
open([
24+
'https://fantastic-admin.hurui.me/logo.svg',
25+
'https://fantastic-mobile.hurui.me/logo.png',
26+
], 1)
1127
}
1228
</script>
1329

@@ -33,9 +49,17 @@ function open() {
3349
</div>
3450
</FaPageMain>
3551
<FaPageMain title="函数式调用">
36-
<FaButton @click="open">
37-
预览
38-
</FaButton>
52+
<div class="space-x-4">
53+
<FaButton @click="openSingle">
54+
预览单张
55+
</FaButton>
56+
<FaButton @click="openMulti">
57+
预览多张
58+
</FaButton>
59+
<FaButton @click="openMulti2">
60+
预览多张(初始预览第2张)
61+
</FaButton>
62+
</div>
3963
</FaPageMain>
4064
</div>
4165
</template>

0 commit comments

Comments
 (0)