oficina / sheets Public

Web: after Bold, a menu or the formula bar, Enter moves on and the grid's keys work

#72, Fable's review of 58b4cb0, finding 1. The library's mousedown outside
the table clears its own selection as well as its current sheet, and own()
put back only the sheet. So after a click on Bold, x Enter saved x without
moving on and y Enter then overwrote it; the arrows, Shift+arrows and
Delete did nothing. own() now also restores the library's selection from
the grid's when it has none.

TestFirstKeystrokeAfterLeavingTheGrid now types a second value after each
Enter (it must land one row down, the first kept) and, after each way of
leaving the grid, presses ArrowDown, Shift+ArrowDown and Delete and checks
the selection and the cleared cell. Without this: "row 2 holds old1, want r".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 687dbf12cd6a4dd743a0cdc1582510df9b67674b parent 58b4cb0
2 files changed, +48 −6
  • internal/sheets/static/typestart.js +8 −2
  • internal/sheets/typestart_leave_browser_test.go +40 −4
diff --git a/internal/sheets/static/typestart.js b/internal/sheets/static/typestart.js
index 00b5dec..0d7d50a 100644
--- a/internal/sheets/static/typestart.js
+++ b/internal/sheets/static/typestart.js
@@ -71,10 +71,16 @@
* its "current" sheet, and a click anywhere outside the table (Bold, a
* menu, the formula bar) sets that to none. Whenever the holder has the
* keyboard, the grid is current again; without this an editor opened by
- * typing ignored Enter and Escape and was saved by the next click. */
+ * typing ignored Enter and Escape and was saved by the next click. The
+ * same click clears the library's own selection while ours stands, and
+ * then Enter saved without moving on (the next value overwrote the cell)
+ * and the arrows and Delete did nothing: put that back too. */
function own() {
var ws = grid() && grid().worksheet();
- if (ws && window.jspreadsheet) window.jspreadsheet.current = ws;
+ var s = grid() && grid().selection();
+ if (!ws || !window.jspreadsheet) return;
+ window.jspreadsheet.current = ws;
+ if (!ws.selectedCell && s && ws.updateSelectionFromCoords) ws.updateSelectionFromCoords(s.c, s.r, s.c2, s.r2);
}
function open(sel) {
diff --git a/internal/sheets/typestart_leave_browser_test.go b/internal/sheets/typestart_leave_browser_test.go
index 9463cb0..dea6502 100644
--- a/internal/sheets/typestart_leave_browser_test.go
+++ b/internal/sheets/typestart_leave_browser_test.go
@@ -29,6 +29,7 @@ func TestFirstKeystrokeAfterLeavingTheGrid(t *testing.T) {
sheet, cells := newSheet(t, app, cookies, "Leaving")
for r := 0; r < 14; r++ {
postCell(t, app, cookies, cells, fmt.Sprintf(`{"r":%d,"c":0,"raw":"old%d"}`, r, r))
+ postCell(t, app, cookies, cells, fmt.Sprintf(`{"r":%d,"c":2,"raw":"c%d"}`, r, r))
}
rig := harness.New(t, func(string) http.Handler { return productionHandler(t, app) })
var set []chromedp.Action[chromedp.Void]
@@ -56,15 +57,21 @@ func TestFirstKeystrokeAfterLeavingTheGrid(t *testing.T) {
t.Fatalf("%s: %v", what, err)
}
}
- raw := func(r int) string {
- read := decodeGrid(t, do(t, app, as(httptest.NewRequest(http.MethodGet, cells+"?rows=16&cols=2", nil), cookies)))
+ rawAt := func(r, col int) string {
+ read := decodeGrid(t, do(t, app, as(httptest.NewRequest(http.MethodGet, cells+"?rows=16&cols=3", nil), cookies)))
for _, c := range read.Cells {
- if c.R == r && c.C == 0 {
+ if c.R == r && c.C == col {
return c.Raw
}
}
return ""
}
+ raw := func(r int) string { return rawAt(r, 0) }
+ selection := func() [4]int {
+ var s [4]int
+ run("read the selection", evalInto(&s, `(() => { const s = SheetsGrid.selection(); return [s.r, s.c, s.r2, s.c2]; })()`))
+ return s
+ }
saved := func(what string, r int, want string) {
t.Helper()
deadline := time.Now().Add(4 * time.Second)
@@ -106,7 +113,7 @@ func TestFirstKeystrokeAfterLeavingTheGrid(t *testing.T) {
run("open the sheet", chromedp.Navigate(fmt.Sprintf("%s/sheets/%d", rig.Origin, sheet.ID)),
chromedp.WaitVisible(chromedp.CSS(cell(13, 0))), chromedp.Sleep(200*time.Millisecond))
for i, way := range ways {
- enter, escape := 2*i, 2*i+1
+ enter, escape := 3*i, 3*i+2
// Enter saves what was typed.
run(way.name+": select", pointerClick(cell(enter, 0)), chromedp.Sleep(150*time.Millisecond))
run(way.name+": leave", append(way.leave, chromedp.Sleep(way.pause))...)
@@ -116,6 +123,13 @@ func TestFirstKeystrokeAfterLeavingTheGrid(t *testing.T) {
t.Errorf("%s: Enter left the editor open", way.name)
}
saved(way.name+", Enter", enter, "q")
+ // Enter moved on, so the next value lands one row down, not over this one.
+ run(way.name+": type the next value", append(append(press("r", "KeyR", 82, "r"), chromedp.Sleep(100*time.Millisecond)),
+ append(press("Enter", "Enter", 13, "\r"), chromedp.Sleep(300*time.Millisecond))...)...)
+ saved(way.name+", the next value", enter+1, "r")
+ if got := raw(enter); got != "q" {
+ t.Errorf("%s: the second value overwrote the first: row %d holds %q", way.name, enter+1, got)
+ }
// Escape cancels, and a click away saves nothing.
run(way.name+": select", pointerClick(cell(escape, 0)), chromedp.Sleep(150*time.Millisecond))
@@ -133,5 +147,27 @@ func TestFirstKeystrokeAfterLeavingTheGrid(t *testing.T) {
if got := raw(escape); got != fmt.Sprintf("old%d", escape) {
t.Errorf("%s: Escape then a click away saved %q over old%d", way.name, got, escape)
}
+
+ // The grid's own keys still work after leaving it: an arrow, an
+ // extending Shift+arrow, and Delete on what that selected.
+ top := 3 * i
+ run(way.name+": select in column C", pointerClick(cell(top, 2)), chromedp.Sleep(150*time.Millisecond))
+ run(way.name+": leave", append(way.leave, chromedp.Sleep(250*time.Millisecond))...)
+ run(way.name+": ArrowDown", append(press("ArrowDown", "ArrowDown", 40, ""), chromedp.Sleep(150*time.Millisecond))...)
+ if s := selection(); s[0] != top+1 || s[1] != 2 {
+ t.Errorf("%s: ArrowDown left the selection at %v, want row %d", way.name, s, top+2)
+ }
+ run(way.name+": leave", append(way.leave, chromedp.Sleep(250*time.Millisecond))...)
+ run(way.name+": Shift+ArrowDown", command(input.DispatchKeyEvent, input.DispatchKeyEventParams{Type: input.DispatchKeyEventTypeRawKeyDown, Key: "ArrowDown", Code: "ArrowDown", WindowsVirtualKeyCode: 40, Modifiers: 8}),
+ command(input.DispatchKeyEvent, input.DispatchKeyEventParams{Type: input.DispatchKeyEventTypeKeyUp, Key: "ArrowDown", Code: "ArrowDown", WindowsVirtualKeyCode: 40, Modifiers: 8}),
+ chromedp.Sleep(150*time.Millisecond))
+ if s := selection(); s[0] != top+1 || s[2] != top+2 {
+ t.Errorf("%s: Shift+ArrowDown selected %v, want rows %d to %d", way.name, s, top+2, top+3)
+ }
+ run(way.name+": leave", append(way.leave, chromedp.Sleep(250*time.Millisecond))...)
+ run(way.name+": Delete", append(press("Delete", "Delete", 46, ""), chromedp.Sleep(400*time.Millisecond))...)
+ if got := rawAt(top+1, 2); got != "" {
+ t.Errorf("%s: Delete left row %d of column C holding %q", way.name, top+2, got)
+ }
}
}