Uniform clone and fromObject behaviour (#3212)

This commit is contained in:
Andrea Bogazzi 2016-09-10 16:09:17 +02:00 committed by GitHub
parent 08f60adbea
commit 876c9ca91f
15 changed files with 65 additions and 42 deletions

View file

@ -238,10 +238,13 @@
* @static
* @memberOf fabric.Circle
* @param {Object} object Object to create an instance from
* @param {function} [callback] invoked with new instance as first argument
* @return {Object} Instance of fabric.Circle
*/
fabric.Circle.fromObject = function(object) {
return new fabric.Circle(object);
fabric.Circle.fromObject = function(object, callback) {
var circle = new fabric.Circle(object);
callback && callback(circle);
return circle;
};
})(typeof exports !== 'undefined' ? exports : this);

View file

@ -202,10 +202,13 @@
* @static
* @memberOf fabric.Ellipse
* @param {Object} object Object to create an instance from
* @param {function} [callback] invoked with new instance as first argument
* @return {fabric.Ellipse}
*/
fabric.Ellipse.fromObject = function(object) {
return new fabric.Ellipse(object);
fabric.Ellipse.fromObject = function(object, callback) {
var ellipse = new fabric.Ellipse(object);
callback && callback(ellipse);
return ellipse;
};
})(typeof exports !== 'undefined' ? exports : this);

View file

