You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(notebook): select on the first press and keep scroll when opening a code cell - #47932
In a Jupyter notebook, double-clicking a word in a code cell you weren't editing used to make the notebook jump to the top of that cell, and the word didn't get selected, so Copy grabbed the wrong text. Now the notebook stays put and the first press selects things the way you expect: double-click picks the word, triple-click picks the line, and dragging selects the range.
What Changed
Before: pressing an inactive code cell swapped the static preview for a Monaco editor. The notebook scrolled to line 1 of the cell, and the selection gesture was lost. Double-click selected nothing, triple-click selected one word, and a drag selected nothing.
After: the notebook keeps its scroll position and the selection starts on the first press.
Mechanism (all in IpynbCellEditor.tsx):
Render before focus. After setPosition, the editor calls render() before focus(). Monaco's hidden input only moves to the caret line when it renders, so focusing first scrolled the notebook to line 1.
Replay the press. The preview's mousedown calls preventDefault and mounts Monaco, so Monaco never saw that press. Monaco counts clicks and starts drags from mousedown, so after mount the press is replayed once, in a microtask, to the element under the pointer, and only if that element is inside this editor's container. Modified presses (Shift/Ctrl/Alt/Meta), button chords, touch, and presses on the preview's scrollbar or padding skip the replay and only place the caret, as before. Ctrl-click still opens the context menu.
Reserve the horizontal scrollbar. Monaco measures long or non-ASCII lines about 200 ms late, then adds a horizontal scrollbar. The height change makes Monaco's mouse handler drop an in-progress drag. When a code cell's preview already overflows sideways, the editor starts with room for the scrollbar, so the late measurement doesn't change the height. Raw cells wrap in Monaco and reserve nothing.
Why
Rendering before focus alone stops the jump, but double-click, triple-click and drag still fail (tested: double-click copied the whole line, drag selected nothing). Replaying the press lets Monaco's own click counting and drag tracking do the work instead of re-implementing selection. An earlier version held the resize until mouseup, but that let the new scrollbar cover the last line during a drag and then shifted the layout on release. Reserving the space up front avoids both.
Drag from 25:3 to 30:10 in the same inactive code cell of demo.ipynb, starting 320.5 px into the notebook in both builds. Recorded by @dzkchen. The final code produces the same result for this drag (verified with the CDP harness below).
Before (main): the notebook jumps to the top on press and nothing is selected.
notebook-before.mp4
After: the scroll stays at 320.5 px and the full range is selected.
notebook-after.mp4
Testing
Reproduced on macOS (m4air), in background Electron (ORCA_BACKGROUND_LAUNCH=1) driven with CDP Input.dispatchMouseEvent. The selection was read through a real copy command. The test notebook had an 82-line tall cell, a wide ASCII cell, a wide non-ASCII cell, a 200-line by 3000-character cell, a raw cell and a Markdown cell.
Tall cell scrolled 320 px in: double-click on word43 keeps scrollTop at 561.5 and copies word43 (main jumped to 0 and copied another line). Single click, an exact drag from line 25 to 30, triple-click on the full line, and shift-click all pass.
Wide non-ASCII cell, drag from line 2 to 6: all 1099 chars selected. The height stays 620 px while Monaco's late measurement lands. Without the reservation, a 608 to 620 px resize stopped the drag after 1 char.
Pressing the preview's scrollbar only places the caret. Double-click in an already active editor still selects the word. Markdown double-click still activates the cell. Raw cell height is correct (208 px, with no stuck 12 px strip).
Modified presses, button chords and emulated touch get no replay. Ctrl-click still produces a context-menu event.
Static checks on the changed file: oxlint, oxfmt, anti-slop oxlint, code-quality native plugins, ast-grep, and the changed-code quality gate are all clean.
Windows and Linux were checked by source review only. On those platforms Monaco also starts selection from mousedown (Windows touch uses TouchHandler extends MouseHandler). The change is renderer-only, so local and SSH workspaces behave the same.
I manually tested these changes locally
Automated tests added/updated, or explained why not below
Every added test fails without this change and avoids the test anti-patterns
No unit test was added. The behavior depends on Monaco's real hit-testing, layout and click counting, and a jsdom test would only mock those. There are no existing notebook tests. The CDP harness above is the regression evidence.
Performance
None that matters. This runs once per press that opens a cell, which isn't a hot path. Per activation it adds one synchronous Monaco render() (0.4 to 0.7 ms median), and the next frame's render then has nothing to do. It also adds one replayed mousedown (0.3 to 0.5 ms), a preview scrollWidth/clientWidth read (median 0.0 ms, max 0.3 ms over 200 reads), and, for code cells that overflow, one getContentHeight()/getLayoutInfo(). The only new listener is a pointerdown on the preview. There are no timers, polling, I/O or subprocesses. End-to-end activation time is within noise of main, and Monaco creation dominates it.
Risk
Level: Medium
Click counting and drag start depend on Monaco handling them from mousedown (monaco-editor 0.55.1 MouseHandler). If a Monaco upgrade changes that, presses fall back to placing the caret only. Editing isn't affected.
The replay sends one extra synthetic mousedown. The only other mousedown listeners above the notebook record state and are idempotent.
One narrow case remains. A line about 4 characters short of overflowing, which Monaco measures late (non-ASCII text or a proportional font), can still add the scrollbar during the first drag and end it early. Main never started that drag at all.
When an open Markdown cell above collapses, the press can land a few lines lower. This is the same as on main.
Back out with a plain revert. No saved data or settings are touched.
Review
The replay only reaches an element inside the editor that opened for this press, and the microtask exits if that editor was disposed first, which keeps it StrictMode-safe. The change stays in the renderer.
Agent skill upstream boundary
Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.
Notes
Original fix by @dzkchen in #47932. The maintainer rework replaced the resize hold with the scrollbar reservation, limited the reservation to code cells, and kept presses on the scrollbar or padding from starting a selection. The commit keeps @dzkchen as author.
Checklist
This PR is small and focused
I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
I stated the risk: what could break, for whom, and how to back it out
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)
No actionable comments were generated in the recent review.
Walkthrough
Cell activation records press coordinates, touch history, preview overflow, and whether a mouse press is eligible for replay. The source editor uses this metadata to reserve horizontal scrollbar height and to replay eligible presses after it positions the caret and renders. The editor clears the reserved height when Monaco reports a content-height change.
Severity of issue fixed: Medium
Merge Risk: Low · up to ac069
This change lets a press in an inactive code cell start text selection immediately. In rare cases, such as a touch cancellation during a mouse drag, the selection may end early. This is a minor, self-recovering glitch, and the change is reasonable to merge after owner review.
Pre-merge checks: 4
Check name
Status
Explanation
Linked Issues check
Directly linked issue #47930 requires that a double-click on a word in an inactive, tall code cell keeps the notebook in place, so Cmd+C copies that word. The whole-PR diff (merge base fbc6829 to hea…
Out of Scope Changes check
The whole-PR diff touches only IpynbCellEditor.tsx. Each change serves the press-to-edit path described in #47930. CellPress adds previewScrollsX and replayMousePress. shouldReplayMousePress gates rep…
Title check
The title is specific and matches the main change. It names the notebook code-cell fix: selection starts on the first press, and scroll position is kept when a code cell opens. The raw summary confirm…
Description check
The description follows the repository template. It includes ELI5, What Changed, Why, Linked Issue (Fixes #47930), Visual Proof with before and after videos, Testing, Performance, Risk with level, Rev…
Full details: Linked Issues check
Explanation
Directly linked issue #47930 requires that a double-click on a word in an inactive, tall code cell keeps the notebook in place, so Cmd+C copies that word. The whole-PR diff (merge base fbc6829 to head ac06926) changes only IpynbCellEditor.tsx. It addresses the cause the issue names: (1) IpynbSourceEditor calls editorInstance.render() after setPosition and before focus(), so the caret and Monaco's hidden input are placed before focus scrolls the notebook. (2) shouldReplayMousePress records replayMousePress only for unmodified primary presses (buttons === 1, no Ctrl/Alt/Meta/Shift, not touch, target inside a <code> element). After mount, a queueMicrotask replays a mousedown at the original point on the editor element, so Monaco receives the first press and can count clicks and track drags. (3) previewScrollsX, set only for code cells, reserves horizontal scrollbar height through minHeight. The reservation clears when contentHeightChanged fires, which keeps the height stable during a drag. The issue requires no automated test. The author-reported harness results (scroll offset held at 320.5 px, full range selected, copy outcomes matched) were not re-run here. Static review cannot confirm that Monaco fully handles the synthetic MouseEvent on all platforms. This uncertainty does not change the verdict because the code implements the requested behavior.
Full details: Out of Scope Changes check
Explanation
The whole-PR diff touches only IpynbCellEditor.tsx. Each change serves the press-to-edit path described in #47930. CellPress adds previewScrollsX and replayMousePress. shouldReplayMousePress gates replay on modifiers, button state, touch, and the code element. pressedByTouchRef and the onPointerDown handler keep touch presses from replaying a mouse press. render() orders caret placement before focus. The minHeight reservation and the queueMicrotask replay keep the editor size stable and deliver the first press. The replay checks editorRef and container containment before dispatching. The #47932 and #23735 references in the PR description describe the same press path or a related combined diff issue; no unrelated file or behavior change appears in the diff. The known risks in the PR description (late Monaco width measurement, collapsing Markdown cells above the code) are correctness limits within the in-scope change, not unrelated changes.
Full details: Title check
Explanation
The title is specific and matches the main change. It names the notebook code-cell fix: selection starts on the first press, and scroll position is kept when a code cell opens. The raw summary confirms the replayed press, the render-before-focus order, and the scrollbar reservation that support this.
Full details: Description check
Explanation
The description follows the repository template. It includes ELI5, What Changed, Why, Linked Issue (Fixes #47930), Visual Proof with before and after videos, Testing, Performance, Risk with level, Review, Agent skill boundary, Notes, and Checklist. The AI Disclosure section is left blank, which the template allows for internal contributors and which the author did not fill in. The Performance section gives a concrete justification. The Testing section gives reproducible steps. The one unchecked box is the lint, typecheck, test, and build item, and the author says CI will cover it.
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Minor · Functional Correctness · Quick win
Ignore cancellation from unrelated pointers.
If another touch pointer is cancelled during a mouse drag, this listener calls finishPress even though the mouse remains down. It removes the height hold and applies any pending scrollbar height change. Monaco 0.55.1 stops drag monitoring when the editor height changes, so selection can stop before release. (raw.githubusercontent.com)
Store the originating pointerId in CellPress. Only finish on cancellation of that pointer. Keep window blur as a global fallback.
Opening an inactive code cell with a press focused Monaco before its
caret rendered, so the notebook scrolled to line 1 of the cell, and
Monaco never saw the first mousedown, so double-click, triple-click and
drag selection failed.
Render the caret before focusing, replay a plain primary press to Monaco
once it mounts so it counts clicks and tracks the drag from the first
press, and reserve the horizontal scrollbar for code cells whose preview
overflows so Monaco's late line measurement cannot change the height and
end the drag. Modified presses, chords, touch and presses on the
preview's scrollbar or padding still only place the caret.
Fixesstablyai#47930
Co-authored-by: Neil <[email protected]>
nwparker
changed the title
fix(notebook): start selection on the first press in code cells
fix(notebook): select on the first press and keep scroll when opening a code cell
Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ELI5
In a Jupyter notebook, double-clicking a word in a code cell you weren't editing used to make the notebook jump to the top of that cell, and the word didn't get selected, so Copy grabbed the wrong text. Now the notebook stays put and the first press selects things the way you expect: double-click picks the word, triple-click picks the line, and dragging selects the range.
What Changed
Before: pressing an inactive code cell swapped the static preview for a Monaco editor. The notebook scrolled to line 1 of the cell, and the selection gesture was lost. Double-click selected nothing, triple-click selected one word, and a drag selected nothing.
After: the notebook keeps its scroll position and the selection starts on the first press.
Mechanism (all in
IpynbCellEditor.tsx):setPosition, the editor callsrender()beforefocus(). Monaco's hidden input only moves to the caret line when it renders, so focusing first scrolled the notebook to line 1.mousedowncallspreventDefaultand mounts Monaco, so Monaco never saw that press. Monaco counts clicks and starts drags frommousedown, so after mount the press is replayed once, in a microtask, to the element under the pointer, and only if that element is inside this editor's container. Modified presses (Shift/Ctrl/Alt/Meta), button chords, touch, and presses on the preview's scrollbar or padding skip the replay and only place the caret, as before. Ctrl-click still opens the context menu.Why
Rendering before focus alone stops the jump, but double-click, triple-click and drag still fail (tested: double-click copied the whole line, drag selected nothing). Replaying the press lets Monaco's own click counting and drag tracking do the work instead of re-implementing selection. An earlier version held the resize until mouseup, but that let the new scrollbar cover the last line during a drag and then shifted the layout on release. Reserving the space up front avoids both.
Linked Issue
Fixes #47930
Visual Proof
Drag from
25:3to30:10in the same inactive code cell ofdemo.ipynb, starting 320.5 px into the notebook in both builds. Recorded by @dzkchen. The final code produces the same result for this drag (verified with the CDP harness below).Before (main): the notebook jumps to the top on press and nothing is selected.
notebook-before.mp4
After: the scroll stays at 320.5 px and the full range is selected.
notebook-after.mp4
Testing
Reproduced on macOS (m4air), in background Electron (
ORCA_BACKGROUND_LAUNCH=1) driven with CDPInput.dispatchMouseEvent. The selection was read through a real copy command. The test notebook had an 82-line tall cell, a wide ASCII cell, a wide non-ASCII cell, a 200-line by 3000-character cell, a raw cell and a Markdown cell.word43keeps scrollTop at 561.5 and copiesword43(main jumped to 0 and copied another line). Single click, an exact drag from line 25 to 30, triple-click on the full line, and shift-click all pass.Windows and Linux were checked by source review only. On those platforms Monaco also starts selection from
mousedown(Windows touch usesTouchHandler extends MouseHandler). The change is renderer-only, so local and SSH workspaces behave the same.No unit test was added. The behavior depends on Monaco's real hit-testing, layout and click counting, and a jsdom test would only mock those. There are no existing notebook tests. The CDP harness above is the regression evidence.
Performance
None that matters. This runs once per press that opens a cell, which isn't a hot path. Per activation it adds one synchronous Monaco
render()(0.4 to 0.7 ms median), and the next frame's render then has nothing to do. It also adds one replayed mousedown (0.3 to 0.5 ms), a previewscrollWidth/clientWidthread (median 0.0 ms, max 0.3 ms over 200 reads), and, for code cells that overflow, onegetContentHeight()/getLayoutInfo(). The only new listener is apointerdownon the preview. There are no timers, polling, I/O or subprocesses. End-to-end activation time is within noise of main, and Monaco creation dominates it.Risk
Level: Medium
mousedown(monaco-editor 0.55.1MouseHandler). If a Monaco upgrade changes that, presses fall back to placing the caret only. Editing isn't affected.mousedown. The only other mousedown listeners above the notebook record state and are idempotent.Review
The replay only reaches an element inside the editor that opened for this press, and the microtask exits if that editor was disposed first, which keeps it StrictMode-safe. The change stays in the renderer.
Agent skill upstream boundary
Notes
Original fix by @dzkchen in #47932. The maintainer rework replaced the resize hold with the scrollbar reservation, limited the reservation to code cells, and kept presses on the scrollbar or padding from starting a selection. The commit keeps @dzkchen as author.
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)