Skip to content

Coalesce typing/deletion, add separate selection for undo - #966

Merged
trueadm merged 2 commits into
mainfrom
undo-redo-with-own-selection
Dec 10, 2021
Merged

trueadm merged 2 commits into
mainfrom
undo-redo-with-own-selection

Conversation

@fantactuka

@fantactuka fantactuka commented Dec 7, 2021 •

Copy link
Copy Markdown
Contributor
  • Merge history entries if changes have same type (insert, delete forward, delete backward), affecting single char at a time and happen within merge time window
  • Add separate selection for undo
    Before:
    Kapture 2021-12-07 at 11 17 31
    After:
    Kapture 2021-12-07 at 11 16 40

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 7, 2021
@fantactuka

Copy link
Copy Markdown
Contributor Author

Submitting for initial feedback, will add some tests to cover changes

@trueadm

trueadm commented Dec 7, 2021

Copy link
Copy Markdown
Collaborator

You'll need to update the e2e tests that are failing with the new logic.

Comment thread packages/outline/src/core/index.js Resolved
@fantactuka
fantactuka force-pushed the undo-redo-with-own-selection branch from 46f57b9 to b29c173 Compare December 8, 2021 18:48
@fantactuka

Copy link
Copy Markdown
Contributor Author

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

@fantactuka
fantactuka force-pushed the undo-redo-with-own-selection branch from b29c173 to ea01355 Compare December 8, 2021 18:53
Comment on lines 115 to 141

@fantactuka fantactuka Dec 8, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@fantactuka fantactuka Dec 8, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@fantactuka
fantactuka force-pushed the undo-redo-with-own-selection branch 2 times, most recently from 90b6645 to f5f90d8 Compare December 8, 2021 20:13
@fantactuka
fantactuka force-pushed the undo-redo-with-own-selection branch from 75dab1d to c492267 Compare December 8, 2021 20:28

@zurfyx zurfyx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@fantactuka fantactuka Dec 8, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines 119 to 124

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment on lines 153 to 170

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@fantactuka fantactuka Dec 9, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one tries to address special char selection with keyboard (to merge walking through the list into single history entry), it might need better selection check though

UPD: found editor.isComposing() which is seems to be better for this usecase

Kapture 2021-12-08 at 18 12 21

Comment on lines 177 to 178

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Opinionated] This is confusing as a currified function. I'd never expect the currification is to track the start time

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@zurfyx zurfyx left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@fantactuka

fantactuka commented Dec 9, 2021 •

Copy link
Copy Markdown
Contributor Author

I've updated ChangeTypes to be less event-like and added check for collapsed selections. Unfortunately check for offset change (prevSelection.anchor.offset + 1 === nextSelection.focus.offset) won't work in plain text scenario:

<p>
A<br />
B<br />
C|
</p>

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").
Instead I'll check if prev anchor/focus node was element and new anchor/focus node is text node that didn't exist before (see updated implementation)

Adding extra text validation also makes sense, just want to discuss a way to check performance implication offline

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's hoist out nextEditorState._nodeMap to a variable.

@trueadm trueadm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@fantactuka
fantactuka force-pushed the undo-redo-with-own-selection branch from 179594b to 0681454 Compare December 9, 2021 22:18
@fantactuka

Copy link
Copy Markdown
Contributor Author

Did some cleanup related to new history tagging, will merge this PR to prevent further rebasing and do smaller adjustments as separate ones

@trueadm
trueadm merged commit 33bc585 into main Dec 10, 2021
@trueadm
trueadm deleted the undo-redo-with-own-selection branch December 10, 2021 01:46
@trueadm

trueadm commented Dec 10, 2021

Copy link
Copy Markdown
Collaborator

Merged to prevent further conflicts.

acywatson pushed a commit that referenced this pull request Apr 9, 2022
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants