From 374bb112792c5f568a4dc20f565926fa9487ebb0 Mon Sep 17 00:00:00 2001 From: Severiano Badajoz Date: Mon, 28 Sep 2020 10:34:47 -0700 Subject: [PATCH] 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 --- client/src/components/graph/setupLasso.js | 29 +++++++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/client/src/components/graph/setupLasso.js b/client/src/components/graph/setupLasso.js index 526d47a5..6610e04c 100644 --- a/client/src/components/graph/setupLasso.js +++ b/client/src/components/graph/setupLasso.js @@ -10,6 +10,7 @@ const Lasso = () => { let lassoPolygon; let lassoPath; let closePath; + let lassoInProgress; const polygonToPath = (polygon) => `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 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(); } + // We're starting a new drag + lassoInProgress = true; lassoPath = g .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 = () => { // remove the close path closePath.remove(); closePath = null; - // succesfully closed + // successfully closed if ( distance(lassoPolygon[0], lassoPolygon[lassoPolygon.length - 1]) < closeDistance ) { + lassoInProgress = false; + lassoPath.attr("d", `${polygonToPath(lassoPolygon)}Z`); dispatch.call("end", lasso, lassoPolygon); // otherwise cancel } else { - lassoPath.remove(); - lassoPath = null; - lassoPolygon = null; - dispatch.call("cancel"); + handleCancel(); } };