Repository navigation
[lexical-rich-text] Bug Fix: Text typed on a selected block decorator starts a paragraph of its own after it, instead of joining the block after it - #9876
Conversation
… starts a paragraph of its own after it, instead of joining the block after it ## Description #9845 fixed text typed while a horizontal rule (or any block decorator) is selected landing at the top of the document. It moved the caret to the start of the block after the decorator, so the typed text joined that block: with `Top / <hr> / End`, selecting the rule and typing `a` gave `Top / <hr> / aEnd`, editing a paragraph the person never pointed at. Typed text now goes into a paragraph right after the decorator: the empty paragraph already there, or a new one. `Top / <hr> / End` becomes `Top / <hr> / a / End`; with nothing after the rule, the text starts a new paragraph as before. That is what Enter's handler is for (a new paragraph after a selected block), what CKEditor 5 does on Enter for a selected block widget and on typing at its fake caret after it, and what ProseMirror's gap cursor does after a block node; none of them puts typed text at the start of the next existing block. Arrow keys are unchanged: they still step to the start of the block after the rule. The pending changeset from #9845 is updated to describe the new behavior (it has not been released). ## Test plan ### Before ``` $ pnpm exec vitest run --project unit RichTextNodeSelectionTyping # on main × types into a new paragraph after it, before the block that follows Tests 1 failed | 3 passed (4) ``` ### After ``` $ pnpm exec vitest run --project unit RichTextNodeSelectionTyping Tests 4 passed (4) $ pnpm exec vitest run --project unit packages/lexical-rich-text packages/lexical/ packages/lexical-playground packages/lexical-extension Tests 4100 passed | 4 skipped (4104) ``` Not exercised locally: the E2E suites and real browsers (CI runs them).
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
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, this looks good. Typing on a selected horizontal rule now starts a paragraph of its own right after it instead of joining the block below, the renamed test in RichTextNodeSelectionTyping.test.ts fails on main and passes here, and the pending changeset's summary and Verify: paragraph match the new behaviour.
A few optional, non-blocking suggestions:
- Empty heading or quote after the rule (
packages/lexical-rich-text/src/index.ts:1477): only an emptyParagraphNodeis reused, so an empty heading or quote right after the rule gets a new paragraph inserted in front of it and stays behind as a blank line. The playground creates that shape itself: with# Titleas the last block, Insert → Horizontal Rule leaves an empty heading after the rule, and clicking the rule and typingathen givesTitle / rule / a / (empty heading), wheremaintyped into the heading. If the text should fill that empty line,$isElementNode(next) && !next.isInline() && next.isEmpty()would reuse it; if a paragraph is intended, a unit test pinning the outcome would document the choice. - Real-browser coverage: both tests dispatch
BEFORE_INPUT_COMMANDfrom jsdom, so the nativebeforeinputpath is not exercised. AGENTS.md (Testing Strategy) puts behaviour that depends on a real selection engine in browser or E2E tests; a case inpackages/lexical-playground/__tests__/e2e/HorizontalRule.spec.mjsthat clicks the rule, types a letter and asserts the HTML would cover it across browsers. - Firefox: typing with the rule selected inserts nothing in Firefox, on
mainand on this branch alike, so the changeset'sVerify:step reproduces in Chromium only. Nothing this PR needs to fix, but worth knowing when verifying.
…orator for typed text, not only an empty paragraph
## Description
Review (CodeBunny, optional): only an empty paragraph after the selected decorator was reused, so an empty heading or quote there (inserting a horizontal rule after a heading that is the last block leaves one) got a new paragraph in front of it and stayed behind as a blank line, where `main` typed into it. Typed text now goes into any empty block element right after the decorator that can be empty (a paragraph, heading or quote), and into a new paragraph otherwise. Two cells pin it: an empty heading after the rule is filled, and a heading with text after it is left alone.
## Test plan
### After
```
$ pnpm exec vitest run --project unit RichTextNodeSelectionTyping
Tests 6 passed (6)
$ pnpm exec vitest run --project unit RichTextNodeSelectionTyping # on main
Tests 2 failed | 4 passed (6)
$ pnpm exec vitest run --project unit packages/lexical-rich-text packages/lexical/ packages/lexical-playground packages/lexical-extension packages/lexical-list packages/lexical-table
Tests 4668 passed | 9 expected fail | 4 skipped (4681)
```
There was a problem hiding this comment.
Thanks, this looks good. Text typed on a selected rule now starts its own paragraph instead of joining the block after it. An empty block already there (a paragraph, or the empty heading that inserting a rule at the end of a heading leaves) is filled, and one undo removes both the letter and the new paragraph. In the playground in Chromium, clicking the rule in Top / --- / End and typing a gives aEnd on main and a / End on this branch. With a list after the rule, you get aitem on main and a new paragraph in front of the list here.
A few optional, non-blocking suggestions:
- An E2E case for the real input path (
packages/lexical-rich-text/src/__tests__/unit/RichTextNodeSelectionTyping.test.ts).type()dispatchesBEFORE_INPUT_COMMANDdirectly in jsdom, so nothing covers clicking the rule, the nativebeforeinputand itspreventDefault. AGENTS.md (Testing Strategy) leaves real input to browser or E2E tests. A case beside the ArrowDown cases inpackages/lexical-playground/__tests__/e2e/HorizontalRule.spec.mjswould pin it: typeTop, Enter,---,End, click thehr, typea, then expectTop / hr / a / End. - Refresh the Test plan output. The PR body still shows the first commit's run (
1 failed | 3 passed (4), then4 passed (4)). The file now has 6 tests, and againstmainit gives2 failed | 4 passed (6), then6 passed (6)on this branch. AGENTS.md (Commits and Pull Requests) asks for the real Before and After output. - Changeset wording (
.changeset/rich-text-type-after-selected-block-decorator.md). The summary says "an empty paragraph or heading", butindex.ts:1476-1482reuses any empty non-inline block whosecanBeEmpty()is true, so an empty quote or code block is filled too. "an empty block already there, such as a paragraph or heading, or a new paragraph" would match. - Nit: the reused block's text format (
packages/lexical-rich-text/src/index.ts:1482).next.select()leaves the selection with format 0, so an empty heading whose text format is bold gets a plain letter, while clicking into that heading and typing gives a bold one.mainbehaves the same. Copyingnext.getTextFormat()andnext.getTextStyle()onto the returned selection withsetFormat/setStylewould match the click path.
For context only: in Firefox, typing on a selected rule inserts nothing, both on main and on this branch, so this handler never runs there. That is outside this change.
| next.isEmpty() && | ||
| next.canBeEmpty() | ||
| ) { | ||
| next.select(); |
There was a problem hiding this comment.
Nit, optional: select() leaves the selection with format 0 and no style, so an empty heading whose text format is bold gets a plain letter, while clicking into that heading and typing gives a bold one (main behaves the same). To match the click path:
const selection = next.select();
selection.setFormat(next.getTextFormat());
selection.setStyle(next.getTextStyle());| --- | ||
|
|
||
| [fix] Typing while a block decorator such as a horizontal rule is selected puts the text at the start of the block after it, or in a new paragraph if nothing follows, instead of at the top of the document | ||
| [fix] Typing while a block decorator such as a horizontal rule is selected puts the text on a line of its own right after it (an empty paragraph or heading already there, or a new paragraph), instead of at the top of the document |
There was a problem hiding this comment.
Optional: the handler reuses any empty non-inline block whose canBeEmpty() is true (index.ts:1476-1482), so an empty quote or code block is filled too, not only a paragraph or heading. Something like "(an empty block already there, such as a paragraph or heading, or a new paragraph)" would match.
|
|
||
| describe('typing while a block decorator is node-selected', () => { | ||
| test('types at the start of the block after it', () => { | ||
| test('types into a new paragraph after it, before the block that follows', () => { |
There was a problem hiding this comment.
Optional: these cases dispatch BEFORE_INPUT_COMMAND in jsdom, so the click that selects the rule, the native beforeinput and its preventDefault stay untested. AGENTS.md (Testing Strategy) leaves real input to browser or E2E tests. A case beside the ArrowDown cases in packages/lexical-playground/__tests__/e2e/HorizontalRule.spec.mjs would pin it: type Top, Enter, --- , End, click the hr, type a, expect Top / hr / a / End.
Description
#9845 fixed text typed while a horizontal rule (or any block decorator) is selected landing at the top of the document. It moved the caret to the start of the block after the decorator, so the typed text joined that block: with
Top / <hr> / End, selecting the rule and typingagaveTop / <hr> / aEnd, editing a paragraph the person never pointed at.Typed text now goes into a line of its own right after the decorator: an empty block already there (a paragraph, or the empty heading inserting a rule after a heading leaves), or a new paragraph.
Top / <hr> / EndbecomesTop / <hr> / a / End; with nothing after the rule, the text starts a new paragraph as before. That is what Enter's handler is for (a new paragraph after a selected block), what CKEditor 5 does on Enter for a selected block widget and on typing at its fake caret after it, and what ProseMirror's gap cursor does after a block node; none of them puts typed text at the start of the next existing block. Arrow keys are unchanged: they still step to the start of the block after the rule.The pending changeset from #9845 is updated to describe the new behavior (it has not been released).
Test plan
Before
After
Not exercised locally: the E2E suites (CI runs them). In Firefox the handler does not run (no
beforeinputwhile a node is selected), onmaintoo.