From c945195438fb1b8e38cbac8dab5737b0c834dbb5 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Fri, 29 Apr 2016 00:34:37 +0200 Subject: [PATCH 1/9] Update itext_behavior.mixin.js --- src/mixins/itext_behavior.mixin.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/mixins/itext_behavior.mixin.js b/src/mixins/itext_behavior.mixin.js index d5cc6d4e64f..2d13a8ac470 100644 --- a/src/mixins/itext_behavior.mixin.js +++ b/src/mixins/itext_behavior.mixin.js @@ -353,6 +353,7 @@ this._updateTextarea(); this._saveEditingProps(); this._setEditingProps(); + this._textBeforeEdit = this.text; this._tick(); this.fire('editing:entered'); @@ -494,7 +495,7 @@ * @chainable */ exitEditing: function() { - + var isTextChanged = this._textBeforeEdit !== this.text this.selected = false; this.isEditing = false; this.selectable = true; @@ -508,7 +509,11 @@ this._currentCursorOpacity = 0; this.fire('editing:exited'); - this.canvas && this.canvas.fire('text:editing:exited', { target: this }); + isTextChanged && this.fire('modified'); + if (this.canvas) { + this.canvas.fire('text:editing:exited', { target: this }); + isTextChanged && this.canvas.fire('object:modified', { target: this }); + } return this; }, From 9de40c04e62b8cc6b359b61198b5566eb8d20711 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Fri, 29 Apr 2016 00:43:04 +0200 Subject: [PATCH 2/9] Update itext_behavior.mixin.js --- src/mixins/itext_behavior.mixin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mixins/itext_behavior.mixin.js b/src/mixins/itext_behavior.mixin.js index 2d13a8ac470..af59a87f7e1 100644 --- a/src/mixins/itext_behavior.mixin.js +++ b/src/mixins/itext_behavior.mixin.js @@ -495,7 +495,7 @@ * @chainable */ exitEditing: function() { - var isTextChanged = this._textBeforeEdit !== this.text + var isTextChanged = (this._textBeforeEdit !== this.text); this.selected = false; this.isEditing = false; this.selectable = true; From 3b8b78aa54607a80a4cd9ecf0a285b76cdb5e324 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Mon, 2 May 2016 07:10:59 +0200 Subject: [PATCH 3/9] Update itext.js --- test/unit/itext.js | 91 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/test/unit/itext.js b/test/unit/itext.js index adeaa0d86ad..8e8af1f3a78 100644 --- a/test/unit/itext.js +++ b/test/unit/itext.js @@ -243,6 +243,67 @@ ok(!iText.isEditing); }); + test('event firing', function() { + var iText = new fabric.IText('test'), + enter = 0, exit = 0, modify = 0; + + function countEnter() { + enter++; + } + + function countEnter() { + exit++; + } + + function countEnter() { + modify++; + } + + iText.on('editing:entered', countEnter); + iText.on('editing:exited', countExit); + iText.on('modified', countModify); + + equal(typeof iText.enterEditing, 'function'); + equal(typeof iText.exitEditing, 'function'); + + iText.enterEditing(); + equal(entered, 1); + equal(exit, 0); + equal(modify, 0); + + iText.exitEditing(); + equal(entered, 1); + equal(exit, 1); + equal(modify, 0); + + iText.enterEditing(); + equal(entered, 2); + equal(exit, 1); + equal(modify, 0); + + iText.text = 'Test+'; + iText.exitEditing(); + equal(entered, 2); + equal(exit, 2); + equal(modify, 1); + }); + + test('insertChar and text:changed', function() { + var iText = new fabric.IText('test'), changed = 0; + + function textChanged () { + changed++; + } + fabric.copiedStyle = style; + equal(typeof iText.insertChar, 'function'); + iText.on('text:changed', textChanged); + equal(changed, 0); + iText.insertChar('foo_'); + equal(iText.text, 'foo_test'); + equal(changed, 1, 'event will fire once'); + }); + + test('insertChars', function() { var iText = new fabric.IText('test'); @@ -263,6 +324,36 @@ equal(iText.text, 't_foo_t'); }); + test('insertChars text:changed', function() { + var iText = new fabric.IText('test'), changed = 0; + function textChanged () { + changed++; + } + equal(typeof iText.insertChars, 'function'); + iText.on('text:changed', textChanged); + equal(changed, 0); + iText.insertChars('foo_'); + equal(changed, 1, 'insertChars fires the event once if there is no style'); + equal(iText.text, 'foo_test'); + }); + + test('insertChars text:changed with copied style', function() { + var iText = new fabric.IText('test'), changed = 0; + style = {0: {fontSize: 20}, 1: {fontSize: 22}}; + function textChanged () { + changed++; + } + fabric.copiedStyle = style; + equal(typeof iText.insertChars, 'function'); + iText.on('text:changed', textChanged); + equal(changed, 0); + iText.insertChars('foo_', true); + equal(changed, 4, 'insertChars fires each character if there is style to use'); + equal(iText.text, 'foo_test'); + deepEqual(iText.styles[0][0], style[0], 'style should be copied'); + }); + + test('insertNewline', function() { var iText = new fabric.IText('test'); From 21e025c687dde75522122e89ecc0f9d320ec9837 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Mon, 2 May 2016 07:15:22 +0200 Subject: [PATCH 4/9] Update itext.js --- test/unit/itext.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/unit/itext.js b/test/unit/itext.js index 8e8af1f3a78..5f86a7dc190 100644 --- a/test/unit/itext.js +++ b/test/unit/itext.js @@ -288,7 +288,7 @@ equal(modify, 1); }); - test('insertChar and text:changed', function() { + test('insertChar and changed', function() { var iText = new fabric.IText('test'), changed = 0; function textChanged () { @@ -296,7 +296,7 @@ } fabric.copiedStyle = style; equal(typeof iText.insertChar, 'function'); - iText.on('text:changed', textChanged); + iText.on('changed', textChanged); equal(changed, 0); iText.insertChar('foo_'); equal(iText.text, 'foo_test'); @@ -324,20 +324,20 @@ equal(iText.text, 't_foo_t'); }); - test('insertChars text:changed', function() { + test('insertChars changed', function() { var iText = new fabric.IText('test'), changed = 0; function textChanged () { changed++; } equal(typeof iText.insertChars, 'function'); - iText.on('text:changed', textChanged); + iText.on('changed', textChanged); equal(changed, 0); iText.insertChars('foo_'); equal(changed, 1, 'insertChars fires the event once if there is no style'); equal(iText.text, 'foo_test'); }); - test('insertChars text:changed with copied style', function() { + test('insertChars changed with copied style', function() { var iText = new fabric.IText('test'), changed = 0; style = {0: {fontSize: 20}, 1: {fontSize: 22}}; function textChanged () { @@ -345,7 +345,7 @@ } fabric.copiedStyle = style; equal(typeof iText.insertChars, 'function'); - iText.on('text:changed', textChanged); + iText.on('changed', textChanged); equal(changed, 0); iText.insertChars('foo_', true); equal(changed, 4, 'insertChars fires each character if there is style to use'); From 28f83e46902f53b70f273e27e10974b3f942ffa5 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Mon, 2 May 2016 07:24:30 +0200 Subject: [PATCH 5/9] Update itext.js --- test/unit/itext.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/test/unit/itext.js b/test/unit/itext.js index 5f86a7dc190..13a8409091e 100644 --- a/test/unit/itext.js +++ b/test/unit/itext.js @@ -251,11 +251,11 @@ enter++; } - function countEnter() { + function countExit() { exit++; } - function countEnter() { + function countModify() { modify++; } @@ -267,23 +267,23 @@ equal(typeof iText.exitEditing, 'function'); iText.enterEditing(); - equal(entered, 1); + equal(enter, 1); equal(exit, 0); equal(modify, 0); iText.exitEditing(); - equal(entered, 1); + equal(enter, 1); equal(exit, 1); equal(modify, 0); iText.enterEditing(); - equal(entered, 2); + equal(enter, 2); equal(exit, 1); equal(modify, 0); iText.text = 'Test+'; iText.exitEditing(); - equal(entered, 2); + equal(enter, 2); equal(exit, 2); equal(modify, 1); }); @@ -294,7 +294,6 @@ function textChanged () { changed++; } - fabric.copiedStyle = style; equal(typeof iText.insertChar, 'function'); iText.on('changed', textChanged); equal(changed, 0); @@ -339,7 +338,7 @@ test('insertChars changed with copied style', function() { var iText = new fabric.IText('test'), changed = 0; - style = {0: {fontSize: 20}, 1: {fontSize: 22}}; + style = {0: {0: {fontSize: 20}, 1: {fontSize: 22}}}; function textChanged () { changed++; } From 29840c63dca6965a60e124ecf9f8a55d920a18e9 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Mon, 2 May 2016 07:31:49 +0200 Subject: [PATCH 6/9] Update itext.js --- test/unit/itext.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/unit/itext.js b/test/unit/itext.js index 13a8409091e..3d0ebb14ca9 100644 --- a/test/unit/itext.js +++ b/test/unit/itext.js @@ -302,6 +302,27 @@ equal(changed, 1, 'event will fire once'); }); + test('insertChar with style', function() { + var iText = new fabric.IText('test'), + style = {fontSize: 4}; + + equal(typeof iText.insertChar, 'function'); + iText.insertChar('f', false, style); + equal(iText.text, 'ftest'); + deepEqual(iText.styles[0][0], style); + }); + + test('insertChar with selectionStart with style', function() { + var iText = new fabric.IText('test'), + style = {fontSize: 4}; + equal(typeof iText.insertChar, 'function'); + iText.selectionStart = 2; + iText.selectionEnd = 2; + iText.insertChar('f', false, style); + equal(iText.text, 'tefst'); + deepEqual(iText.styles[0][2], style); + }); + test('insertChars', function() { var iText = new fabric.IText('test'); From eb802fbab9582fe6322b549b4ba20b0374159ce1 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Mon, 2 May 2016 07:42:50 +0200 Subject: [PATCH 7/9] Update itext.js --- test/unit/itext.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/itext.js b/test/unit/itext.js index 3d0ebb14ca9..56374d55306 100644 --- a/test/unit/itext.js +++ b/test/unit/itext.js @@ -359,11 +359,11 @@ test('insertChars changed with copied style', function() { var iText = new fabric.IText('test'), changed = 0; - style = {0: {0: {fontSize: 20}, 1: {fontSize: 22}}}; + style = {0: {fontSize: 20}, 1: {fontSize: 22}}; function textChanged () { changed++; } - fabric.copiedStyle = style; + fabric.copiedTextStyle = style; equal(typeof iText.insertChars, 'function'); iText.on('changed', textChanged); equal(changed, 0); From afce18d6c1c3bbfc4c7f33c771d4f43c38d0928d Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Mon, 2 May 2016 08:13:11 +0200 Subject: [PATCH 8/9] Update itext.js --- test/unit/itext.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/itext.js b/test/unit/itext.js index 56374d55306..7082b89a30a 100644 --- a/test/unit/itext.js +++ b/test/unit/itext.js @@ -368,7 +368,7 @@ iText.on('changed', textChanged); equal(changed, 0); iText.insertChars('foo_', true); - equal(changed, 4, 'insertChars fires each character if there is style to use'); + equal(changed, 1, 'insertChars fires once even if style is used'); equal(iText.text, 'foo_test'); deepEqual(iText.styles[0][0], style[0], 'style should be copied'); }); From 2c5a35dac8af5b8d5c41394b139167125c61af9b Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Mon, 2 May 2016 08:20:34 +0200 Subject: [PATCH 9/9] Update itext.js --- test/unit/itext.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/itext.js b/test/unit/itext.js index 7082b89a30a..0cc6807a38e 100644 --- a/test/unit/itext.js +++ b/test/unit/itext.js @@ -358,7 +358,7 @@ }); test('insertChars changed with copied style', function() { - var iText = new fabric.IText('test'), changed = 0; + var iText = new fabric.IText('test'), changed = 0, style = {0: {fontSize: 20}, 1: {fontSize: 22}}; function textChanged () { changed++;