Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(notebooks): keep shift-enter as a line break in text (#116318)

Marius Andraefcc5ad181f7
diff --git a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.jupyter.test.tsx b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.jupyter.test.tsx
index 65789be220ae..122e073931d6 100644
--- a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.jupyter.test.tsx
+++ b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.jupyter.test.tsx
@@ -238,8 +238,9 @@ describe('MarkdownNotebook in Jupyter mode', () => {
)
})
- it('leaves markdown edit mode with Escape and adds a code cell below with B', async () => {
- const notebook = renderJupyterNotebook(TWO_CELLS)
+ it('keeps Shift+Enter as text input until Escape switches to cell commands', async () => {
+ const markdown = '# Title\n\nIntro'
+ const notebook = renderJupyterNotebook(markdown)
const intro = [...notebook.container.querySelectorAll<HTMLElement>('[data-markdown-notebook-node-id]')].find(
(element) => element.textContent === 'Intro'
)!
@@ -251,15 +252,16 @@ describe('MarkdownNotebook in Jupyter mode', () => {
window.getSelection()?.addRange(range)
})
const canvas = notebook.container.querySelector('.MarkdownNotebook__canvas') as HTMLElement
+ expect(fireEvent.keyDown(canvas, { key: 'Enter', shiftKey: true })).toBe(true)
+ expect(notebook.latest()).toEqual(markdown)
+
press(canvas, 'Escape')
await flushFrames()
expect(document.activeElement).toBe(notebook.sink())
- press(notebook.sink(), 'b')
+ press(notebook.sink(), 'Enter', { shiftKey: true })
await flushFrames()
- expect(notebook.latest()).toEqual(
- '# Title\n\nIntro\n\n<Py code="" />\n\n<Py code="a = 1" />\n\n<Py code="b = 2" />'
- )
+ expect(notebook.latest()).toEqual('# Title\n\nIntro\n\n<Py code="" />')
})
it('deletes every cell picked with Shift+Down', async () => {
diff --git a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
index a92e34c38207..a7c84f4bd07a 100644
--- a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
+++ b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
@@ -6032,8 +6032,6 @@ function MarkdownNotebookEditor({
}
}, [jupyterMode, jupyterStore])
- // A markdown cell in edit mode is text with the caret in it. Escape and the run keys there act
- // on the cell, as in Jupyter, instead of typing; every other key keeps its text-editing meaning.
const handleJupyterTextKeyDown = (event: KeyboardEvent<HTMLDivElement>): boolean => {
if (!jupyterMode || mode !== 'edit' || insertMenu || event.target !== event.currentTarget) {
return false
@@ -6055,7 +6053,7 @@ function MarkdownNotebookEditor({
null,
Date.now()
)
- if (!command) {
+ if (!command || command === 'run-and-advance') {
return false
}
event.preventDefault()
diff --git a/frontend/src/lib/components/MarkdownNotebook/README.md b/frontend/src/lib/components/MarkdownNotebook/README.md
index 76e844261ff5..9fb30fbf2dc6 100644
--- a/frontend/src/lib/components/MarkdownNotebook/README.md
+++ b/frontend/src/lib/components/MarkdownNotebook/README.md
@@ -83,6 +83,8 @@ The shell only handles keys that happened inside its own DOM. A block that rende
The `jupyterMode` prop lays the notebook out and drives it the way JupyterLab does. The notebooks scene passes it behind the `notebook-jupyter-mode` flag, and each user can turn it off from the notebook's View menu.
+`Shift+Enter` inside markdown text inserts a line break within the text cell. `Escape` selects the markdown cell in command mode, where `Shift+Enter` advances to the next cell and inserts a code cell if there is none. Python and SQL editors keep `Shift+Enter` as run and advance.
+
- The notebook is a list of cells (`getNotebookJupyterCells` in `jupyterCells.ts`). Each component tag in `cellTagNames` is a code cell with an `In [n]:` prompt. A run of text blocks that share a card is one markdown cell, the way a Jupyter markdown cell holds several paragraphs. Every other block (an insight, a divider) is a cell of its own and reads as rich output. A cell is named by its first node's id.
- A selected cell is in command mode, and a cell whose source holds the caret is in edit mode. A code cell's shell takes focus in command mode. Text never takes focus itself, because the canvas is the editing host, so a markdown cell in command mode puts focus on an invisible sink (`MarkdownNotebook__jupyter-command-sink`). Without it, letter keys would type into the text.
- `resolveNotebookJupyterKey` in `jupyterMode.ts` maps key presses to commands, including the two-key sequences (`D D`, `I I`, `0 0`). In edit mode it only claims the run keys, Escape, and the split chord, so typing never adds, deletes, or converts a cell. `executeCommand` in the editor implements every command against the document, so each edit is undoable and merges like any other.
diff --git a/frontend/src/scenes/notebooks/Notebook/NotebookJupyterShortcutsModal.tsx b/frontend/src/scenes/notebooks/Notebook/NotebookJupyterShortcutsModal.tsx
index 8c00b3961fa1..ebe1203ffafc 100644
--- a/frontend/src/scenes/notebooks/Notebook/NotebookJupyterShortcutsModal.tsx
+++ b/frontend/src/scenes/notebooks/Notebook/NotebookJupyterShortcutsModal.tsx
@@ -47,7 +47,7 @@ const COMMAND_MODE_SHORTCUTS: Shortcut[] = [
const EDIT_MODE_SHORTCUTS: Shortcut[] = [
{ keys: [['escape']], description: 'Leave the editor and select the cell' },
- { keys: [['shift', 'enter']], description: 'Run the cell and select the next one' },
+ { keys: [['shift', 'enter']], description: 'Insert a line break in text; run and advance in code' },
{ keys: [['command', 'enter']], description: 'Run the cell' },
{ keys: [['option', 'enter']], description: 'Run the cell and insert a new one below' },
{ keys: [['command', 'shift', 'minus']], description: 'Split the cell at the cursor' },