Skip to content

Commit

Permalink
build
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaMan123 committed Aug 25, 2022
1 parent e52a059 commit 8371a05
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 55 deletions.
206 changes: 152 additions & 54 deletions dist/fabric.js
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,9 @@ fabric.CommonMethods = {
groupSVGElements: function(elements, options, path) {
var object;
if (elements && elements.length === 1) {
if (typeof path !== 'undefined') {
elements[0].sourcePath = path;
}
return elements[0];
}
if (options) {
Expand Down Expand Up @@ -1838,6 +1841,113 @@ fabric.CommonMethods = {
}
return new fabric.Group([a], { clipPath: b, inverted: inverted });
},

/**
* @memberOf fabric.util
* @param {Object} prevStyle first style to compare
* @param {Object} thisStyle second style to compare
* @param {boolean} forTextSpans whether to check overline, underline, and line-through properties
* @return {boolean} true if the style changed
*/
hasStyleChanged: function(prevStyle, thisStyle, forTextSpans) {
forTextSpans = forTextSpans || false;
return (prevStyle.fill !== thisStyle.fill ||
prevStyle.stroke !== thisStyle.stroke ||
prevStyle.strokeWidth !== thisStyle.strokeWidth ||
prevStyle.fontSize !== thisStyle.fontSize ||
prevStyle.fontFamily !== thisStyle.fontFamily ||
prevStyle.fontWeight !== thisStyle.fontWeight ||
prevStyle.fontStyle !== thisStyle.fontStyle ||
prevStyle.deltaY !== thisStyle.deltaY) ||
(forTextSpans &&
(prevStyle.overline !== thisStyle.overline ||
prevStyle.underline !== thisStyle.underline ||
prevStyle.linethrough !== thisStyle.linethrough));
},

/**
* Returns the array form of a text object's inline styles property with styles grouped in ranges
* rather than per character. This format is less verbose, and is better suited for storage
* so it is used in serialization (not during runtime).
* @memberOf fabric.util
* @param {object} styles per character styles for a text object
* @param {String} text the text string that the styles are applied to
* @return {{start: number, end: number, style: object}[]}
*/
stylesToArray: function(styles, text) {
// clone style structure to prevent mutation
var styles = fabric.util.object.clone(styles, true),
textLines = text.split('\n'),
charIndex = -1, prevStyle = {}, stylesArray = [];
//loop through each textLine
for (var i = 0; i < textLines.length; i++) {
if (!styles[i]) {
//no styles exist for this line, so add the line's length to the charIndex total
charIndex += textLines[i].length;
continue;
}
//loop through each character of the current line
for (var c = 0; c < textLines[i].length; c++) {
charIndex++;
var thisStyle = styles[i][c];
//check if style exists for this character
if (thisStyle) {
var styleChanged = fabric.util.hasStyleChanged(prevStyle, thisStyle, true);
if (styleChanged) {
stylesArray.push({
start: charIndex,
end: charIndex + 1,
style: thisStyle
});
}
else {
//if style is the same as previous character, increase end index
stylesArray[stylesArray.length - 1].end++;
}
}
prevStyle = thisStyle || {};
}
}
return stylesArray;
},

/**
* Returns the object form of the styles property with styles that are assigned per
* character rather than grouped by range. This format is more verbose, and is
* only used during runtime (not for serialization/storage)
* @memberOf fabric.util
* @param {Array} styles the serialized form of a text object's styles
* @param {String} text the text string that the styles are applied to
* @return {Object}
*/
stylesFromArray: function(styles, text) {
if (!Array.isArray(styles)) {
return styles;
}
var textLines = text.split('\n'),
charIndex = -1, styleIndex = 0, stylesObject = {};
//loop through each textLine
for (var i = 0; i < textLines.length; i++) {
//loop through each character of the current line
for (var c = 0; c < textLines[i].length; c++) {
charIndex++;
//check if there's a style collection that includes the current character
if (styles[styleIndex]
&& styles[styleIndex].start <= charIndex
&& charIndex < styles[styleIndex].end) {
//create object for line index if it doesn't exist
stylesObject[i] = stylesObject[i] || {};
//assign a style at this character's index
stylesObject[i][c] = Object.assign({}, styles[styleIndex].style);
//if character is at the end of the current style collection, move to the next
if (charIndex === styles[styleIndex].end - 1) {
styleIndex++;
}
}
}
}
return stylesObject;
}
};
})(typeof exports !== 'undefined' ? exports : this);

