Skip to content

Commit

Permalink
fixes deep cloning of style (#3502)
Browse files Browse the repository at this point in the history
* fixes deep cloning of style

* better fix and tests
  • Loading branch information
asturur committed Dec 28, 2016
1 parent 794fe7f commit f8b3607
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/mixins/itext_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@
}
for (var i = 0, len = _chars.length; i < len; i++) {
if (useCopiedStyle) {
style = fabric.copiedTextStyle[i];
style = fabric.util.object.clone(fabric.copiedTextStyle[i], true);
}
this.insertChar(_chars[i], i < len - 1, style);
}
Expand Down
7 changes: 1 addition & 6 deletions src/mixins/itext_key_behavior.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,7 @@ fabric.util.object.extend(fabric.IText.prototype, /** @lends fabric.IText.protot
}

fabric.copiedText = selectedText;
fabric.copiedTextStyle = fabric.util.object.clone(
this.getSelectionStyles(
this.selectionStart,
this.selectionEnd
)
);
fabric.copiedTextStyle = this.getSelectionStyles(this.selectionStart, this.selectionEnd);
e.stopImmediatePropagation();
e.preventDefault();
this._copyDone = true;
Expand Down
19 changes: 18 additions & 1 deletion test/unit/itext_key_behaviour.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,5 +235,22 @@
equal(iText.selectionEnd, 31, 'should not move');
selection = 0;
});

test('copy and paste', function() {
var event = { stopImmediatePropagation: function(){}, preventDefault: function(){} };
var iText = new fabric.IText('test', { styles: { 0: { 0: { fill: 'red' }, 1: { fill: 'blue' }}}});
iText.selectionStart = 0;
iText.selectionEnd = 2;
iText.copy(event);
equal(fabric.copiedText, 'te', 'it copied first 2 characters');
equal(fabric.copiedTextStyle[0], iText.styles[0][0], 'style is referenced');
equal(fabric.copiedTextStyle[1], iText.styles[0][1], 'style is referenced');
iText.selectionStart = 0;
iText.selectionEnd = 0;
iText.paste(event);
equal(iText.text, 'tetest', 'text has been copied');
notEqual(iText.styles[0][0], iText.styles[0][2], 'style is not referenced');
notEqual(iText.styles[0][1], iText.styles[0][3], 'style is not referenced');
deepEqual(iText.styles[0][0], iText.styles[0][2], 'style is copied');
deepEqual(iText.styles[0][1], iText.styles[0][3], 'style is copied');
});
})();

0 comments on commit f8b3607

Please sign in to comment.