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.

fix: scroll to cell (#1905)

Myles Scolnickc38f05a063f1
diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.ts
index 3efd16ff4a..333c657ba2 100644
--- a/frontend/src/core/cells/cells.ts
+++ b/frontend/src/core/cells/cells.ts
@@ -754,10 +754,16 @@ const {
},
showCellIfHidden: (state, action: { cellId: CellId }) => {
const { cellId } = action;
+ const prev = state.cellIds;
+ const result = state.cellIds.findAndExpandDeep(cellId);
+
+ if (result.equals(prev)) {
+ return state;
+ }
+
return {
...state,
- cellIds: state.cellIds.findAndExpandDeep(cellId),
- scrollKey: cellId,
+ cellIds: result,
};
},
splitCell: (state, action: { cellId: CellId }) => {
diff --git a/frontend/src/utils/__tests__/id-tree.test.ts b/frontend/src/utils/__tests__/id-tree.test.ts
index c53e801d02..c55fadbbc4 100644
--- a/frontend/src/utils/__tests__/id-tree.test.ts
+++ b/frontend/src/utils/__tests__/id-tree.test.ts
@@ -116,9 +116,10 @@ describe("CollapsibleTree", () => {
).toThrowErrorMatchingInlineSnapshot(
"[Error: Node one is before node two]",
);
+
expect(() => {
- tree.collapse("two", undefined);
- tree.collapse("two", undefined);
+ tree = tree.collapse("two", undefined);
+ tree = tree.collapse("two", undefined);
}).toThrowErrorMatchingInlineSnapshot(
"[Error: Node two is already collapsed]",
);
@@ -249,13 +250,17 @@ describe("CollapsibleTree", () => {
four
"
`);
- expect(tree.findAndExpandDeep("four").toString()).toMatchInlineSnapshot(`
+ const prevTree = tree;
+ tree = tree.findAndExpandDeep("four");
+ expect(tree.toString()).toMatchInlineSnapshot(`
"one
two
three
four
"
`);
+ // doesn't mutate
+ expect(prevTree.toString()).not.toEqual(tree.toString());
});
it("finds and expands correctly at a non-leaf", () => {
@@ -267,13 +272,17 @@ describe("CollapsibleTree", () => {
four
"
`);
- expect(tree.findAndExpandDeep("three").toString()).toMatchInlineSnapshot(`
+ const prevTree = tree;
+ tree = tree.findAndExpandDeep("three");
+ expect(tree.toString()).toMatchInlineSnapshot(`
"one
two
three
four
"
`);
+ // doesn't mutate
+ expect(prevTree.toString()).not.toEqual(tree.toString());
});
it("can delete nodes", () => {
diff --git a/frontend/src/utils/id-tree.tsx b/frontend/src/utils/id-tree.tsx
index 362401262c..9eb59a3853 100644
--- a/frontend/src/utils/id-tree.tsx
+++ b/frontend/src/utils/id-tree.tsx
@@ -39,6 +39,10 @@ export class TreeNode<T> {
}
return String(this.value);
}
+
+ equals(other: TreeNode<T>): boolean {
+ return this.value === other.value;
+ }
}
export class CollapsibleTree<T> {
@@ -148,16 +152,17 @@ export class CollapsibleTree<T> {
throw new Error(`Node ${until} is before node ${id}`);
}
- const node = this.nodes[nodeIndex];
+ const nodes = [...this.nodes];
+ const node = nodes[nodeIndex];
if (node.isCollapsed) {
throw new Error(`Node ${id} is already collapsed`);
}
// Fold the next nodes into the current node
- node.children = this.nodes.splice(nodeIndex + 1, untilIndex - nodeIndex);
- node.isCollapsed = true;
+ const children = nodes.splice(nodeIndex + 1, untilIndex - nodeIndex);
+ nodes[nodeIndex] = new TreeNode(node.value, true, children);
- return new CollapsibleTree(this.nodes);
+ return new CollapsibleTree(nodes);
}
/**
@@ -171,16 +176,16 @@ export class CollapsibleTree<T> {
);
}
- const node = this.nodes[nodeIndex];
+ let nodes = [...this.nodes];
+ const node = nodes[nodeIndex];
if (!node.isCollapsed) {
throw new Error(`Node ${id} is already expanded`);
}
- node.isCollapsed = false;
- this.nodes = arrayInsertMany(this.nodes, nodeIndex + 1, node.children);
- node.children = [];
+ nodes[nodeIndex] = new TreeNode(node.value, false, []);
+ nodes = arrayInsertMany(nodes, nodeIndex + 1, node.children);
- return new CollapsibleTree(this.nodes);
+ return new CollapsibleTree(nodes);
}
/**
@@ -250,12 +255,13 @@ export class CollapsibleTree<T> {
*/
delete(idx: number): CollapsibleTree<T> {
const id = this.atOrThrow(idx);
+ let tree = new CollapsibleTree(this.nodes);
try {
- this.expand(id);
+ tree = tree.expand(id);
} catch {
// Don't care if its not expanded
}
- return new CollapsibleTree(arrayDelete(this.nodes, idx));
+ return new CollapsibleTree(arrayDelete(tree.nodes, idx));
}
/**
@@ -270,15 +276,19 @@ export class CollapsibleTree<T> {
*/
findAndExpandDeep(id: T): CollapsibleTree<T> {
const found = this.find(id);
+ if (found.length === 0) {
+ return this;
+ }
+ let result = new CollapsibleTree<T>(this.nodes);
for (const node of found) {
try {
- this.expand(node);
+ result = result.expand(node);
} catch {
// Don't care if its the last node and its not expanded
}
}
- return new CollapsibleTree(this.nodes);
+ return result;
}
/**
@@ -303,6 +313,13 @@ export class CollapsibleTree<T> {
return findNode(this.nodes, []);
}
+ equals(other: CollapsibleTree<T>): boolean {
+ return (
+ this.nodes.length === other.nodes.length &&
+ this.nodes.every((n, i) => n.value === other.nodes[i].value)
+ );
+ }
+
toString(): string {
let depth = 0;
let result = "";