Skip to content

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.

feat: added collapseAllCells() to cells.ts and tests (#4799)

Joaquin Coromina3ff3d1f1a43d
diff --git a/frontend/src/core/cells/__tests__/cells.test.ts b/frontend/src/core/cells/__tests__/cells.test.ts
index 6615648bf4..5a0f044939 100644
--- a/frontend/src/core/cells/__tests__/cells.test.ts
+++ b/frontend/src/core/cells/__tests__/cells.test.ts
@@ -1447,7 +1447,7 @@ describe("cell reducer", () => {
expect(state.cellIds.atOrThrow(FIRST_COLUMN).isCollapsed(id)).toBe(false);
});
- it("can expand all cells in multiple columns", () => {
+ it("can collapse and expand all cells in multiple columns", () => {
actions.createNewCell({ cellId: firstCellId, before: false });
actions.createNewCell({
cellId: "1" as CellId,
@@ -1497,12 +1497,10 @@ describe("cell reducer", () => {
},
};
- actions.collapseCell({ cellId: firstColumnHeaderId });
+ actions.collapseAllCells();
expect(
state.cellIds.atOrThrow(FIRST_COLUMN).isCollapsed(firstColumnHeaderId),
).toBe(true);
-
- actions.collapseCell({ cellId: secondColumnHeaderId });
expect(
state.cellIds.atOrThrow(SECOND_COLUMN).isCollapsed(secondColumnHeaderId),
).toBe(true);
@@ -1516,7 +1514,7 @@ describe("cell reducer", () => {
).toBe(false);
});
- it("can expand nested cells in one call", () => {
+ it("can collapse and expand nested cells in one call", () => {
actions.createNewCell({ cellId: firstCellId, before: false });
actions.createNewCell({
cellId: "1" as CellId,
@@ -1550,15 +1548,18 @@ describe("cell reducer", () => {
},
};
- actions.collapseCell({ cellId: subheaderId });
- expect(state.cellIds.atOrThrow(FIRST_COLUMN).isCollapsed(subheaderId)).toBe(
+ // Check if both the parent and child are collapsed
+ actions.collapseAllCells();
+ expect(state.cellIds.atOrThrow(FIRST_COLUMN).isCollapsed(headerId)).toBe(
true,
);
- actions.collapseCell({ cellId: headerId });
- expect(state.cellIds.atOrThrow(FIRST_COLUMN).isCollapsed(headerId)).toBe(
+ actions.expandCell({ cellId: headerId });
+ expect(state.cellIds.atOrThrow(FIRST_COLUMN).isCollapsed(subheaderId)).toBe(
true,
);
+ actions.collapseCell({ cellId: headerId });
+ // Check if both the parent and child are expanded
actions.expandAllCells();
expect(state.cellIds.atOrThrow(FIRST_COLUMN).isCollapsed(headerId)).toBe(
false,
diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.ts
index 0ae34d66bc..427dbd3339 100644
--- a/frontend/src/core/cells/cells.ts
+++ b/frontend/src/core/cells/cells.ts
@@ -1076,6 +1076,60 @@ const {
scrollKey: cellId,
};
},
+ collapseAllCells: (state) => {
+ return {
+ ...state,
+ cellIds: state.cellIds.transformAll((column) => {
+ // Get all the top-level outlines
+ const outlines = column.topLevelIds.map((id) => {
+ const cell = state.cellRuntime[id];
+ return cell.outline;
+ });
+
+ // Find the start/end of the collapsed ranges
+ const nodes = [...column.nodes];
+ const rangeIndexes: {
+ start: CellIndex;
+ end: CellIndex;
+ }[] = [];
+ const reversedCollapseRanges = [];
+
+ // Iterate in reverse order (bottom-up) to process children first
+ let i = nodes.length - 1;
+ while (i >= 0) {
+ const range = findCollapseRange(i, outlines);
+ if (range) {
+ const startIndex = i;
+ let endIndex = range[1];
+
+ // 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 });
+
+ // Add the range to the list of ranges
+ const cellId = column.atOrThrow(startIndex);
+ const until = column.atOrThrow(endIndex);
+ reversedCollapseRanges.push({ id: cellId, until });
+ } else {
+ reversedCollapseRanges.push(null);
+ }
+ i--;
+ }
+
+ const collapseRanges = reversedCollapseRanges.reverse();
+ return column.collapseAll(collapseRanges);
+ }),
+ };
+ },
expandAllCells: (state) => {
return {
...state,