UNPKG

1.6 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 UserAgent = require("fbjs/lib/UserAgent");
16
17function editOnFocus(editor, e) {
18 var editorState = editor._latestEditorState;
19 var currentSelection = editorState.getSelection();
20
21 if (currentSelection.getHasFocus()) {
22 return;
23 }
24
25 var selection = currentSelection.set('hasFocus', true);
26 editor.props.onFocus && editor.props.onFocus(e); // When the tab containing this text editor is hidden and the user does a
27 // find-in-page in a _different_ tab, Chrome on Mac likes to forget what the
28 // selection was right after sending this focus event and (if you let it)
29 // moves the cursor back to the beginning of the editor, so we force the
30 // selection here instead of simply accepting it in order to preserve the
31 // old cursor position. See https://crbug.com/540004.
32 // But it looks like this is fixed in Chrome 60.0.3081.0.
33 // Other browsers also don't have this bug, so we prefer to acceptSelection
34 // when possible, to ensure that unfocusing and refocusing a Draft editor
35 // doesn't preserve the selection, matching how textareas work.
36
37 if (UserAgent.isBrowser('Chrome < 60.0.3081.0')) {
38 editor.update(EditorState.forceSelection(editorState, selection));
39 } else {
40 editor.update(EditorState.acceptSelection(editorState, selection));
41 }
42}
43
44module.exports = editOnFocus;
\No newline at end of file