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.
 
 
 
 

49 lines
1.5 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. * @flow strict-local
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. const EditorState = require("./EditorState");
  13. function keyCommandUndo(e: SyntheticKeyboardEvent<>, editorState: EditorState, updateFn: (editorState: EditorState) => void): void {
  14. const undoneState = EditorState.undo(editorState); // If the last change to occur was a spellcheck change, allow the undo
  15. // event to fall through to the browser. This allows the browser to record
  16. // the unwanted change, which should soon lead it to learn not to suggest
  17. // the correction again.
  18. if (editorState.getLastChangeType() === 'spellcheck-change') {
  19. const nativelyRenderedContent = undoneState.getCurrentContent();
  20. updateFn(EditorState.set(undoneState, {
  21. nativelyRenderedContent
  22. }));
  23. return;
  24. } // Otheriwse, manage the undo behavior manually.
  25. e.preventDefault();
  26. if (!editorState.getNativelyRenderedContent()) {
  27. updateFn(undoneState);
  28. return;
  29. } // Trigger a re-render with the current content state to ensure that the
  30. // component tree has up-to-date props for comparison.
  31. updateFn(EditorState.set(editorState, {
  32. nativelyRenderedContent: null
  33. })); // Wait to ensure that the re-render has occurred before performing
  34. // the undo action.
  35. setTimeout(() => {
  36. updateFn(undoneState);
  37. }, 0);
  38. }
  39. module.exports = keyCommandUndo;