Expand Down Expand Up @@ -3155,7 +3265,7 @@ fabric.CommonMethods = {
var normalizedProperty = (property === 'float' || property === 'cssFloat')
? (typeof elementStyle.styleFloat === 'undefined' ? 'cssFloat' : 'styleFloat')
: property;
elementStyle[normalizedProperty] = styles[property];
elementStyle.setProperty(normalizedProperty, styles[property]);
}
}
return element;
Expand Down Expand Up @@ -12998,14 +13108,6 @@ fabric.PatternBrush = fabric.util.createClass(fabric.PencilBrush, /** @lends fab
_target && target.fire('mouseout', { e: e });
});
this._hoveredTargets = [];

if (this._iTextInstances) {
this._iTextInstances.forEach(function(obj) {
if (obj.isEditing) {
obj.hiddenTextarea.focus();
}
});
}
},

/**
Expand Down Expand Up @@ -19988,7 +20090,15 @@ fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prot
fabric.loadSVGFromURL(pathUrl, function (elements) {
var path = elements[0];
path.setOptions(object);
callback && callback(path);
if (object.clipPath) {
fabric.util.enlivenObjects([object.clipPath], function(elivenedObjects) {
path.clipPath = elivenedObjects[0];
callback && callback(path);
});
}
else {
callback && callback(path);
}
});
}
else {
Expand Down Expand Up @@ -20595,20 +20705,27 @@ fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prot
// it has to be an url or something went wrong.
fabric.loadSVGFromURL(objects, function (elements) {
var group = fabric.util.groupSVGElements(elements, object, objects);
var clipPath = options.clipPath;
delete options.clipPath;
group.set(options);
callback && callback(group);
if (clipPath) {
fabric.util.enlivenObjects([clipPath], function(elivenedObjects) {
group.clipPath = elivenedObjects[0];
callback && callback(group);
});
}
else {
callback && callback(group);
}
});
return;
}
fabric.util.enlivenObjects(objects, function (enlivenedObjects) {
var options = fabric.util.object.clone(object, true);
delete options.objects;
fabric.util.enlivenObjectEnlivables(object, options, function () {
callback && callback(new fabric.Group(enlivenedObjects, options, true));
});
});
};

})(typeof exports !== 'undefined' ? exports : this);


Expand Down Expand Up @@ -26856,7 +26973,7 @@ fabric.Image.filters.BaseFilter.fromObject = function(object, callback) {
// if we have charSpacing, we render char by char
actualStyle = actualStyle || this.getCompleteStyleDeclaration(lineIndex, i);
nextStyle = this.getCompleteStyleDeclaration(lineIndex, i + 1);
timeToRender = this._hasStyleChanged(actualStyle, nextStyle);
timeToRender = fabric.util.hasStyleChanged(actualStyle, nextStyle, false);
}
if (timeToRender) {
if (path) {
Expand Down Expand Up @@ -27026,34 +27143,6 @@ fabric.Image.filters.BaseFilter.fromObject = function(object, callback) {
return this;
},

/**
* @private
* @param {Object} prevStyle
* @param {Object} thisStyle
*/
_hasStyleChanged: function(prevStyle, thisStyle) {
return prevStyle.fill !== thisStyle.fill ||
prevStyle.stroke !== thisStyle.stroke ||
prevStyle.strokeWidth !== thisStyle.strokeWidth ||
prevStyle.fontSize !== thisStyle.fontSize ||
prevStyle.fontFamily !== thisStyle.fontFamily ||
prevStyle.fontWeight !== thisStyle.fontWeight ||
prevStyle.fontStyle !== thisStyle.fontStyle ||
prevStyle.deltaY !== thisStyle.deltaY;
},

/**
* @private
* @param {Object} prevStyle
* @param {Object} thisStyle
*/
_hasStyleChangedForSvg: function(prevStyle, thisStyle) {
return this._hasStyleChanged(prevStyle, thisStyle) ||
prevStyle.overline !== thisStyle.overline ||
prevStyle.underline !== thisStyle.underline ||
prevStyle.linethrough !== thisStyle.linethrough;
},

/**
* @private
* @param {Number} lineIndex index text line
Expand Down Expand Up @@ -27315,8 +27404,7 @@ fabric.Image.filters.BaseFilter.fromObject = function(object, callback) {
toObject: function(propertiesToInclude) {
var allProperties = additionalProps.concat(propertiesToInclude);
var obj = this.callSuper('toObject', allProperties);
// styles will be overridden with a properly cloned structure
obj.styles = clone(this.styles, true);
obj.styles = fabric.util.stylesToArray(this.styles, this.text);
if (obj.path) {
obj.path = this.path.toObject();
}
Expand Down Expand Up @@ -27482,6 +27570,7 @@ fabric.Image.filters.BaseFilter.fromObject = function(object, callback) {
var objectCopy = clone(object), path = object.path;
delete objectCopy.path;
return fabric.Object._fromObject('Text', objectCopy, function(textInstance) {
textInstance.styles = fabric.util.stylesFromArray(object.styles, object.text);
if (path) {
fabric.Object._fromObject('Path', path, function(pathInstance) {
textInstance.set('path', pathInstance);
Expand Down Expand Up @@ -28339,15 +28428,18 @@ fabric.Image.filters.BaseFilter.fromObject = function(object, callback) {
* @param {function} [callback] invoked with new instance as argument
*/
fabric.IText.fromObject = function(object, callback) {
parseDecoration(object);
if (object.styles) {
for (var i in object.styles) {
for (var j in object.styles[i]) {
parseDecoration(object.styles[i][j]);
var styles = fabric.util.stylesFromArray(object.styles, object.text);
//copy object to prevent mutation
var objCopy = Object.assign({}, object, { styles: styles });
parseDecoration(objCopy);
if (objCopy.styles) {
for (var i in objCopy.styles) {
for (var j in objCopy.styles[i]) {
parseDecoration(objCopy.styles[i][j]);
}
}
}
fabric.Object._fromObject('IText', object, callback, 'text');
fabric.Object._fromObject('IText', objCopy, callback, 'text');
};
})();

Expand Down Expand Up @@ -28740,6 +28832,9 @@ fabric.Image.filters.BaseFilter.fromObject = function(object, callback) {
return;
}

// regain focus
document.activeElement !== this.hiddenTextarea && this.hiddenTextarea.focus();

var newSelectionStart = this.getSelectionStartFromPointer(options.e),
currentStart = this.selectionStart,
currentEnd = this.selectionEnd;
Expand Down Expand Up @@ -30093,7 +30188,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
this[prop] += direction === 'Left' ? -1 : 1;
return true;
}
if (typeof newValue !== undefined && this[prop] !== newValue) {
if (typeof newValue !== 'undefined' && this[prop] !== newValue) {
this[prop] = newValue;
return true;
}
Expand Down Expand Up @@ -30411,7 +30506,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
// if we have charSpacing, we render char by char
actualStyle = actualStyle || this.getCompleteStyleDeclaration(lineIndex, i);
nextStyle = this.getCompleteStyleDeclaration(lineIndex, i + 1);
timeToRender = this._hasStyleChangedForSvg(actualStyle, nextStyle);
timeToRender = fabric.util.hasStyleChanged(actualStyle, nextStyle, true);
}
if (timeToRender) {
style = this._getStyleDeclaration(lineIndex, i) || { };
Expand Down Expand Up @@ -30965,7 +31060,10 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
* @param {Function} [callback] Callback to invoke when an fabric.Textbox instance is created
*/
fabric.Textbox.fromObject = function(object, callback) {
return fabric.Object._fromObject('Textbox', object, callback, 'text');
var styles = fabric.util.stylesFromArray(object.styles, object.text);
//copy object to prevent mutation
var objCopy = Object.assign({}, object, { styles: styles });
return fabric.Object._fromObject('Textbox', objCopy, callback, 'text');
};
})(typeof exports !== 'undefined' ? exports : this);

Expand Down
2 changes: 1 addition & 1 deletion dist/fabric.min.js

Large diffs are not rendered by default.

0 comments on commit 8371a05

Please sign in to comment.