Skip to content

Add a react extension that tracks transactions#3

Merged
smoores-dev merged 1 commit intomainfrom
effects-and-annotations
Aug 26, 2025
Merged

Add a react extension that tracks transactions#3
smoores-dev merged 1 commit intomainfrom
effects-and-annotations

Conversation

@smoores-dev
Copy link
Member

Ok, so here are the constraints we're under:

  1. While CodeMirror's EditorView has an API for replacing the entire EditorState, unlike in ProseMirror, doing so is very expensive. Doing so on every keypress, even for small docs, simply will not work — it will also produce broken behavior, like flashing highlighting and viewport resetting, etc.
  2. CodeMirror's functionality relies heavily on effects, and somewhat less heavily on annotations, which are tracked in Transactions. There is no way to inspect an EditorState and determine which effects have been applied to produce it, nor is there a way to compare two EditorStates and determine what effects would transform one to the other.

This means that in order to keep the EditorView in sync with the lifted EditorState, we must track the set of transactions that has been applied to the lifted EditorState, and apply them to the EditorView's state.

To accomplish this, we introduce a new react extension that has a StateField that literally just accumulates a list of transactions that it sees. The useEditor hook then pulls those transactions out of that StateField and applies them to the view's state.

In order to avoid applying the same transaction multiple times, we keep a set of transactions that we've already seen, and filter them out of the StateField. This ought to be safe — Transactions must be based on the EditorState they're being applied to, so it's impossible to dispatch the same transaction instance twice (they would have to have different state bases).

This is a breaking change!

@smoores-dev smoores-dev merged commit 0830527 into main Aug 26, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant