Skip to content

[lexical] Refactor: Move event module globals into per-editor InputState#8809

Merged
etrepum merged 7 commits into
facebook:mainfrom
mayrang:refactor/input-state-fsm
Jul 8, 2026
Merged

[lexical] Refactor: Move event module globals into per-editor InputState#8809
etrepum merged 7 commits into
facebook:mainfrom
mayrang:refactor/input-state-fsm

Conversation

@mayrang

@mayrang mayrang commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Description

LexicalEvents.ts has 15 module-level let variables that track composition state, keyboard timing, selection-change flags, and other input-related signals. Because they're module globals, every editor instance on the same page shares them. That's fine with a single editor, but with nested or sibling editors a flag set by one instance leaks into another — for example, isSelectionChangeFromMouseDown set by a pointerdown in a nested editor stays stale in the parent because only one selectionchange fires.

This PR moves all 15 variables into a per-editor InputState record on editor._inputState, so each editor tracks its own input state independently. This is the structural half of the composition FSM work discussed in #8793; a follow-up PR will add explicit authority transfer between composition phases.

This is split into its own PR because the structural move (globals → per-editor fields) is a large mechanical diff that's easy to verify in isolation: every existing test should pass without modification. The follow-up PR builds on this foundation to add explicit phase-transition guards and authority transfer, which is a behavioral change that benefits from a separate review.

What changed

InputState interface + createInputState() factory (LexicalEditor.ts) — All 15 fields live here. The positional tuple [number, string, number, NodeKey, number] for collapsed selection format becomes a named CollapsedSelectionFormat interface. Three boolean composition flags plus safariEndCompositionEventData are unified into a compositionPhase discriminated union ('idle' | 'composing' | 'ending-firefox' | 'ending-safari') and a single compositionEndData string. Everything is @internal.

Event handler migration (LexicalEvents.ts) — Every reference to the old globals now reads from editor._inputState or a local const inputState = editor._inputState binding. Helpers like isPossiblyAndroidKeyPress take inputState as a parameter instead of closing over module state.

Stale flag fixisSelectionChangeFromMouseDown had the cross-editor bug described above. The new code clears the flag on all editors registered on the same document when selectionchange fires.

resetEditor cleanup — Clears handledSelectionCommandTimeoutId before replacing _inputState to avoid orphaned timeouts.

Caller updates (LexicalSelection.ts)markSelectionChangeFromDOMUpdate and markCollapsedSelectionFormat now take editor as the first argument.

Design notes

Why per-editor, not per-document or per-root? Composition state is inherently per-editor: each LexicalEditor has its own _compositionKey, update queue, and selection. Grouping input state at the document level would still allow cross-contamination between sibling editors on the same document. Per-root was considered but adds no benefit over per-editor since each editor already owns exactly one root.

Why a flat record instead of a class? InputState is a plain object with no methods — just a bag of mutable fields read and written by event handlers. A class would add a prototype chain and constructor ceremony with no behavioral benefit. The createInputState() factory keeps initialization in one place.

What stays as module globals? The per-document registries (rootElementToDocument, documentRegistrations, documentSelectionChange) are genuinely per-document — they track which editors are attached to a given Document for selectionchange dispatch. Moving them to per-editor state would require cross-editor coordination that already exists naturally at the module level.

compositionPhase union vs. boolean flags — The old isFirefoxEndingComposition / isSafariEndingComposition booleans were mutually exclusive states of the same concept. A discriminated union makes the state machine explicit: exactly one phase is active at a time, and the valid transitions are readable from the type.

Test plan

  • pnpm test-unit — 3682 pass
  • pnpm test-e2e-chromium — 768 pass
  • tsc / prettier / eslint clean
  • No test changes needed — all existing composition tests (unit, browser, e2e) pass without modification, confirming behavioral equivalence.

mayrang added 4 commits July 8, 2026 03:28
…tState

Add InputState and CollapsedSelectionFormat interfaces with a
createInputState() factory to LexicalEditor.ts. Replace all 15
module-level let variables in LexicalEvents.ts with per-editor
editor._inputState.* access. Internal helpers that lack an editor
parameter receive InputState directly. Export functions
markSelectionChangeFromDOMUpdate and markCollapsedSelectionFormat
now take editor as the first parameter.
Replace isFirefoxEndingComposition (boolean) and
isSafariEndingComposition (boolean) with a single compositionPhase
discriminated union: 'idle' | 'composing' | 'ending-firefox' |
'ending-safari'. Set compositionPhase to 'idle' in the Chrome
$handleCompositionEnd path for state machine completeness. Clear
handledSelectionCommandTimeoutId in resetEditor before replacing
_inputState.
Replace the positional [number, string, number, NodeKey, number]
tuple with a named CollapsedSelectionFormat interface (format, style,
offset, key, timeStamp). Clear isSelectionChangeFromMouseDown on all
document-registered editors when consuming the flag, preserving the
previous single-global semantics when pointerdown bubbles from a
nested editor.
Restore browser event-ordering comments in onCompositionEnd explaining
why Firefox and Safari defer composition handling. Deduplicate the
Safari compositionPhase reset in $handleKeyDown. Reuse the
already-computed documentRegistrations lookup in the stale-flag
clear-all. Add @internal markers to markSelectionChangeFromDOMUpdate
and markCollapsedSelectionFormat. Update stale test comment referencing
the removed isFirefoxEndingComposition variable.
@vercel

vercel Bot commented Jul 7, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Jul 8, 2026 6:30am
lexical-playground Ready Ready Preview Jul 8, 2026 6:30am

Request Review

@meta-cla meta-cla 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 Jul 7, 2026
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Jul 7, 2026
etrepum
etrepum previously approved these changes Jul 8, 2026

@etrepum etrepum 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.

Really liking the direction here, encapsulating all of this input state is going to make this so much easier to maintain

@etrepum etrepum dismissed their stale review July 8, 2026 04:59

It looks like there's some kind of conflict after the imports update that caused the build to fail

@etrepum etrepum added this pull request to the merge queue Jul 8, 2026
Merged via the queue into facebook:main with commit 932aa67 Jul 8, 2026
46 checks passed
@etrepum etrepum mentioned this pull request Jul 9, 2026
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. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants