@@ -4,7 +4,7 @@ import { Disposable, DisposableCollection, IPoint, Rectangle } from '@flowgram.a
44import { FlowNodeEntity , FlowNodeTransformData } from '@flowgram.ai/document' ;
55import { FlowNodeBaseType } from '@flowgram.ai/document' ;
66import { FlowDocument } from '@flowgram.ai/document' ;
7- import { EntityManager , PlaygroundConfigEntity } from '@flowgram.ai/core' ;
7+ import { EntityManager , MouseTouchEvent , PlaygroundConfigEntity } from '@flowgram.ai/core' ;
88
99import type { MinimapRenderContext , MinimapServiceOptions , MinimapCanvasStyle } from './type' ;
1010import { MinimapDraw } from './draw' ;
@@ -312,26 +312,29 @@ export class FlowMinimapService {
312312 private addEventListeners ( ) : void {
313313 this . canvas . addEventListener ( 'wheel' , this . handleWheel ) ;
314314 this . canvas . addEventListener ( 'mousedown' , this . handleStartDrag ) ;
315+ this . canvas . addEventListener ( 'touchstart' , this . handleStartDrag , { passive : false } ) ;
315316 this . canvas . addEventListener ( 'mousemove' , this . handleCursor ) ;
316317 }
317318
318319 private removeEventListeners ( ) : void {
319320 this . canvas . removeEventListener ( 'wheel' , this . handleWheel ) ;
320321 this . canvas . removeEventListener ( 'mousedown' , this . handleStartDrag ) ;
322+ this . canvas . removeEventListener ( 'touchstart' , this . handleStartDrag ) ;
321323 this . canvas . removeEventListener ( 'mousemove' , this . handleCursor ) ;
322324 }
323325
324326 private handleWheel = ( event : WheelEvent ) : void => { } ;
325327
326- private handleStartDrag = ( event : MouseEvent ) : void => {
327- event . preventDefault ( ) ;
328+ private handleStartDrag = ( event : MouseEvent | TouchEvent ) : void => {
329+ MouseTouchEvent . preventDefault ( event ) ;
328330 event . stopPropagation ( ) ;
329331 const renderContext = this . createRenderContext ( ) ;
330332 const { viewRect, scale, offset } = renderContext ;
331333 const canvasRect = this . canvas . getBoundingClientRect ( ) ;
334+ const { clientX, clientY } = MouseTouchEvent . getEventCoord ( event ) ;
332335 const mousePoint : IPoint = {
333- x : event . clientX - canvasRect . left ,
334- y : event . clientY - canvasRect . top ,
336+ x : clientX - canvasRect . left ,
337+ y : clientY - canvasRect . top ,
335338 } ;
336339
337340 const viewRectOnCanvas = this . rectOnCanvas ( {
@@ -344,20 +347,26 @@ export class FlowMinimapService {
344347 }
345348 this . isDragging = true ;
346349 this . dragStart = mousePoint ;
350+ // click
347351 document . addEventListener ( 'mousemove' , this . handleDragging ) ;
348352 document . addEventListener ( 'mouseup' , this . handleEndDrag ) ;
353+ // touch
354+ document . addEventListener ( 'touchmove' , this . handleDragging , { passive : false } ) ;
355+ document . addEventListener ( 'touchend' , this . handleEndDrag ) ;
356+ document . addEventListener ( 'touchcancel' , this . handleEndDrag ) ;
349357 } ;
350358
351- private handleDragging = ( event : MouseEvent ) : void => {
359+ private handleDragging = ( event : MouseEvent | TouchEvent ) : void => {
352360 if ( ! this . isDragging || ! this . dragStart ) return ;
353- event . preventDefault ( ) ;
361+ MouseTouchEvent . preventDefault ( event ) ;
354362 event . stopPropagation ( ) ;
355363
356364 const renderContext = this . createRenderContext ( ) ;
357365 const { scale } = renderContext ;
358366 const canvasRect = this . canvas . getBoundingClientRect ( ) ;
359- const mouseX = event . clientX - canvasRect . left ;
360- const mouseY = event . clientY - canvasRect . top ;
367+ const { clientX, clientY } = MouseTouchEvent . getEventCoord ( event ) ;
368+ const mouseX = clientX - canvasRect . left ;
369+ const mouseY = clientY - canvasRect . top ;
361370
362371 const deltaX = ( mouseX - this . dragStart . x ) / scale ;
363372 const deltaY = ( mouseY - this . dragStart . y ) / scale ;
@@ -368,11 +377,16 @@ export class FlowMinimapService {
368377 this . render ( ) ;
369378 } ;
370379
371- private handleEndDrag = ( event : MouseEvent ) : void => {
372- event . preventDefault ( ) ;
380+ private handleEndDrag = ( event : MouseEvent | TouchEvent ) : void => {
381+ MouseTouchEvent . preventDefault ( event ) ;
373382 event . stopPropagation ( ) ;
383+ // click
374384 document . removeEventListener ( 'mousemove' , this . handleDragging ) ;
375385 document . removeEventListener ( 'mouseup' , this . handleEndDrag ) ;
386+ // touch
387+ document . removeEventListener ( 'touchmove' , this . handleDragging ) ;
388+ document . removeEventListener ( 'touchend' , this . handleEndDrag ) ;
389+ document . removeEventListener ( 'touchcancel' , this . handleEndDrag ) ;
376390 this . isDragging = false ;
377391 this . dragStart = undefined ;
378392 this . setActivate ( this . isMouseInCanvas ( event ) ) ;
@@ -404,13 +418,14 @@ export class FlowMinimapService {
404418 }
405419 } ;
406420
407- private isMouseInCanvas ( event : MouseEvent ) : boolean {
421+ private isMouseInCanvas ( event : MouseEvent | TouchEvent ) : boolean {
408422 const canvasRect = this . canvas . getBoundingClientRect ( ) ;
423+ const { clientX, clientY } = MouseTouchEvent . getEventCoord ( event ) ;
409424 return (
410- event . clientX >= canvasRect . left &&
411- event . clientX <= canvasRect . right &&
412- event . clientY >= canvasRect . top &&
413- event . clientY <= canvasRect . bottom
425+ clientX >= canvasRect . left &&
426+ clientX <= canvasRect . right &&
427+ clientY >= canvasRect . top &&
428+ clientY <= canvasRect . bottom
414429 ) ;
415430 }
416431
0 commit comments