Repository navigation
[lexical-rich-text] Bug Fix: Text typed after selecting a horizontal rule no longer jumps to the top of the document - #9845
Conversation
…rule no longer jumps to the top of the document ## Description Letters typed while a horizontal rule, or any other block decorator, is selected land at the very top of the document instead of next to it. ### Why Type `---` and a space to make a horizontal rule. Put the caret at the end of the paragraph above it and press the down arrow. The rule is now selected and gets its highlight, and the caret is gone. While a rule is selected, the editor takes the browser's own selection away. Now type the letter `q`. It does not go next to the rule. With no caret to follow, the browser puts it in the very first position in the document, which can be a long way off screen. The person typing sees the rule highlighted and reasonably expects the letter to follow it. Nothing says it went elsewhere. The only way back is to notice, undo, and move the caret to the right place by hand. The arrow keys and Enter already step off a selected rule. Typed text did not. ### What When one block-level decorator, such as a horizontal rule, is selected and a letter is typed, the caret first moves to the start of the block after it. If nothing block-level follows, it goes into a new paragraph after the rule. Then the letter is typed as usual. ### Shape This copies what Enter already does for a selected decorator in the same file. It is one handler on `BEFORE_INPUT_COMMAND`, registered at low priority so it runs before the default handler. Nothing is exported. ### What this does not change Plain text editors. meta#9282 reports the same cause there, and meta#9286 and meta#9710 cover the arrow keys. Arrow keys, Enter and Backspace on a selected rule are as before. Inline decorators and selections of more than one node are left alone. Related: meta#9282 ## Changeset `.changeset/rich-text-type-after-selected-block-decorator.md` ## Test plan New file: `packages/lexical-rich-text/src/__tests__/unit/RichTextNodeSelectionTyping.test.ts`. ### Before `pnpm exec vitest run --project unit packages/lexical-rich-text/src/__tests__/unit/RichTextNodeSelectionTyping.test.ts` without the change to `index.ts`: ``` × types at the start of the block after it × types into a new paragraph when nothing follows Tests 2 failed | 1 passed (3) ``` In Chromium on the playground: type `first`, Enter, `before`, Enter, `--- `, `after`; put the caret at the end of `before`; press ArrowDown; type `q`: ``` ["qfirst","before","<HR>","after"] ``` The same steps on playground.lexical.dev put the `q` at the top of the document: `qWelcome to the playground`. ### After ``` Test Files 21 passed (21) Tests 175 passed (175) ``` Chromium, same steps: ``` ["first","before","<HR>","qafter"] ``` Full unit suite: 29,809 passed, 4 failed. The 4 are in `lexical-eslint-plugin`, `lexical-html`, `lexical-y` and `lexical` (docs, README and generated-JSON checks) and fail the same way without this change. E2E, Chromium, rich text: 620 passed. 15 did not start (Composition and Share launch their own browser, which is not installed here) and 2 DateTime tests failed under load and pass on re-run. E2E, Chromium, plain text: 80 passed, 12 did not start for the same reason. Not exercised: Firefox, WebKit, the collab runs, the browser-mode tests and the production build.
|
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: |
|
AI review status for this pull request.
|
There was a problem hiding this comment.
Thanks, this looks good. The new handler runs ahead of core's default beforeinput handler, so the browser's own insertion is cancelled and the letter goes through the normal controlled insertion in the same update. In the playground on Chromium, your steps give qfirst on main and qafter on this branch, and one undo removes both the letter and the paragraph the handler inserted. In Firefox, typing with a selected rule inserts nothing, both before and after this change.
A few optional, non-blocking suggestions:
- Keep the formatting of the text the letter lands in front of (
packages/lexical-rich-text/src/index.ts:1474).next.selectStart()returns a selection with no format, so with a boldafteryou get a plainqfollowed by a boldafter. Clicking at the start of that text and typing gives a boldq, because the selection-change handler copies the text node's format. Copying it here keeps the two paths consistent:const selection = next.selectStart(); const anchorNode = selection.anchor.getNode(); if ($isTextNode(anchorNode)) { selection.setFormat(anchorNode.getFormat()); selection.setStyle(anchorNode.getStyle()); }
- Assert that the browser default is prevented (
packages/lexical-rich-text/src/__tests__/unit/RichTextNodeSelectionTyping.test.ts,type()at line 64). The bug is the browser inserting at the top of the document. In jsdom, the signal for that isevent.defaultPrevented, which istruewith this change andfalseonmain. Returning the event fromtype()and asserting it in the first two cases would pin the fix. AGENTS.md's testing section leaves real input and selection behaviour to browser or E2E tests, so a case next to the ArrowDown cases inpackages/lexical-playground/__tests__/e2e/HorizontalRule.spec.mjswould also be welcome. - Nit: the third case ('leaves a range selection where it was') passes only because jsdom performs no native insertion. In a browser, the
qwould be typed at the caret. Assertingevent.defaultPrevented === falseplus the selection would say what the test means.
IME composition (insertCompositionText) still never reaches this handler, because core returns before dispatching for it. That would be a natural follow-up rather than something for this PR.
| ) { | ||
| const next = nodes[0].getNextSibling(); | ||
| if ($isElementNode(next) && !next.isInline()) { | ||
| next.selectStart(); |
There was a problem hiding this comment.
Optional: selectStart() returns a selection with format 0 and no style, so typing before bold text gives a plain q followed by a bold after. Clicking at the start of that text and typing gives a bold q, because the selection-change handler copies the text node's format. Copying it keeps the two paths consistent:
const selection = next.selectStart();
const anchorNode = selection.anchor.getNode();
if ($isTextNode(anchorNode)) {
selection.setFormat(anchorNode.getFormat());
selection.setStyle(anchorNode.getStyle());
}| data, | ||
| inputType: 'insertText', | ||
| }); | ||
| editor.update(() => editor.dispatchCommand(BEFORE_INPUT_COMMAND, event), { |
There was a problem hiding this comment.
Optional: the bug is the browser inserting at the top of the document, and in jsdom the signal for that is event.defaultPrevented: true with this change, false on main. Returning the event from type() and asserting expect(event.defaultPrevented).toBe(true) in the first two cases would pin the fix. In the third case, false would replace asserting that no q appeared, which only holds because jsdom performs no native insertion.
Description
Letters typed while a horizontal rule, or any other block decorator, is selected land at the very top of the document instead of next to it.
Why
Type
---and a space to make a horizontal rule. Put the caret at the end of the paragraph above it and press the down arrow. The rule is now selected and gets its highlight, and the caret is gone. While a rule is selected, the editor takes the browser's own selection away.Now type the letter
q. It does not go next to the rule. With no caret to follow, the browser puts it in the very first position in the document, which can be a long way off screen.The person typing sees the rule highlighted and reasonably expects the letter to follow it. Nothing says it went elsewhere. The only way back is to notice, undo, and move the caret to the right place by hand.
The arrow keys and Enter already step off a selected rule. Typed text did not.
What
When one block-level decorator, such as a horizontal rule, is selected and a letter is typed, the caret first moves to the start of the block after it. If nothing block-level follows, it goes into a new paragraph after the rule. Then the letter is typed as usual.
Shape
This copies what Enter already does for a selected decorator in the same file. It is one handler on
BEFORE_INPUT_COMMAND, registered at low priority so it runs before the default handler. Nothing is exported.What this does not change
Plain text editors. #9282 reports the same cause there, and #9286 and #9710 cover the arrow keys. Arrow keys, Enter and Backspace on a selected rule are as before. Inline decorators and selections of more than one node are left alone.
Related: #9282
Changeset
.changeset/rich-text-type-after-selected-block-decorator.mdTest plan
New file:
packages/lexical-rich-text/src/__tests__/unit/RichTextNodeSelectionTyping.test.ts.Before
pnpm exec vitest run --project unit packages/lexical-rich-text/src/__tests__/unit/RichTextNodeSelectionTyping.test.tswithout the change toindex.ts:In Chromium on the playground: type
first, Enter,before, Enter,---,after; put the caret at the end ofbefore; press ArrowDown; typeq:The same steps on playground.lexical.dev put the
qat the top of the document:qWelcome to the playground.After
Chromium, same steps:
Full unit suite: 29,809 passed, 4 failed. The 4 are in
lexical-eslint-plugin,lexical-html,lexical-yandlexical(docs, README and generated-JSON checks) and fail the same way without this change.E2E, Chromium, rich text: 620 passed. 15 did not start (Composition and Share launch their own browser, which is not installed here) and 2 DateTime tests failed under load and pass on re-run. E2E, Chromium, plain text: 80 passed, 12 did not start for the same reason.
Not exercised: Firefox, WebKit, the collab runs, the browser-mode tests and the production build.