@ -524,7 +524,6 @@
* @memberOf fabric.Group
* @param {Object} object Object to create a group from
* @param {Function} [callback] Callback to invoke when an group instance is created
* @return {fabric.Group} An instance of fabric.Group
*/
fabric.Group.fromObject = function(object, callback) {
fabric.util.enlivenObjects(object.objects, function(enlivenedObjects) {

View file

@ -350,15 +350,6 @@
return '#<fabric.Image: { src: "' + this.getSrc() + '" }>';
},
/**
* Returns a clone of an instance
* @param {Function} callback Callback is invoked with a clone as a first argument
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
*/
clone: function(callback, propertiesToInclude) {
this.constructor.fromObject(this.toObject(propertiesToInclude), callback);
},
/**
* Applies filters assigned to this image (from "filters" array)
* @method applyFilters
@ -614,7 +605,7 @@
* Creates an instance of fabric.Image from its object representation
* @static
* @param {Object} object Object to create an instance from
* @param {Function} [callback] Callback to invoke when an image instance is created
* @param {Function} callback Callback to invoke when an image instance is created
*/
fabric.Image.fromObject = function(object, callback) {
fabric.util.loadImage(object.src, function(img) {

View file

@ -1139,9 +1139,12 @@
* @static
* @memberOf fabric.IText
* @param {Object} object Object to create an instance from
* @param {function} [callback] invoked with new instance as argument
* @return {fabric.IText} instance of fabric.IText
*/
fabric.IText.fromObject = function(object) {
return new fabric.IText(object.text, clone(object));
fabric.IText.fromObject = function(object, callback) {
var iText = new fabric.IText(object.text, clone(object));
callback && callback(iText);
return iText;
};
})();

View file

@ -298,11 +298,14 @@
* @static
* @memberOf fabric.Line
* @param {Object} object Object to create an instance from
* @param {function} [callback] invoked with new instance as first argument
* @return {fabric.Line} instance of fabric.Line
*/
fabric.Line.fromObject = function(object) {
var points = [object.x1, object.y1, object.x2, object.y2];
return new fabric.Line(points, object);
fabric.Line.fromObject = function(object, callback) {
var points = [object.x1, object.y1, object.x2, object.y2],
line = new fabric.Line(points, object);
callback && callback(line);
return line;
};
/**

View file

@ -1292,7 +1292,8 @@
},
/**
* Clones an instance
* Clones an instance, some objects are async, so using callback method will work for every object.
* Using the direct return does not work for images and groups.
* @param {Function} callback Callback is invoked with a clone as a first argument
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
* @return {fabric.Object} clone of an instance

View file

@ -917,24 +917,27 @@
* @static
* @memberOf fabric.Path
* @param {Object} object
* @param {Function} callback Callback to invoke when an fabric.Path instance is created
* @param {Function} [callback] Callback to invoke when an fabric.Path instance is created
*/
fabric.Path.fromObject = function(object, callback) {
// remove this pattern rom 2.0, accept just object.
var path;
if (typeof object.path === 'string') {
fabric.loadSVGFromURL(object.path, function (elements) {
var path = elements[0],
pathUrl = object.path;
var pathUrl = object.path;
path = elements[0];
delete object.path;
fabric.util.object.extend(path, object);
path.setSourcePath(pathUrl);
callback(path);
callback && callback(path);
});
}
else {
callback(new fabric.Path(object.path, object));
path = new fabric.Path(object.path, object);
callback && callback(path);
return path;
}
};

View file

@ -241,9 +241,10 @@
* @static
* @memberOf fabric.PathGroup
* @param {Object} object Object to create an instance from
* @param {Function} callback Callback to invoke when an fabric.PathGroup instance is created
* @param {Function} [callback] Callback to invoke when an fabric.PathGroup instance is created
*/
fabric.PathGroup.fromObject = function(object, callback) {
// remove this pattern from 2.0 accepts only object
if (typeof object.paths === 'string') {
fabric.loadSVGFromURL(object.paths, function (elements) {

View file

@ -223,10 +223,13 @@
* @static
* @memberOf fabric.Polygon
* @param {Object} object Object to create an instance from
* @param {Function} [callback] Callback to invoke when an fabric.Path instance is created
* @return {fabric.Polygon} Instance of fabric.Polygon
*/
fabric.Polygon.fromObject = function(object) {
return new fabric.Polygon(object.points, object, true);
fabric.Polygon.fromObject = function(object, callback) {
var polygon = new fabric.Polygon(object.points, object);
callback && callback(polygon);
return polygon;
};
})(typeof exports !== 'undefined' ? exports : this);

View file

@ -167,11 +167,13 @@
* @static
* @memberOf fabric.Polyline
* @param {Object} object Object to create an instance from
* @param {Function} [callback] Callback to invoke when an fabric.Path instance is created
* @return {fabric.Polyline} Instance of fabric.Polyline
*/
fabric.Polyline.fromObject = function(object) {
var points = object.points;
return new fabric.Polyline(points, object, true);
fabric.Polyline.fromObject = function(object, callback) {
var polyline = new fabric.Polyline(object.points, object);
callback && callback(polyline);
return polyline;
};
})(typeof exports !== 'undefined' ? exports : this);

View file

@ -233,10 +233,13 @@
* @static
* @memberOf fabric.Rect
* @param {Object} object Object to create an instance from
* @param {Function} [callback] Callback to invoke when an fabric.Rect instance is created
* @return {Object} instance of fabric.Rect
*/
fabric.Rect.fromObject = function(object) {
return new fabric.Rect(object);
fabric.Rect.fromObject = function(object, callback) {
var rect = new fabric.Rect(object);
callback && callback(rect);
return rect;
};
})(typeof exports !== 'undefined' ? exports : this);

View file

@ -50,7 +50,6 @@
text: true,
charSpacing: true,
textAlign: true,
stroke: false,
strokeWidth: false,
},
@ -1225,10 +1224,13 @@
* @static
* @memberOf fabric.Text
* @param {Object} object Object to create an instance from
* @param {Function} [callback] Callback to invoke when an fabric.Text instance is created
* @return {fabric.Text} Instance of fabric.Text
*/
fabric.Text.fromObject = function(object) {
return new fabric.Text(object.text, clone(object));
fabric.Text.fromObject = function(object, callback) {
var text = new fabric.Text(object.text, clone(object));
callback && callback(text);
return text;
};
fabric.util.createAccessors(fabric.Text);

View file

@ -443,10 +443,13 @@
* @static
* @memberOf fabric.Textbox
* @param {Object} object Object to create an instance from
* @param {Function} [callback] Callback to invoke when an fabric.Textbox instance is created
* @return {fabric.Textbox} instance of fabric.Textbox
*/
fabric.Textbox.fromObject = function(object) {
return new fabric.Textbox(object.text, clone(object));
fabric.Textbox.fromObject = function(object, callback) {
var textbox = new fabric.Textbox(object.text, clone(object));
callback && callback(textbox);
return textbox;
};
/**
* Returns the default controls visibility required for Textboxes.

View file

@ -115,10 +115,13 @@
* @static
* @memberOf fabric.Triangle
* @param {Object} object Object to create an instance from
* @param {Function} [callback] Callback to invoke when an fabric.Triangle instance is created
* @return {Object} instance of Canvas.Triangle
*/
fabric.Triangle.fromObject = function(object) {
return new fabric.Triangle(object);
fabric.Triangle.fromObject = function(object, callback) {
var triangle = new fabric.Triangle(object);
callback && callback(triangle);
return triangle;
};
})(typeof exports !== 'undefined' ? exports : this);