| 1 | /* Typing on a selected cell replaces it, from the first key. |
| 2 | * |
| 3 | * Paul, 2026-11-26: "the first character typed focusses the cell and |
| 4 | * doesn't actually type into it." Two causes, both fixed here: |
| 5 | * |
| 6 | * - grid.js filled the in-cell editor with the cell's formula a frame |
| 7 | * after it opened. Opened by typing, that overwrote the typed key |
| 8 | * with the old contents and selected them, so the next key replaced |
| 9 | * everything. The fill now happens only for an edit that keeps the |
| 10 | * contents (Enter, F2, double-click): `replacing` says which. |
| 11 | * |
| 12 | * - A browser composes text (´ then e makes é; an input method's かな) |
| 13 | * only inside a focused text control, and the grid is not one. The |
| 14 | * library ignores a dead key ("Dead"), and with nothing editable |
| 15 | * focused an input method never engages, so the first key arrives as a |
| 16 | * bare letter and "ka" becomes "kあ". So while a cell is selected a |
| 17 | * text field with no on-screen keyboard (inputmode=none) holds focus on |
| 18 | * it; the key that starts typing opens the cell's editor in the same |
| 19 | * keydown, before the browser places the text or composition, and the |
| 20 | * editor receives it. If a browser composes into the holder anyway, |
| 21 | * what it composed moves into the editor when the composition ends. |
| 22 | */ |
| 23 | (function () { |
| 24 | 'use strict'; |
| 25 | |
| 26 | var surface = document.querySelector('[data-sheet-surface]'); |
| 27 | if (!surface || surface.dataset.mayWrite !== '1') return; |
| 28 | |
| 29 | var state = { replacing: false }; |
| 30 | window.SheetsTypeStart = state; |
| 31 | |
| 32 | var holder = document.createElement('textarea'); |
| 33 | holder.setAttribute('data-type-holder', ''); |
| 34 | holder.setAttribute('inputmode', 'none'); |
| 35 | holder.setAttribute('autocomplete', 'off'); |
| 36 | holder.setAttribute('autocapitalize', 'off'); |
| 37 | holder.setAttribute('spellcheck', 'false'); |
| 38 | holder.setAttribute('aria-label', 'Selected cell'); |
| 39 | holder.tabIndex = -1; |
| 40 | |
| 41 | function grid() { return window.SheetsGrid; } |
| 42 | function idle() { |
| 43 | var g = grid(); |
| 44 | var ws = g && g.worksheet(); |
| 45 | return !!(ws && !ws.edition && g.selection()); |
| 46 | } |
| 47 | function cellAt(sel) { |
| 48 | var ws = grid().worksheet(); |
| 49 | var row = ws && ws.records && ws.records[sel.r]; |
| 50 | return row && row[sel.c] ? row[sel.c].element : null; |
| 51 | } |
| 52 | |
| 53 | /* Where the keyboard is when nobody else has it: the page itself, the |
| 54 | * grid, or the holder. Never the formula bar, a menu or a dialog. */ |
| 55 | function unclaimed(el) { |
| 56 | return !el || el === document.body || el === holder || |
| 57 | (el.closest && el.closest('[data-sheet-grid]') && !el.closest('td.editor')); |
| 58 | } |
| 59 | |
| 60 | /* A key that is text, or the start of some: a printable key (Option and |
| 61 | * AltGr characters included), a dead key, or an input method's key. */ |
| 62 | function startsText(e) { |
| 63 | if (e.isComposing) return false; |
| 64 | if (e.key === 'Dead' || e.key === 'Process' || e.keyCode === 229) return true; |
| 65 | if (!e.key || e.key.length !== 1) return false; |
| 66 | if (e.metaKey) return false; |
| 67 | return !e.ctrlKey || e.altKey; /* Ctrl+Alt is AltGr on Windows */ |
| 68 | } |
| 69 | |
| 70 | /* The library drives an open editor (Enter, Escape, the arrows) only for |
| 71 | * its "current" sheet, and a click anywhere outside the table (Bold, a |
| 72 | * menu, the formula bar) sets that to none. Whenever the holder has the |
| 73 | * keyboard, the grid is current again; without this an editor opened by |
| 74 | * typing ignored Enter and Escape and was saved by the next click. The |
| 75 | * same click clears the library's own selection while ours stands, and |
| 76 | * then Enter saved without moving on (the next value overwrote the cell) |
| 77 | * and the arrows and Delete did nothing: put that back too. */ |
| 78 | function own() { |
| 79 | var ws = grid() && grid().worksheet(); |
| 80 | var s = grid() && grid().selection(); |
| 81 | if (!ws || !window.jspreadsheet) return; |
| 82 | window.jspreadsheet.current = ws; |
| 83 | if (!ws.selectedCell && s && ws.updateSelectionFromCoords) ws.updateSelectionFromCoords(s.c, s.r, s.c2, s.r2); |
| 84 | } |
| 85 | |
| 86 | function open(sel) { |
| 87 | var td = cellAt(sel); |
| 88 | var ws = grid().worksheet(); |
| 89 | if (!td || !ws.openEditor) return null; |
| 90 | own(); |
| 91 | state.replacing = true; |
| 92 | try { ws.openEditor(td, true); } finally { state.replacing = false; } |
| 93 | return td.querySelector('input, textarea'); |
| 94 | } |
| 95 | |
| 96 | /* Capture phase: ahead of the library's own document handler, which |
| 97 | * then finds an edit already open and leaves the key to the editor. */ |
| 98 | document.addEventListener('keydown', function (e) { |
| 99 | var g = grid(); |
| 100 | var ws = g && g.worksheet(); |
| 101 | /* Enter, Tab or Escape closing an edit: WebKit sends no focusout when |
| 102 | * the library removes the editor, so take the keyboard back here too. */ |
| 103 | if (ws && ws.edition && (e.key === 'Enter' || e.key === 'Tab' || e.key === 'Escape')) claim(); |
| 104 | if (!g || !idle() || !unclaimed(document.activeElement)) return; |
| 105 | if (document.activeElement === holder) own(); |
| 106 | if (startsText(e)) open(g.selection()); |
| 107 | }, true); |
| 108 | |
| 109 | /* A composition that landed in the holder after all moves to the cell. */ |
| 110 | function rescue() { |
| 111 | var text = holder.value; |
| 112 | holder.value = ''; |
| 113 | if (!text || !idle()) return; |
| 114 | var input = open(grid().selection()); |
| 115 | if (!input) return; |
| 116 | input.value = text; |
| 117 | input.setSelectionRange(text.length, text.length); |
| 118 | } |
| 119 | holder.addEventListener('input', function (e) { if (!e.isComposing) rescue(); }); |
| 120 | holder.addEventListener('compositionend', function () { setTimeout(rescue, 0); }); |
| 121 | |
| 122 | function colName(c) { |
| 123 | var s = ''; |
| 124 | for (c += 1; c > 0; c = Math.floor((c - 1) / 26)) s = String.fromCharCode(65 + (c - 1) % 26) + s; |
| 125 | return s; |
| 126 | } |
| 127 | |
| 128 | /* Over the cell, so an input method's candidates appear beside it, and |
| 129 | * named like it, so a screen reader still hears which cell this is. */ |
| 130 | function place(sel) { |
| 131 | var td = sel && cellAt(sel); |
| 132 | if (!td) return; |
| 133 | var box = td.getBoundingClientRect(); |
| 134 | holder.style.left = Math.round(box.left) + 'px'; |
| 135 | holder.style.top = Math.round(box.top) + 'px'; |
| 136 | var ref = colName(sel.c) + (sel.r + 1); |
| 137 | holder.setAttribute('aria-label', td.textContent ? ref + ', ' + td.textContent : ref); |
| 138 | } |
| 139 | |
| 140 | /* After the browser's own focus move for the click that selected. */ |
| 141 | function claim() { |
| 142 | setTimeout(function () { |
| 143 | if (!idle() || !unclaimed(document.activeElement)) return; |
| 144 | place(grid().selection()); |
| 145 | holder.focus({ preventScroll: true }); |
| 146 | }, 0); |
| 147 | } |
| 148 | |
| 149 | function ready() { |
| 150 | var g = grid(); |
| 151 | if (!g || !g.mayWrite) return; |
| 152 | surface.appendChild(holder); |
| 153 | holder.addEventListener('focus', own); |
| 154 | g.onSelection(claim); |
| 155 | /* Every "back to the grid" (after a menu item, a paste, the formula |
| 156 | * bar) lands on the holder, so the next key composes from the first. */ |
| 157 | var focusHost = g.focus; |
| 158 | g.focus = function () { |
| 159 | if (!idle()) return focusHost(); |
| 160 | place(g.selection()); |
| 161 | holder.focus({ preventScroll: true }); |
| 162 | }; |
| 163 | /* An edit closed (Enter, Tab, Escape, a click away): the page has the |
| 164 | * keyboard again, so the holder takes it back. */ |
| 165 | document.addEventListener('focusout', function (e) { |
| 166 | if (e.target && e.target.closest && e.target.closest('td.editor')) claim(); |
| 167 | }); |
| 168 | } |
| 169 | |
| 170 | if (window.SheetsGrid) ready(); |
| 171 | else document.addEventListener('sheets:ready', ready, { once: true }); |
| 172 | }()); |
| 173 | |