Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions draftlogs/8221_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- Fix cartesian pan and zoombox drags that the browser cancels with `touchcancel`, which left the plot translated away from its axes [[#8221](https://github.com/plotly/plotly.js/pull/8221)], with thanks to @dominikvrbic for the contribution!
- Fix a touch drag of a legend or another component that the browser cancels, which kept its listeners and changed the result of the next drag [[#8221](https://github.com/plotly/plotly.js/pull/8221)], with thanks to @dominikvrbic for the contribution!
- Fix a cartesian drag that stayed active after `Plotly.react` removed its subplot [[#8221](https://github.com/plotly/plotly.js/pull/8221)], with thanks to @dominikvrbic for the contribution!
- Fix a legend drag that kept its document listeners after `Plotly.purge` [[#8221](https://github.com/plotly/plotly.js/pull/8221)], with thanks to @dominikvrbic for the contribution!
90 changes: 75 additions & 15 deletions src/components/dragelement/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ dragElement.unhoverRaw = unhover.raw;
* Provide custom clamping function for small displacements.
* By default, clamping is done using `minDrag` to x and y displacements
* independently.
* cancelFn (optional) function(dragged, removed)
* runs after the browser cancels the touch or its target leaves the plot.
* dragged is true if the input moved more than minDrag.
* removed is true if Plotly removed the drag target.
*/
dragElement.init = function init(options) {
var gd = options.gd;
Expand Down Expand Up @@ -109,7 +113,18 @@ dragElement.init = function init(options) {

var clampFn = options.clampFn || _clampFn;

let moveEvent;
let endEvent;
let touchId;
let button;

function onStart(e) {
const active = gd._activeDragElement;
if (active) {
const removed = !gd.contains(active);
if (!removed && !active._lostEnd(e)) return;
active._cancelDrag(removed);
}
// make dragging and dragged into properties of gd
// so that others can look at and modify them
gd._dragged = false;
Expand Down Expand Up @@ -149,22 +164,50 @@ dragElement.init = function init(options) {
document.documentElement.style.cursor = window.getComputedStyle(element).cursor;
}

document.addEventListener('mouseup', onDone);
document.addEventListener('touchend', onDone);
gd._activeDragElement = element;
element._cancelDrag = (removed = true) => cancelDrag(removed);
element._lostEnd = lostEnd;

const touch = e.changedTouches && e.changedTouches[0];
touchId = touch && touch.identifier;
button = e.button;
moveEvent = touch ? 'touchmove' : 'mousemove';
endEvent = touch ? 'touchend' : 'mouseup';
if (touch) document.addEventListener('touchcancel', onCancel);
document.addEventListener(endEvent, onDone);

if(options.dragmode !== false) {
e.preventDefault();
document.addEventListener('mousemove', onMove);
document.addEventListener('touchmove', onMove, {passive: false});
document.addEventListener(moveEvent, onMove, {passive: false});
}

return;
}

// A new press of the same mouse button, a touch list without the owning contact,
// or a new contact with the identifier of the owner shows that the browser did not
// deliver the end of this drag. A browser can reuse the identifier of an ended contact.
function lostEnd(e) {
if (touchId == null) return !e.changedTouches && e.button === button;
if (!e.touches) return false;
const hasOwner = contacts => Array.from(contacts).some(contact => contact.identifier === touchId);
return hasOwner(e.changedTouches) || !hasOwner(e.touches);
}

function getContact(e) {
if (touchId == null) return e;
const contacts = e.changedTouches;
for (let i = 0; i < contacts.length; i++) {
if (contacts[i].identifier === touchId) return contacts[i];
}
}

function onMove(e) {
const contact = getContact(e);
if (!contact) return;
e.preventDefault();

var offset = pointerOffset(e);
const offset = pointerOffset(contact);
var minDrag = options.minDrag || constants.MINDRAG;
var dxdy = clampFn(offset[0] - startX, offset[1] - startY, minDrag);
var dx = dxdy[0];
Expand All @@ -187,24 +230,41 @@ dragElement.init = function init(options) {
return;
}

function onDone(e) {
delete gd._dragdata;
function onCancel(e) {
if (getContact(e)) cancelDrag(false);
}

if(options.dragmode !== false) {
e.preventDefault();
document.removeEventListener('mousemove', onMove);
document.removeEventListener('touchmove', onMove);
}
function cancelDrag(removed) {
const dragged = gd._dragged;
endDrag();
gd._dragging = false;
gd._dragged = false;
gd._mouseDownTime = 0;
if (options.cancelFn) options.cancelFn(dragged, removed);
}

document.removeEventListener('mouseup', onDone);
document.removeEventListener('touchend', onDone);
function endDrag() {
delete element._cancelDrag;
delete element._lostEnd;
delete gd._activeDragElement;
delete gd._dragdata;

document.removeEventListener(moveEvent, onMove);
document.removeEventListener(endEvent, onDone);
document.removeEventListener('touchcancel', onCancel);

if(hasHover) {
removeElement(dragCover);
} else if(cursor) {
} else if(cursor != null) {
dragCover.documentElement.style.cursor = cursor;
cursor = null;
}
}

function onDone(e) {
if (!getContact(e)) return;
endDrag();
if (options.dragmode !== false) e.preventDefault();

if(!gd._dragging) {
gd._dragged = false;
Expand Down
Loading