1- import { defineComponent , ref , unref , computed , reactive , watch } from 'vue' ;
2-
3- import { FadeTransition } from '/@/components/Transition/index' ;
1+ import { defineComponent , ref , unref , computed , reactive , watchEffect } from 'vue' ;
42
53import { basicProps } from './props' ;
64import { Props } from './types' ;
@@ -11,9 +9,9 @@ import { CloseOutlined, LeftOutlined, RightOutlined } from '@ant-design/icons-vu
119import resumeSvg from '/@/assets/svg/preview/resume.svg' ;
1210import rotateSvg from '/@/assets/svg/preview/p-rotate.svg' ;
1311import scaleSvg from '/@/assets/svg/preview/scale.svg' ;
14- import unscaleSvg from '/@/assets/svg/preview/unscale.svg' ;
12+ import unScaleSvg from '/@/assets/svg/preview/unscale.svg' ;
1513import loadingSvg from '/@/assets/images/loading.svg' ;
16- import unrotateSvg from '/@/assets/svg/preview/unrotate.svg' ;
14+ import unRotateSvg from '/@/assets/svg/preview/unrotate.svg' ;
1715enum StatueEnum {
1816 LOADING ,
1917 DONE ,
@@ -29,6 +27,7 @@ interface ImgState {
2927 status : StatueEnum ;
3028 moveX : number ;
3129 moveY : number ;
30+ show : boolean ;
3231}
3332
3433const prefixCls = 'img-preview' ;
@@ -46,7 +45,9 @@ export default defineComponent({
4645 currentIndex : 0 ,
4746 moveX : 0 ,
4847 moveY : 0 ,
48+ show : props . show ,
4949 } ) ;
50+
5051 const wrapElRef = ref < HTMLDivElement | null > ( null ) ;
5152 const imgElRef = ref < HTMLImageElement | null > ( null ) ;
5253
@@ -133,16 +134,15 @@ export default defineComponent({
133134 }
134135
135136 // 关闭
136- function handleClose ( ) {
137- const { instance } = props ;
138- if ( instance ) {
139- instance . show = false ;
140- }
137+ function handleClose ( e : MouseEvent ) {
138+ e && e . stopPropagation ( ) ;
139+ imgState . show = false ;
141140 // 移除火狐浏览器下的鼠标滚动事件
142141 document . body . removeEventListener ( 'DOMMouseScroll' , scrollFunc ) ;
143142 // 恢复火狐及Safari浏览器下的图片拖拽
144143 document . ondragstart = null ;
145144 }
145+
146146 // 图片复原
147147 function resume ( ) {
148148 initState ( ) ;
@@ -202,26 +202,15 @@ export default defineComponent({
202202 const { imageList } = props ;
203203 return imageList . length > 1 ;
204204 } ) ;
205- watch (
206- ( ) => props . show ,
207- ( show ) => {
208- if ( show ) {
209- init ( ) ;
210- }
211- } ,
212- {
213- immediate : true ,
205+
206+ watchEffect ( ( ) => {
207+ if ( props . show ) {
208+ init ( ) ;
214209 }
215- ) ;
216- watch (
217- ( ) => props . imageList ,
218- ( ) => {
210+ if ( props . imageList ) {
219211 initState ( ) ;
220- } ,
221- {
222- immediate : true ,
223212 }
224- ) ;
213+ } ) ;
225214
226215 const renderClose = ( ) => {
227216 return (
@@ -247,7 +236,7 @@ export default defineComponent({
247236 return (
248237 < div class = { `${ prefixCls } __controller` } >
249238 < div class = { `${ prefixCls } __controller-item` } onClick = { ( ) => scaleFunc ( - 0.15 ) } >
250- < img src = { unscaleSvg } />
239+ < img src = { unScaleSvg } />
251240 </ div >
252241 < div class = { `${ prefixCls } __controller-item` } onClick = { ( ) => scaleFunc ( 0.15 ) } >
253242 < img src = { scaleSvg } />
@@ -256,7 +245,7 @@ export default defineComponent({
256245 < img src = { resumeSvg } />
257246 </ div >
258247 < div class = { `${ prefixCls } __controller-item` } onClick = { ( ) => rotateFunc ( - 90 ) } >
259- < img src = { unrotateSvg } />
248+ < img src = { unRotateSvg } />
260249 </ div >
261250 < div class = { `${ prefixCls } __controller-item` } onClick = { ( ) => rotateFunc ( 90 ) } >
262251 < img src = { rotateSvg } />
@@ -277,41 +266,32 @@ export default defineComponent({
277266 } ;
278267 return ( ) => {
279268 return (
280- < FadeTransition >
281- { ( ) =>
282- props . show && (
283- < div class = { prefixCls } ref = { wrapElRef } onMouseup = { handleMouseUp } >
284- < div class = { `${ prefixCls } -content` } >
285- < img
286- width = "32"
287- src = { loadingSvg }
288- v-show = { imgState . status === StatueEnum . LOADING }
289- class = { `${ prefixCls } -image` }
290- />
291- < img
292- v-show = { imgState . status === StatueEnum . DONE }
293- style = { unref ( getImageStyle ) }
294- class = { `${ prefixCls } -image` }
295- ref = { imgElRef }
296- src = { imgState . currentUrl }
297- onMousedown = { handleAddMoveListener }
298- />
299- < img
300- width = "32"
301- src = { loadingSvg }
302- v-show = { imgState . status === StatueEnum . LOADING }
303- class = { `${ prefixCls } -image` }
304- />
305- { renderClose ( ) }
306- { renderIndex ( ) }
307- { renderController ( ) }
308- { renderArrow ( 'left' ) }
309- { renderArrow ( 'right' ) }
310- </ div >
311- </ div >
312- )
313- }
314- </ FadeTransition >
269+ imgState . show && (
270+ < div class = { prefixCls } ref = { wrapElRef } onMouseup = { handleMouseUp } >
271+ < div class = { `${ prefixCls } -content` } >
272+ < img
273+ width = "32"
274+ src = { loadingSvg }
275+ class = { [
276+ `${ prefixCls } -image` ,
277+ imgState . status === StatueEnum . LOADING ? '' : 'hidden' ,
278+ ] }
279+ />
280+ < img
281+ style = { unref ( getImageStyle ) }
282+ class = { [ `${ prefixCls } -image` , imgState . status === StatueEnum . DONE ? '' : 'hidden' ] }
283+ ref = { imgElRef }
284+ src = { imgState . currentUrl }
285+ onMousedown = { handleAddMoveListener }
286+ />
287+ { renderClose ( ) }
288+ { renderIndex ( ) }
289+ { renderController ( ) }
290+ { renderArrow ( 'left' ) }
291+ { renderArrow ( 'right' ) }
292+ </ div >
293+ </ div >
294+ )
315295 ) ;
316296 } ;
317297 } ,
0 commit comments