A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix(frontend): collapse all sections when nested heading ranges share an end cell (#10923)
cspipaonba9cd07b6d92
diff --git a/frontend/src/core/cells/__tests__/cells.test.ts b/frontend/src/core/cells/__tests__/cells.test.tsindex df138a4955..43eaa6f4ae 100644--- a/frontend/src/core/cells/__tests__/cells.test.ts+++ b/frontend/src/core/cells/__tests__/cells.test.ts@@ -2037,6 +2037,150 @@ describe("cell reducer", () => {);});+ it("can collapse all cells when nested heading ranges end at the same cell", () => {+ actions.createNewCell({ cellId: firstCellId, before: false });+ actions.createNewCell({+ cellId: cellId("1"),+ before: false,+ code: "# Header",+ });+ actions.createNewCell({+ cellId: cellId("2"),+ before: false,+ code: "## Subheader",+ });+ actions.createNewCell({+ cellId: cellId("3"),+ before: false,+ code: "### Subsubheader",+ });++ const headerId = state.cellIds.atOrThrow(FIRST_COLUMN).atOrThrow(1);+ state.cellRuntime[headerId] = {+ ...state.cellRuntime[headerId],+ outline: {+ items: [{ name: "Header", level: 1, by: { id: "header" } }],+ },+ };++ const subheaderId = state.cellIds.atOrThrow(FIRST_COLUMN).atOrThrow(2);+ state.cellRuntime[subheaderId] = {+ ...state.cellRuntime[subheaderId],+ outline: {+ items: [{ name: "Subheader", level: 2, by: { id: "subheader" } }],+ },+ };++ const subsubheaderId = state.cellIds.atOrThrow(FIRST_COLUMN).atOrThrow(3);+ state.cellRuntime[subsubheaderId] = {+ ...state.cellRuntime[subsubheaderId],+ outline: {+ items: [{ name: "Subsubheader", level: 3, by: { id: "subsubheader" } }],+ },+ };++ // The header, subheader and subsubheader ranges all end at the last+ // cell. Collapsing all used to throw "Node ... not found in tree" and+ // leave the whole notebook unchanged.+ actions.collapseAllCells();+ expect(state.cellIds.atOrThrow(FIRST_COLUMN).isCollapsed(headerId)).toBe(+ true,+ );+ expect(state.cellIds.atOrThrow(FIRST_COLUMN).topLevelIds).toEqual([+ firstCellId,+ headerId,+ ]);++ // Each level is nested under its parent, collapsed+ actions.expandCell({ cellId: headerId });+ expect(state.cellIds.atOrThrow(FIRST_COLUMN).isCollapsed(subheaderId)).toBe(+ true,+ );+ actions.expandCell({ cellId: subheaderId });+ expect(+ state.cellIds.atOrThrow(FIRST_COLUMN).isCollapsed(subsubheaderId),+ ).toBe(true);+ });++ it("can collapse all cells when a deep chain is followed by a later section", () => {+ actions.createNewCell({ cellId: firstCellId, before: false });+ actions.createNewCell({+ cellId: cellId("1"),+ before: false,+ code: "# Header",+ });+ actions.createNewCell({+ cellId: cellId("2"),+ before: false,+ code: "## Section A",+ });+ actions.createNewCell({+ cellId: cellId("3"),+ before: false,+ code: "### Sub of A",+ });+ actions.createNewCell({+ cellId: cellId("4"),+ before: false,+ code: "## Section B",+ });++ const headerId = state.cellIds.atOrThrow(FIRST_COLUMN).atOrThrow(1);+ state.cellRuntime[headerId] = {+ ...state.cellRuntime[headerId],+ outline: {+ items: [{ name: "Header", level: 1, by: { id: "header" } }],+ },+ };++ const sectionAId = state.cellIds.atOrThrow(FIRST_COLUMN).atOrThrow(2);+ state.cellRuntime[sectionAId] = {+ ...state.cellRuntime[sectionAId],+ outline: {+ items: [{ name: "Section A", level: 2, by: { id: "section-a" } }],+ },+ };++ const subsectionId = state.cellIds.atOrThrow(FIRST_COLUMN).atOrThrow(3);+ state.cellRuntime[subsectionId] = {+ ...state.cellRuntime[subsectionId],+ outline: {+ items: [{ name: "Sub of A", level: 3, by: { id: "sub-of-a" } }],+ },+ };++ const sectionBId = state.cellIds.atOrThrow(FIRST_COLUMN).atOrThrow(4);+ state.cellRuntime[sectionBId] = {+ ...state.cellRuntime[sectionBId],+ outline: {+ items: [{ name: "Section B", level: 2, by: { id: "section-b" } }],+ },+ };++ actions.collapseAllCells();+ expect(state.cellIds.atOrThrow(FIRST_COLUMN).isCollapsed(headerId)).toBe(+ true,+ );+ expect(state.cellIds.atOrThrow(FIRST_COLUMN).topLevelIds).toEqual([+ firstCellId,+ headerId,+ ]);++ // Both sections sit under the header, collapsed, with the subsection+ // nested under section A+ actions.expandCell({ cellId: headerId });+ expect(state.cellIds.atOrThrow(FIRST_COLUMN).isCollapsed(sectionAId)).toBe(+ true,+ );+ expect(state.cellIds.atOrThrow(FIRST_COLUMN).isCollapsed(sectionBId)).toBe(+ true,+ );+ actions.expandCell({ cellId: sectionAId });+ expect(+ state.cellIds.atOrThrow(FIRST_COLUMN).isCollapsed(subsectionId),+ ).toBe(true);+ });+it("can show hidden cells", () => {actions.createNewCell({ cellId: firstCellId, before: false });actions.createNewCell({ cellId: cellId("1"), before: false });diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.tsindex 7acf05ad26..f4128bb7ec 100644--- a/frontend/src/core/cells/cells.ts+++ b/frontend/src/core/cells/cells.ts@@ -1365,10 +1365,7 @@ const {// Find the start/end of the collapsed rangesconst nodes = [...column.nodes];- const rangeIndexes: {- start: CellIndex;- end: CellIndex;- }[] = [];+ const visibleEndByOriginalEnd = new Map<CellIndex, CellIndex>();const reversedCollapseRanges = [];// Iterate in reverse order (bottom-up) to process children first@@ -1377,20 +1374,13 @@ const {const range = findCollapseRange(i, outlines);if (range) {const startIndex = i;- let endIndex = range[1];+ const originalEnd = range[1];+ const endIndex =+ visibleEndByOriginalEnd.get(originalEnd) ?? originalEnd;- // Check if the parent's end point is inside any already-collapsed child range- const parentEndInChild = rangeIndexes.find(- (child) => child.start <= endIndex && child.end === endIndex,- );-- if (parentEndInChild) {- // Adjust the new endIndex to the child's start- endIndex = parentEndInChild.start;- }-- // Store this range for future child checks- rangeIndexes.push({ start: startIndex, end: endIndex });+ // After this section collapses, its heading represents this+ // endpoint for any enclosing section that shares it.+ visibleEndByOriginalEnd.set(originalEnd, startIndex);// Add the range to the list of rangesconst cellId = column.atOrThrow(startIndex);