|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow |
| 8 | + */ |
| 9 | + |
| 10 | +import type {EventResponderContext} from 'events/EventTypes'; |
| 11 | +import {REACT_EVENT_COMPONENT_TYPE} from 'shared/ReactSymbols'; |
| 12 | + |
| 13 | +const targetEventTypes = ['pointerdown', 'pointercancel']; |
| 14 | +const rootEventTypes = ['pointerup', {name: 'pointermove', passive: false}]; |
| 15 | + |
| 16 | +type DragState = { |
| 17 | + dragTarget: null | EventTarget, |
| 18 | + isPointerDown: boolean, |
| 19 | + isDragging: boolean, |
| 20 | + startX: number, |
| 21 | + startY: number, |
| 22 | + x: number, |
| 23 | + y: number, |
| 24 | +}; |
| 25 | + |
| 26 | +// In the case we don't have PointerEvents (Safari), we listen to touch events |
| 27 | +// too |
| 28 | +if (typeof window !== 'undefined' && window.PointerEvent === undefined) { |
| 29 | + targetEventTypes.push('touchstart', 'touchend', 'mousedown', 'touchcancel'); |
| 30 | + rootEventTypes.push('mouseup', 'mousemove', { |
| 31 | + name: 'touchmove', |
| 32 | + passive: false, |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +function dispatchDragEvent( |
| 37 | + context: EventResponderContext, |
| 38 | + name: string, |
| 39 | + listener: (e: Object) => void, |
| 40 | + state: DragState, |
| 41 | + discrete: boolean, |
| 42 | + eventData?: { |
| 43 | + diffX: number, |
| 44 | + diffY: number, |
| 45 | + }, |
| 46 | +): void { |
| 47 | + context.dispatchEvent(name, listener, state.dragTarget, discrete, eventData); |
| 48 | +} |
| 49 | + |
| 50 | +const DragResponder = { |
| 51 | + targetEventTypes, |
| 52 | + createInitialState(): DragState { |
| 53 | + return { |
| 54 | + dragTarget: null, |
| 55 | + isPointerDown: false, |
| 56 | + isDragging: false, |
| 57 | + startX: 0, |
| 58 | + startY: 0, |
| 59 | + x: 0, |
| 60 | + y: 0, |
| 61 | + }; |
| 62 | + }, |
| 63 | + handleEvent( |
| 64 | + context: EventResponderContext, |
| 65 | + props: Object, |
| 66 | + state: DragState, |
| 67 | + ): void { |
| 68 | + const {eventTarget, eventType, event} = context; |
| 69 | + |
| 70 | + switch (eventType) { |
| 71 | + case 'touchstart': |
| 72 | + case 'mousedown': |
| 73 | + case 'pointerdown': { |
| 74 | + if (!state.isDragging) { |
| 75 | + const obj = |
| 76 | + eventType === 'touchstart' ? (event: any).changedTouches[0] : event; |
| 77 | + const x = (state.startX = (obj: any).screenX); |
| 78 | + const y = (state.startY = (obj: any).screenY); |
| 79 | + state.x = x; |
| 80 | + state.y = y; |
| 81 | + state.dragTarget = eventTarget; |
| 82 | + state.isPointerDown = true; |
| 83 | + context.addRootEventTypes(rootEventTypes); |
| 84 | + } |
| 85 | + break; |
| 86 | + } |
| 87 | + case 'touchmove': |
| 88 | + case 'mousemove': |
| 89 | + case 'pointermove': { |
| 90 | + if (context.isPassive()) { |
| 91 | + return; |
| 92 | + } |
| 93 | + if (state.isPointerDown) { |
| 94 | + const obj = |
| 95 | + eventType === 'touchmove' ? (event: any).changedTouches[0] : event; |
| 96 | + const x = (obj: any).screenX; |
| 97 | + const y = (obj: any).screenY; |
| 98 | + state.x = x; |
| 99 | + state.y = y; |
| 100 | + if (!state.isDragging && x !== state.startX && y !== state.startY) { |
| 101 | + let shouldEnableDragging = true; |
| 102 | + |
| 103 | + if ( |
| 104 | + props.onShouldClaimOwnership && |
| 105 | + props.onShouldClaimOwnership() |
| 106 | + ) { |
| 107 | + shouldEnableDragging = context.requestOwnership(state.dragTarget); |
| 108 | + } |
| 109 | + if (shouldEnableDragging) { |
| 110 | + state.isDragging = true; |
| 111 | + if (props.onDragChange) { |
| 112 | + const dragChangeEventListener = () => { |
| 113 | + props.onDragChange(true); |
| 114 | + }; |
| 115 | + context.dispatchEvent( |
| 116 | + 'dragchange', |
| 117 | + dragChangeEventListener, |
| 118 | + state.dragTarget, |
| 119 | + true, |
| 120 | + ); |
| 121 | + } |
| 122 | + } else { |
| 123 | + state.dragTarget = null; |
| 124 | + state.isPointerDown = false; |
| 125 | + context.removeRootEventTypes(rootEventTypes); |
| 126 | + } |
| 127 | + } else { |
| 128 | + if (props.onDragMove) { |
| 129 | + const eventData = { |
| 130 | + diffX: x - state.startX, |
| 131 | + diffY: y - state.startY, |
| 132 | + }; |
| 133 | + dispatchDragEvent( |
| 134 | + context, |
| 135 | + 'dragmove', |
| 136 | + props.onDragMove, |
| 137 | + state, |
| 138 | + false, |
| 139 | + eventData, |
| 140 | + ); |
| 141 | + } |
| 142 | + (event: any).preventDefault(); |
| 143 | + } |
| 144 | + } |
| 145 | + break; |
| 146 | + } |
| 147 | + case 'pointercancel': |
| 148 | + case 'touchcancel': |
| 149 | + case 'touchend': |
| 150 | + case 'mouseup': |
| 151 | + case 'pointerup': { |
| 152 | + if (state.isDragging) { |
| 153 | + if (props.onShouldClaimOwnership) { |
| 154 | + context.releaseOwnership(state.dragTarget); |
| 155 | + } |
| 156 | + if (props.onDragEnd) { |
| 157 | + dispatchDragEvent(context, 'dragend', props.onDragEnd, state, true); |
| 158 | + } |
| 159 | + if (props.onDragChange) { |
| 160 | + const dragChangeEventListener = () => { |
| 161 | + props.onDragChange(false); |
| 162 | + }; |
| 163 | + context.dispatchEvent( |
| 164 | + 'dragchange', |
| 165 | + dragChangeEventListener, |
| 166 | + state.dragTarget, |
| 167 | + true, |
| 168 | + ); |
| 169 | + } |
| 170 | + state.isDragging = false; |
| 171 | + } |
| 172 | + if (state.isPointerDown) { |
| 173 | + state.dragTarget = null; |
| 174 | + state.isPointerDown = false; |
| 175 | + context.removeRootEventTypes(rootEventTypes); |
| 176 | + } |
| 177 | + break; |
| 178 | + } |
| 179 | + } |
| 180 | + }, |
| 181 | +}; |
| 182 | + |
| 183 | +export default { |
| 184 | + $$typeof: REACT_EVENT_COMPONENT_TYPE, |
| 185 | + props: null, |
| 186 | + responder: DragResponder, |
| 187 | +}; |
0 commit comments