diff --git a/dist/content-kit-editor.js b/dist/content-kit-editor.js index 826ddddd4..0d413dc30 100755 --- a/dist/content-kit-editor.js +++ b/dist/content-kit-editor.js @@ -3,7 +3,7 @@ * @version 0.1.0 * @author Garth Poitras (http://garthpoitras.com/) * @license MIT - * Last modified: Jul 17, 2014 + * Last modified: Jul 18, 2014 */ (function(exports, document) { @@ -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; @@ -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 === '
') { @@ -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) { diff --git a/src/js/embed-intent.js b/src/js/embed-intent.js index 0deb1c4bd..3bd3c5a9f 100644 --- a/src/js/embed-intent.js +++ b/src/js/embed-intent.js @@ -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 === '
') { @@ -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) { diff --git a/src/js/utils/selection-utils.js b/src/js/utils/selection-utils.js index 1f7f1760a..7fd421f81 100644 --- a/src/js/utils/selection-utils.js +++ b/src/js/utils/selection-utils.js @@ -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;