From 7291bbee1b9f50a38f79d27e773ae8e708c9e608 Mon Sep 17 00:00:00 2001 From: Quinten Kock Date: Sun, 30 Nov 2025 02:37:15 +0100 Subject: [PATCH] Make closing a Displayable focus a neighbor --- src/app/editorgrid.ts | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/app/editorgrid.ts b/src/app/editorgrid.ts index cdb3b7a..3e440d9 100644 --- a/src/app/editorgrid.ts +++ b/src/app/editorgrid.ts @@ -22,9 +22,41 @@ const EditorWrapper = ( ) => { // Set the delete function on the editor when it's created van.derive(() => { - if (editor.val) { - editor.val.setDeleteFunction(del); - } + if (!editor || !editor.val) return; + + const wrappedDelete = () => { + // TODO: find a better way to get the list containing this EditorWrapper + const list = editors[currentTab.val] || []; + + // Find nearest non-empty neighbor (scan left then right) + let neighborState: State | null = null; + for (let i = k - 1; i >= 0; i--) { + const c = list[i]; + if (c) { + neighborState = c; + break; + } + } + if (!neighborState) { + for (let i = k + 1; i < list.length; i++) { + const c = list[i]; + if (c && c.val) { + neighborState = c; + break; + } + } + } + + // Call the original delete function which updates the reactive list / DOM + del(); + + // After reactive update, focus the neighbor if available + if (neighborState) { + neighborState.focus(); + } + }; + + editor.val.setDeleteFunction(wrappedDelete); }); return v.div( @@ -98,9 +130,8 @@ function shortcutHandler(e: KeyboardEvent) { addTerminal(); } e.preventDefault(); - } } -document.addEventListener("keyup", shortcutHandler, { capture: true, }); -document.addEventListener("keydown", shortcutHandler, { capture: true, }); \ No newline at end of file +document.addEventListener("keyup", shortcutHandler, { capture: true }); +document.addEventListener("keydown", shortcutHandler, { capture: true });