11<script setup lang="ts">
22import type { HTMLAttributes } from ' vue'
33import { cn } from ' @/utils'
4- import { Dialog , DialogContent } from ' ./dialog '
4+ import Preview from ' ./preview.vue '
55
66defineOptions ({
77 name: ' FaImagePreview' ,
@@ -21,13 +21,6 @@ const isLoading = ref(true)
2121const isError = ref (false )
2222const isOpen = ref (false )
2323
24- // 图片操作相关状态
25- const scale = ref (1 )
26- const rotate = ref (0 )
27- const position = ref ({ x: 0 , y: 0 })
28- const isDragging = ref (false )
29- const dragStart = ref ({ x: 0 , y: 0 })
30-
3124function handleLoad() {
3225 isLoading .value = false
3326 emits (' load' )
@@ -44,87 +37,6 @@ function handleClick() {
4437 isOpen .value = true
4538 }
4639}
47-
48- // 图片操作函数
49- function handleZoomIn() {
50- scale .value = Math .min (scale .value + 0.25 , 3 )
51- }
52-
53- function handleZoomOut() {
54- scale .value = Math .max (scale .value - 0.25 , 0.5 )
55- }
56-
57- function handleOriginalSize() {
58- scale .value = 1
59- }
60-
61- function handleRotateLeft() {
62- rotate .value = rotate .value - 90
63- }
64-
65- function handleRotateRight() {
66- rotate .value = rotate .value + 90
67- }
68-
69- // 处理滚轮缩放
70- function handleWheel(e : WheelEvent ) {
71- e .preventDefault ()
72- if (e .deltaY < 0 ) {
73- handleZoomIn ()
74- }
75- else {
76- handleZoomOut ()
77- }
78- }
79-
80- // 处理鼠标按下事件
81- function handleMouseDown(e : MouseEvent ) {
82- isDragging .value = true
83- dragStart .value = {
84- x: e .clientX - position .value .x ,
85- y: e .clientY - position .value .y ,
86- }
87- }
88-
89- // 处理鼠标移动事件
90- function handleMouseMove(e : MouseEvent ) {
91- if (! isDragging .value ) {
92- return
93- }
94- position .value = {
95- x: e .clientX - dragStart .value .x ,
96- y: e .clientY - dragStart .value .y ,
97- }
98- }
99-
100- // 处理鼠标松开事件
101- function handleMouseUp() {
102- isDragging .value = false
103- }
104-
105- // 重置图片状态
106- function resetImageState() {
107- scale .value = 1
108- rotate .value = 0
109- position .value = { x: 0 , y: 0 }
110- }
111-
112- // 监听鼠标事件
113- onMounted (() => {
114- window .addEventListener (' mousemove' , handleMouseMove )
115- window .addEventListener (' mouseup' , handleMouseUp )
116- })
117-
118- onUnmounted (() => {
119- window .removeEventListener (' mousemove' , handleMouseMove )
120- window .removeEventListener (' mouseup' , handleMouseUp )
121- })
122-
123- function handleAnimationEnd() {
124- if (! isOpen .value ) {
125- resetImageState ()
126- }
127- }
12840 </script >
12941
13042<template >
@@ -148,40 +60,6 @@ function handleAnimationEnd() {
14860 <FaIcon name =" i-ph:image-broken-duotone" class =" size-8 text-secondary-foreground/50" />
14961 </slot >
15062 </div >
151- <Dialog v-model:open =" isOpen" >
152- <DialogContent class =" size-full" @animation-end =" handleAnimationEnd" >
153- <div class =" relative size-full" @wheel =" handleWheel" >
154- <img
155- :src =" src"
156- class =" mx-auto max-h-full max-w-full object-contain"
157- :class =" {
158- 'transition-all duration-300': !isDragging,
159- }"
160- :style =" {
161- transform: `translate(${position.x}px, ${position.y}px) scale(${scale}) rotate(${rotate}deg)`,
162- cursor: isDragging ? 'grabbing' : 'grab',
163- }"
164- @mousedown.prevent =" handleMouseDown"
165- >
166- <FaButtonGroup class =" absolute bottom-4 left-1/2 scale-125 -translate-x-1/2" >
167- <FaButton variant =" outline" size =" icon" class =" border-none bg-muted/50" @click =" handleZoomIn" >
168- <FaIcon name =" i-carbon:zoom-in" class =" size-5" />
169- </FaButton >
170- <FaButton variant =" outline" size =" icon" class =" border-none bg-muted/50" @click =" handleZoomOut" >
171- <FaIcon name =" i-carbon:zoom-out" class =" size-5" />
172- </FaButton >
173- <FaButton variant =" outline" size =" icon" class =" border-none bg-muted/50" @click =" handleOriginalSize" >
174- <FaIcon name =" i-lucide:maximize" class =" size-5" />
175- </FaButton >
176- <FaButton variant =" outline" size =" icon" class =" border-none bg-muted/50" @click =" handleRotateLeft" >
177- <FaIcon name =" i-carbon:rotate" class =" size-5" />
178- </FaButton >
179- <FaButton variant =" outline" size =" icon" class =" border-none bg-muted/50" @click =" handleRotateRight" >
180- <FaIcon name =" i-carbon:rotate-180" class =" size-5" />
181- </FaButton >
182- </FaButtonGroup >
183- </div >
184- </DialogContent >
185- </Dialog >
63+ <Preview v-model =" isOpen" :src />
18664 </div >
18765</template >
0 commit comments