From f5574cb48af1418833185d408128c0886807320d Mon Sep 17 00:00:00 2001 From: Sidney Keese Date: Wed, 18 Nov 2020 09:25:21 -0800 Subject: [PATCH] Allow link-previews to trigger without needing a space Co-authored-by: Chris Svenningsen --- ts/components/CompositionInput.tsx | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/ts/components/CompositionInput.tsx b/ts/components/CompositionInput.tsx index da9188a4cab..8b4e352fc17 100644 --- a/ts/components/CompositionInput.tsx +++ b/ts/components/CompositionInput.tsx @@ -377,14 +377,23 @@ export const CompositionInput: React.ComponentType = props => { return; } - if (propsRef.current.onEditorStateChange) { - const selection = quill.getSelection(); - - propsRef.current.onEditorStateChange( - text, - mentions, - selection ? selection.index : undefined - ); + const { onEditorStateChange } = propsRef.current; + + if (onEditorStateChange) { + // `getSelection` inside the `onChange` event handler will be the + // selection value _before_ the change occurs. `setTimeout` 0 here will + // let `getSelection` return the selection after the change takes place. + // this is necessary for `maybeGrabLinkPreview` as it needs the correct + // `caretLocation` from the post-change selection index value. + setTimeout(() => { + const selection = quill.getSelection(); + + onEditorStateChange( + text, + mentions, + selection ? selection.index : undefined + ); + }, 0); } }