UNPKG

1.95 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 *
9 * @emails oncall+draft_js
10 */
11'use strict';
12
13var EditorState = require("./EditorState");
14
15var containsNode = require("fbjs/lib/containsNode");
16
17var getActiveElement = require("fbjs/lib/getActiveElement");
18
19function editOnBlur(editor, e) {
20 // In a contentEditable element, when you select a range and then click
21 // another active element, this does trigger a `blur` event but will not
22 // remove the DOM selection from the contenteditable.
23 // This is consistent across all browsers, but we prefer that the editor
24 // behave like a textarea, where a `blur` event clears the DOM selection.
25 // We therefore force the issue to be certain, checking whether the active
26 // element is `body` to force it when blurring occurs within the window (as
27 // opposed to clicking to another tab or window).
28 var ownerDocument = e.currentTarget.ownerDocument;
29
30 if ( // This ESLint rule conflicts with `sketchy-null-bool` flow check
31 // eslint-disable-next-line no-extra-boolean-cast
32 !Boolean(editor.props.preserveSelectionOnBlur) && getActiveElement(ownerDocument) === ownerDocument.body) {
33 var _selection = ownerDocument.defaultView.getSelection();
34
35 var editorNode = editor.editor;
36
37 if (_selection.rangeCount === 1 && containsNode(editorNode, _selection.anchorNode) && containsNode(editorNode, _selection.focusNode)) {
38 _selection.removeAllRanges();
39 }
40 }
41
42 var editorState = editor._latestEditorState;
43 var currentSelection = editorState.getSelection();
44
45 if (!currentSelection.getHasFocus()) {
46 return;
47 }
48
49 var selection = currentSelection.set('hasFocus', false);
50 editor.props.onBlur && editor.props.onBlur(e);
51 editor.update(EditorState.acceptSelection(editorState, selection));
52}
53
54module.exports = editOnBlur;
\No newline at end of file