d2-ui
Version:
36 lines (30 loc) • 1.23 kB
JavaScript
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule editOnFocus
*
*/
;
var EditorState = require('./EditorState');
function editOnFocus(e) {
var editorState = this.props.editorState;
var currentSelection = editorState.getSelection();
if (currentSelection.getHasFocus()) {
return;
}
var selection = currentSelection.set('hasFocus', true);
this.props.onFocus && this.props.onFocus(e);
// When the tab containing this text editor is hidden and the user does a
// find-in-page in a _different_ tab, Chrome on Mac likes to forget what the
// selection was right after sending this focus event and (if you let it)
// moves the cursor back to the beginning of the editor, so we force the
// selection here instead of simply accepting it in order to preserve the
// old cursor position. See https://crbug.com/540004.
this.update(EditorState.forceSelection(editorState, selection));
}
module.exports = editOnFocus;