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.
 
 
 
 

70 lines
2.1 KiB

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