Skip to content
This repository was archived by the owner on Nov 4, 2019. It is now read-only.

Commit c41a297

Browse files
committed
Fix firing onChange handler twice for equal content.
1 parent 417b799 commit c41a297

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

addon/components/medium-editor.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ const MediumEditorComponent = Component.extend({
5858
*/
5959
onChange: null,
6060

61+
_prevValue: '',
62+
6163
init() {
6264
this._super(...arguments);
6365
this._setOptions();
@@ -94,11 +96,22 @@ const MediumEditorComponent = Component.extend({
9496
}
9597
});
9698

99+
this._setOnChange(editor);
100+
},
101+
102+
_setOnChange(editor) {
97103
let onChangeHandler = get(this, 'onChange');
98104
if (typeof onChangeHandler === 'function') {
99-
editor.subscribe('editableInput', () => {
100-
onChangeHandler(editor.getContent());
101-
});
105+
let handler = () => {
106+
let newValue = editor.getContent();
107+
let isUpdated = get(this, '_prevValue') !== newValue;
108+
if (isUpdated) {
109+
set(this, '_prevValue', newValue);
110+
onChangeHandler(newValue);
111+
}
112+
};
113+
114+
editor.subscribe('editableInput', handler);
102115
}
103116
},
104117

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ember-medium-editor",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "medium-editor for Ember Apps",
55
"keywords": [
66
"medium-editor",

tests/integration/components/medium-editor-test.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ test('it should trigger onChange action when content changed', function(assert)
3232
assert.expect(1);
3333

3434
this.set('onChange', (actual) => {
35-
// Action triggered twice with ember 2.4 and 2.8.
36-
// Hack to use asertions only for second call.
37-
if (actual !== '') {
38-
assert.equal(actual, '<p>typed value</p>'); // eslint-disable-line
39-
}
35+
assert.equal(actual, '<p>typed value</p>');
4036
});
4137
this.render(hbs`{{medium-editor onChange=(action onChange)}}`);
4238

0 commit comments

Comments
 (0)