Skip to content

Commit

Permalink
Fix #390
Browse files Browse the repository at this point in the history
  • Loading branch information
daviferreira committed Jan 31, 2015
1 parent 5099b96 commit 2935091
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
16 changes: 12 additions & 4 deletions dist/js/medium-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,21 @@ if (typeof module === 'object') {
this.elements = Array.prototype.slice.apply(selector);
},

bindBlur: function (i) {
bindBlur: function () {
var self = this,
blurFunction = function (e) {
var isDescendantOfEditorElements = false,
i;
for (i = 0; i < self.elements.length; i += 1) {
if (isDescendant(self.elements[i], e.target)) {
isDescendantOfEditorElements = true;
break;
}
}
// If it's not part of the editor, or the toolbar
if (e.target !== self.toolbar
&& e.target !== self.elements[0]
&& !isDescendant(self.elements[0], e.target)
&& self.elements.indexOf(e.target) === -1
&& !isDescendantOfEditorElements
&& !isDescendant(self.toolbar, e.target)
&& !isDescendant(self.anchorPreview, e.target)) {

Expand Down Expand Up @@ -497,7 +505,7 @@ if (typeof module === 'object') {
// Bind the return and tab keypress events
this.bindReturn(i)
.bindKeydown(i)
.bindBlur(i)
.bindBlur()
.bindClick(i);
}

Expand Down
2 changes: 1 addition & 1 deletion dist/js/medium-editor.min.js

Large diffs are not rendered by default.

46 changes: 45 additions & 1 deletion spec/activate.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*global MediumEditor, describe, it, expect, spyOn, jasmine, fireEvent,
afterEach, beforeEach, selectElementContents, runs, waitsFor,
tearDown */
tearDown, xit */

describe('Activate/Deactivate TestCase', function () {
'use strict';
Expand Down Expand Up @@ -87,6 +87,50 @@ describe('Activate/Deactivate TestCase', function () {
expect(editor.hideToolbarActions).not.toHaveBeenCalled();
});

// regression test for https://github.com/daviferreira/medium-editor/issues/390
xit('should work with multiple elements of the same class', function () {
var editor,
el,
elements = [],
i;

for (i = 0; i < 3; i += 1) {
el = document.createElement('div');
el.className = 'editor';
el.textContent = i;
elements.push(
document.body.appendChild(el)
);
}

jasmine.clock().install();

editor = new MediumEditor('.editor');

spyOn(editor, 'hideToolbarActions').and.callThrough(); // via: handleBlur

selectElementContents(editor.elements[0]);
fireEvent(editor.elements[0], 'click');
jasmine.clock().tick(51);
expect(editor.hideToolbarActions).not.toHaveBeenCalled();

selectElementContents(editor.elements[1]);
fireEvent(editor.elements[1], 'click');
jasmine.clock().tick(51);
expect(editor.hideToolbarActions).not.toHaveBeenCalled();

selectElementContents(editor.elements[2]);
fireEvent(editor.elements[2], 'click');
jasmine.clock().tick(51);
expect(editor.hideToolbarActions).not.toHaveBeenCalled();

elements.forEach(function (el) {
document.body.removeChild(el);
});

jasmine.clock().uninstall();
});

// regression test for https://github.com/daviferreira/medium-editor/issues/197
it('should not crash when deactivated immediately after a mouse click', function () {
var editor = new MediumEditor('.editor');
Expand Down
16 changes: 12 additions & 4 deletions src/js/medium-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,13 +437,21 @@ if (typeof module === 'object') {
this.elements = Array.prototype.slice.apply(selector);
},

bindBlur: function (i) {
bindBlur: function () {
var self = this,
blurFunction = function (e) {
var isDescendantOfEditorElements = false,
i;
for (i = 0; i < self.elements.length; i += 1) {
if (isDescendant(self.elements[i], e.target)) {
isDescendantOfEditorElements = true;
break;
}
}
// If it's not part of the editor, or the toolbar
if (e.target !== self.toolbar
&& e.target !== self.elements[0]
&& !isDescendant(self.elements[0], e.target)
&& self.elements.indexOf(e.target) === -1
&& !isDescendantOfEditorElements
&& !isDescendant(self.toolbar, e.target)
&& !isDescendant(self.anchorPreview, e.target)) {

Expand Down Expand Up @@ -498,7 +506,7 @@ if (typeof module === 'object') {
// Bind the return and tab keypress events
this.bindReturn(i)
.bindKeydown(i)
.bindBlur(i)
.bindBlur()
.bindClick(i);
}

Expand Down

0 comments on commit 2935091

Please sign in to comment.