Skip to content

fix: use TextSelection.between for list item cursor placement - #2422

Merged
Saul-Mirone merged 1 commit into
mainfrom
fix/list-item-selection-robustness
Jul 25, 2026
Merged

Saul-Mirone merged 1 commit into
mainfrom
fix/list-item-selection-robustness

Conversation

@Saul-Mirone

Copy link
Copy Markdown
Member
  • I read the contributing guide
  • I agree to follow the code of conduct

Summary

Follow-up hardening for the list item block cursor placement fixed in #2412.

When the deferred requestAnimationFrame callback restores the saved selection, it resolves the captured anchor/head offsets against the current document and builds a TextSelection from them. Constructing new TextSelection($anchor, $head) assumes both resolved positions still sit inside a textblock. If the document shifted between mount and the next frame so that a position no longer lands in inline content, that assumption breaks and we can produce an invalid selection.

This PR swaps new TextSelection(...) for TextSelection.between(...), ProseMirror's recommended factory:

  • In the normal case (cursor inside the newly created list item's paragraph) it returns the exact same selection — no behavior change.
  • When a resolved position no longer sits inside a textblock, it falls back to the nearest valid selection instead of constructing an invalid one.

No changeset is added — it is generated by the release tooling.

How did you test this change?

  • pnpm --filter @milkdown/components test — 22 passed
  • tsc --noEmit on @milkdown/components — no type errors
  • pnpm test:lint (oxlint) — 0 warnings / 0 errors

Fall back to the nearest valid selection when the resolved anchor/head
no longer sit inside a textblock, instead of constructing a possibly
invalid TextSelection directly.
Copilot AI review requested due to automatic review settings July 25, 2026 16:30
@changeset-bot

changeset-bot Bot commented Jul 25, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: e97b4ac

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel

vercel Bot commented Jul 25, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
milkdown-storybook Ready Ready Preview, Comment Jul 25, 2026 4:30pm

Request Review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens cursor restoration for the list-item block NodeView by using ProseMirror’s safer selection factory when restoring a deferred selection in a requestAnimationFrame callback, preventing invalid selections if the document structure changes between frames.

Changes:

  • Replace new TextSelection($anchor, $head) with TextSelection.between($anchor, $head) to safely fall back to a valid selection when resolved positions are no longer inside a textblock.
  • Add an inline comment documenting the rationale for using between.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@pkg-pr-new

pkg-pr-new Bot commented Jul 25, 2026

Copy link
Copy Markdown

Open in StackBlitz

@milkdown/components

npm i https://pkg.pr.new/@milkdown/components@2422

@milkdown/core

npm i https://pkg.pr.new/@milkdown/core@2422

@milkdown/crepe

npm i https://pkg.pr.new/@milkdown/crepe@2422

@milkdown/ctx

npm i https://pkg.pr.new/@milkdown/ctx@2422

@milkdown/exception

npm i https://pkg.pr.new/@milkdown/exception@2422

@milkdown/kit

npm i https://pkg.pr.new/@milkdown/kit@2422

@milkdown/prose

npm i https://pkg.pr.new/@milkdown/prose@2422

@milkdown/transformer

npm i https://pkg.pr.new/@milkdown/transformer@2422

@milkdown/utils

npm i https://pkg.pr.new/@milkdown/utils@2422

@milkdown/react

npm i https://pkg.pr.new/@milkdown/react@2422

@milkdown/vue

npm i https://pkg.pr.new/@milkdown/vue@2422

@milkdown/plugin-automd

npm i https://pkg.pr.new/@milkdown/plugin-automd@2422

@milkdown/plugin-block

npm i https://pkg.pr.new/@milkdown/plugin-block@2422

@milkdown/plugin-clipboard

npm i https://pkg.pr.new/@milkdown/plugin-clipboard@2422

@milkdown/plugin-collab

npm i https://pkg.pr.new/@milkdown/plugin-collab@2422

@milkdown/plugin-cursor

npm i https://pkg.pr.new/@milkdown/plugin-cursor@2422

@milkdown/plugin-diff

npm i https://pkg.pr.new/@milkdown/plugin-diff@2422

@milkdown/plugin-emoji

npm i https://pkg.pr.new/@milkdown/plugin-emoji@2422

@milkdown/plugin-highlight

npm i https://pkg.pr.new/@milkdown/plugin-highlight@2422

@milkdown/plugin-history

npm i https://pkg.pr.new/@milkdown/plugin-history@2422

@milkdown/plugin-indent

npm i https://pkg.pr.new/@milkdown/plugin-indent@2422

@milkdown/plugin-listener

npm i https://pkg.pr.new/@milkdown/plugin-listener@2422

@milkdown/plugin-prism

npm i https://pkg.pr.new/@milkdown/plugin-prism@2422

@milkdown/plugin-slash

npm i https://pkg.pr.new/@milkdown/plugin-slash@2422

@milkdown/plugin-streaming

npm i https://pkg.pr.new/@milkdown/plugin-streaming@2422

@milkdown/plugin-tooltip

npm i https://pkg.pr.new/@milkdown/plugin-tooltip@2422

@milkdown/plugin-trailing

npm i https://pkg.pr.new/@milkdown/plugin-trailing@2422

@milkdown/plugin-upload

npm i https://pkg.pr.new/@milkdown/plugin-upload@2422

@milkdown/preset-commonmark

npm i https://pkg.pr.new/@milkdown/preset-commonmark@2422

@milkdown/preset-gfm

npm i https://pkg.pr.new/@milkdown/preset-gfm@2422

@milkdown/theme-nord

npm i https://pkg.pr.new/@milkdown/theme-nord@2422

commit: e97b4ac

@Saul-Mirone
Saul-Mirone added this pull request to the merge queue Jul 25, 2026
Merged via the queue into main with commit 9077fbc Jul 25, 2026
16 checks passed
@Saul-Mirone
Saul-Mirone deleted the fix/list-item-selection-robustness branch July 25, 2026 23:24
@github-actions github-actions Bot mentioned this pull request Aug 3, 2026

This branch was successfully deployed

1 active deployment
Preview e97b4acf Deployed Jul 25, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants