diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index 16b13b9b..a65d8f4b 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -124,7 +124,6 @@ var snapToAngle = svgedit.math.snapToAngle; svgedit.units.init(curConfig); var unit_types = svgedit.units.getTypeMap(); - // import from svgutils.js var getUrlFromAttr = this.getUrlFromAttr = svgedit.utilities.getUrlFromAttr; var getHref = this.getHref = svgedit.utilities.getHref; @@ -140,7 +139,7 @@ svgedit.utilities.snapToGrid = function(value){ var stepSize = curConfig.snappingStep; var unit = curConfig.baseUnit; if(unit !== "px") { - stepSize *= svgedit.units.typeMap[unit]; + stepSize *= svgedit.units.getTypeMap()[unit]; } value = Math.round(value/stepSize)*stepSize; return value; @@ -341,7 +340,7 @@ $(opac_ani).attr({ // TODO(codedread): Migrate this into units.js // Set the scope for these functions -var convertToNum, unitConvertAttrs; +var convertToNum; (function() { // TODO(codedread): Remove these arrays and maps, they are now in units.js. @@ -433,48 +432,6 @@ var convertToNum, unitConvertAttrs; return valid; }; - - // Function: unitConvertAttrs - // Converts all applicable attributes to the given baseUnit - unitConvertAttrs = canvas.unitConvertAttrs = function(element) { - var elName = element.tagName; - var unit = curConfig.baseUnit; - var attrs; - switch (elName) - { - case "line": - attrs = ['x1', 'x2', 'y1', 'y2']; - break; - case "circle": - attrs = ['cx', 'cy', 'r']; - break; - case "ellipse": - attrs = ['cx', 'cy', 'rx', 'ry']; - break; - case "foreignObject": - case "rect": - case "image": - case "use": - attrs = ['x', 'y', 'width', 'height']; - break; - case "text": - attrs = ['x', 'y']; - break; - } - if(!attrs) return; - var len = attrs.length - for(var i = 0; i < len; i++) { - var attr = attrs[i]; - var cur = element.getAttribute(attr); - if(cur) { - if(!isNaN(cur)) { - element.setAttribute(attr, (cur / unit_types[unit]) + unit); - } else { - // Convert existing? - } - } - } - }; })(); @@ -4647,7 +4604,7 @@ var getMouseTarget = this.getMouseTarget = function(evt) { } else if (element != null) { canvas.addedNew = true; - if(useUnit) unitConvertAttrs(element); + if(useUnit) svgedit.units.convertAttrs(element); var ani_dur = .2, c_ani; if(opac_ani.beginElement && element.getAttribute('opacity') != cur_shape.opacity) { @@ -7260,8 +7217,8 @@ var svgToString = this.svgToString = function(elem, indent) { // } if(unit !== "px") { - res.w = shortFloat(svgedit.units.convertUnit(res.w, unit)) + unit; - res.h = shortFloat(svgedit.units.convertUnit(res.h, unit)) + unit; + res.w = this.convertUnit(res.w, unit) + unit; + res.h = this.convertUnit(res.h, unit) + unit; } out.push(' width="' + res.w + '" height="' + res.h + '"' + vb + ' xmlns="'+svgns+'"'); diff --git a/editor/units.js b/editor/units.js index e0d4cbb1..b1e1e60b 100644 --- a/editor/units.js +++ b/editor/units.js @@ -138,4 +138,39 @@ svgedit.units.setUnitAttr = function(elem, attr, val) { elem.setAttribute(attr, val); }; +var attrsToConvert = { + "line": ['x1', 'x2', 'y1', 'y2'], + "circle": ['cx', 'cy', 'r'], + "ellipse": ['cx', 'cy', 'rx', 'ry'], + "foreignObject": ['x', 'y', 'width', 'height'], + "rect": ['x', 'y', 'width', 'height'], + "image": ['x', 'y', 'width', 'height'], + "use": ['x', 'y', 'width', 'height'], + "text": ['x', 'y'] +}; + +// Function: svgedit.units.convertAttrs +// Converts all applicable attributes to the configured baseUnit +// +// Parameters: +// element - a DOM element whose attributes should be converted +svgedit.units.convertAttrs = function(element) { + var elName = element.tagName; + var unit = svgedit.units.config_.baseUnit; + var attrs = attrsToConvert[elName]; + if(!attrs) return; + var len = attrs.length + for(var i = 0; i < len; i++) { + var attr = attrs[i]; + var cur = element.getAttribute(attr); + if(cur) { + if(!isNaN(cur)) { + element.setAttribute(attr, (cur / svgedit.units.typeMap_[unit]) + unit); + } else { + // Convert existing? + } + } + } +}; + })(); \ No newline at end of file