Skip to content

fix(editor): preserve rich markdown selection across tab switches - #14940

Open
poorpaper wants to merge 2 commits into
stablyai:mainfrom
poorpaper:fix/editor-view-state-switching
Open

poorpaper wants to merge 2 commits into
stablyai:mainfrom
poorpaper:fix/editor-view-state-switching

Conversation

@poorpaper

@poorpaper poorpaper commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

Switching away from a rich Markdown file and back now returns the caret and selection to the same place instead of resetting them to the start.

What Changed

  • Cache each rich Markdown selection with the existing pane-scoped editor view-state key.
  • Restore the selection synchronously on remount and keep deferred autofocus from overwriting it.
  • Let a remounted rich editor reclaim DOM focus from the editor tab that activated it, while leaving unrelated controls and dialogs alone.
  • Preserve the existing empty-document and explicit Explorer focus behavior.
  • Remove legacy and pane-scoped selection entries when the owning tab closes.
  • Add unit coverage plus a real Electron A → B → A regression test that asserts the native browser selection.

Why

Monaco already snapshots scroll and cursor state synchronously, and the rich editor already restores scroll state. The missing piece was ProseMirror selection state. A restored selection also had to be coordinated with the existing deferred autofocus; otherwise the next animation frame moved it back to the document start.

Windows Electron validation caught one additional focus handoff: after clicking the original tab, the restored ProseMirror selection was correct internally but the tab element still owned DOM focus, so no native selection was visible. Commit 4bf7e1e54 allows that specific tab-to-editor handoff and adds E2E coverage for it.

This keeps the existing pane-scoped key design, so two panes showing the same file retain independent view state.

Linked Issue

Fixes #14800

Visual Proof

Windows Electron, rich Markdown editor, A → B → A:

1. Select text in A 2. Switch to B 3. Return to A; selection restored
Selected text before switching tabs Intermediate rich Markdown tab Text selection restored after returning to the original tab

The final capture shows the same native blue highlight after the original editor was unmounted and remounted.

Testing

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

  • Windows Electron Playwright E2E: 1 passed; native window.getSelection() restored after A → B → A.

  • Windows Electron E2E build: passed.

  • Focused Vitest: 2 files, 13 tests passed on Windows and in the remote Linux container.

  • Oxfmt check: changed files passed.

  • Native-plugin and type-aware Oxlint: 0 warnings, 0 errors on Windows and remote Linux.

  • Remote Linux container: Node 24, pnpm 10.24.0, offline cached install.

  • Full renderer typecheck was attempted previously in a 2.2 GiB container but did not complete because the compiler entered sustained memory pressure; CI/full-suite checks remain pending.

AI Disclosure

OpenAI Codex (GPT-5) was used for repository inspection, implementation, test development, and code review. The submitted diff was independently reviewed twice, and the author remains responsible for the changes.

Review

  • Security: in-memory numeric document positions only; no new external input or persistence.
  • Cross-platform and SSH: no platform APIs or filesystem paths added.
  • Backwards compatibility: existing scroll, empty-document autofocus, explicit Explorer focus behavior, and unrelated-control focus guards are retained.
  • Performance: one bounded LRU entry per rich editor view-state key, using the existing cache limit.

Checklist

  • This PR is small and focused
  • I explained what changed and why (including ELI5)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

Author

  • X / Twitter: N/A

@poorpaper
poorpaper marked this pull request as ready for review August 16, 2026 19:23
@coderabbitai

coderabbitai Bot commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a0a35884-8eb7-4493-8e58-b88cf8786511

📥 Commits

Reviewing files that changed from the base of the PR and between 16f2bb6 and 4bf7e1e.

📒 Files selected for processing (3)
  • src/renderer/src/components/editor/rich-markdown-auto-focus.test.ts
  • src/renderer/src/components/editor/rich-markdown-auto-focus.ts
  • tests/e2e/rich-markdown-selection-restore.spec.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/renderer/src/components/editor/rich-markdown-auto-focus.test.ts
  • src/renderer/src/components/editor/rich-markdown-auto-focus.ts

Included review availability: Your plan includes up to 10 reviews per rolling hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The change adds a ProseMirror selection cache for rich Markdown editors. The editor saves selections during cleanup and restores clamped ranges after remounts. Ordinary autofocus preserves restored text selections, while forced handoffs and non-text selections focus at the document start. Closed edit tabs remove their related cache entries. Tests cover caching, restoration, autofocus, cleanup, and tab switching.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR addresses rich Markdown selection restoration, but issue #14800 also requires Monaco/source-editor cursor and scroll preservation. Add the Monaco/source-editor implementation and regression coverage, or link this PR to a rich-Markdown-specific issue.
✅ Passed checks (4 passed)
Check name Status Explanation
Out of Scope Changes check ✅ Passed The implementation, cleanup logic, and tests are focused on rich Markdown selection restoration and directly support the stated objectives.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title clearly and concisely describes preserving rich Markdown selections across tab switches.
Description check ✅ Passed The description covers the required sections, linked issue, visual proof, testing, risks, AI disclosure, and implementation rationale.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (1)
src/renderer/src/components/editor/rich-markdown-auto-focus.ts (1)

38-44: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Condense the new focus comment.

Use one concise comment for the selection rule. Remove the standalone // marker.

Proposed cleanup
-    // Why: a freshly-created empty document has an AllSelection, so focus it at
-    // the start to render a normal caret. Ordinary remounts may already have a
-    // restored TextSelection, which null preserves instead of resetting it.
-    // Explicit handoffs still start at position 1.
+    // Preserve restored TextSelection unless a forced or non-text selection needs 'start'.
     const focusPosition =
       force || !(nextEditor.state.selection instanceof TextSelection) ? 'start' : null
-    //

As per coding guidelines, “Comments must be concise, non-obvious, and brief—prefer one line.”

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: e8c06bf3-a8c1-40ad-9c4c-b4dcdbf59540

📥 Commits

Reviewing files that changed from the base of the PR and between bf6dc6f and 16f2bb6.

📒 Files selected for processing (8)
  • src/renderer/src/components/editor/rich-markdown-auto-focus.test.ts
  • src/renderer/src/components/editor/rich-markdown-auto-focus.ts
  • src/renderer/src/components/editor/useClosedEditorTabCleanup.test.tsx
  • src/renderer/src/components/editor/useClosedEditorTabCleanup.ts
  • src/renderer/src/components/editor/useEditorScrollRestore.test.tsx
  • src/renderer/src/components/editor/useEditorScrollRestore.ts
  • src/renderer/src/lib/scroll-cache.test.ts
  • src/renderer/src/lib/scroll-cache.ts

Included review availability: Your plan includes up to 10 reviews per rolling hour; 9 remain after this review.

This branch has not been deployed

No deployments
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.

Editor scroll/cursor position lost after switching tabs or files (no restart needed)

4 participants