Skip to content

[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

Merged
zurfyx merged 2 commits into
mainfrom
zurfyx/rich-text-type-on-block-new-paragraph
Nov 11, 2026

Conversation

@zurfyx

@zurfyx zurfyx commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

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 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> / 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
 × types into a new paragraph before a heading with text after it
      Tests  2 failed | 4 passed (6)

After

$ pnpm exec vitest run --project unit RichTextNodeSelectionTyping
      Tests  6 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)

Not exercised locally: the E2E suites (CI runs them). In Firefox the handler does not run (no beforeinput while a node is selected), on main too.

… 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).
@vercel

vercel Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
lexical Ready Ready Preview Nov 11, 2026 1:55pm UTC
lexical-playground Ready Ready Preview Nov 11, 2026 1:55pm UTC

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 Nov 11, 2026
@rootnode-bot

rootnode-bot Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 11, 2026, 2:13 PM UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@9876

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@9876

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@9876

@lexical/code

npm i https://pkg.pr.new/@lexical/code@9876

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@9876

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@9876

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@9876

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@9876

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@9876

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@9876

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@9876

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@9876

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@9876

@lexical/file

npm i https://pkg.pr.new/@lexical/file@9876

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@9876

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@9876

@lexical/history

npm i https://pkg.pr.new/@lexical/history@9876

@lexical/html

npm i https://pkg.pr.new/@lexical/html@9876

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@9876

@lexical/link

npm i https://pkg.pr.new/@lexical/link@9876

@lexical/list

npm i https://pkg.pr.new/@lexical/list@9876

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@9876

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@9876

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@9876

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@9876

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@9876

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@9876

@lexical/react

npm i https://pkg.pr.new/@lexical/react@9876

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@9876

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@9876

@lexical/table

npm i https://pkg.pr.new/@lexical/table@9876

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@9876

@lexical/text

npm i https://pkg.pr.new/@lexical/text@9876

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@9876

@lexical/y

npm i https://pkg.pr.new/@lexical/y@9876

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@9876

commit: 200c952

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Empty heading or quote after the rule (packages/lexical-rich-text/src/index.ts:1477): only an empty ParagraphNode is 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 # Title as the last block, Insert → Horizontal Rule leaves an empty heading after the rule, and clicking the rule and typing a then gives Title / rule / a / (empty heading), where main typed 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.
  2. Real-browser coverage: both tests dispatch BEFORE_INPUT_COMMAND from jsdom, so the native beforeinput path is not exercised. AGENTS.md (Testing Strategy) puts behaviour that depends on a real selection engine in browser or E2E tests; a case in packages/lexical-playground/__tests__/e2e/HorizontalRule.spec.mjs that clicks the rule, types a letter and asserts the HTML would cover it across browsers.
  3. Firefox: typing with the rule selected inserts nothing in Firefox, on main and on this branch alike, so the changeset's Verify: step reproduces in Chromium only. Nothing this PR needs to fix, but worth knowing when verifying.

Automated review (maintainer link)

Comment thread packages/lexical-rich-text/src/index.ts Resolved
…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)
```

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. An E2E case for the real input path (packages/lexical-rich-text/src/__tests__/unit/RichTextNodeSelectionTyping.test.ts). type() dispatches BEFORE_INPUT_COMMAND directly in jsdom, so nothing covers clicking the rule, the native beforeinput and its preventDefault. 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, then expect Top / hr / a / End.
  2. Refresh the Test plan output. The PR body still shows the first commit's run (1 failed | 3 passed (4), then 4 passed (4)). The file now has 6 tests, and against main it gives 2 failed | 4 passed (6), then 6 passed (6) on this branch. AGENTS.md (Commits and Pull Requests) asks for the real Before and After output.
  3. Changeset wording (.changeset/rich-text-type-after-selected-block-decorator.md). The summary says "an empty paragraph or heading", but index.ts:1476-1482 reuses any empty non-inline block whose canBeEmpty() 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.
  4. 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. main behaves the same. Copying next.getTextFormat() and next.getTextStyle() onto the returned selection with setFormat/setStyle would 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.

Automated review (maintainer link)

next.isEmpty() &&
next.canBeEmpty()
) {
next.select();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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', () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@zurfyx
zurfyx added this pull request to the merge queue Nov 11, 2026
Merged via the queue into main with commit 2324691 Nov 11, 2026
57 checks passed

This branch was successfully deployed

2 active deployments
Preview – lexical — 200c9523 Deployed Nov 11, 2026 by vercel[bot]
Preview – lexical-playground — 200c9523 Deployed Nov 11, 2026 by vercel[bot]
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant