UNPKG

1.48 kBJavaScriptView Raw
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
13var EditorState = require("./EditorState");
14
15function keyCommandUndo(e, editorState, updateFn) {
16 var undoneState = EditorState.undo(editorState); // If the last change to occur was a spellcheck change, allow the undo
17 // event to fall through to the browser. This allows the browser to record
18 // the unwanted change, which should soon lead it to learn not to suggest
19 // the correction again.
20
21 if (editorState.getLastChangeType() === 'spellcheck-change') {
22 var nativelyRenderedContent = undoneState.getCurrentContent();
23 updateFn(EditorState.set(undoneState, {
24 nativelyRenderedContent: nativelyRenderedContent
25 }));
26 return;
27 } // Otheriwse, manage the undo behavior manually.
28
29
30 e.preventDefault();
31
32 if (!editorState.getNativelyRenderedContent()) {
33 updateFn(undoneState);
34 return;
35 } // Trigger a re-render with the current content state to ensure that the
36 // component tree has up-to-date props for comparison.
37
38
39 updateFn(EditorState.set(editorState, {
40 nativelyRenderedContent: null
41 })); // Wait to ensure that the re-render has occurred before performing
42 // the undo action.
43
44 setTimeout(function () {
45 updateFn(undoneState);
46 }, 0);
47}
48
49module.exports = keyCommandUndo;
\No newline at end of file