|
1 | 1 | // @flow
|
2 | 2 | import React, {PropTypes} from 'react';
|
3 |
| -import {matchesSelector, addEvent, removeEvent, addUserSelectStyles, |
| 3 | +import {matchesSelector, addEvent, removeEvent, addUserSelectStyles, getTouchIdentifier, |
4 | 4 | removeUserSelectStyles, styleHacks} from './utils/domFns';
|
5 | 5 | import {createCoreData, getControlPosition, snapToGrid} from './utils/positionFns';
|
6 | 6 | import {dontSetMe} from './utils/shims';
|
@@ -29,7 +29,7 @@ type CoreState = {
|
29 | 29 | dragging: boolean,
|
30 | 30 | lastX: number,
|
31 | 31 | lastY: number,
|
32 |
| - touchIdentifier: number |
| 32 | + touchIdentifier: ?number |
33 | 33 | };
|
34 | 34 |
|
35 | 35 | //
|
@@ -164,7 +164,7 @@ export default class DraggableCore extends React.Component {
|
164 | 164 | dragging: false,
|
165 | 165 | // Used while dragging to determine deltas.
|
166 | 166 | lastX: NaN, lastY: NaN,
|
167 |
| - touchIdentifier: NaN |
| 167 | + touchIdentifier: null |
168 | 168 | };
|
169 | 169 |
|
170 | 170 | componentWillUnmount() {
|
@@ -195,12 +195,13 @@ export default class DraggableCore extends React.Component {
|
195 | 195 | // Set touch identifier in component state if this is a touch event. This allows us to
|
196 | 196 | // distinguish between individual touches on multitouch screens by identifying which
|
197 | 197 | // touchpoint was set to this element.
|
198 |
| - if (e.targetTouches){ |
199 |
| - this.setState({touchIdentifier: e.targetTouches[0].identifier}); |
200 |
| - } |
| 198 | + const touchIdentifier = getTouchIdentifier(e); |
| 199 | + this.setState({touchIdentifier}); |
201 | 200 |
|
202 | 201 | // Get the current drag point from the event. This is used as the offset.
|
203 |
| - const {x, y} = getControlPosition(e, this); |
| 202 | + const position = getControlPosition(e, touchIdentifier, this); |
| 203 | + if (position == null) return; // not possible but satisfies flow |
| 204 | + const {x, y} = position; |
204 | 205 |
|
205 | 206 | // Create an event object with all the data parents need to make a decision here.
|
206 | 207 | const coreEvent = createCoreData(this, x, y);
|
@@ -234,12 +235,15 @@ export default class DraggableCore extends React.Component {
|
234 | 235 | };
|
235 | 236 |
|
236 | 237 | handleDrag: EventHandler<MouseEvent> = (e) => {
|
237 |
| - // Return if this is a touch event, but not the correct one for this element |
238 |
| - if (e.targetTouches && (e.targetTouches[0].identifier !== this.state.touchIdentifier)) return; |
239 | 238 |
|
240 |
| - let {x, y} = getControlPosition(e, this); |
| 239 | + // Get the current drag point from the event. This is used as the offset. |
| 240 | + const position = getControlPosition(e, this.state.touchIdentifier, this); |
| 241 | + if (position == null) return; |
| 242 | + let {x, y} = position; |
241 | 243 |
|
242 | 244 | // Snap to grid if prop has been provided
|
| 245 | + if (x !== x) debugger; |
| 246 | + |
243 | 247 | if (Array.isArray(this.props.grid)) {
|
244 | 248 | let deltaX = x - this.state.lastX, deltaY = y - this.state.lastY;
|
245 | 249 | [deltaX, deltaY] = snapToGrid(this.props.grid, deltaX, deltaY);
|
@@ -267,16 +271,14 @@ export default class DraggableCore extends React.Component {
|
267 | 271 | handleDragStop: EventHandler<MouseEvent> = (e) => {
|
268 | 272 | if (!this.state.dragging) return;
|
269 | 273 |
|
270 |
| - // Short circuit if this is not the correct touch event. `changedTouches` contains all |
271 |
| - // touch points that have been removed from the surface. |
272 |
| - if (e.changedTouches && (e.changedTouches[0].identifier !== this.state.touchIdentifier)) return; |
| 274 | + const position = getControlPosition(e, this.state.touchIdentifier, this); |
| 275 | + if (position == null) return; |
| 276 | + const {x, y} = position; |
| 277 | + const coreEvent = createCoreData(this, x, y); |
273 | 278 |
|
274 | 279 | // Remove user-select hack
|
275 | 280 | if (this.props.enableUserSelectHack) removeUserSelectStyles();
|
276 | 281 |
|
277 |
| - const {x, y} = getControlPosition(e, this); |
278 |
| - const coreEvent = createCoreData(this, x, y); |
279 |
| - |
280 | 282 | log('DraggableCore: handleDragStop: %j', coreEvent);
|
281 | 283 |
|
282 | 284 | // Reset the el.
|
|
0 commit comments