Store caret position in history and use this info on Undo/Redo #884
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.
Opened this PR to discuss a couple of problems I found with RichText text based blocks and history.
Trying to fix #303 - Cursor positioning on Rich Text-based blocks after undoing is pos+1 - found out it is a problem with the caret position not kept retained in history.
We're only storing the content in history, then when the user taps on the undo/redo buttons the text is changed but the caret stays in the same position of before if possible. (it's often moved to the end of the block in case undo removes some text and
caret-position > newText.length
).I then started working on adding the position of the caret, together with the content, in the history stack, and made it "kind of working", even if it feels a bit of an hack - for ref the GB side branch is available here.
While trying to fix #303 I've encountered lot of problems with the history, and decided to switch back to
develop
to check if I can replicate them there, since was having hard time on figuring out if it was my code the culprit of the bad behavior.Below is a list of problems I see with history on
develop
:History in not empty on a clean start of the editor!? As soon as you tap in a block the
Back
button will be available, and if you tap on it "an action" is actually executed, the "redo" button slightly visible for a fraction of second, and the the "undo" is available again. See the GIF I attached belowIf you add a new empty block it's hard to get rid of hit by tapping on
Undo
. For the same reason of above, it seems two actions are executed in a sequence in a very small time.If you want to get rid of the block you need to tap "undo" many times, very fast.
Investigated a bit, and it seems to me that the problem is due to the
onSelectionChange
called at init and every time the user adds new text -onSelectionChange
is called right after theonChange
callback. So basically we're updating the JS side lot of times 🤔Not quite sure why we're calling
this.props.onChange( this.lastContent );
when the selection does change. cc @marecar3I think it's better to fix the problems with the history before starting storing caret position in it. Otherwise we're messing things up a little more.
Thoughts?