@@ -355,6 +355,8 @@ class ImageViewerPlugin extends BasePlugin {
355355 }
356356
357357 process = ( ) => {
358+ this . _handleImageInteraction ( )
359+
358360 this . utils . eventHub . addEventListener ( this . utils . eventHub . eventType . toggleSettingPage , hide => hide && this . close ( ) )
359361
360362 if ( this . config . CLICK_MASK_TO_EXIT ) {
@@ -374,11 +376,6 @@ class ImageViewerPlugin extends BasePlugin {
374376 const fn = fnList [ ev . deltaY > 0 ? 1 : 0 ]
375377 if ( typeof fn === "function" ) fn ( )
376378 } , { passive : false } )
377- this . entities . image . addEventListener ( "mousedown" , ev => {
378- const fnList = this . getFnList ( ev , "MOUSEDOWN" )
379- const fn = fnList [ ev . button ]
380- if ( typeof fn === "function" ) fn ( )
381- } )
382379 this . entities . ops . addEventListener ( "click" , ev => {
383380 const target = ev . target . closest ( "[option]" )
384381 if ( ! target ) return
@@ -406,6 +403,54 @@ class ImageViewerPlugin extends BasePlugin {
406403 } , { passive : true } )
407404 }
408405
406+ _handleImageInteraction = ( ) => {
407+ const raf = this . utils . getRafManager ( )
408+
409+ this . entities . image . addEventListener ( "mousedown" , ev => {
410+ const executeAction = ( ) => {
411+ const fnList = this . getFnList ( ev , "MOUSEDOWN" )
412+ const fn = fnList [ ev . button ]
413+ if ( typeof fn === "function" ) fn ( )
414+ }
415+
416+ if ( ev . button !== 0 ) {
417+ executeAction ( )
418+ return
419+ }
420+
421+ ev . preventDefault ( )
422+
423+ let isMoved = false
424+ const startX = ev . clientX
425+ const startY = ev . clientY
426+ const initialTx = this . operations . state . translateX
427+ const initialTy = this . operations . state . translateY
428+
429+ this . entities . image . classList . add ( "dragging" )
430+
431+ const onMouseMove = ev => {
432+ const deltaX = ev . clientX - startX
433+ const deltaY = ev . clientY - startY
434+ if ( ! isMoved && Math . max ( Math . abs ( deltaX ) , Math . abs ( deltaY ) ) > 3 ) isMoved = true
435+ raf . schedule ( ( ) => {
436+ this . operations . state . translateX = initialTx + deltaX
437+ this . operations . state . translateY = initialTy + deltaY
438+ this . operations . apply ( false )
439+ } )
440+ }
441+ const onMouseUp = ( ) => {
442+ document . removeEventListener ( "mousemove" , onMouseMove )
443+ document . removeEventListener ( "mouseup" , onMouseUp )
444+ this . entities . image . classList . remove ( "dragging" )
445+ raf . cancel ( )
446+ if ( ! isMoved ) executeAction ( )
447+ }
448+
449+ document . addEventListener ( "mousemove" , onMouseMove )
450+ document . addEventListener ( "mouseup" , onMouseUp )
451+ } )
452+ }
453+
409454 getFnList = ( ev , method ) => {
410455 const modifiers = [ ]
411456 if ( this . utils . metaKeyPressed ( ev ) ) modifiers . push ( "CTRL" )
0 commit comments