You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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