Skip to content

Commit 86c813e

Browse files
authored
fix(notebooks): reset insert offset when anchor cell moves (supabase#49694)
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES ## What kind of change does this PR introduce? Bug fix ## What is the current behavior? When a cell gets moved via `move_cell` operation in `deriveNotebookDiff`, the `insertedAfter` offset map is not cleared for that anchor cell. This causes later `insert_cell` operations anchored on the same (now-moved) cell to apply the stale offset on top of the correct current-position lookup, resulting in the new cell landing after the wrong position. ## What is the new behavior? The offset for an anchor cell is now cleared from `insertedAfter` when it gets moved, since cells previously inserted after it stay behind at its old location and should not affect subsequent inserts at its new position. A regression test has been added that reproduces the exact ticket scenario (insert after cell-1, move cell-1 after cell-3, insert after cell-1 again) and verifies the correct final cell order. ## Additional context Fixes: https://linear.app/supabase/issue/FE-4308/insert-anchored-to-a-previously-moved-cell-lands-after-the-wrong-cell <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Fixed notebook cell insertions after moving an anchor cell, ensuring new inserts appear relative to the anchor’s updated position. * Preserved the placement of inserts made before the anchor cell was moved. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 8790e65 commit 86c813e

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

apps/studio/data/content/notebooks/notebook-operations.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,41 @@ describe('applyNotebookOperations', () => {
121121
])
122122
})
123123

124+
it('resets the insert offset for an anchor cell once it gets moved', () => {
125+
// cell-1 gets an insert right after it, then cell-1 itself moves after cell-3. A later
126+
// insert anchored on cell-1 must land right after its *new* position — the first insert
127+
// stayed behind at cell-1's old spot, so it shouldn't count toward this offset anymore.
128+
const notebook: NotebookWire = {
129+
schema_version: 1,
130+
cells: [
131+
{ _tag: 'markdown_cell', _id: 'cell-1', text: '1' },
132+
{ _tag: 'markdown_cell', _id: 'cell-2', text: '2' },
133+
{ _tag: 'markdown_cell', _id: 'cell-3', text: '3' },
134+
{ _tag: 'markdown_cell', _id: 'cell-4', text: '4' },
135+
],
136+
}
137+
const ops: NotebookOperation[] = [
138+
{
139+
_tag: 'insert_cell',
140+
after_cell_id: 'cell-1',
141+
cell: { _tag: 'markdown_cell', text: 'first' },
142+
},
143+
{ _tag: 'move_cell', cell_id: 'cell-1', after_cell_id: 'cell-3' },
144+
{
145+
_tag: 'insert_cell',
146+
after_cell_id: 'cell-1',
147+
cell: { _tag: 'markdown_cell', text: 'second' },
148+
},
149+
]
150+
151+
const result = applyNotebookOperations(notebook, ops)
152+
153+
expect(result.success).toBe(true)
154+
if (!result.success) return
155+
const texts = result.notebook.cells.map((cell) => ('text' in cell ? cell.text : undefined))
156+
expect(texts).toEqual(['first', '2', '3', '1', 'second', '4'])
157+
})
158+
124159
it('resolves a move anchored on another moved cell using its new position', () => {
125160
// cell-1 moves after cell-3 first, landing at [cell-2, cell-3, cell-1]; cell-2 then moves
126161
// after cell-1's *new* position, giving [cell-3, cell-1, cell-2].

apps/studio/data/content/notebooks/notebook-operations.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ export function deriveNotebookDiff(
256256
}
257257

258258
entries.splice(found.index, 1)
259+
// Cells already inserted after this anchor stay behind at its old position, so a
260+
// later insert anchored on it should start counting from its new position again.
261+
insertedAfter.delete(operation.cell_id)
259262
const error = insertAfter(operation.after_cell_id, {
260263
_tag: 'moved',
261264
cell: found.cell,

0 commit comments

Comments
 (0)