oficina / sheets Public

Web: a value that lands under an open editor is shown when it closes, not lost

#72, Fable's review of 58b4cb0, finding 2. 41ebeea stopped a batch from
setting the td of a cell whose editor is open (that wiped the edit), but
the skipped display was never applied: C1 (=B1*2) kept showing 10 after B1
became 7 and its editor was closed, and a cell kept its old text while the
server held a new one, until some later batch touched it.

The display is now held per cell and applied when the editor closes without
writing (Escape, or Enter on unchanged text); an edit that writes paints
from its own response, so it drops the held value. Enter on unchanged text
still writes nothing, so it never puts the old value back over a newer one.

TestFirstKeystrokeKeepsWhatLandsUnderAnOpenEditor: a dependent recomputed
under C1's editor then Escape (shows 14), the cell's own new value then
Escape (shows fresh), and Enter on unchanged text (shows fresh2, server
keeps fresh2). Without this: 10, old0, old1.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 48f8d5f30960da461b3c07ada4fcd54ae5ee4dc2 parent 687dbf1
2 files changed, +121 −24
  • internal/sheets/static/grid.js +24 −24
  • internal/sheets/typestart_leave_browser_test.go +97 −0
diff --git a/internal/sheets/static/grid.js b/internal/sheets/static/grid.js
index 157411f..88db0fe 100644
--- a/internal/sheets/static/grid.js
+++ b/internal/sheets/static/grid.js
@@ -46,6 +46,7 @@
* one that forgets is invisible until a format outlives its cell. */
var meta = Object.create(null);
var styles = Object.create(null); /* format key -> the style */
+ var held = Object.create(null); /* "r,c" -> a display that landed under an open editor */
var version = 0; /* the newest version painted */
var ws = null;
var sel = null; /* {r,c,r2,c2}; r,c is the anchor */
@@ -88,8 +89,7 @@
});
}
- /* Layout arrives with the batch it belongs to, so a column widened
- * in another tab widens here in the same frame the edit lands. */
+ /* Layout arrives with its batch: a column widened elsewhere widens here. */
if (window.SheetsSizes) window.SheetsSizes.apply(ws, payload.sizes);
applying = true;
@@ -97,10 +97,11 @@
payload.cells.forEach(function (cell) {
if (cell.r >= rows || cell.c >= cols) return;
meta[key(cell.r, cell.c)] = { raw: cell.raw || '', f: cell.f, n: cell.n };
- /* Never under an open editor: setting its td would wipe the edit,
- * and closing it then would save an empty cell. */
+ /* Never set the td under an open editor (that wiped the edit, and
+ * closing it saved an empty cell): hold the display until it closes. */
var ed = ws.edition;
- if (!ed || +ed[2] !== cell.c || +ed[3] !== cell.r) ws.setValueFromCoords(cell.c, cell.r, cell.display, true);
+ if (ed && +ed[2] === cell.c && +ed[3] === cell.r) held[key(cell.r, cell.c)] = cell.display;
+ else ws.setValueFromCoords(cell.c, cell.r, cell.display, true);
paint.repaint(cell.r, cell.c);
});
} finally {
@@ -127,21 +128,12 @@
.catch(function () { /* the stream's own reconnect will try again */ });
}
- /* post sends one change and paints what comes back.
- *
- * One function for all three — a cell, a format patch, a size —
- * because all three are the same exchange: send a change, receive the
- * batch it produced, paint that. Three copies of it is three places
- * for the version check or the failure path to drift.
- *
- * The response is applied even though the grid may already show the
- * typed text, because the server owns the displayed string: it is
- * what turns "=6*7" into "42", and what corrects a value the engine
- * read differently from how it looked.
- *
- * Every failure refetches. The write may or may not have landed, and
- * refetching is the only honest answer — the alternative is leaving
- * the grid showing something the server never accepted. */
+ /* post sends one change (a cell, a format patch, a size: one exchange,
+ * so the version check and failure path cannot drift between three) and
+ * paints the batch it produced, even over typed text: the server owns
+ * the displayed string ("=6*7" is "42"). Every failure refetches: the
+ * write may or may not have landed, and the grid must not keep showing
+ * something the server never accepted. */
function post(path, body) {
body.client = clientId;
return fetch(base + path, {
@@ -186,6 +178,7 @@
* browser tabs found it. */
onchange: function (instance, cell, c, r, value) {
if (applying) return;
+ delete held[key(r, c)]; /* this edit's own response will paint it */
write(parseInt(r, 10), parseInt(c, 10), value === null ? '' : String(value));
},
onselection: function (instance, x1, y1, x2, y2) {
@@ -193,10 +186,17 @@
r2: parseInt(y2, 10), c2: parseInt(x2, 10) };
announce();
},
- /* An edit that keeps the cell (Enter, F2, double-click) shows its
- * formula, not its result, filled on the next frame because the
- * input does not exist yet. One opened by typing replaces the cell
- * (typestart.js): filling it would overwrite the typed key. */
+ /* F2 or double-click shows the formula, filled a frame later (no input
+ * yet); typing replaces the cell (typestart.js), so is never filled. */
+ /* An edit closed without writing (Escape, or Enter on unchanged
+ * text): show what landed under it meanwhile, not the stale text. */
+ oneditionend: function (instance, td, c, r) {
+ var k = key(parseInt(r, 10), parseInt(c, 10)), d = held[k];
+ if (d === undefined) return;
+ delete held[k];
+ applying = true;
+ try { ws.setValueFromCoords(parseInt(c, 10), parseInt(r, 10), d, true); } finally { applying = false; }
+ },
oneditionstart: function (instance, td, c, r) {
var text = at(parseInt(r, 10), parseInt(c, 10)).raw;
if (!text || (window.SheetsTypeStart && window.SheetsTypeStart.replacing)) return;
diff --git a/internal/sheets/typestart_leave_browser_test.go b/internal/sheets/typestart_leave_browser_test.go
index dea6502..b1209a6 100644
--- a/internal/sheets/typestart_leave_browser_test.go
+++ b/internal/sheets/typestart_leave_browser_test.go
@@ -171,3 +171,100 @@ func TestFirstKeystrokeAfterLeavingTheGrid(t *testing.T) {
}
}
}
+
+// TestFirstKeystrokeKeepsWhatLandsUnderAnOpenEditor: a batch for a cell
+// whose editor is open must not wipe the editor (41ebeea), and must not be
+// lost either. Fable's review of 58b4cb0: the skipped repaint was never
+// applied, so C1 (=B1*2) kept showing 10 after B1 became 7 and its editor
+// was closed, and a cell kept its old text while the server held a new one.
+// The values arrive through the tab's own write path, so the order is fixed.
+func TestFirstKeystrokeKeepsWhatLandsUnderAnOpenEditor(t *testing.T) {
+ app, cookies := signIn(t)
+ sheet, cells := newSheet(t, app, cookies, "Under an editor")
+ for _, body := range []string{`{"r":0,"c":0,"raw":"old0"}`, `{"r":1,"c":0,"raw":"old1"}`, `{"r":0,"c":1,"raw":"5"}`, `{"r":0,"c":2,"raw":"=B1*2"}`} {
+ postCell(t, app, cookies, cells, body)
+ }
+ rig := harness.New(t, func(string) http.Handler { return productionHandler(t, app) })
+ var set []chromedp.Action[chromedp.Void]
+ for _, c := range cookies {
+ set = append(set, setCookie(rig.Origin, c))
+ }
+ rig.Run(set...)
+ cell := func(r, c int) string { return fmt.Sprintf(`td[data-x="%d"][data-y="%d"]`, c, r) }
+ press := func(k, code string, vk int64, text string) []chromedp.Action[chromedp.Void] {
+ kind := input.DispatchKeyEventTypeKeyDown
+ if text == "" {
+ kind = input.DispatchKeyEventTypeRawKeyDown
+ }
+ return []chromedp.Action[chromedp.Void]{
+ command(input.DispatchKeyEvent, input.DispatchKeyEventParams{Type: kind, Key: k, Code: code, WindowsVirtualKeyCode: vk, NativeVirtualKeyCode: vk, Text: text, UnmodifiedText: text}),
+ command(input.DispatchKeyEvent, input.DispatchKeyEventParams{Type: input.DispatchKeyEventTypeKeyUp, Key: k, Code: code, WindowsVirtualKeyCode: vk, NativeVirtualKeyCode: vk}),
+ }
+ }
+ run := func(what string, actions ...chromedp.Action[chromedp.Void]) {
+ t.Helper()
+ ctx, cancel := context.WithTimeout(rig.Context(), 20*time.Second)
+ defer cancel()
+ if err := chromedp.Do(ctx, actions...); err != nil {
+ t.Fatalf("%s: %v", what, err)
+ }
+ }
+ shows := func(r, c int) string {
+ var text string
+ run("read a cell", evalInto(&text, fmt.Sprintf(`document.querySelector(%q).textContent`, cell(r, c))))
+ return text
+ }
+ editorOpen := func() bool {
+ var open bool
+ run("editor?", evalInto(&open, `!!document.querySelector('td.editor')`))
+ return open
+ }
+ edit := func(r, c int) {
+ t.Helper()
+ run("F2", append([]chromedp.Action[chromedp.Void]{chromedp.Click(chromedp.CSS(cell(r, c))), chromedp.Sleep(150 * time.Millisecond)},
+ append(press("F2", "F2", 113, ""), chromedp.Sleep(150*time.Millisecond))...)...)
+ if !editorOpen() {
+ t.Fatalf("F2 opened no editor on row %d column %d", r+1, c+1)
+ }
+ }
+ write := func(r, c int, raw string) {
+ run("a write lands", evalInto((*any)(nil), fmt.Sprintf(`SheetsGrid.write(%d, %d, %q), null`, r, c, raw)), chromedp.Sleep(600*time.Millisecond))
+ }
+
+ run("open the sheet", chromedp.Navigate(fmt.Sprintf("%s/sheets/%d", rig.Origin, sheet.ID)),
+ chromedp.WaitVisible(chromedp.CSS(cell(1, 0))), chromedp.Sleep(200*time.Millisecond))
+ if got := shows(0, 2); got != "10" {
+ t.Fatalf("C1 shows %q before anything, want 10", got)
+ }
+
+ // A dependent recomputed under C1's editor, then Escape.
+ edit(0, 2)
+ write(0, 1, "7")
+ run("Escape", append(press("Escape", "Escape", 27, ""), chromedp.Sleep(200*time.Millisecond))...)
+ if got := shows(0, 2); got != "14" {
+ t.Errorf("C1 shows %q after B1 became 7 under its editor, want 14", got)
+ }
+
+ // The cell's own new value under its editor, then Escape.
+ edit(0, 0)
+ write(0, 0, "fresh")
+ run("Escape", append(press("Escape", "Escape", 27, ""), chromedp.Sleep(200*time.Millisecond))...)
+ if got := shows(0, 0); got != "fresh" {
+ t.Errorf("A1 shows %q after a new value landed under its editor, want fresh", got)
+ }
+
+ // Enter on text nobody changed writes nothing, so it shows the new value
+ // and does not put the old one back on the server.
+ edit(1, 0)
+ write(1, 0, "fresh2")
+ run("Enter", append(press("Enter", "Enter", 13, "\r"), chromedp.Sleep(500*time.Millisecond))...)
+ if got := shows(1, 0); got != "fresh2" {
+ t.Errorf("A2 shows %q after Enter on unchanged text, want fresh2", got)
+ }
+ read := decodeGrid(t, do(t, app, as(httptest.NewRequest(http.MethodGet, cells+"?rows=4&cols=3", nil), cookies)))
+ for _, c := range read.Cells {
+ if c.R == 1 && c.C == 0 && c.Raw != "fresh2" {
+ t.Errorf("Enter on unchanged text wrote %q over fresh2", c.Raw)
+ }
+ }
+}