Skip to content

Commit cf1a703

Browse files
committed
fix the clientX and clientY of touch sensor events
1 parent 7a98fcc commit cf1a703

File tree

5 files changed

+45
-79
lines changed

5 files changed

+45
-79
lines changed

scripts/test/helpers/constants.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export const defaultTouchEventOptions = {
22
touches: [
33
{
4+
clientX: 0,
5+
clientY: 0,
46
pageX: 0,
57
pageY: 0,
68
},

src/Draggable/Plugins/Mirror/Mirror.js

+2-49
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export const onDragMove = Symbol('onDragMove');
1313
export const onDragStop = Symbol('onDragStop');
1414
export const onMirrorCreated = Symbol('onMirrorCreated');
1515
export const onMirrorMove = Symbol('onMirrorMove');
16-
export const onScroll = Symbol('onScroll');
1716
export const getAppendableContainer = Symbol('getAppendableContainer');
1817

1918
/**
@@ -67,31 +66,11 @@ export default class Mirror extends AbstractPlugin {
6766
...this.getOptions(),
6867
};
6968

70-
/**
71-
* Scroll offset for touch devices because the mirror is positioned fixed
72-
* @property {Object} scrollOffset
73-
* @property {Number} scrollOffset.x
74-
* @property {Number} scrollOffset.y
75-
*/
76-
this.scrollOffset = {x: 0, y: 0};
77-
78-
/**
79-
* Initial scroll offset for touch devices because the mirror is positioned fixed
80-
* @property {Object} scrollOffset
81-
* @property {Number} scrollOffset.x
82-
* @property {Number} scrollOffset.y
83-
*/
84-
this.initialScrollOffset = {
85-
x: window.scrollX,
86-
y: window.scrollY,
87-
};
88-
8969
this[onDragStart] = this[onDragStart].bind(this);
9070
this[onDragMove] = this[onDragMove].bind(this);
9171
this[onDragStop] = this[onDragStop].bind(this);
9272
this[onMirrorCreated] = this[onMirrorCreated].bind(this);
9373
this[onMirrorMove] = this[onMirrorMove].bind(this);
94-
this[onScroll] = this[onScroll].bind(this);
9574
}
9675

9776
/**
@@ -131,15 +110,6 @@ export default class Mirror extends AbstractPlugin {
131110
return;
132111
}
133112

134-
if ('ontouchstart' in window) {
135-
document.addEventListener('scroll', this[onScroll], true);
136-
}
137-
138-
this.initialScrollOffset = {
139-
x: window.scrollX,
140-
y: window.scrollY,
141-
};
142-
143113
const {source, originalSource, sourceContainer, sensorEvent} = dragEvent;
144114

145115
// Last sensor position of mirror move
@@ -233,13 +203,6 @@ export default class Mirror extends AbstractPlugin {
233203
}
234204

235205
[onDragStop](dragEvent) {
236-
if ('ontouchstart' in window) {
237-
document.removeEventListener('scroll', this[onScroll], true);
238-
}
239-
240-
this.initialScrollOffset = {x: 0, y: 0};
241-
this.scrollOffset = {x: 0, y: 0};
242-
243206
if (!this.mirror) {
244207
return;
245208
}
@@ -261,13 +224,6 @@ export default class Mirror extends AbstractPlugin {
261224
}
262225
}
263226

264-
[onScroll]() {
265-
this.scrollOffset = {
266-
x: window.scrollX - this.initialScrollOffset.x,
267-
y: window.scrollY - this.initialScrollOffset.y,
268-
};
269-
}
270-
271227
/**
272228
* Mirror created handler
273229
* @param {MirrorCreatedEvent} mirrorEvent
@@ -293,7 +249,6 @@ export default class Mirror extends AbstractPlugin {
293249
source,
294250
sensorEvent,
295251
mirrorClass,
296-
scrollOffset: this.scrollOffset,
297252
options: this.options,
298253
passedThreshX: true,
299254
passedThreshY: true,
@@ -337,7 +292,6 @@ export default class Mirror extends AbstractPlugin {
337292
options: this.options,
338293
initialX: this.initialX,
339294
initialY: this.initialY,
340-
scrollOffset: this.scrollOffset,
341295
passedThreshX: mirrorEvent.passedThreshX,
342296
passedThreshY: mirrorEvent.passedThreshY,
343297
lastMovedX: this.lastMovedX,
@@ -491,7 +445,6 @@ function positionMirror({withFrame = false, initial = false} = {}) {
491445
mirrorOffset,
492446
initialY,
493447
initialX,
494-
scrollOffset,
495448
options,
496449
passedThreshX,
497450
passedThreshY,
@@ -511,11 +464,11 @@ function positionMirror({withFrame = false, initial = false} = {}) {
511464

512465
if (mirrorOffset) {
513466
const x = passedThreshX
514-
? Math.round((sensorEvent.clientX - mirrorOffset.left - scrollOffset.x) / (options.thresholdX || 1)) *
467+
? Math.round((sensorEvent.clientX - mirrorOffset.left) / (options.thresholdX || 1)) *
515468
(options.thresholdX || 1)
516469
: Math.round(lastMovedX);
517470
const y = passedThreshY
518-
? Math.round((sensorEvent.clientY - mirrorOffset.top - scrollOffset.y) / (options.thresholdY || 1)) *
471+
? Math.round((sensorEvent.clientY - mirrorOffset.top) / (options.thresholdY || 1)) *
519472
(options.thresholdY || 1)
520473
: Math.round(lastMovedY);
521474

src/Draggable/Plugins/Scrollable/Scrollable.js

+7-21
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,11 @@ export default class Scrollable extends AbstractPlugin {
4949
};
5050

5151
/**
52-
* Keeps current mouse position
53-
* @property {Object} currentMousePosition
54-
* @property {Number} currentMousePosition.clientX
55-
* @property {Number} currentMousePosition.clientY
52+
* Keeps current sensor event
53+
* @property {SensorEvent} currentSensorEvent
5654
* @type {Object|null}
5755
*/
58-
this.currentMousePosition = null;
56+
this.currentSensorEvent = null;
5957

6058
/**
6159
* Scroll animation frame
@@ -159,18 +157,7 @@ export default class Scrollable extends AbstractPlugin {
159157
return;
160158
}
161159

162-
const sensorEvent = dragEvent.sensorEvent;
163-
const scrollOffset = {x: 0, y: 0};
164-
165-
if ('ontouchstart' in window) {
166-
scrollOffset.y = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
167-
scrollOffset.x = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
168-
}
169-
170-
this.currentMousePosition = {
171-
clientX: sensorEvent.clientX - scrollOffset.x,
172-
clientY: sensorEvent.clientY - scrollOffset.y,
173-
};
160+
this.currentSensorEvent = dragEvent.sensorEvent;
174161

175162
this.scrollAnimationFrame = requestAnimationFrame(this[scroll]);
176163
}
@@ -186,15 +173,15 @@ export default class Scrollable extends AbstractPlugin {
186173
this.scrollableElement = null;
187174
this.scrollAnimationFrame = null;
188175
this.findScrollableElementFrame = null;
189-
this.currentMousePosition = null;
176+
this.currentSensorEvent = null;
190177
}
191178

192179
/**
193180
* Scroll function that does the heavylifting
194181
* @private
195182
*/
196183
[scroll]() {
197-
if (!this.scrollableElement || !this.currentMousePosition) {
184+
if (!this.scrollableElement || !this.currentSensorEvent) {
198185
return;
199186
}
200187

@@ -209,8 +196,7 @@ export default class Scrollable extends AbstractPlugin {
209196

210197
const documentScrollingElement = getDocumentScrollingElement();
211198
const scrollableElement = this.scrollableElement;
212-
const clientX = this.currentMousePosition.clientX;
213-
const clientY = this.currentMousePosition.clientY;
199+
const {clientX, clientY} = this.currentSensorEvent;
214200

215201
if (scrollableElement !== document.body && scrollableElement !== document.documentElement && !cutOff) {
216202
const {offsetHeight, offsetWidth} = scrollableElement;

src/Draggable/Sensors/TouchSensor/TouchSensor.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ export default class TouchSensor extends Sensor {
140140
[startDrag]() {
141141
const startEvent = this.startEvent;
142142
const container = this.currentContainer;
143-
const touch = touchCoords(startEvent);
143+
const {clientX, clientY} = touchCoords(startEvent);
144144

145145
const dragStartEvent = new DragStartSensorEvent({
146-
clientX: touch.pageX,
147-
clientY: touch.pageY,
146+
clientX,
147+
clientY,
148148
target: startEvent.target,
149149
container,
150150
originalEvent: startEvent,
@@ -190,12 +190,12 @@ export default class TouchSensor extends Sensor {
190190
if (!this.dragging) {
191191
return;
192192
}
193-
const {pageX, pageY} = touchCoords(event);
193+
const {clientX, clientY, pageX, pageY} = touchCoords(event);
194194
const target = document.elementFromPoint(pageX - window.scrollX, pageY - window.scrollY);
195195

196196
const dragMoveEvent = new DragMoveSensorEvent({
197-
clientX: pageX,
198-
clientY: pageY,
197+
clientX,
198+
clientY,
199199
target,
200200
container: this.currentContainer,
201201
originalEvent: event,
@@ -227,14 +227,14 @@ export default class TouchSensor extends Sensor {
227227

228228
document.removeEventListener('touchmove', this[onTouchMove]);
229229

230-
const {pageX, pageY} = touchCoords(event);
230+
const {clientX, clientY, pageX, pageY} = touchCoords(event);
231231
const target = document.elementFromPoint(pageX - window.scrollX, pageY - window.scrollY);
232232

233233
event.preventDefault();
234234

235235
const dragStopEvent = new DragStopSensorEvent({
236-
clientX: pageX,
237-
clientY: pageY,
236+
clientX,
237+
clientY,
238238
target,
239239
container: this.currentContainer,
240240
originalEvent: event,

src/Draggable/Sensors/TouchSensor/tests/TouchSensor.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,31 @@ describe('TouchSensor', () => {
9797

9898
expect(touchEndEvent.defaultPrevented).toBe(true);
9999
});
100+
101+
it('event parameter', () => {
102+
const touchEvent = {
103+
pageX: 21,
104+
pageY: 22,
105+
clientX: 11,
106+
clientY: 12,
107+
};
108+
109+
function testParameter(event) {
110+
expect(event.detail.clientX).toBe(touchEvent.clientX);
111+
expect(event.detail.clientY).toBe(touchEvent.clientY);
112+
}
113+
114+
sandbox.addEventListener('drag:start', testParameter);
115+
116+
sandbox.addEventListener('drag:move', testParameter);
117+
118+
sandbox.addEventListener('drag:stop', testParameter);
119+
120+
touchStart(draggableElement, {touches: [touchEvent]});
121+
waitForDragDelay();
122+
touchMove(draggableElement, {touches: [touchEvent]});
123+
touchRelease(draggableElement, {touches: [touchEvent]});
124+
});
100125
});
101126

102127
describe('using distance', () => {

0 commit comments

Comments
 (0)