diff --git a/src/component/handlers/edit/editOnBlur.js b/src/component/handlers/edit/editOnBlur.js index dfd3515f9d..2dbb230580 100644 --- a/src/component/handlers/edit/editOnBlur.js +++ b/src/component/handlers/edit/editOnBlur.js @@ -14,12 +14,14 @@ import type DraftEditor from 'DraftEditor.react'; -var EditorState = require('EditorState'); -var UserAgent = require('UserAgent'); +const DraftFeatureFlags = require('DraftFeatureFlags'); +const EditorState = require('EditorState'); +const UserAgent = require('UserAgent'); -var getActiveElement = require('getActiveElement'); +const containsNode = require('containsNode'); +const getActiveElement = require('getActiveElement'); -var isWebKit = UserAgent.isEngine('WebKit'); +const isWebKit = UserAgent.isEngine('WebKit'); function editOnBlur(editor: DraftEditor, e: SyntheticEvent): void { // Webkit has a bug in which blurring a contenteditable by clicking on @@ -29,7 +31,16 @@ function editOnBlur(editor: DraftEditor, e: SyntheticEvent): void { // to force it when blurring occurs within the window (as opposed to // clicking to another tab or window). if (isWebKit && getActiveElement() === document.body) { - global.getSelection().removeAllRanges(); + const selection = global.getSelection(); + const editorNode = editor.refs.editor; + if ( + !DraftFeatureFlags.draft_cautious_range_removal_on_blur || + selection.rangeCount === 1 && + containsNode(editorNode, selection.anchorNode) && + containsNode(editorNode, selection.focusNode) + ) { + selection.removeAllRanges(); + } } var editorState = editor._latestEditorState; diff --git a/src/component/utils/DraftFeatureFlags-core.js b/src/component/utils/DraftFeatureFlags-core.js index ba0b6f1391..194ced084c 100644 --- a/src/component/utils/DraftFeatureFlags-core.js +++ b/src/component/utils/DraftFeatureFlags-core.js @@ -14,6 +14,7 @@ var DraftFeatureFlags = { draft_accept_selection_after_refocus: false, + draft_cautious_range_removal_on_blur: false, draft_killswitch_allow_nontextnodes: false, draft_segmented_entities_behavior: false, };