From dd41925b258173cdf1a23ca7108c539c923bf501 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Fri, 20 Nov 2020 09:08:13 +1100 Subject: [PATCH] Added decimal support to measure tool and added calculated precision --- src/components/map/MapMeasure.js | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/components/map/MapMeasure.js b/src/components/map/MapMeasure.js index bd96019..e27541c 100644 --- a/src/components/map/MapMeasure.js +++ b/src/components/map/MapMeasure.js @@ -21,11 +21,26 @@ function MapMeasure({ map, selectedToolSettings, active, gridSize }) { const [drawingShapeData, setDrawingShapeData] = useState(null); const [isBrushDown, setIsBrushDown] = useState(false); - const toolScale = - active && selectedToolSettings.scale.match(/(\d*)([a-zA-Z]*)/); - const toolMultiplier = - active && !isNaN(parseInt(toolScale[1])) ? parseInt(toolScale[1]) : 1; - const toolUnit = active && toolScale[2]; + function parseToolScale(scale) { + if (typeof scale === "string") { + const match = scale.match(/(\d*)(\.\d*)?([a-zA-Z]*)/); + const integer = parseFloat(match[1]); + const fractional = parseFloat(match[2]); + const unit = match[3] || ""; + if (!isNaN(integer) && !isNaN(fractional)) { + return { + multiplier: integer + fractional, + unit: unit, + digits: match[2].length - 1, + }; + } else if (!isNaN(integer) && isNaN(fractional)) { + return { multiplier: integer, unit: unit, digits: 0 }; + } + } + return { multiplier: 1, unit: "", digits: 0 }; + } + + const measureScale = parseToolScale(active && selectedToolSettings.scale); useEffect(() => { if (!active) { @@ -125,9 +140,9 @@ function MapMeasure({ map, selectedToolSettings, active, gridSize }) { >