From ccb793e78cc66ce54ccba99ed02d42ecbe79b6e7 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Mon, 25 Jun 2018 12:39:10 +0800 Subject: [PATCH] - Fix: Detection of whether to keep ellipse (rx and ry when just created are now returning 0 instead of null); fixes #262 - Refactoring: Simplify `isValidUnit` --- editor/svgcanvas.js | 2 +- editor/units.js | 24 ++++++++++-------------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index 0194d848..0d076855 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -2151,7 +2151,7 @@ const mouseUp = function (evt) { break; case 'ellipse': attrs = $(element).attr(['rx', 'ry']); - keep = (attrs.rx != null || attrs.ry != null); + keep = (attrs.rx || attrs.ry); break; case 'fhellipse': if ((freehand.maxx - freehand.minx) > 0 && diff --git a/editor/units.js b/editor/units.js index ae491c03..136108fd 100644 --- a/editor/units.js +++ b/editor/units.js @@ -235,21 +235,19 @@ export const convertToNum = function (attr, val) { * @param val - String with the attribute value to check */ export const isValidUnit = function (attr, val, selectedElement) { - let valid = false; if (unitAttrs.includes(attr)) { // True if it's just a number if (!isNaN(val)) { - valid = true; - } else { - // Not a number, check if it has a valid unit - val = val.toLowerCase(); - Object.keys(typeMap_).forEach((unit) => { - if (valid) { return; } - const re = new RegExp('^-?[\\d\\.]+' + unit + '$'); - if (re.test(val)) { valid = true; } - }); + return true; } - } else if (attr === 'id') { + // Not a number, check if it has a valid unit + val = val.toLowerCase(); + return Object.keys(typeMap_).some((unit) => { + const re = new RegExp('^-?[\\d\\.]+' + unit + '$'); + return re.test(val); + }); + } + if (attr === 'id') { // if we're trying to change the id, make sure it's not already present in the doc // and the id value is valid. @@ -264,7 +262,5 @@ export const isValidUnit = function (attr, val, selectedElement) { } catch (e) {} return result; } - valid = true; - - return valid; + return true; };