Repository navigation
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
There was a problem hiding this comment.
Thanks for tracking this down. The fix does what #5016 asks: with a real click into another input and real keystrokes while AutoLinkPlugin re-renders, focus stays in the input on this branch in both Chromium and Firefox. On main, focus jumps to the editor after the first key.
One regression to fix before merging
packages/lexical/src/LexicalUtils.ts:773: the skip tags are passed through the tag option, but update tags belong to the pending editor state, not to a single editor.update() call. Any later editor.update() in the same tick joins that batch (see packages/lexical-website/docs/concepts/updates.mdx) and inherits SKIP_DOM_SELECTION_TAG, so $commitPendingUpdates skips $updateDOMSelection for the whole batch. The description covers the order where the focus request comes first, but the reverse order breaks:
Browser test, prepopulated hello world, editor not focused |
main |
this PR |
|---|---|---|
<AutoLinkPlugin/> then <AutoFocusPlugin/> |
caret at 11 (end) | caret at 0 |
<HashtagPlugin/> then <AutoFocusPlugin/> |
caret at 11 | caret at 0 |
<AutoLinkPlugin/> then a plugin whose effect calls editor.focus() |
focused | not focused |
editor.registerNodeTransform(TextNode, ...) then editor.focus() |
focused | not focused |
The Backwards Compatibility section of AGENTS.md asks that existing API behaviour stay intact, and nothing documents a required plugin order inside LexicalComposer. One way to keep the skip scoped to the re-validation is to commit it on its own when the editor is unfocused ({discrete: true, tag: [...]}), so that later updates start a fresh batch. That variant keeps the four new tests passing, at the cost of running the transform synchronously at registration. Deciding at commit time is another option.
Suggestions (optional, non-blocking)
- Add a test that orders the focus request after the registration; it would have caught the regression above. The Testing Strategy section of AGENTS.md puts behaviour that depends on a real selection engine in browser tests (
packages/**/__tests__/browser/**). The current tests place the DOM selection by hand in jsdom (Issue5016Repro.test.ts:89,LexicalAutoLinkPlugin.test.tsx:110). That matches Firefox but not Chromium, where the selection leaves the editor when the input is clicked. - The Test plan's After section describes the results; the Commits and Pull Requests section of AGENTS.md asks for the actual pasted output.
… an unfocused editor registerNodeTransform marks existing nodes of that type dirty so the new transform runs on them. That update restored the DOM selection, which focused the editor root, so a plugin re-registering its transforms (eg. AutoLinkPlugin with inline matchers) took focus from another input. When the editor does not have focus, also tag that update with SKIP_DOM_SELECTION_TAG and SKIP_SELECTION_FOCUS_TAG. Closes meta#5016 Signed-off-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com> Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s the DOM selection Update tags last for the whole batch, so tagging the re-validation with SKIP_DOM_SELECTION_TAG also dropped a focus request or selection change made later in the same tick (eg. AutoFocusPlugin mounted after AutoLinkPlugin). Tag the re-validation with an internal tag, and skip the DOM selection at commit only if the selection did not change in the batch. Add browser tests for both orders. Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2e7427d to
81feabf
Compare
|
Thanks for the review, that was a real regression. Fixed in the latest push by deciding at commit time, the second option you mention:
Also rebased onto — Ved Thakar |
There was a problem hiding this comment.
Thanks for working on #5016. This looks good to me. Checking at commit time in packages/lexical/src/LexicalUpdates.ts stops a transform registration from pulling focus into an unfocused editor. editor.focus(), AutoFocusPlugin and a selection set in the same batch still apply, which addresses the earlier concern about skip tags leaking into batched updates. The new browser test runs in the real selection engine, and its two companion cases cover that batching path.
The suggestions below are optional and do not block the merge:
- Add a
Verify:paragraph to the changeset (.changeset/transform-registration-keeps-focus.md). CONTRIBUTING.md ("Changesets") and.changeset/README.mdask a fix that changes observable editing behaviour to carry one, for example:
Verify: with a plugin that re-registers its transforms on render (AutoLinkPlugin with an inline matchers array), typing in another input keeps focus in that input; editor.focus() and AutoFocusPlugin still focus the editor. - Update the description. "What this changes" still says
SKIP_DOM_SELECTION_TAGandSKIP_SELECTION_FOCUS_TAGare added to the update options, but the branch now uses the internalREVALIDATE_UNFOCUSED_TAGand decides at commit time. AGENTS.md ("Commits and Pull Requests") asks the Test plan to paste the real Before and After output. Please addpackages/lexical/src/__tests__/browser/Issue5016TransformFocus.test.tswith its output, since the description becomes the squash commit. - Transforms that move the caret (
LexicalUpdates.ts:822-827). When the newly registered transform itself moves the caret,Point.setmarks the selection dirty and the editor still takes focus. One example is the firstregisterLexicalHashtagoverhello #worldwith the caret at the end while another input is focused. This matchesmainand theREVALIDATE_UNFOCUSED_TAGJSDoc, so it is not a regression. A sentence in the changeset, or a follow-up that tells transform-driven selection moves apart from requested ones, would set expectations. - Browser coverage for the React case. The
AutoLinkPlugintest is jsdom-only and recreates the browser's retained selection withsetBaseAndExtent. A version underpackages/lexical-react/src/__tests__/browser/that uses a controlled input anduserEvent.keyboardwould cover the issue's setup end to end.
| 'lexical': patch | ||
| --- | ||
|
|
||
| [fix] Registering a node transform, as `AutoLinkPlugin` does when it re-renders, no longer moves focus into an editor that does not have it |
There was a problem hiding this comment.
Optional: CONTRIBUTING.md ("Changesets") and .changeset/README.md ask a fix that changes observable editing behaviour for a Verify: paragraph, so it is listed under "Behavior changes". For example:
Verify: with a plugin that re-registers its transforms on render (AutoLinkPlugin with an inline matchers array), typing in another input keeps focus in that input; editor.focus() and AutoFocusPlugin still focus the editor.| !( | ||
| tags.has(REVALIDATE_UNFOCUSED_TAG) && | ||
| pendingSelection !== null && | ||
| !pendingSelection.dirty && |
There was a problem hiding this comment.
Non-blocking: transforms the new registration runs can set dirty here themselves. A transform that splits or replaces the caret's text node goes through Point.set, which marks the selection dirty. For example, the first registerLexicalHashtag over hello #world with the caret at the end moves the caret to #world@6, and the editor still takes focus from a focused outside input. That matches main and the tag's JSDoc, so it is not a regression. It may be worth a line in the changeset, or a follow-up that tells transform-driven moves apart from requested ones.
Description
This solves issue #5016: with
AutoLinkPluginmounted, typing in another input on the page moves focus back into the editor.Current behavior.
registerNodeTransformcallsmarkNodesWithTypesAsDirty, which opens aneditor.update()that marks every existing node of that type dirty so the new transform runs on content that already exists. That update is taggedHISTORY_MERGE_TAGonly. At commit, dirty nodes meanneedsUpdateis true, so$updateDOMSelectionruns. If the browser's document selection is still in the editor (browsers keep it there when you focus an<input>), it callsrootElement.focus(). Otherwise it moves the document selection into the editor. Either way the editor takes focus from the input.AutoLinkPlugin's effect depends onmatchers, so an inline matchers array (as in the issue's sandbox) re-registers the transforms on every render of the parent, which happens on every keystroke in the other input.What this changes.
markNodesWithTypesAsDirty, when the update is a new one and the editor's root element does not contain the active element, the update is also tagged with an internal tag (revalidate-unfocused). This re-validation is not a user edit, so on its own it must not move focus or the selection into an editor that doesn't have focus.editor.focus()or any selection change made later in the same tick marks the selection dirty and is applied as onmain(eg.AutoFocusPluginmounted afterAutoLinkPlugin).HISTORY_MERGE_TAGonly). A transform that changes the DOM under the caret still gets the DOM selection restored.Relation to #9177. #9177 (open) moves
HISTORY_MERGE_TAGinto the update callback with$addUpdateTag, so it also applies to an already-open update. It does not touch the selection or focus, so it doesn't fix this issue. The two changes are compatible. If #9177 lands first, the conflict is limited to the options argument of the sameeditor.update()call.I left
AutoLinkPlugin's dependency on matcher identity alone. With this fix, re-registering is harmless for focus, and memoizing matchers is an API decision for maintainers.Closes #5016
Test plan
New tests:
packages/lexical/src/__tests__/unit/Issue5016Repro.test.ts:registerNodeTransform, the transform has run on the existing text, the update is taggedHISTORY_MERGE_TAG, anddocument.activeElementis still the input.packages/lexical-react/src/__tests__/unit/LexicalAutoLinkPlugin.test.tsx› "does not take focus from another input when it re-renders": the issue's setup.AutoLinkPlugingets an inline matchers array and the parent re-renders while the input has focus.Before
On
mainwith the tests applied and the source change reverted:After
On this branch, rebased onto current
main:packages/lexical/src/__tests__/browser/Issue5016TransformFocus.test.ts(browser, Chromium, real click into another input): 3 passed. "registering a transform leaves focus in another input" fails onmain; "editor.focus() after registering a transform still focuses the editor" and "a selection made after registering a transform is still applied" fail with the first version of this PR, which tagged the batch withSKIP_DOM_SELECTION_TAG.Issue5016Repro.test.ts,LexicalAutoLinkPlugin.test.tsxandLexicalEditor.test(unit): 99 passed.tsc,tsc-test, eslint and prettier on the changed files: clean. Firefox and WebKit were not run locally.Author: Ved Thakar (@Vedthakar)
🤖 Generated with Claude Code