Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

há 3 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. import type DraftEditor from "./DraftEditor.react";
  13. const DraftModifier = require("./DraftModifier");
  14. const EditorState = require("./EditorState");
  15. const Style = require("fbjs/lib/Style");
  16. const getFragmentFromSelection = require("./getFragmentFromSelection");
  17. const getScrollPosition = require("fbjs/lib/getScrollPosition");
  18. const isNode = require("./isInstanceOfNode");
  19. /**
  20. * On `cut` events, native behavior is allowed to occur so that the system
  21. * clipboard is set properly. This means that we need to take steps to recover
  22. * the editor DOM state after the `cut` has occurred in order to maintain
  23. * control of the component.
  24. *
  25. * In addition, we can keep a copy of the removed fragment, including all
  26. * styles and entities, for use as an internal paste.
  27. */
  28. function editOnCut(editor: DraftEditor, e: SyntheticClipboardEvent<>): void {
  29. const editorState = editor._latestEditorState;
  30. const selection = editorState.getSelection();
  31. const element = e.target;
  32. let scrollPosition; // No selection, so there's nothing to cut.
  33. if (selection.isCollapsed()) {
  34. e.preventDefault();
  35. return;
  36. } // Track the current scroll position so that it can be forced back in place
  37. // after the editor regains control of the DOM.
  38. if (isNode(element)) {
  39. const node: Node = (element: any);
  40. scrollPosition = getScrollPosition(Style.getScrollParent(node));
  41. }
  42. const fragment = getFragmentFromSelection(editorState);
  43. editor.setClipboard(fragment); // Set `cut` mode to disable all event handling temporarily.
  44. editor.setMode('cut'); // Let native `cut` behavior occur, then recover control.
  45. setTimeout(() => {
  46. editor.restoreEditorDOM(scrollPosition);
  47. editor.exitCurrentMode();
  48. editor.update(removeFragment(editorState));
  49. }, 0);
  50. }
  51. function removeFragment(editorState: EditorState): EditorState {
  52. const newContent = DraftModifier.removeRange(editorState.getCurrentContent(), editorState.getSelection(), 'forward');
  53. return EditorState.push(editorState, newContent, 'remove-range');
  54. }
  55. module.exports = editOnCut;