Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. * @flow strict-local
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. import type DraftEditor from "./DraftEditor.react";
  13. const EditorState = require("./EditorState");
  14. const containsNode = require("fbjs/lib/containsNode");
  15. const getActiveElement = require("fbjs/lib/getActiveElement");
  16. function editOnBlur(editor: DraftEditor, e: SyntheticEvent<HTMLElement>): void {
  17. // In a contentEditable element, when you select a range and then click
  18. // another active element, this does trigger a `blur` event but will not
  19. // remove the DOM selection from the contenteditable.
  20. // This is consistent across all browsers, but we prefer that the editor
  21. // behave like a textarea, where a `blur` event clears the DOM selection.
  22. // We therefore force the issue to be certain, checking whether the active
  23. // element is `body` to force it when blurring occurs within the window (as
  24. // opposed to clicking to another tab or window).
  25. const {
  26. ownerDocument
  27. } = e.currentTarget;
  28. if ( // This ESLint rule conflicts with `sketchy-null-bool` flow check
  29. // eslint-disable-next-line no-extra-boolean-cast
  30. !Boolean(editor.props.preserveSelectionOnBlur) && getActiveElement(ownerDocument) === ownerDocument.body) {
  31. const selection = ownerDocument.defaultView.getSelection();
  32. const editorNode = editor.editor;
  33. if (selection.rangeCount === 1 && containsNode(editorNode, selection.anchorNode) && containsNode(editorNode, selection.focusNode)) {
  34. selection.removeAllRanges();
  35. }
  36. }
  37. const editorState = editor._latestEditorState;
  38. const currentSelection = editorState.getSelection();
  39. if (!currentSelection.getHasFocus()) {
  40. return;
  41. }
  42. const selection = currentSelection.set('hasFocus', false);
  43. editor.props.onBlur && editor.props.onBlur(e);
  44. editor.update(EditorState.acceptSelection(editorState, selection));
  45. }
  46. module.exports = editOnBlur;