Skip to content

Commit

Permalink
bug fix: embed intent hiding
Browse files Browse the repository at this point in the history
  • Loading branch information
gpoitch committed Jul 18, 2014
1 parent 6d69d9c commit 822d40a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
18 changes: 14 additions & 4 deletions dist/content-kit-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @version 0.1.0
* @author Garth Poitras <garth22@gmail.com> (http://garthpoitras.com/)
* @license MIT
* Last modified: Jul 17, 2014
* Last modified: Jul 18, 2014
*/

(function(exports, document) {
Expand Down Expand Up @@ -218,8 +218,8 @@ function tagsInSelection(selection) {
}

function selectionIsInElement(selection, element) {
var node = selection.focusNode,
parentNode = node.parentNode;
var node = selection.anchorNode,
parentNode = node && node.parentNode;
while(parentNode) {
if (parentNode === element) {
return true;
Expand Down Expand Up @@ -1040,6 +1040,10 @@ var EmbedIntent = (function() {
embedIntent.isActive = false;

function embedIntentHandler(e) {
if (!selectionIsInElement(window.getSelection(), rootElement)) {
embedIntent.hide();
return;
}
var currentNode = getCurrentSelectionRootNode();
var currentNodeHTML = currentNode.innerHTML;
if (currentNodeHTML === '' || currentNodeHTML === '<br>') {
Expand All @@ -1051,7 +1055,13 @@ var EmbedIntent = (function() {
}

rootElement.addEventListener('keyup', embedIntentHandler);
document.addEventListener('mouseup', embedIntentHandler);
document.addEventListener('mouseup', function(e) { setTimeout(function() { embedIntentHandler(e); }); });

document.addEventListener('keyup', function(e) {
if (e.keyCode === Keycodes.ESC) {
embedIntent.deactivate();
}
});

window.addEventListener('resize', function() {
if(embedIntent.isShowing) {
Expand Down
12 changes: 11 additions & 1 deletion src/js/embed-intent.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ var EmbedIntent = (function() {
embedIntent.isActive = false;

function embedIntentHandler(e) {
if (!selectionIsInElement(window.getSelection(), rootElement)) {
embedIntent.hide();
return;
}
var currentNode = getCurrentSelectionRootNode();
var currentNodeHTML = currentNode.innerHTML;
if (currentNodeHTML === '' || currentNodeHTML === '<br>') {
Expand All @@ -34,7 +38,13 @@ var EmbedIntent = (function() {
}

rootElement.addEventListener('keyup', embedIntentHandler);
document.addEventListener('mouseup', embedIntentHandler);
document.addEventListener('mouseup', function(e) { setTimeout(function() { embedIntentHandler(e); }); });

document.addEventListener('keyup', function(e) {
if (e.keyCode === Keycodes.ESC) {
embedIntent.deactivate();
}
});

window.addEventListener('resize', function() {
if(embedIntent.isShowing) {
Expand Down
4 changes: 2 additions & 2 deletions src/js/utils/selection-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ function tagsInSelection(selection) {
}

function selectionIsInElement(selection, element) {
var node = selection.focusNode,
parentNode = node.parentNode;
var node = selection.anchorNode,
parentNode = node && node.parentNode;
while(parentNode) {
if (parentNode === element) {
return true;
Expand Down

0 comments on commit 822d40a

Please sign in to comment.