fix(widget-string): fix cursor jumping to end of line #4607
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.
Closes #3578
Closes #4539
The approach taken here seems suboptimal since it's generally not necessary when writing React components to manually update cursor location. Please let me know if anyone has a better solution.
I did do some debugging to try to identify the root issue, but did not find the root cause.
The summary of what I found is this: It seems that nested widgets undergo additional rendering because they trigger a state update for their parent component as well as themself. For example, when editing the alt text of an image using the built-in image block element the updating markdown string causes the parent markdown widget to update.
Summary
This PR fixes the cursor problem described in detail in the linked issues. To summarize: Nested inputs within any collection would consistently force the cursor to the end of the line. Making nested inputs unusable for content editing.
"Nested" refers to text inputs within another widget—the alt text input element on the image widget for example.
Test plan
No automated tests have been created. I tested this by first running
yarn start
and then inserting an image into the markdown widget. I inserted an image into a post in the "Posts" collection of the test server, then typed in the "Alt text" field. The cursor remained where you would expect it to remain.Although the URL may change, it looks something like: http://localhost:8080/#/collections/posts/entries/2020-11-20-post-number-19