diff --git a/editor/history.js b/editor/history.js index 98eb5a36..a3bce074 100644 --- a/editor/history.js +++ b/editor/history.js @@ -36,13 +36,23 @@ var removedElements = {}; * An interface that all command objects must implement. * * interface svgedit.history.HistoryCommand { - * void apply(); - * void unapply(); + * void apply(svgedit.history.HistoryEventHandler); + * void unapply(svgedit.history.HistoryEventHandler); * Element[] elements(); * String getText(); * * static String type(); * } + * + * Interface: svgedit.history.HistoryEventHandler + * An interface for objects that will handle history events. + * + * interface svgedit.history.HistoryEventHandler { + * void handleHistoryEvent(eventType, command); + * } + * + * eventType is a string conforming to one of the HistoryEvent types. + * command is an object fulfilling the HistoryCommand interface. */ // Class: svgedit.history.MoveElementCommand @@ -443,18 +453,6 @@ svgedit.history.BatchCommand.prototype.isEmpty = function() { }; -/** - * Interface: svgedit.history.HistoryEventHandler - * An interface for objects that will handle history events. - * - * interface svgedit.history.HistoryEventHandler { - * void handleHistoryEvent(eventType, command); - * } - * - * eventType is a string conforming to one of the HistoryEvent types (see above). - * command is an object fulfilling the HistoryCommand interface (see above). - */ - // Class: svgedit.history.UndoManager // Parameters: // historyEventHandler - an object that conforms to the HistoryEventHandler interface diff --git a/editor/svg-editor.js b/editor/svg-editor.js index 55bd6450..d3081981 100644 --- a/editor/svg-editor.js +++ b/editor/svg-editor.js @@ -1535,8 +1535,8 @@ } if(unit) { - x = svgCanvas.convertUnit(x); - y = svgCanvas.convertUnit(y); + x = svgedit.units.convertUnit(x); + y = svgedit.units.convertUnit(y); } $('#selected_x').val(x || 0); @@ -1560,8 +1560,8 @@ if(point) { var seg_type = $('#seg_type'); if(unit) { - point.x = svgCanvas.convertUnit(point.x); - point.y = svgCanvas.convertUnit(point.y); + point.x = svgedit.units.convertUnit(point.x); + point.y = svgedit.units.convertUnit(point.y); } $('#path_node_x').val(point.x); $('#path_node_y').val(point.y); @@ -1622,7 +1622,7 @@ var attrVal = elem.getAttribute(item); if(curConfig.baseUnit !== 'px' && elem[item]) { var bv = elem[item].baseVal.value; - attrVal = svgCanvas.convertUnit(bv); + attrVal = svgedit.units.convertUnit(bv); } $('#' + el_name + '_' + item).val(attrVal || 0); @@ -2734,8 +2734,8 @@ // update resolution option with actual resolution var res = svgCanvas.getResolution(); if(curConfig.baseUnit !== "px") { - res.w = svgCanvas.convertUnit(res.w) + curConfig.baseUnit; - res.h = svgCanvas.convertUnit(res.h) + curConfig.baseUnit; + res.w = svgedit.units.convertUnit(res.w) + curConfig.baseUnit; + res.h = svgedit.units.convertUnit(res.h) + curConfig.baseUnit; } $('#canvas_width').val(res.w); diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index 239db3f9..08b89f25 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -385,7 +385,8 @@ svgedit.units.init({ getBaseUnit: function() { return curConfig.baseUnit; }, getElement: getElem, getHeight: function() { return svgcontent.getAttribute("height")/current_zoom; }, - getWidth: function() { return svgcontent.getAttribute("width")/current_zoom; } + getWidth: function() { return svgcontent.getAttribute("width")/current_zoom; }, + getRoundDigits: function() { return save_options.round_digits; } }); // import from units.js var convertToNum = canvas.convertToNum = svgedit.units.convertToNum; @@ -425,7 +426,7 @@ canvas.undoMgr = new svgedit.history.UndoManager({ call("changed", elems); var cmdType = cmd.type(); - var isApply = eventType == EventTypes.AFTER_APPLY; + var isApply = (eventType == EventTypes.AFTER_APPLY); if (cmdType == MoveElementCommand.type()) { var parent = isApply ? cmd.newParent : cmd.oldParent; if (parent == svgcontent) { @@ -634,32 +635,6 @@ this.addExtension = function(name, ext_func) { console.log('Cannot add extension "' + name + '", an extension by that name already exists"'); } }; - - -// Function: shortFloat -// Rounds a given value to a float with number of digits defined in save_options -// -// Parameters: -// val - The value as a String, Number or Array of two numbers to be rounded -// -// Returns: -// If a string/number was given, returns a Float. If an array, return a string -// with comma-seperated floats -var shortFloat = function(val) { - var digits = save_options.round_digits; - if(!isNaN(val)) { - // Note that + converts to Number - return +((+val).toFixed(digits)); - } else if($.isArray(val)) { - return shortFloat(val[0]) + ',' + shortFloat(val[1]); - } else { - return parseFloat(val).toFixed(digits) - 0; - } -} - -this.convertUnit = function(val, unit) { - return shortFloat(svgedit.units.convertUnit(val, unit)); -}; // This method rounds the incoming value to the nearest value based on the current_zoom var round = this.round = function(val) { @@ -5826,9 +5801,9 @@ var pathActions = canvas.pathActions = function() { var addToD = function(pnts, more, last) { var str = ''; var more = more?' '+more.join(' '):''; - var last = last?' '+shortFloat(last):''; + var last = last?' '+svgedit.units.shortFloat(last):''; $.each(pnts, function(i, pnt) { - pnts[i] = shortFloat(pnt); + pnts[i] = svgedit.units.shortFloat(pnt); }); d += letter + pnts.join(' ') + more + last; } @@ -6113,16 +6088,16 @@ this.svgToString = function(elem, indent) { // if(curConfig.baseUnit !== "px") { // var unit = curConfig.baseUnit; // var unit_m = svgedit.units.getTypeMap()[unit]; -// res.w = shortFloat(res.w / unit_m) -// res.h = shortFloat(res.h / unit_m) +// res.w = svgedit.units.shortFloat(res.w / unit_m) +// res.h = svgedit.units.shortFloat(res.h / unit_m) // vb = ' viewBox="' + [0, 0, res.w, res.h].join(' ') + '"'; // res.w += unit; // res.h += unit; // } if(unit !== "px") { - res.w = this.convertUnit(res.w, unit) + unit; - res.h = this.convertUnit(res.h, unit) + unit; + res.w = svgedit.units.convertUnit(res.w, unit) + unit; + res.h = svgedit.units.convertUnit(res.h, unit) + unit; } out.push(' width="' + res.w + '" height="' + res.h + '"' + vb + ' xmlns="'+svgns+'"'); @@ -6174,9 +6149,9 @@ this.svgToString = function(elem, indent) { out.push(" "); if(attr.localName === 'd') attrVal = pathActions.convertPath(elem, true); if(!isNaN(attrVal)) { - attrVal = shortFloat(attrVal); + attrVal = svgedit.units.shortFloat(attrVal); } else if(unit_re.test(attrVal)) { - attrVal = shortFloat(attrVal) + unit; + attrVal = svgedit.units.shortFloat(attrVal) + unit; } // Embed images when saving @@ -9657,7 +9632,6 @@ this.getPrivateMethods = function() { round: round, runExtensions: runExtensions, sanitizeSvg: sanitizeSvg, - shortFloat: shortFloat, SVGEditTransformList: svgedit.transformlist.SVGTransformList, toString: toString, transformBox: svgedit.math.transformBox, diff --git a/editor/svgutils.js b/editor/svgutils.js index f5ba950e..3dbf0f9d 100644 --- a/editor/svgutils.js +++ b/editor/svgutils.js @@ -186,7 +186,7 @@ svgedit.utilities.convertToXMLReferences = function(input) { return output; }; -// Function: text2xml +// Function: svgedit.utilities.text2xml // Cross-browser compatible method of converting a string to an XML tree // found this function here: http://groups.google.com/group/jquery-dev/browse_thread/thread/c6d11387c580a77f svgedit.utilities.text2xml = function(sXML) { @@ -209,7 +209,7 @@ svgedit.utilities.text2xml = function(sXML) { return out; }; -// Function: bboxToObj +// Function: svgedit.utilities.bboxToObj // Converts a SVGRect into an object. // // Parameters: @@ -226,7 +226,7 @@ svgedit.utilities.bboxToObj = function(bbox) { } }; -// Function: walkTree +// Function: svgedit.utilities.walkTree // Walks the tree and executes the callback on each element in a top-down fashion // // Parameters: @@ -242,7 +242,7 @@ svgedit.utilities.walkTree = function(elem, cbFn){ } }; -// Function: walkTreePost +// Function: svgedit.utilities.walkTreePost // Walks the tree and executes the callback on each element in a depth-first fashion // TODO: FIXME: Shouldn't this be calling walkTreePost? // diff --git a/editor/units.js b/editor/units.js index 237489ae..8be858cb 100644 --- a/editor/units.js +++ b/editor/units.js @@ -51,6 +51,7 @@ var typeMap_ = {px: 1}; * function getElement() - returns an element in the container given an id * function getHeight() - returns the container's height * function getWidth() - returns the container's width + * function getRoundDigits() - returns the number of digits number should be rounded to */ /** @@ -95,6 +96,26 @@ svgedit.units.getTypeMap = function() { return typeMap_; }; +// Function: svgedit.units.shortFloat +// Rounds a given value to a float with number of digits defined in save_options +// +// Parameters: +// val - The value as a String, Number or Array of two numbers to be rounded +// +// Returns: +// If a string/number was given, returns a Float. If an array, return a string +// with comma-seperated floats +svgedit.units.shortFloat = function(val) { + var digits = elementContainer_.getRoundDigits(); + if(!isNaN(val)) { + // Note that + converts to Number + return +((+val).toFixed(digits)); + } else if($.isArray(val)) { + return svgedit.units.shortFloat(val[0]) + ',' + svgedit.units.shortFloat(val[1]); + } + return parseFloat(val).toFixed(digits) - 0; +}; + // Function: svgedit.units.convertUnit // Converts the number to given unit or baseUnit svgedit.units.convertUnit = function(val, unit) { @@ -102,7 +123,7 @@ svgedit.units.convertUnit = function(val, unit) { // baseVal.convertToSpecifiedUnits(unitNumMap[unit]); // var val = baseVal.valueInSpecifiedUnits; // baseVal.convertToSpecifiedUnits(1); - return val / typeMap_[unit]; + return svgedit.unit.shortFloat(val / typeMap_[unit]); }; // Function: svgedit.units.setUnitAttr @@ -249,7 +270,9 @@ svgedit.units.isValidUnit = function(attr, val) { result = (elem == null); } catch(e) {} return result; - } else valid = true; + } else { + valid = true; + } return valid; }; diff --git a/test/all_tests.html b/test/all_tests.html index 2fe0c3ef..3ae234e5 100644 --- a/test/all_tests.html +++ b/test/all_tests.html @@ -12,6 +12,7 @@ + \ No newline at end of file diff --git a/test/units_test.html b/test/units_test.html new file mode 100644 index 00000000..bbb263b6 --- /dev/null +++ b/test/units_test.html @@ -0,0 +1,60 @@ + + + + + + + + + + +

Unit Tests for units.js

+

+

+
    +
+ + +