summaryrefslogtreecommitdiff
path: root/node_modules/pigeon-maps/src/overlays/Draggable.tsx
blob: 4cac3c64d1359c17acfb154a0837fb71f22bcf75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import React, { useEffect, useRef, useState } from 'react'
import { PigeonProps, Point } from '../types'

function isDescendentOf(element, ancestor) {
  while (element) {
    if (element === ancestor) {
      return true
    }
    element = element.parentElement
  }

  return false
}

interface DraggableProps extends PigeonProps {
  className?: string
  style?: React.CSSProperties

  children?: React.ReactNode

  onDragStart?: (anchor: Point) => void
  onDragMove?: (anchor: Point) => void
  onDragEnd?: (anchor: Point) => void
}

interface DraggableState {
  isDragging: boolean
  startX?: number
  startY?: number
  startLeft?: number
  startTop?: number
  deltaX: number
  deltaY: number
}

const defaultState: DraggableState = {
  isDragging: false,
  startX: undefined,
  startY: undefined,
  startLeft: undefined,
  startTop: undefined,
  deltaX: 0,
  deltaY: 0,
}

export function Draggable(props: DraggableProps): JSX.Element {
  const dragRef = useRef<HTMLDivElement>()
  const propsRef = useRef<DraggableProps>(props)
  const stateRef = useRef({ ...defaultState })
  const [_state, _setState] = useState(defaultState)

  propsRef.current = props

  const setState = (stateUpdate: Partial<DraggableState>): void => {
    const newState = { ...stateRef.current, ...stateUpdate }
    stateRef.current = newState
    _setState(newState)
  }

  const { mouseEvents, touchEvents } = props.mapProps

  useEffect(() => {
    const handleDragStart = (event: MouseEvent | TouchEvent) => {
      if (isDescendentOf(event.target, dragRef.current)) {
        event.preventDefault()

        setState({
          isDragging: true,
          startX: ('touches' in event ? event.touches[0] : event).clientX,
          startY: ('touches' in event ? event.touches[0] : event).clientY,
          startLeft: propsRef.current.left,
          startTop: propsRef.current.top,
          deltaX: 0,
          deltaY: 0,
        })

        if (propsRef.current.onDragStart) {
          const { left, top, offset, pixelToLatLng } = propsRef.current
          propsRef.current.onDragMove(pixelToLatLng([left + (offset ? offset[0] : 0), top + (offset ? offset[1] : 0)]))
        }
      }
    }

    const handleDragMove = (event: MouseEvent | TouchEvent) => {
      if (!stateRef.current.isDragging) {
        return
      }

      event.preventDefault()

      const x = ('touches' in event ? event.touches[0] : event).clientX
      const y = ('touches' in event ? event.touches[0] : event).clientY

      const deltaX = x - stateRef.current.startX
      const deltaY = y - stateRef.current.startY

      setState({ deltaX, deltaY })

      if (propsRef.current.onDragMove) {
        const { offset, pixelToLatLng } = propsRef.current
        const { startLeft, startTop } = stateRef.current

        propsRef.current.onDragMove(
          pixelToLatLng([startLeft + deltaX + (offset ? offset[0] : 0), startTop + deltaY + (offset ? offset[1] : 0)])
        )
      }
    }

    const handleDragEnd = (event: MouseEvent | TouchEvent) => {
      if (!stateRef.current.isDragging) {
        return
      }

      event.preventDefault()

      const { offset, pixelToLatLng } = propsRef.current
      const { deltaX, deltaY, startLeft, startTop } = stateRef.current

      propsRef.current.onDragEnd?.(
        pixelToLatLng([startLeft + deltaX + (offset ? offset[0] : 0), startTop + deltaY + (offset ? offset[1] : 0)])
      )

      setState({
        isDragging: false,
        startX: undefined,
        startY: undefined,
        startLeft: undefined,
        startTop: undefined,
        deltaX: 0,
        deltaY: 0,
      })
    }

    const wa = (e: string, t: EventListener, o?: AddEventListenerOptions) => window.addEventListener(e, t, o)
    const wr = (e: string, t: EventListener) => window.removeEventListener(e, t)

    if (mouseEvents) {
      wa('mousedown', handleDragStart)
      wa('mousemove', handleDragMove)
      wa('mouseup', handleDragEnd)
    }

    if (touchEvents) {
      wa('touchstart', handleDragStart, { passive: false })
      wa('touchmove', handleDragMove, { passive: false })
      wa('touchend', handleDragEnd, { passive: false })
    }

    return () => {
      if (mouseEvents) {
        wr('mousedown', handleDragStart)
        wr('mousemove', handleDragMove)
        wr('mouseup', handleDragEnd)
      }

      if (touchEvents) {
        wr('touchstart', handleDragStart)
        wr('touchmove', handleDragMove)
        wr('touchend', handleDragEnd)
      }
    }
  }, [mouseEvents, touchEvents])

  const { left, top, className, style } = props
  const { deltaX, deltaY, startLeft, startTop, isDragging } = _state

  return (
    <div
      style={{
        cursor: isDragging ? 'grabbing' : 'grab',
        ...(style || {}),
        position: 'absolute',
        transform: `translate(${isDragging ? startLeft + deltaX : left}px, ${isDragging ? startTop + deltaY : top}px)`,
      }}
      ref={dragRef}
      className={`pigeon-drag-block${className ? ` ${className}` : ''}`}
    >
      {props.children}
    </div>
  )
}