diff --git a/frontend/components/CellInput.js b/frontend/components/CellInput.js index e3a57c6421..9133b305a2 100644 --- a/frontend/components/CellInput.js +++ b/frontend/components/CellInput.js @@ -631,17 +631,23 @@ export const CellInput = ({ // Remove selection on blur EditorView.domEventHandlers({ blur: (event, view) => { - // collapse the selection into a single point - view.dispatch({ - selection: { - anchor: view.state.selection.main.head, - }, - scrollIntoView: false, - }) - // and blur the DOM again (because the previous transaction might have re-focused it) - view.contentDOM.blur() - - set_cm_forced_focus(null) + // it turns out that this condition is true *exactly* if and only if the blur event was triggered by blurring the window + let caused_by_window_blur = document.activeElement === view.contentDOM + + if (!caused_by_window_blur) { + // then it's caused by focusing something other than this cell in the editor. + // in this case, we want to collapse the selection into a single point, for aesthetic reasons. + view.dispatch({ + selection: { + anchor: view.state.selection.main.head, + }, + scrollIntoView: false, + }) + // and blur the DOM again (because the previous transaction might have re-focused it) + view.contentDOM.blur() + + set_cm_forced_focus(null) + } }, }), pluto_paste_plugin({