Update useSyntheticChange
to only call execCommand
if input is focused
#3562
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
useSyntheticChange
powers all updates to theMarkdownEditor
textarea under the hood. Using this hook instead of directly updating the input value allows us to preserve the caret position and browser undo stack. However, it usesexecCommand
which has all sorts of gotchas since it essentially just simulates the user typing in the input. This means the input needs to be focused, which is fine because it usually is when you are editing Markdown.When uploading a file, the delay between interaction and update may be significant. In this case, the user might have started working on something else. For example, they might have opened a dialog which activated a focus trap. Then we can't pull focus into the editor, so we can't use
execCommand
to update the input.Currently in this case we still call
execCommand
, unexpectedly updating whatever input is focused instead. This is obviously not great since it means we may insert file URLs into random spots. So instead, this PR updated the hook to ensure the input is focused before trying to callexecCommand
. If not, it falls back to the already-available fallback method of directly updating the input.Closes #3548
Merge checklist
[ ] Added/updated testsexecCommand
is not available in JSDom so we always use the fallback method in the test suite anyway.[ ] Added/updated documentationTake a look at the What we look for in reviews section of the contributing guidelines for more information on how we review PRs.