Handle case where new drag starts while existing lasso is not finished (#1864)

* handle case where new drag starts while existing lasso is not finished

* flip variable
This commit is contained in:
Severiano Badajoz
2020-09-28 10:34:47 -07:00
committed by GitHub
parent 3e2d7174fd
commit 374bb11279
+24 -5
View File
@@ -10,6 +10,7 @@ const Lasso = () => {
let lassoPolygon; let lassoPolygon;
let lassoPath; let lassoPath;
let closePath; let closePath;
let lassoInProgress;
const polygonToPath = (polygon) => const polygonToPath = (polygon) =>
`M${polygon.map((d) => d.join(",")).join("L")}`; `M${polygon.map((d) => d.join(",")).join("L")}`;
@@ -25,8 +26,18 @@ const Lasso = () => {
lassoPolygon = [d3.mouse(svg.node())]; // current x y of mouse within element lassoPolygon = [d3.mouse(svg.node())]; // current x y of mouse within element
if (lassoPath) { if (lassoPath) {
// If the existing path is in progress
if (lassoInProgress) {
// cancel the existing lasso
handleCancel();
// Don't continue with current drag start
return;
}
lassoPath.remove(); lassoPath.remove();
} }
// We're starting a new drag
lassoInProgress = true;
lassoPath = g lassoPath = g
.append("path") .append("path")
@@ -67,25 +78,33 @@ const Lasso = () => {
} }
}; };
const handleCancel = () => {
lassoPath.remove();
closePath = closePath?.remove();
lassoPath = null;
lassoPolygon = null;
closePath = null;
dispatch.call("cancel");
};
const handleDragEnd = () => { const handleDragEnd = () => {
// remove the close path // remove the close path
closePath.remove(); closePath.remove();
closePath = null; closePath = null;
// succesfully closed // successfully closed
if ( if (
distance(lassoPolygon[0], lassoPolygon[lassoPolygon.length - 1]) < distance(lassoPolygon[0], lassoPolygon[lassoPolygon.length - 1]) <
closeDistance closeDistance
) { ) {
lassoInProgress = false;
lassoPath.attr("d", `${polygonToPath(lassoPolygon)}Z`); lassoPath.attr("d", `${polygonToPath(lassoPolygon)}Z`);
dispatch.call("end", lasso, lassoPolygon); dispatch.call("end", lasso, lassoPolygon);
// otherwise cancel // otherwise cancel
} else { } else {
lassoPath.remove(); handleCancel();
lassoPath = null;
lassoPolygon = null;
dispatch.call("cancel");
} }
}; };