Elffuss BraidHub

utopia-ia/elffuss-code public

Closing an editor tab keeps focus in the tabs, and a recreated file shows in the tree

- Closing an editor tab from the keyboard (Delete, Cmd/Ctrl+W, or "Don't
  save" in the unsaved-changes dialog) removed the focused tab and left
  focus on <body>; conversation tabs already did this right. Closing from
  the tab bar (including its ✕) now focuses the tab that stays active, or
  the "no files open" state when none is left. Cancel still returns focus
  to the tab, through the dialog.
- 6de16ab made the user's own saves bypass code.write, and with it the
  onFileWritten -> refreshTree() hook. Saving a tab whose file had been
  deleted from the explorer recreated the file on disk, but the explorer
  did not show it. saveTab() now refreshes the tree when the tab was
  marked deleted.

editor-save.mjs, parts 8 and 9: the recreated file is back in the tree,
and focus lands on the remaining tab after Delete and after "Don't save".
All three fail on the previous tree.
Changeseta173c6d98b7c7136f52202c40d78c2a1a8f6b67ec05c37e7bb3cf1c3ee2cdbd6
AuthorKikoCis <kikocisneros@gmail.com> · 2026-10-10 19:30 +02:00
CommitterKikoCis <kikocisneros@gmail.com> · 2026-10-11 14:15 +02:00
Parents5d9d7359c26a

Browse the files at this changeset

2 files changed

modified tests/editor-save.mjs +36 −0
@@ -14,6 +14,10 @@
1414 // «Copiar ruta» lo dice.
1515 // 6. El árbol se usa con el teclado (↓ y Enter) y Escape cierra su menú.
1616 // 7. «Ir a símbolo» sin fichero abierto lo dice en vez de no hacer nada.
17+// 8. Guardar una pestaña cuyo fichero se borró lo vuelve a crear Y el
18+// explorador lo enseña (antes quedaba fuera del árbol).
19+// 9. Cerrar una pestaña con el teclado (Supr, o «No guardar») deja el foco en
20+// la pestaña que queda (antes caía a <body>).
1721 //
1822 // BASE=http://127.0.0.1:8898 node tests/editor-save.mjs
1923 import { chromium } from 'playwright';
@@ -156,6 +160,38 @@
156160 await p.waitForTimeout(100);
157161 ok('6 · Shift+F10 abre el menú del árbol y Escape lo cierra', menuAbierto === 1 && await p.locator('#tree-menu').count() === 0);
158162
163+// 8 · guardar una pestaña cuyo fichero se borró lo vuelve a crear, y el árbol lo enseña
164+await abre('notes/todo.md');
165+await escribeEnEditor('- otra tarea\n');
166+await p.locator('#tree .file[data-path="notes/todo.md"]').click({ button: 'right' });
167+await p.locator('#tree-menu .tm-item', { hasText: 'Eliminar' }).click();
168+await p.waitForTimeout(400);
169+ok('8 · borrar un fichero con cambios deja su pestaña abierta (marcada)', (await pestañas()).some(t => t.path === 'notes/todo.md'), JSON.stringify(await pestañas()));
170+await p.click('#menubar button[data-menu="file"]');
171+await p.locator('#topmenu .menu-item', { hasText: 'Guardar' }).click();
172+await p.waitForTimeout(400);
173+ok('8 · guardarla lo vuelve a crear en disco…', (await disco('notes/todo.md'))?.includes('otra tarea'), String(await disco('notes/todo.md')));
174+ok('8 · …y el explorador lo enseña otra vez', await p.locator('#tree .file[data-path="notes/todo.md"]').count() === 1);
175+
176+// 9 · cerrar una pestaña con el teclado deja el foco en la pestaña que queda
177+await abre('README.md');
178+await abre('src/app.js');
179+await p.locator('#tabs-bar .tab.active').focus();
180+await p.keyboard.press('Delete');
181+await p.waitForTimeout(300);
182+const foco9 = await p.evaluate(() => ({ tab: document.activeElement?.classList.contains('tab') ? document.activeElement.dataset.path : null, tag: document.activeElement?.tagName }));
183+ok('9 · Supr en una pestaña la cierra y el foco pasa a la pestaña activa (no a <body>)', !!foco9.tab && foco9.tab !== 'src/app.js', JSON.stringify(foco9));
184+await abre('src/app.js');
185+await escribeEnEditor('// sin guardar\n');
186+await p.locator('#tabs-bar .tab.active').focus();
187+await p.keyboard.press('Delete');
188+await p.waitForTimeout(200);
189+await p.keyboard.press('Tab'); // de «Guardar» a «No guardar»
190+await p.keyboard.press('Enter');
191+await p.waitForTimeout(300);
192+const foco9b = await p.evaluate(() => ({ tab: document.activeElement?.classList.contains('tab') ? document.activeElement.dataset.path : null, tag: document.activeElement?.tagName }));
193+ok('9 · …también tras «No guardar» en el diálogo de cambios', !!foco9b.tab && !(await pestañas()).some(t => t.path === 'src/app.js'), JSON.stringify(foco9b));
194+
159195 // 7 · Ir a símbolo sin fichero abierto
160196 await p.evaluate(() => { for (const x of document.querySelectorAll('#tabs-bar .tab-x')) x.click(); });
161197 await p.waitForTimeout(300);
modified web/js/ide.js +21 −2
@@ -216,6 +216,7 @@
216216 code.invalidateFileList?.();
217217 }
218218 async function saveTab(tab) {
219+ const recreado = !!tab.deleted;
219220 try {
220221 await writeOwn(tab.path, tab.model.getValue());
221222 } catch (e) {
@@ -225,6 +226,10 @@
225226 tab.dirty = false;
226227 tab.deleted = false;
227228 renderTabs();
229+ // Guardar una pestaña cuyo fichero se había borrado lo vuelve a crear, y el
230+ // explorador tiene que enseñarlo: writeOwn no pasa por code.write, que era
231+ // quien avisaba al árbol (onFileWritten → refreshTree).
232+ if (recreado) await refreshTree();
228233 alertBar(t('saved', { path: tab.path }));
229234 return true;
230235 }
@@ -282,6 +287,20 @@
282287 await applyView();
283288 return true;
284289 }
290+// Cerrar desde la barra de pestañas (Supr, Cmd/Ctrl+W, la ✕, o «No guardar» en
291+// el diálogo): el foco va a la pestaña que queda activa, o al aviso de «sin
292+// ficheros abiertos» si no queda ninguna. Antes caía a <body> (las pestañas de
293+// conversación ya lo hacían bien; las del editor no).
294+async function closeTabYEnfoca(path) {
295+ if (!(await closeTab(path))) return false; // cancelado: el diálogo devuelve el foco a la pestaña
296+ const activa = $('tabs-bar')?.querySelector('.tab.active');
297+ if (activa) activa.focus();
298+ else {
299+ const vacio = document.getElementById('editor-empty');
300+ if (vacio) { vacio.tabIndex = -1; vacio.focus(); } else editor?.focus();
301+ }
302+ return true;
303+}
285304 // Varias seguidas («Cerrar otras/todas»): se para en la primera que cancelas.
286305 async function closeTabs(paths) {
287306 for (const p of paths) if (!(await closeTab(p))) return false;
@@ -346,7 +365,7 @@
346365 x.tabIndex = -1;
347366 x.setAttribute('aria-label', t(tb.dirty ? 'closeFileDirty' : 'closeFile', { name: baseOf(tb.path) }));
348367 x.title = x.getAttribute('aria-label');
349- x.onclick = e => { e.stopPropagation(); closeTab(tb.path); };
368+ x.onclick = e => { e.stopPropagation(); closeTabYEnfoca(tb.path); };
350369 el.append(ico, name, x);
351370 el.onclick = () => openFile(tb.path);
352371 el.oncontextmenu = e => { e.preventDefault(); openTabMenu(e.clientX, e.clientY, tb.path); };
@@ -357,7 +376,7 @@
357376 const next = tabs[(i + (e.key === 'ArrowRight' ? 1 : tabs.length - 1)) % tabs.length];
358377 if (next) openFile(next.path).then(() => bar.querySelector('.tab.active')?.focus());
359378 } else if (e.key === 'Delete' || ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'w')) {
360- e.preventDefault(); closeTab(tb.path);
379+ e.preventDefault(); closeTabYEnfoca(tb.path);
361380 } else if (e.key === 'ContextMenu' || (e.shiftKey && e.key === 'F10')) {
362381 e.preventDefault(); const r = el.getBoundingClientRect(); openTabMenu(r.left, r.bottom, tb.path);
363382 }