From c1cdc6d40eb7986bd7607c67b84054e8c3847895 Mon Sep 17 00:00:00 2001 From: ShaMan123 Date: Sun, 12 Jun 2022 07:24:57 +0300 Subject: [PATCH 1/4] chore(): move away from extend --- src/controls.actions.js | 6 ++---- src/parser.js | 10 +++++----- src/util/animate.js | 5 +---- src/util/animate_color.js | 2 +- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/controls.actions.js b/src/controls.actions.js index 659cde1a619..a28b04f5bc0 100644 --- a/src/controls.actions.js +++ b/src/controls.actions.js @@ -32,10 +32,8 @@ function fireEvent(eventName, options) { var target = options.transform.target, - canvas = target.canvas, - canvasOptions = fabric.util.object.clone(options); - canvasOptions.target = target; - canvas && canvas.fire('object:' + eventName, canvasOptions); + canvas = target.canvas; + canvas && canvas.fire('object:' + eventName, Object.assign({}, options, { target: target })); target.fire(eventName, options); } diff --git a/src/parser.js b/src/parser.js index cd729a08b7b..f7e3a35c931 100644 --- a/src/parser.js +++ b/src/parser.js @@ -876,11 +876,11 @@ }, { }); // add values parsed from style, which take precedence over attributes // (see: http://www.w3.org/TR/SVG/styling.html#UsingPresentationAttributes) - var cssAttrs = extend( + var cssAttrs = Object.assign( getGlobalStylesForElement(element, svgUid), fabric.parseStyleAttribute(element) ); - ownAttributes = extend( + ownAttributes = Object.assign( ownAttributes, cssAttrs ); @@ -902,7 +902,7 @@ if (normalizedStyle && normalizedStyle.font) { fabric.parseFontDeclaration(normalizedStyle.font, normalizedStyle); } - var mergedAttrs = extend(parentAttributes, normalizedStyle); + var mergedAttrs = Object.assign(parentAttributes, normalizedStyle); return fabric.svgValidParentsRegEx.test(element.nodeName) ? mergedAttrs : _setStrokeFillOpacity(mergedAttrs); }, @@ -1026,10 +1026,10 @@ return; } if (allRules[_rule]) { - fabric.util.object.extend(allRules[_rule], ruleObj); + Object.assign(allRules[_rule], ruleObj); } else { - allRules[_rule] = fabric.util.object.clone(ruleObj); + allRules[_rule] = Object.assign({}, ruleObj); } }); }); diff --git a/src/util/animate.js b/src/util/animate.js index bc795adb929..67b270944de 100644 --- a/src/util/animate.js +++ b/src/util/animate.js @@ -1,8 +1,5 @@ (function () { - var extend = fabric.util.object.extend, - clone = fabric.util.object.clone; - /** * * @typedef {Object} AnimationOptions @@ -157,7 +154,7 @@ return index > -1 && fabric.runningAnimations.splice(index, 1)[0]; }; - context = extend(clone(options), { + context = Object.assign({}, options, { cancel: function () { cancel = true; return removeFromRegistry(); diff --git a/src/util/animate_color.js b/src/util/animate_color.js index 5d819a2a4ca..8183ca8f618 100644 --- a/src/util/animate_color.js +++ b/src/util/animate_color.js @@ -33,7 +33,7 @@ originalOnChange = options.onChange; options = options || {}; - return fabric.util.animate(fabric.util.object.extend(options, { + return fabric.util.animate(Object.assign(options, { duration: duration || 500, startValue: startColor, endValue: endColor, From 1d10bfaab2d2cbf806b5789d9e2211f52908aacf Mon Sep 17 00:00:00 2001 From: ShaMan123 Date: Sun, 12 Jun 2022 07:45:02 +0300 Subject: [PATCH 2/4] chore(): replace shallow clone/extend --- src/filters/blendimage_filter.class.js | 4 +--- src/mixins/animation.mixin.js | 7 +------ src/mixins/canvas_serialization.mixin.js | 2 +- src/mixins/itext_behavior.mixin.js | 16 +++++++--------- src/mixins/itext_key_behavior.mixin.js | 2 +- src/parser.js | 6 ++---- src/shapes/group.class.js | 5 ++--- src/shapes/image.class.js | 2 +- src/shapes/rect.class.js | 2 +- src/shapes/text.class.js | 2 +- 10 files changed, 18 insertions(+), 30 deletions(-) diff --git a/src/filters/blendimage_filter.class.js b/src/filters/blendimage_filter.class.js index 5ff2d480da5..7f0edbc11b7 100644 --- a/src/filters/blendimage_filter.class.js +++ b/src/filters/blendimage_filter.class.js @@ -237,9 +237,7 @@ */ fabric.Image.filters.BlendImage.fromObject = function(object) { return fabric.Image.fromObject(object.image).then(function(image) { - var options = fabric.util.object.clone(object); - options.image = image; - return new fabric.Image.filters.BlendImage(options); + return new fabric.Image.filters.BlendImage(Object.assign({}, options, { image: image })); }); }; diff --git a/src/mixins/animation.mixin.js b/src/mixins/animation.mixin.js index 4890a78094c..04b415dba12 100644 --- a/src/mixins/animation.mixin.js +++ b/src/mixins/animation.mixin.js @@ -157,12 +157,7 @@ fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prot to = to.toString(); - if (!options) { - options = { }; - } - else { - options = fabric.util.object.clone(options); - } + options = Object.assign({}, options); if (~property.indexOf('.')) { propPair = property.split('.'); diff --git a/src/mixins/canvas_serialization.mixin.js b/src/mixins/canvas_serialization.mixin.js index e82d665b090..e3496d51d73 100644 --- a/src/mixins/canvas_serialization.mixin.js +++ b/src/mixins/canvas_serialization.mixin.js @@ -27,7 +27,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, /** @lends fabric.Stati // serialize if it wasn't already var serialized = (typeof json === 'string') ? JSON.parse(json) - : fabric.util.object.clone(json); + : Object.assign({}, json); var _this = this, renderOnAddRemove = this.renderOnAddRemove; diff --git a/src/mixins/itext_behavior.mixin.js b/src/mixins/itext_behavior.mixin.js index 9ded51c0747..275ca744503 100644 --- a/src/mixins/itext_behavior.mixin.js +++ b/src/mixins/itext_behavior.mixin.js @@ -1,7 +1,5 @@ (function() { - var clone = fabric.util.object.clone; - fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.prototype */ { /** @@ -713,7 +711,7 @@ shiftLineStyles: function(lineIndex, offset) { // shift all line styles by offset upward or downward // do not clone deep. we need new array, not new style objects - var clonedStyles = clone(this.styles); + var clonedStyles = Object.assign({}, this.styles); for (var line in this.styles) { var numericLine = parseInt(line, 10); if (numericLine > lineIndex) { @@ -782,10 +780,10 @@ // we clone current char style onto the next (otherwise empty) line while (qty > 0) { if (copiedStyle && copiedStyle[qty - 1]) { - this.styles[lineIndex + qty] = { 0: clone(copiedStyle[qty - 1]) }; + this.styles[lineIndex + qty] = { 0: Object.assign({}, copiedStyle[qty - 1]) }; } else if (currentCharStyle) { - this.styles[lineIndex + qty] = { 0: clone(currentCharStyle) }; + this.styles[lineIndex + qty] = { 0: Object.assign({}, currentCharStyle) }; } else { delete this.styles[lineIndex + qty]; @@ -806,8 +804,8 @@ if (!this.styles) { this.styles = {}; } - var currentLineStyles = this.styles[lineIndex], - currentLineStylesCloned = currentLineStyles ? clone(currentLineStyles) : {}; + var currentLineStyles = this.styles[lineIndex], + currentLineStylesCloned = currentLineStyles ? Object.assign({}, currentLineStyles) : {}; quantity || (quantity = 1); // shift all char styles by quantity forward @@ -831,7 +829,7 @@ if (!this.styles[lineIndex]) { this.styles[lineIndex] = {}; } - this.styles[lineIndex][charIndex + quantity] = clone(copiedStyle[quantity]); + this.styles[lineIndex][charIndex + quantity] = Object.assign({}, copiedStyle[quantity]); } return; } @@ -840,7 +838,7 @@ } var newStyle = currentLineStyles[charIndex ? charIndex - 1 : 1]; while (newStyle && quantity--) { - this.styles[lineIndex][charIndex + quantity] = clone(newStyle); + this.styles[lineIndex][charIndex + quantity] = Object.assign({}, newStyle); } }, diff --git a/src/mixins/itext_key_behavior.mixin.js b/src/mixins/itext_key_behavior.mixin.js index 8372dbd44f0..dbbb5d7c907 100644 --- a/src/mixins/itext_key_behavior.mixin.js +++ b/src/mixins/itext_key_behavior.mixin.js @@ -50,7 +50,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot * your prototype. * the map change will affect all Instances unless you need for only some text Instances * in that case you have to clone this object and assign your Instance. - * this.keysMap = fabric.util.object.clone(this.keysMap); + * this.keysMap = Object.assign({}, this.keysMap); * The function must be in fabric.Itext.prototype.myFunction And will receive event as args[0] */ keysMap: { diff --git a/src/parser.js b/src/parser.js index f7e3a35c931..ebb9bdba90a 100644 --- a/src/parser.js +++ b/src/parser.js @@ -8,8 +8,6 @@ */ var fabric = global.fabric || (global.fabric = { }), - extend = fabric.util.object.extend, - clone = fabric.util.object.clone, toFixed = fabric.util.toFixed, parseUnit = fabric.util.parseUnit, multiplyTransformMatrices = fabric.util.multiplyTransformMatrices, @@ -742,7 +740,7 @@ delete fabric.cssRules[svgUid]; delete fabric.clipPaths[svgUid]; } - }, clone(options), reviver, parsingOptions); + }, Object.assign({}, options), reviver, parsingOptions); }; function recursivelyParseGradientsXlink(doc, gradient) { @@ -773,7 +771,7 @@ fabric.reNum + '(?:px|cm|mm|em|pt|pc|in)*)(?:\\/(normal|' + fabric.reNum + '))?\\s+(.*)'); - extend(fabric, { + fabric.util.object.extend(fabric, { /** * Parses a short font declaration, building adding its properties to a style object * @static diff --git a/src/shapes/group.class.js b/src/shapes/group.class.js index f04a0723705..b78c724d6db 100644 --- a/src/shapes/group.class.js +++ b/src/shapes/group.class.js @@ -8,8 +8,7 @@ transformPoint = fabric.util.transformPoint, applyTransformToObject = fabric.util.applyTransformToObject, degreesToRadians = fabric.util.degreesToRadians, - clone = fabric.util.object.clone, - extend = fabric.util.object.extend; + clone = fabric.util.object.clone; if (fabric.Group) { fabric.warn('fabric.Group is already defined'); @@ -184,7 +183,7 @@ * @private */ __objectMonitor: function (opt) { - this._applyLayoutStrategy(extend(clone(opt), { + this._applyLayoutStrategy(Object.assign({}, opt, { type: 'object_modified' })); this._set('dirty', true); diff --git a/src/shapes/image.class.js b/src/shapes/image.class.js index f4a1fc78ace..0a246de11ab 100644 --- a/src/shapes/image.class.js +++ b/src/shapes/image.class.js @@ -684,7 +684,7 @@ * @returns {Promise} */ fabric.Image.fromObject = function(_object) { - var object = fabric.util.object.clone(_object), + var object = Object.assign({}, _object), filters = object.filters, resizeFilter = object.resizeFilter; // the generic enliving will fail on filters for now diff --git a/src/shapes/rect.class.js b/src/shapes/rect.class.js index 4b3d4166e12..2b13fd7f1ca 100644 --- a/src/shapes/rect.class.js +++ b/src/shapes/rect.class.js @@ -167,7 +167,7 @@ parsedAttributes.top = parsedAttributes.top || 0; parsedAttributes.height = parsedAttributes.height || 0; parsedAttributes.width = parsedAttributes.width || 0; - var rect = new fabric.Rect(extend((options ? fabric.util.object.clone(options) : { }), parsedAttributes)); + var rect = new fabric.Rect(Object.assign({}, options, parsedAttributes)); rect.visible = rect.visible && rect.width > 0 && rect.height > 0; callback(rect); }; diff --git a/src/shapes/text.class.js b/src/shapes/text.class.js index 1d99be796cf..2ea9cbbe867 100644 --- a/src/shapes/text.class.js +++ b/src/shapes/text.class.js @@ -1649,7 +1649,7 @@ var parsedAttributes = fabric.parseAttributes(element, fabric.Text.ATTRIBUTE_NAMES), parsedAnchor = parsedAttributes.textAnchor || 'left'; - options = fabric.util.object.extend((options ? clone(options) : { }), parsedAttributes); + options = Object.assign({}, options, parsedAttributes); options.top = options.top || 0; options.left = options.left || 0; From 949bd8f5f4d88a91fec4339a3e2a18aaa041f613 Mon Sep 17 00:00:00 2001 From: ShaMan123 Date: Sun, 12 Jun 2022 07:45:45 +0300 Subject: [PATCH 3/4] chore(): remove redundant(?) cloning --- src/gradient.class.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/gradient.class.js b/src/gradient.class.js index 5e6d5a5f925..b8080209a50 100644 --- a/src/gradient.class.js +++ b/src/gradient.class.js @@ -71,8 +71,6 @@ } /* _FROM_SVG_END_ */ - var clone = fabric.util.object.clone; - /** * Gradient class * @class fabric.Gradient @@ -218,8 +216,8 @@ * @return {String} SVG representation of an gradient (linear/radial) */ toSVG: function(object, options) { - var coords = clone(this.coords, true), i, len, options = options || {}, - markup, commonAttributes, colorStops = clone(this.colorStops, true), + var coords = this.coords, i, len, options = options || {}, + markup, commonAttributes, colorStops = this.colorStops, needsSwap = coords.r1 > coords.r2, transform = this.gradientTransform ? this.gradientTransform.concat() : fabric.iMatrix.concat(), offsetX = -this.offsetX, offsetY = -this.offsetY, @@ -320,7 +318,7 @@ * @return {CanvasGradient} */ toLive: function(ctx) { - var gradient, coords = fabric.util.object.clone(this.coords), i, len; + var gradient, coords = this.coords, i, len; if (!this.type) { return; From e8cdea612c648b8c4c71a772a65099c892e159b1 Mon Sep 17 00:00:00 2001 From: ShaMan123 Date: Sun, 12 Jun 2022 07:50:12 +0300 Subject: [PATCH 4/4] fix(): lint --- src/filters/blendimage_filter.class.js | 2 +- src/shapes/rect.class.js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/filters/blendimage_filter.class.js b/src/filters/blendimage_filter.class.js index 7f0edbc11b7..8567c2deb02 100644 --- a/src/filters/blendimage_filter.class.js +++ b/src/filters/blendimage_filter.class.js @@ -237,7 +237,7 @@ */ fabric.Image.filters.BlendImage.fromObject = function(object) { return fabric.Image.fromObject(object.image).then(function(image) { - return new fabric.Image.filters.BlendImage(Object.assign({}, options, { image: image })); + return new fabric.Image.filters.BlendImage(Object.assign({}, object, { image: image })); }); }; diff --git a/src/shapes/rect.class.js b/src/shapes/rect.class.js index 2b13fd7f1ca..567a982ca1d 100644 --- a/src/shapes/rect.class.js +++ b/src/shapes/rect.class.js @@ -2,8 +2,7 @@ 'use strict'; - var fabric = global.fabric || (global.fabric = { }), - extend = fabric.util.object.extend; + var fabric = global.fabric || (global.fabric = { }); if (fabric.Rect) { fabric.warn('fabric.Rect is already defined');