From 7d4c58a0f1d1ac491bb67b0e31db676e3dee3cf7 Mon Sep 17 00:00:00 2001 From: Delaruno20 Date: Wed, 25 Nov 2026 01:46:07 +0100 Subject: [PATCH] feat(notebook): fold cells and outputs in the notebook editor (#5650) A chevron in each cell toolbar folds the cell to one line (its first non-blank line, or a Markdown heading's text) with its output count; clicking the line unfolds it. Code cells with outputs get Hide Output / Show Output, which folds only the outputs, and the notebook toolbar gets Collapse All Cells / Expand All Cells. Fold state is view state on NotebookSession; the .ipynb file is not modified. The singular output counts use their own catalog keys so they don't collide with the run output pane's "{name} output". Co-authored-by: Delaruno20 <192448112+Delaruno20@users.noreply.github.com> --- src/features/notebook/Notebook.module.css | 48 +++++++++++ src/features/notebook/NotebookEditor.tsx | 99 ++++++++++++++++++++++- src/features/notebook/collapsed.test.ts | 24 ++++++ src/features/notebook/session.ts | 4 + src/platform/locales/text/fr.json | 11 +++ src/platform/locales/text/zh-TW.json | 11 +++ src/platform/locales/text/zh.json | 11 +++ 7 files changed, 206 insertions(+), 2 deletions(-) create mode 100644 src/features/notebook/collapsed.test.ts diff --git a/src/features/notebook/Notebook.module.css b/src/features/notebook/Notebook.module.css index d65939b837..d7dd0d961e 100644 --- a/src/features/notebook/Notebook.module.css +++ b/src/features/notebook/Notebook.module.css @@ -109,6 +109,54 @@ .cellEditor { min-height: 48px; } +.collapsedCell, +.hiddenOutputs { + display: flex; + align-items: baseline; + gap: var(--space-3); + width: 100%; + margin: 0; + border: 0; + background: none; + color: var(--fg-muted); + text-align: left; + cursor: pointer; +} +.collapsedCell { + padding: 6px 12px; + font: var(--openidea-editor-font-size, var(--fs-code)) / var(--openidea-editor-line-height, var(--lh-code, 1.5)) + var(--font-mono); +} +.collapsedMarkdown { + font: 600 var(--markdown-preview-font-size, var(--fs-ui)) var(--font-ui); +} +.collapsedText { + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + color: var(--fg); +} +.collapsedOutputs { + flex: none; + margin-left: auto; + font: var(--fs-small) var(--font-ui); +} +.hiddenOutputs { + border-top: 1px solid var(--border); + padding: 4px 12px; + font-size: var(--fs-small); + font-style: italic; +} +.collapsedCell:hover, +.hiddenOutputs:hover { + background: var(--bg-hover); +} +.collapsedCell:focus-visible, +.hiddenOutputs:focus-visible { + outline: 2px solid var(--accent); + outline-offset: -2px; +} .outputs { border-top: 1px solid var(--border); padding: 8px 12px; diff --git a/src/features/notebook/NotebookEditor.tsx b/src/features/notebook/NotebookEditor.tsx index 74c8534e16..eb98094b35 100644 --- a/src/features/notebook/NotebookEditor.tsx +++ b/src/features/notebook/NotebookEditor.tsx @@ -2,6 +2,7 @@ import { useCallback, useEffect, useLayoutEffect, + useReducer, useRef, useState, useSyncExternalStore, @@ -30,6 +31,19 @@ function NotebookDocument({ session }: { session: NotebookSession }) { const scroll = useRef(null); const [selected, select] = useState(session.selectedCell); const [reloadOpen, setReloadOpen] = useState(false); + // Fold state lives in the session (it outlives scrolled-away cells); this re-renders on a change. + const [, refold] = useReducer((n: number) => n + 1, 0); + const toggle = (folds: Set, id: string) => { + if (!folds.delete(id)) folds.add(id); + refold(); + }; + const foldAll = (folded: boolean) => { + for (const cell of session.cells()) { + if (folded) session.collapsedCells.add(cell.id); + else session.collapsedCells.delete(cell.id); + } + refold(); + }; const cells = session.cells(); const active = cells.find((cell) => cell.id === selected) ?? cells.find((cell) => cell.cellType === "code"); const invoke = useCallback( @@ -172,6 +186,19 @@ function NotebookDocument({ session }: { session: NotebookSession }) { {tx(state.running ? "busy" : state.kernel)} + foldAll(true)} + /> + foldAll(false)} + /> + choose(cell.id)} + onToggleCollapsed={() => toggle(session.collapsedCells, cell.id)} + onToggleOutputs={() => toggle(session.hiddenOutputs, cell.id)} /> ); @@ -337,7 +368,11 @@ function NotebookCellView({ running, doc, reveal, + collapsed, + outputsHidden, onSelect, + onToggleCollapsed, + onToggleOutputs, }: { session: NotebookSession; cell: NotebookCell; @@ -348,7 +383,11 @@ function NotebookCellView({ running: boolean; doc?: NotebookCellDoc; reveal: CellReveal | null; + collapsed: boolean; + outputsHidden: boolean; onSelect: () => void; + onToggleCollapsed: () => void; + onToggleOutputs: () => void; }) { const [editing, setEditing] = useState(session.markdownEditing.has(cell.id)); const known = cell.cellType === "code" || cell.cellType === "markdown"; @@ -361,6 +400,13 @@ function NotebookCellView({ aria-label={`${tx("Cell")} ${index + 1}: ${cell.cellType}`} >
+ {cell.cellType === "code" ? `[${cell.executionCount ?? " "}]` : `${index + 1}`} @@ -403,6 +449,14 @@ function NotebookCellView({ }} /> )} + {cell.cellType === "code" && cell.outputs.length > 0 && !collapsed && ( + + )} session.edit({ type: "deleteCell", cell: cell.id })} />
- {!known ? ( + {collapsed ? ( + + ) : !known ? (

{tx("Raw or unknown cell; original content is preserved read-only.")}

{cell.source}
@@ -473,7 +529,46 @@ function NotebookCellView({ reveal={reveal} /> )} - {cell.cellType === "code" && } + {collapsed || cell.cellType !== "code" ? null : outputsHidden && cell.outputs.length > 0 ? ( + + ) : ( + + )} ); } + +/** What a folded cell shows: its first non-blank line (a Markdown heading without its marks) and its output count. */ +export function collapsedSummary(cell: Pick): { + text: string; + outputs: number; +} { + const line = + cell.source + .split("\n") + .find((l) => l.trim()) + ?.trim() ?? ""; + return { + text: cell.cellType === "markdown" ? line.replace(/^#+\s*/, "") : line, + outputs: cell.cellType === "code" ? cell.outputs.length : 0, + }; +} + +/** A folded cell: its first line and what it hides. */ +function CollapsedCell({ cell, onExpand }: { cell: NotebookCell; onExpand: () => void }) { + const { text, outputs } = collapsedSummary(cell); + return ( + + ); +} diff --git a/src/features/notebook/collapsed.test.ts b/src/features/notebook/collapsed.test.ts new file mode 100644 index 0000000000..58a180a336 --- /dev/null +++ b/src/features/notebook/collapsed.test.ts @@ -0,0 +1,24 @@ +import { describe, expect, it } from "vitest"; +import { collapsedSummary } from "./NotebookEditor"; + +describe("a folded notebook cell", () => { + it("shows a code cell's first non-blank line and its output count", () => { + expect(collapsedSummary({ cellType: "code", source: "\n import pandas as pd\nx = 1", outputs: [{}, {}] })).toEqual( + { + text: "import pandas as pd", + outputs: 2, + }, + ); + }); + + it("shows a Markdown heading without its marks, and no outputs", () => { + expect(collapsedSummary({ cellType: "markdown", source: "## Results\nbody", outputs: [] })).toEqual({ + text: "Results", + outputs: 0, + }); + }); + + it("is empty for a blank cell", () => { + expect(collapsedSummary({ cellType: "code", source: " \n", outputs: [] })).toEqual({ text: "", outputs: 0 }); + }); +}); diff --git a/src/features/notebook/session.ts b/src/features/notebook/session.ts index 046f5e2fc4..d4f0d150a5 100644 --- a/src/features/notebook/session.ts +++ b/src/features/notebook/session.ts @@ -101,6 +101,10 @@ export class NotebookSession { // EditorState contains selection and syntax state, but deliberately no local history. readonly editors = new Map(); readonly markdownEditing = new Set(); + /** Cells folded to one line, source and outputs (view state, not saved in the notebook). */ + readonly collapsedCells = new Set(); + /** Code cells whose outputs alone are folded. */ + readonly hiddenOutputs = new Set(); selectedCell: string | null = null; scrollTop = 0; /** The last `reveal` request a cell editor carried out. */ diff --git a/src/platform/locales/text/fr.json b/src/platform/locales/text/fr.json index d675bbbf18..f98cd6fdbd 100644 --- a/src/platform/locales/text/fr.json +++ b/src/platform/locales/text/fr.json @@ -4780,6 +4780,17 @@ "No system fonts were returned. Bundled fonts and manual input are available.": "Aucune police système renvoyée. Les polices intégrées et la saisie manuelle sont disponibles.", "Type a family name or search installed fonts.": "Saisissez un nom de famille ou recherchez parmi les polices installées.", "Add Code Cell": "Ajouter une cellule de code", + "Collapse All Cells": "Replier toutes les cellules", + "Expand All Cells": "Déplier toutes les cellules", + "Collapse Cell": "Replier la cellule", + "Expand Cell": "Déplier la cellule", + "Hide Output": "Masquer la sortie", + "Show Output": "Afficher la sortie", + "Empty cell": "Cellule vide", + "1 output": "1 sortie", + "{count} outputs": "{count} sorties", + "1 output hidden": "1 sortie masquée", + "{count} outputs hidden": "{count} sorties masquées", "Add Markdown Cell": "Ajouter une cellule Markdown", "Cell": "Cellule", "Cell outputs": "Sorties de la cellule", diff --git a/src/platform/locales/text/zh-TW.json b/src/platform/locales/text/zh-TW.json index eef729d08a..1426e29d82 100644 --- a/src/platform/locales/text/zh-TW.json +++ b/src/platform/locales/text/zh-TW.json @@ -5952,6 +5952,17 @@ "No system fonts were returned. Bundled fonts and manual input are available.": "未發現系統字型。可選擇內建字型或手動輸入。", "Type a family name or search installed fonts.": "輸入字型家族名稱或搜尋已安裝字型。", "Add Code Cell": "新增程式碼單元格", + "Collapse All Cells": "摺疊所有單元格", + "Expand All Cells": "展開所有單元格", + "Collapse Cell": "摺疊單元格", + "Expand Cell": "展開單元格", + "Hide Output": "隱藏輸出", + "Show Output": "顯示輸出", + "Empty cell": "空單元格", + "1 output": "1 個輸出", + "{count} outputs": "{count} 個輸出", + "1 output hidden": "已隱藏 1 個輸出", + "{count} outputs hidden": "已隱藏 {count} 個輸出", "Add Markdown Cell": "新增 Markdown 單元格", "Cell": "單元格", "Cell outputs": "單元格輸出", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 589e647a94..9d8028127f 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -6505,6 +6505,17 @@ "No system fonts were returned. Bundled fonts and manual input are available.": "未发现系统字体。可选择内置字体或手动输入。", "Type a family name or search installed fonts.": "输入字体家族名称或搜索已安装字体。", "Add Code Cell": "添加代码单元格", + "Collapse All Cells": "折叠所有单元格", + "Expand All Cells": "展开所有单元格", + "Collapse Cell": "折叠单元格", + "Expand Cell": "展开单元格", + "Hide Output": "隐藏输出", + "Show Output": "显示输出", + "Empty cell": "空单元格", + "1 output": "1 个输出", + "{count} outputs": "{count} 个输出", + "1 output hidden": "已隐藏 1 个输出", + "{count} outputs hidden": "已隐藏 {count} 个输出", "Add Markdown Cell": "添加 Markdown 单元格", "Cell": "单元格", "Cell outputs": "单元格输出",