Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
Pass synthetic event to 'handleBeforeInput' callback
Browse files Browse the repository at this point in the history
Summary:
For purposes of logging, so that we can get the exact timestamp from the event
object for this event.

Reviewed By: bvaughn

Differential Revision: D7954113

fbshipit-source-id: 94e90385cd47bf0fe045d91fb8fb5167a1fffc05
  • Loading branch information
flarnie authored and facebook-github-bot committed May 10, 2018
1 parent fa88ee1 commit b86b5ce
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/APIReference-Editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ for details on usage.

#### handleBeforeInput
```
handleBeforeInput?: (chars: string, editorState: EditorState) => DraftHandleValue
handleBeforeInput?: (chars: string, editorState: EditorState, eventTimeStamp: number) => DraftHandleValue
```
Handle the characters to be inserted from a `beforeInput` event. Returning `'handled'`
causes the default behavior of the `beforeInput` event to be prevented (i.e. it is
Expand Down
1 change: 1 addition & 0 deletions src/component/base/DraftEditorProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export type DraftEditorProps = {
handleBeforeInput?: (
chars: string,
editorState: EditorState,
eventTimeStamp: number,
) => DraftHandleValue,

handlePastedText?: (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ const DraftEditorCompositionHandler = {
* twice could break the DOM, we only use the first event. Example: Arabic
* Google Input Tools on Windows 8.1 fires `compositionend` three times.
*/
onCompositionEnd: function(editor: DraftEditor): void {
onCompositionEnd: function(editor: DraftEditor, e: SyntheticEvent<>): void {
resolved = false;
stillComposing = false;
setTimeout(() => {
if (!resolved) {
DraftEditorCompositionHandler.resolveComposition(editor);
DraftEditorCompositionHandler.resolveComposition(editor, e);
}
}, RESOLVE_DELAY);
},
Expand All @@ -93,7 +93,7 @@ const DraftEditorCompositionHandler = {
// 20ms timer expires (ex: type option-E then backspace, or type A then
// backspace in 2-Set Korean), we should immediately resolve the
// composition and reinterpret the key press in edit mode.
DraftEditorCompositionHandler.resolveComposition(editor);
DraftEditorCompositionHandler.resolveComposition(editor, e);
editor._onKeyDown(e);
return;
}
Expand Down Expand Up @@ -129,7 +129,10 @@ const DraftEditorCompositionHandler = {
* Resetting innerHTML will move focus to the beginning of the editor,
* so we update to force it back to the correct place.
*/
resolveComposition: function(editor: DraftEditor): void {
resolveComposition: function(
editor: DraftEditor,
event: SyntheticEvent<>,
): void {
if (stillComposing) {
return;
}
Expand Down Expand Up @@ -165,7 +168,11 @@ const DraftEditorCompositionHandler = {
gkx('draft_handlebeforeinput_composed_text') &&
editor.props.handleBeforeInput &&
isEventHandled(
editor.props.handleBeforeInput(composedChars, editorState),
editor.props.handleBeforeInput(
composedChars,
editorState,
event.timeStamp,
),
)
) {
return;
Expand Down
4 changes: 3 additions & 1 deletion src/component/handlers/edit/editOnBeforeInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ function editOnBeforeInput(
// start of the block.
if (
editor.props.handleBeforeInput &&
isEventHandled(editor.props.handleBeforeInput(chars, editorState))
isEventHandled(
editor.props.handleBeforeInput(chars, editorState, e.timeStamp),
)
) {
e.preventDefault();
return;
Expand Down

0 comments on commit b86b5ce

Please sign in to comment.