Skip to content

Commit 6f2bf66

Browse files
authored
[Part 2] Pass fresh editorState to edit handlers (facebookarchive#1113)
I had mirrored parts of this change from FB to github but missed some things. This is the second part of the same change - context below. Earlier part merged as facebookarchive#1112 --- There have been bugs reported in cases where the users of Draft are updating 'editorState' in their custom handlers for keyDown, paste, etc. This can be a problem when they are using an 'editorState' that is in the parent's props or state and is not in sync, at that moment, with the editorState in Draft. This allows the parent to use the most updated editorState in custom handlers. This change was already reviewed by @spicyj and credit for this fix goes to @AlaNouri for making this change.
1 parent 3a390da commit 6f2bf66

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/component/base/DraftEditorProps.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ export type DraftEditorProps = {
107107

108108
// Useful for managing special behavior for pressing the `Return` key. E.g.
109109
// removing the style from an empty list item.
110-
handleReturn?: (e: SyntheticKeyboardEvent) => DraftHandleValue,
110+
handleReturn?: (
111+
e: SyntheticKeyboardEvent,
112+
editorState: EditorState,
113+
) => DraftHandleValue,
111114

112115
// Map a key command string provided by your key binding function to a
113116
// specified behavior.
@@ -121,9 +124,16 @@ export type DraftEditorProps = {
121124
// to trigger some special behavior. E.g. immediately converting `:)` to an
122125
// emoji Unicode character, or replacing ASCII quote characters with smart
123126
// quotes.
124-
handleBeforeInput?: (chars: string) => DraftHandleValue,
127+
handleBeforeInput?: (
128+
chars: string,
129+
editorState: EditorState,
130+
) => DraftHandleValue,
125131

126-
handlePastedText?: (text: string, html?: string) => DraftHandleValue,
132+
handlePastedText?: (
133+
text: string,
134+
html?: string,
135+
editorState: EditorState,
136+
) => DraftHandleValue,
127137

128138
handlePastedFiles?: (files: Array<Blob>) => DraftHandleValue,
129139

src/component/handlers/edit/editOnBeforeInput.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function editOnBeforeInput(editor: DraftEditor, e: SyntheticInputEvent): void {
8181
editor._pendingStateFromBeforeInput = undefined;
8282
}
8383

84-
var editorState = editor._latestEditorState;
84+
const editorState = editor._latestEditorState;
8585

8686
var chars = e.data;
8787

src/component/handlers/edit/editOnKeyDown.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function editOnKeyDown(editor: DraftEditor, e: SyntheticKeyboardEvent): void {
9494
// no special handling is performed, fall through to command handling.
9595
if (
9696
editor.props.handleReturn &&
97-
isEventHandled(editor.props.handleReturn(e))
97+
isEventHandled(editor.props.handleReturn(e, editorState))
9898
) {
9999
return;
100100
}

0 commit comments

Comments
 (0)