Repository navigation
Coalesce typing/deletion, add separate selection for undo - #966
Conversation
|
Submitting for initial feedback, will add some tests to cover changes |
|
You'll need to update the e2e tests that are failing with the new logic. |
46f57b9 to
b29c173
Compare
|
Major update in PR: now changeType is calculated on every change (oppose to only mergeable cases before), so now we always know what was the previous edit and merge if changes were typing-related and had same type |
b29c173 to
ea01355
Compare
There was a problem hiding this comment.
Wondering it it's sufficient validation for the case, when we insert new text node into an empty element node (paragraph, list item, etc). What I tried to check here:
- text node didn't exist before
- it's the only child of element (block)
There was a problem hiding this comment.
Ok, just found that for plain text editor inserting new paragraph is made by linebreak, so we can't rely on new text node being the only child of an element.
Updating to check that dirty nodes only include one textNode that didn't exist before as indication of new text inserted
90b6645 to
f5f90d8
Compare
75dab1d to
c492267
Compare
zurfyx
left a comment
There was a problem hiding this comment.
Quite a dense PR but LGTM for the most part! I don't quite follow the undo selection, it would've been nice to split these in a separate PR but I guess it's fine as it is now
Side note:
You're checking dirty nodes so keep in mind that any of your computations can be a result of updates and transforms. For example, you can type an a but instead be converted to a b
There was a problem hiding this comment.
Why are ChangeTypes so specific to typing events? Updates can be caused by updates/transforms/selection which this function doesn't seem to account for
There was a problem hiding this comment.
Maybe NO_TYPING is misleading here and OTHER would be a better option, the goal of the function is to detect single character changes add single char, delete single char (both forward/backward) and replace single char
There was a problem hiding this comment.
So this logic here is trying to fix hashtags and mentions I suppose? Unfortunately we don't really have a way to tell whether an element was mutated because of part of events or any other sort of updates/transforms, the event update is still an update. We should probably discuss offline whether we really need this and/or can find an alternative as this may fail after we land #964 (trigger transform on siblings -> inserting a node in between will mark siblings as dirty).
(Would it be more reliable to look at selection? -- saying selection because even if we track beforeInput + input + keydown + etc. selection can still move as part of updates/transforms)
There was a problem hiding this comment.
Replace forward or no typing? Tricky one. I guess what you're saying makes sense cause you can empty have sort of empty updates with dirty nodes but I would definitely not expect this return from the function
There was a problem hiding this comment.
[Opinionated] This is confusing as a currified function. I'd never expect the currification is to track the start time
There was a problem hiding this comment.
Yeah, tried to isolate all merge action logic that otherwise would've been spread across useEffect below. Maybe a better name would help here to indicate that this function creates getter with internal state
There was a problem hiding this comment.
@fantactuka @trueadm - I want to discuss about the overall approach but at the same time I don't want to be blocker so feel free to dismiss this and merge the PR.
The current approach is very tied to events and Outline doesn't really have a good way to map events to EditorState. That is because an event triggers an update, but the new EditorState may be a combination of multiple updates + transforms. For example, there's no guarantee that after a beforeInput with data a there will be a letter a anywhere in the tree (a transformer might have gotten rid of it like emoticons :)).
If the events approach was reliable, I'd probably suggesting listening to the events directly or commands (even though commands are tricky because not every key press triggers a command) as you would have more control over the user interaction.
But given Outline mutation flow I'd consider an approach based on EditorState: given a previous EditorState with certain content and selection and an EditorState with certain content and selection can I merge them? This is very similar to your approach but there's a subtle difference, there's no mention of events.
if (prevSelection.isCollapsed() && nextSelection.isCollapsed()) {
if (prevSelection.anchor.offset + 1 === nextSelection.focus.offset &&
prevNode.getTextContent().length + 1 === nextNode.getTextContent() &&
nextNode.getTextContent().startsWith(prevNode.getTextContent.substring(...)) &&
nextNode.getTextContent().endsWith(...) {
// Character was inserted before the selection but does no necessarily mean it
// was caused by a key press event. However, we may still want to merge the history
// like it's a key press.
return CHARACTER_INSERTED_BEFORE_SELECTION;
}
}
WDYT?
There was a problem hiding this comment.
The dirty nodes array is based on a Set, so order is not guaranteed.
const dirtyLeaves = Array.from(dirtyLeavesSet);
const dirtyElements = Array.from(dirtyElementsSet);
You may want to do something like everything except one
|
I've updated ChangeTypes to be less event-like and added check for collapsed selections. Unfortunately check for offset change ( here prev focus/anchor offset before we insert "C" would be 4 (within paragraph element), and new focus/anchor offset is 1 (within text node after "C"). Adding extra text validation also makes sense, just want to discuss a way to check performance implication offline |
There was a problem hiding this comment.
Let's hoist out nextEditorState._nodeMap to a variable.
trueadm
left a comment
There was a problem hiding this comment.
Overall the approach looks good. It would be good to see if this has any perceivable impact on typing performance though. When you run this on the playground, do you notice any difference with typing metrics enabled from the bottom right toggle?
Check for simple text during multi node changes; EditorState clone to be readonly Put is-applying-history flag into history state so that it can be shared across nested/root editors that share same history state
179594b to
0681454
Compare
|
Did some cleanup related to new history tagging, will merge this PR to prevent further rebasing and do smaller adjustments as separate ones |
|
Merged to prevent further conflicts. |
* Coalesce typing/deletion, add separate selection for undo Check for simple text during multi node changes; EditorState clone to be readonly Put is-applying-history flag into history state so that it can be shared across nested/root editors that share same history state * Use historic tag to prevent history calculations, cleanup

Before:
After: