| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "strconv" |
| 7 | |
| 8 | "amadan.net/oficina/sheets/internal/sheets/store" |
| 9 | ) |
| 10 | |
| 11 | // sizeWrite is the POST body: one column's width or one row's height. |
| 12 | // |
| 13 | // Exactly one of Col and Row is set, and the handler refuses both or |
| 14 | // neither rather than picking. A request that names two things is a |
| 15 | // client bug, and guessing which it meant is how the wrong column ends |
| 16 | // up resized on the day somebody's serialiser starts emitting zeroes |
| 17 | // for absent fields. |
| 18 | type sizeWrite struct { |
| 19 | Col *int `json:"col,omitempty"` |
| 20 | Row *int `json:"row,omitempty"` |
| 21 | Size int `json:"size"` |
| 22 | Client string `json:"client"` |
| 23 | } |
| 24 | |
| 25 | // handleSizePost stores a column width or a row height. |
| 26 | // |
| 27 | // It answers with a sizes payload and publishes the same one, so a |
| 28 | // column widened in one tab widens in every other tab watching the |
| 29 | // sheet — the same live-sync contract every other edit has. A layout |
| 30 | // change that did not propagate would leave two people looking at |
| 31 | // visibly different documents and no way to tell which was right. |
| 32 | // |
| 33 | // Like formatting, a resize writes no event and is therefore not |
| 34 | // undoable. Same reason, same named limitation. |
| 35 | func (a *App) handleSizePost(w http.ResponseWriter, r *http.Request) { |
| 36 | viewer, ok := a.actor(r) |
| 37 | if !ok { |
| 38 | http.Error(w, "Sign in first.", http.StatusForbidden) |
| 39 | return |
| 40 | } |
| 41 | h, ok := a.openTab(w, r, SheetEditor) |
| 42 | if !ok { |
| 43 | return |
| 44 | } |
| 45 | defer h.Release() |
| 46 | wr := h.Guarded(a.writeGuard(r.Context(), sheetIDFrom(r), viewer, SheetEditor)) |
| 47 | |
| 48 | // One route, two decoders, converging on one sizeWrite before |
| 49 | // anything is checked — the same shape handleFormatPost took, and |
| 50 | // for the same reason: the bounds, the exactly-one rule and the |
| 51 | // store call must exist once. The scriptless table view posts a |
| 52 | // form here; the grid posts JSON. |
| 53 | var in sizeWrite |
| 54 | form := isFormPost(r) |
| 55 | if form { |
| 56 | r.Body = http.MaxBytesReader(w, r.Body, 1<<10) |
| 57 | var ok bool |
| 58 | if in, ok = sizeFromForm(r); !ok { |
| 59 | http.Error(w, "Malformed request.", http.StatusBadRequest) |
| 60 | return |
| 61 | } |
| 62 | } else if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<10)).Decode(&in); err != nil { |
| 63 | http.Error(w, "Malformed request.", http.StatusBadRequest) |
| 64 | return |
| 65 | } |
| 66 | if (in.Col == nil) == (in.Row == nil) { |
| 67 | http.Error(w, "Name exactly one of col and row.", http.StatusBadRequest) |
| 68 | return |
| 69 | } |
| 70 | |
| 71 | var err error |
| 72 | switch { |
| 73 | case in.Col != nil: |
| 74 | if *in.Col < 0 || *in.Col >= maxWindowCols*10 { |
| 75 | http.Error(w, "Column out of range.", http.StatusBadRequest) |
| 76 | return |
| 77 | } |
| 78 | var colID uint64 |
| 79 | if colID, err = h.EnsureCol(*in.Col); err == nil { |
| 80 | err = wr.SetColWidth(colID, in.Size) |
| 81 | } |
| 82 | default: |
| 83 | if *in.Row < 0 || *in.Row >= maxWindowRows*10 { |
| 84 | http.Error(w, "Row out of range.", http.StatusBadRequest) |
| 85 | return |
| 86 | } |
| 87 | var rowID uint64 |
| 88 | if rowID, err = h.EnsureRow(*in.Row); err == nil { |
| 89 | err = wr.SetRowHeight(rowID, in.Size) |
| 90 | } |
| 91 | } |
| 92 | if a.guardRefused(w, r, err) { |
| 93 | return |
| 94 | } |
| 95 | if err != nil { |
| 96 | // A size outside the bounds is the caller's fault and a storage |
| 97 | // failure is ours. Both answer the same fixed string: the error |
| 98 | // carries a coordinate the caller already knows and nothing |
| 99 | // else, because it is one hop from a log line (AGENTS.md). |
| 100 | http.Error(w, "That size could not be applied.", http.StatusBadRequest) |
| 101 | return |
| 102 | } |
| 103 | |
| 104 | resp, err := sizesResponse(h, in.Client) |
| 105 | if err != nil { |
| 106 | a.fail(w, r, err) |
| 107 | return |
| 108 | } |
| 109 | a.touchSheet(sheetIDFrom(r)) |
| 110 | a.publish(tabIDFrom(r), resp) |
| 111 | if form { |
| 112 | // 303 back to the table, built here from the sheet id in the |
| 113 | // path and a page this bounds itself — never from the request. |
| 114 | // Same reasoning as the format route's redirect. |
| 115 | http.Redirect(w, r, tableReturn(sheetIDFrom(r), r.FormValue("page")), http.StatusSeeOther) |
| 116 | return |
| 117 | } |
| 118 | writeJSON(w, http.StatusOK, resp) |
| 119 | } |
| 120 | |
| 121 | // sizeFromForm decodes a sizeWrite out of an ordinary HTML form. |
| 122 | // |
| 123 | // Nothing is validated here. The exactly-one-of-col-and-row rule, the |
| 124 | // bounds and the store call all run afterwards in the handler on |
| 125 | // whichever request arrived — so the form path and the JSON path cannot |
| 126 | // drift into two rule sets, which is the same decision formatFromForm |
| 127 | // records. |
| 128 | // |
| 129 | // Presence, not value, decides which of col and row was named: an |
| 130 | // absent field and a field holding "0" are different requests, and |
| 131 | // column 0 is a real column. That is the same reason the JSON shape |
| 132 | // uses pointers. |
| 133 | func sizeFromForm(r *http.Request) (sizeWrite, bool) { |
| 134 | var in sizeWrite |
| 135 | if err := r.ParseForm(); err != nil { |
| 136 | return in, false |
| 137 | } |
| 138 | read := func(name string) (*int, bool) { |
| 139 | if _, present := r.Form[name]; !present { |
| 140 | return nil, true |
| 141 | } |
| 142 | n, err := strconv.Atoi(r.FormValue(name)) |
| 143 | if err != nil { |
| 144 | return nil, false |
| 145 | } |
| 146 | return &n, true |
| 147 | } |
| 148 | col, ok := read("col") |
| 149 | if !ok { |
| 150 | return in, false |
| 151 | } |
| 152 | row, ok := read("row") |
| 153 | if !ok { |
| 154 | return in, false |
| 155 | } |
| 156 | size, err := strconv.Atoi(r.FormValue("size")) |
| 157 | if err != nil { |
| 158 | return in, false |
| 159 | } |
| 160 | in.Col, in.Row, in.Size = col, row, size |
| 161 | return in, true |
| 162 | } |
| 163 | |
| 164 | // sizesResponse builds a payload carrying the tab's sizes and no cells. |
| 165 | // |
| 166 | // The WHOLE size table, not just the one that changed. It is one small |
| 167 | // map — only sized rows and columns appear in it — and sending all of |
| 168 | // it means the client applies rather than merges, so a tab that missed |
| 169 | // a frame while disconnected converges on the next one it sees instead |
| 170 | // of staying one resize behind forever. |
| 171 | func sizesResponse(h *store.Handle, client string) (gridResponse, error) { |
| 172 | cols, rows, err := h.Sizes() |
| 173 | if err != nil { |
| 174 | return gridResponse{}, err |
| 175 | } |
| 176 | return gridResponse{ |
| 177 | // Unchanged: a resize appends no event, so nothing has advanced |
| 178 | // the tab's version. Sending the current one rather than zero |
| 179 | // matters — a client discards a batch whose version trails what |
| 180 | // it has painted. |
| 181 | Version: h.Version(), |
| 182 | Cells: []gridCell{}, |
| 183 | Sizes: &wireSizes{Cols: intKeys(cols), Rows: intKeys(rows)}, |
| 184 | Client: client, |
| 185 | }, nil |
| 186 | } |
| 187 | |
| 188 | // wireSizes is a tab's column widths and row heights on the wire. |
| 189 | // |
| 190 | // Keyed by position rendered as a string, because JSON object keys are |
| 191 | // strings — the same shape and the same reason as the format table. |
| 192 | type wireSizes struct { |
| 193 | Cols map[string]int `json:"cols,omitempty"` |
| 194 | Rows map[string]int `json:"rows,omitempty"` |
| 195 | } |
| 196 | |
| 197 | func intKeys(m map[int]int) map[string]int { |
| 198 | if len(m) == 0 { |
| 199 | return nil |
| 200 | } |
| 201 | out := make(map[string]int, len(m)) |
| 202 | for k, v := range m { |
| 203 | // itoaPos, comments.go — a grid position rendered the same way |
| 204 | // the A1 references are, so a key means the same thing on every |
| 205 | // wire this app has. |
| 206 | out[itoaPos(k)] = v |
| 207 | } |
| 208 | return out |
| 209 | } |
| 210 | |