From c5f60743f5f181f193a4a6c18883e3d0a6570009 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Sat, 12 Nov 2016 23:54:42 +0100 Subject: [PATCH] To object origin (#3416) * deleted double default cleaning * rework object export * fixed tests * fix fabric2 api change. * removed unused var --- src/shapes/circle.class.js | 6 +----- src/shapes/ellipse.class.js | 5 +---- src/shapes/image.class.js | 23 +++++++++-------------- src/shapes/itext.class.js | 10 +--------- src/shapes/object.class.js | 5 ++--- src/shapes/path.class.js | 11 ++--------- src/shapes/path_group.class.js | 5 +---- src/shapes/rect.class.js | 9 +-------- src/shapes/text.class.js | 30 +++++++++++++----------------- src/shapes/textbox.class.js | 4 +--- test/unit/canvas.js | 2 +- test/unit/canvas_static.js | 4 ++-- 12 files changed, 35 insertions(+), 79 deletions(-) diff --git a/src/shapes/circle.class.js b/src/shapes/circle.class.js index e8515fa2543..df56730d239 100644 --- a/src/shapes/circle.class.js +++ b/src/shapes/circle.class.js @@ -84,11 +84,7 @@ * @return {Object} object representation of an instance */ toObject: function(propertiesToInclude) { - return extend(this.callSuper('toObject', propertiesToInclude), { - radius: this.get('radius'), - startAngle: this.startAngle, - endAngle: this.endAngle - }); + return this.callSuper('toObject', ['radius', 'startAngle', 'endAngle'].concat(propertiesToInclude)); }, /* _TO_SVG_START_ */ diff --git a/src/shapes/ellipse.class.js b/src/shapes/ellipse.class.js index 6feb6e58f12..d6226590d1a 100644 --- a/src/shapes/ellipse.class.js +++ b/src/shapes/ellipse.class.js @@ -101,10 +101,7 @@ * @return {Object} object representation of an instance */ toObject: function(propertiesToInclude) { - return extend(this.callSuper('toObject', propertiesToInclude), { - rx: this.get('rx'), - ry: this.get('ry') - }); + return this.callSuper('toObject', ['rx', 'ry'].concat(propertiesToInclude)); }, /* _TO_SVG_START_ */ diff --git a/src/shapes/image.class.js b/src/shapes/image.class.js index ddaddca3b18..71c228d89b4 100644 --- a/src/shapes/image.class.js +++ b/src/shapes/image.class.js @@ -259,24 +259,19 @@ this.resizeFilters.forEach(function(filterObj) { filterObj && resizeFilters.push(filterObj.toObject()); }); - - var object = extend(this.callSuper('toObject', propertiesToInclude), { - src: this.getSrc(), - filters: filters, - resizeFilters: resizeFilters, - crossOrigin: this.crossOrigin, - alignX: this.alignX, - alignY: this.alignY, - meetOrSlice: this.meetOrSlice - }); + var object = extend( + this.callSuper( + 'toObject', + ['crossOrigin', 'alignX', 'alignY', 'meetOrSlice'].concat(propertiesToInclude) + ), { + src: this.getSrc(), + filters: filters, + resizeFilters: resizeFilters, + }); object.width /= scaleX; object.height /= scaleY; - if (!this.includeDefaultValues) { - this._removeDefaultValues(object); - } - return object; }, diff --git a/src/shapes/itext.class.js b/src/shapes/itext.class.js index 9382c911937..e68e6e68922 100644 --- a/src/shapes/itext.class.js +++ b/src/shapes/itext.class.js @@ -1146,16 +1146,8 @@ * @return {Object} object representation of an instance */ toObject: function(propertiesToInclude) { - var clonedStyles = { }, i, j, row; - for (i in this.styles) { - row = this.styles[i]; - clonedStyles[i] = { }; - for (j in row) { - clonedStyles[i][j] = clone(row[j]); - } - } return fabric.util.object.extend(this.callSuper('toObject', propertiesToInclude), { - styles: clonedStyles + styles: clone(this.styles, true) }); } }); diff --git a/src/shapes/object.class.js b/src/shapes/object.class.js index 1ef44c44828..644ece7cdc0 100644 --- a/src/shapes/object.class.js +++ b/src/shapes/object.class.js @@ -892,12 +892,12 @@ skewY: toFixed(this.skewY, NUM_FRACTION_DIGITS) }; + fabric.util.populateWithProperties(this, object, propertiesToInclude); + if (!this.includeDefaultValues) { object = this._removeDefaultValues(object); } - fabric.util.populateWithProperties(this, object, propertiesToInclude); - return object; }, @@ -918,7 +918,6 @@ _removeDefaultValues: function(object) { var prototype = fabric.util.getKlass(object.type).prototype, stateProperties = prototype.stateProperties; - stateProperties.forEach(function(prop) { if (object[prop] === prototype[prop]) { delete object[prop]; diff --git a/src/shapes/path.class.js b/src/shapes/path.class.js index fdeb20ea439..2a2af92819b 100644 --- a/src/shapes/path.class.js +++ b/src/shapes/path.class.js @@ -473,16 +473,9 @@ * @return {Object} object representation of an instance */ toObject: function(propertiesToInclude) { - var o = extend(this.callSuper('toObject', propertiesToInclude), { - path: this.path.map(function(item) { return item.slice() }), - pathOffset: this.pathOffset + var o = extend(this.callSuper('toObject', ['sourcePath', 'pathOffset'].concat(propertiesToInclude)), { + path: this.path.map(function(item) { return item.slice() }) }); - if (this.sourcePath) { - o.sourcePath = this.sourcePath; - } - if (this.transformMatrix) { - o.transformMatrix = this.transformMatrix; - } return o; }, diff --git a/src/shapes/path_group.class.js b/src/shapes/path_group.class.js index 9a0b4307b76..056b2fba4f1 100644 --- a/src/shapes/path_group.class.js +++ b/src/shapes/path_group.class.js @@ -143,12 +143,9 @@ * @return {Object} object representation of an instance */ toObject: function(propertiesToInclude) { - var o = extend(parentToObject.call(this, propertiesToInclude), { + var o = extend(parentToObject.call(this, ['sourcePath'].concat(propertiesToInclude)), { paths: invoke(this.getObjects(), 'toObject', propertiesToInclude) }); - if (this.sourcePath) { - o.sourcePath = this.sourcePath; - } return o; }, diff --git a/src/shapes/rect.class.js b/src/shapes/rect.class.js index 4faa0467334..0077d3680f8 100644 --- a/src/shapes/rect.class.js +++ b/src/shapes/rect.class.js @@ -150,14 +150,7 @@ * @return {Object} object representation of an instance */ toObject: function(propertiesToInclude) { - var object = extend(this.callSuper('toObject', propertiesToInclude), { - rx: this.get('rx') || 0, - ry: this.get('ry') || 0 - }); - if (!this.includeDefaultValues) { - this._removeDefaultValues(object); - } - return object; + return this.callSuper('toObject', ['rx', 'ry'].concat(propertiesToInclude)); }, /* _TO_SVG_START_ */ diff --git a/src/shapes/text.class.js b/src/shapes/text.class.js index 11bdeed8d62..e9bf682cf6d 100644 --- a/src/shapes/text.class.js +++ b/src/shapes/text.class.js @@ -3,7 +3,6 @@ 'use strict'; var fabric = global.fabric || (global.fabric = { }), - extend = fabric.util.object.extend, clone = fabric.util.object.clone, toFixed = fabric.util.toFixed, NUM_FRACTION_DIGITS = fabric.Object.NUM_FRACTION_DIGITS, @@ -872,22 +871,19 @@ * @return {Object} Object representation of an instance */ toObject: function(propertiesToInclude) { - var object = extend(this.callSuper('toObject', propertiesToInclude), { - text: this.text, - fontSize: this.fontSize, - fontWeight: this.fontWeight, - fontFamily: this.fontFamily, - fontStyle: this.fontStyle, - lineHeight: this.lineHeight, - textDecoration: this.textDecoration, - textAlign: this.textAlign, - textBackgroundColor: this.textBackgroundColor, - charSpacing: this.charSpacing - }); - if (!this.includeDefaultValues) { - this._removeDefaultValues(object); - } - return object; + var additionalProperties = [ + 'text', + 'fontSize', + 'fontWeight', + 'fontFamily', + 'fontStyle', + 'lineHeight', + 'textDecoration', + 'textAlign', + 'textBackgroundColor', + 'charSpacing' + ].concat(propertiesToInclude); + return this.callSuper('toObject', additionalProperties); }, /* _TO_SVG_START_ */ diff --git a/src/shapes/textbox.class.js b/src/shapes/textbox.class.js index 8c1c4cb0010..870c174ab63 100644 --- a/src/shapes/textbox.class.js +++ b/src/shapes/textbox.class.js @@ -435,9 +435,7 @@ * @return {Object} object representation of an instance */ toObject: function(propertiesToInclude) { - return fabric.util.object.extend(this.callSuper('toObject', propertiesToInclude), { - minWidth: this.minWidth - }); + return this.callSuper('toObject', ['minWidth'].concat(propertiesToInclude)); } }); /** diff --git a/test/unit/canvas.js b/test/unit/canvas.js index f37dee01985..ce23997a54a 100644 --- a/test/unit/canvas.js +++ b/test/unit/canvas.js @@ -41,7 +41,7 @@ var PATH_DATALESS_JSON = '{"objects":[{"type":"path","originX":"left","originY":"top","left":100,"top":100,"width":200,"height":200,"fill":"rgb(0,0,0)",' + '"stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,' + '"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,' + - '"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"path":"http://example.com/","pathOffset":{"x":200,"y":200}}],"background":""}'; + '"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"pathOffset":{"x":200,"y":200},"path":"http://example.com/"}],"background":""}'; var RECT_JSON = '{"objects":[{"type":"rect","originX":"left","originY":"top","left":0,"top":0,"width":10,"height":10,"fill":"rgb(0,0,0)",' + '"stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,' + diff --git a/test/unit/canvas_static.js b/test/unit/canvas_static.js index cde812b821d..035f34c5bac 100644 --- a/test/unit/canvas_static.js +++ b/test/unit/canvas_static.js @@ -28,7 +28,7 @@ var PATH_DATALESS_JSON = '{"objects":[{"type":"path","originX":"left","originY":"top","left":100,"top":100,"width":200,"height":200,"fill":"rgb(0,0,0)",' + '"stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,' + '"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,' + - '"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"path":"http://example.com/","pathOffset":{"x":200,"y":200}}],"background":""}'; + '"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"pathOffset":{"x":200,"y":200},"path":"http://example.com/"}],"background":""}'; var RECT_JSON = '{"objects":[{"type":"rect","originX":"left","originY":"top","left":0,"top":0,"width":10,"height":10,"fill":"rgb(0,0,0)",' + '"stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,' + @@ -38,7 +38,7 @@ var RECT_JSON_WITH_PADDING = '{"objects":[{"type":"rect","originX":"left","originY":"top","left":0,"top":0,"width":10,"height":20,"fill":"rgb(0,0,0)",' + '"stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,' + '"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,' + - '"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"padding":123,"foo":"bar","rx":0,"ry":0}],"background":""}'; + '"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"rx":0,"ry":0,"padding":123,"foo":"bar"}],"background":""}'; function getAbsolutePath(path) { var isAbsolute = /^https?:/.test(path);