From c72e5099aa780a87ed5e4f3545bd0d1f811ba7f3 Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Tue, 12 Jan 2016 11:51:18 -0800 Subject: [PATCH] fix(composer): `whilePreservingSelection` should not call select if not activeElement --- src/components/contenteditable/editor-api.coffee | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/components/contenteditable/editor-api.coffee b/src/components/contenteditable/editor-api.coffee index c0fc91745..5f1bd0639 100644 --- a/src/components/contenteditable/editor-api.coffee +++ b/src/components/contenteditable/editor-api.coffee @@ -36,6 +36,10 @@ class EditorAPI currentSelection: -> @_extendedSelection whilePreservingSelection: (fn) -> + # We only preserve selection if the active element is actually within the + # contenteditable. Otherwise, we can unintentionally "steal" focus back if + # `whilePreservingSelection` is called by a plugin when we are not focused. + return fn() unless document.activeElement is @rootNode or @rootNode.contains(document.activeElement) sel = @currentSelection().exportSelection() fn() @select(sel)