| 1 | /* phone.js: the document screen on a phone (design/phone-web, approved by |
| 2 | Paul 2026-11-28; slice P1). A classic deferred script, for everyone who |
| 3 | can see the document; nothing here is needed for the page to work: |
| 4 | without it the large title is the rename field and its button, ••• is a |
| 5 | <details> of links, and the bar never folds. |
| 6 | |
| 7 | - The fold: once the large title has scrolled away under the bar, the |
| 8 | bar shows its inline title (estilo-bar-folded on the compact bar). |
| 9 | An IntersectionObserver, not a scroll listener: nothing runs while |
| 10 | the page scrolls, and nothing here reads layout on a scroll. |
| 11 | - ••• runs the menu bar's commands: a row with data-docs-proxy clicks |
| 12 | the menu bar's item of the same command, so the bar stays the one |
| 13 | command table (menu.go), and mirrors its disabled and pressed state. |
| 14 | Rename turns the large title into its field. |
| 15 | - The large title renames in place: a tap on it shows the field; |
| 16 | Return renames (a fetch of the form's POST, so the page stays, and a |
| 17 | failure reopens the field and says so), Esc or Cancel puts it back, |
| 18 | and leaving a changed field renames. |
| 19 | - Aa (in the bar or on the quick bar) opens the Format sheet: the |
| 20 | inspector, which below 704px is a half-height bottom sheet (slice |
| 21 | P2). The editor takes the caret out of the document while it is open |
| 22 | and keeps the selection drawn (a docs:format-sheet event; see |
| 23 | fixtures/editor-entry.js). ×, Escape, a swipe down on the grabber or |
| 24 | Aa again closes it. |
| 25 | - The quick format bar rides the on-screen keyboard, 8px above it, on |
| 26 | visualViewport; where there is none it is a strip under the bar. |
| 27 | - While the keyboard is up the bar folds, and it is kept in the visual |
| 28 | viewport: iOS scrolls the visual viewport within the layout one, and |
| 29 | a sticky bar at the layout viewport's top was left above the screen. |
| 30 | - It marks the page (data-docs-phone on <html>), so on a phone the |
| 31 | corner toast gives way to the subtitle and the inline line, which the |
| 32 | editor drives itself (fixtures/editor-entry.js). |
| 33 | */ |
| 34 | (function () { |
| 35 | "use strict"; |
| 36 | var bar = document.querySelector('[estilo-bar~="compact"]'); |
| 37 | var more = document.querySelector("[docs-more]"); |
| 38 | var block = document.querySelector("[docs-title-block]"); |
| 39 | // The compact bar's own query (estilo chrome.css). |
| 40 | var compact = window.matchMedia && window.matchMedia("(max-width: 703.98px), (max-height: 480px) and (pointer: coarse)"); |
| 41 | |
| 42 | // The fold: folded once the large title has scrolled away, and while |
| 43 | // the on-screen keyboard is up (keyboardUp, below), which leaves the |
| 44 | // text only the room between the bar and the keyboard. |
| 45 | var away = false; |
| 46 | var keyboardUp = false; |
| 47 | var fold = function () { |
| 48 | if (!bar) return; |
| 49 | var shown = !!block && block.getClientRects().length > 0; |
| 50 | if (shown && (away || keyboardUp)) bar.setAttribute("estilo-bar-folded", ""); |
| 51 | else bar.removeAttribute("estilo-bar-folded"); |
| 52 | }; |
| 53 | if (bar && block && typeof IntersectionObserver === "function") { |
| 54 | var io = null; |
| 55 | var observe = function () { |
| 56 | if (io) io.disconnect(); |
| 57 | var h = Math.round(bar.getBoundingClientRect().height); |
| 58 | io = new IntersectionObserver(function (entries) { |
| 59 | away = !entries[entries.length - 1].isIntersecting; |
| 60 | fold(); |
| 61 | }, { rootMargin: "-" + h + "px 0px 0px 0px", threshold: 0 }); |
| 62 | io.observe(block); |
| 63 | }; |
| 64 | observe(); |
| 65 | if (compact && compact.addEventListener) compact.addEventListener("change", observe); |
| 66 | } |
| 67 | |
| 68 | // The large title, renamed in place. |
| 69 | var form = document.querySelector("[docs-title-form]"); |
| 70 | var input = form && form.querySelector("[docs-title-input]"); |
| 71 | var edit = form && form.querySelector("[docs-title-edit]"); |
| 72 | var startRename = function () {}; |
| 73 | if (form && input && edit) { |
| 74 | var cancel = form.querySelector("[docs-title-cancel]"); |
| 75 | var stop = function (refocus) { |
| 76 | form.removeAttribute("data-editing"); |
| 77 | if (refocus) edit.focus(); |
| 78 | }; |
| 79 | // Back to the title as it was. An emptied field is this too: a title |
| 80 | // cleared and left is never meant as "Untitled" (Fable on #179). |
| 81 | var putBack = function (refocus) { |
| 82 | input.value = input.defaultValue; |
| 83 | var said = form.querySelector("[docs-title-error]"); |
| 84 | if (said) said.hidden = true; |
| 85 | stop(refocus); |
| 86 | }; |
| 87 | var changed = function () { |
| 88 | var v = input.value.trim(); |
| 89 | return v !== "" && v !== input.defaultValue.trim(); |
| 90 | }; |
| 91 | // Renaming is a fetch of the form's own POST, asking for the answer a |
| 92 | // native client gets (Accept: application/json), so success is exactly |
| 93 | // the server's 204; the page never leaves (Fable on #179). The title |
| 94 | // changes at once, everywhere it is drawn, and goes back if the rename |
| 95 | // fails: refused, or no connection. Then the field opens again with |
| 96 | // what was typed and the hint's place says it could not rename. Only |
| 97 | // the newest rename's failure puts anything back: an older one that |
| 98 | // fails late is ignored. The server makes a title one line |
| 99 | // (handlers.go's cleanTitle), and so does this, to show what it keeps. |
| 100 | var barTitle = document.querySelector("[estilo-bar-title-input]"); |
| 101 | var inlineTitle = document.querySelector("[docs-inline-title] > b"); |
| 102 | var text = form.querySelector("[docs-title-text]"); |
| 103 | var failed = form.querySelector("[docs-title-error]"); |
| 104 | var shown = input.defaultValue; |
| 105 | var sequence = 0; |
| 106 | var show = function (title) { |
| 107 | input.defaultValue = title; |
| 108 | if (text) text.textContent = title; |
| 109 | if (inlineTitle) inlineTitle.textContent = title; |
| 110 | if (barTitle) barTitle.value = barTitle.defaultValue = title; |
| 111 | if (shown && document.title.indexOf(shown) === 0) document.title = title + document.title.slice(shown.length); |
| 112 | shown = title; |
| 113 | }; |
| 114 | var rename = function () { |
| 115 | var typed = input.value; |
| 116 | var title = typed.replace(/[\s\u0000-\u001f\u007f-\u009f\u2028\u2029]+/g, " ").trim(); |
| 117 | var before = input.defaultValue; |
| 118 | var body = new URLSearchParams(new FormData(form)); |
| 119 | var mine = ++sequence; |
| 120 | if (failed) failed.hidden = true; |
| 121 | show(title); |
| 122 | input.value = title; |
| 123 | stop(false); |
| 124 | fetch(form.action, { method: "POST", body: body, credentials: "same-origin", headers: { Accept: "application/json" } }) |
| 125 | .then(function (r) { if (r.status !== 204) throw new Error("refused"); }) |
| 126 | .catch(function () { |
| 127 | if (mine !== sequence) return; |
| 128 | show(before); |
| 129 | input.value = typed; |
| 130 | if (failed) failed.hidden = false; |
| 131 | form.setAttribute("data-editing", ""); |
| 132 | input.focus(); |
| 133 | }); |
| 134 | }; |
| 135 | startRename = function () { |
| 136 | form.setAttribute("data-editing", ""); |
| 137 | input.focus(); |
| 138 | input.select(); |
| 139 | }; |
| 140 | edit.addEventListener("click", startRename); |
| 141 | input.addEventListener("keydown", function (e) { |
| 142 | if (e.key === "Enter") { |
| 143 | e.preventDefault(); |
| 144 | if (changed()) rename(); |
| 145 | else putBack(true); |
| 146 | } else if (e.key === "Escape") { |
| 147 | e.preventDefault(); |
| 148 | putBack(true); |
| 149 | } |
| 150 | }); |
| 151 | input.addEventListener("blur", function () { |
| 152 | if (!form.hasAttribute("data-editing")) return; |
| 153 | if (changed()) rename(); |
| 154 | else putBack(false); |
| 155 | }); |
| 156 | if (cancel) { |
| 157 | // Pressing Cancel would take focus from the field first, and leaving |
| 158 | // a changed field renames; so the press keeps focus in the field, |
| 159 | // and the click puts the title back. |
| 160 | cancel.addEventListener("pointerdown", function (e) { e.preventDefault(); }); |
| 161 | cancel.addEventListener("mousedown", function (e) { e.preventDefault(); }); |
| 162 | cancel.addEventListener("click", function () { putBack(true); }); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // •••. |
| 167 | if (more) { |
| 168 | var summary = more.querySelector("summary"); |
| 169 | var close = function () { more.open = false; }; |
| 170 | var target = function (row) { |
| 171 | return document.querySelector('[estilo-menubar] [data-docs-command="' + row.getAttribute("data-docs-proxy") + '"]'); |
| 172 | }; |
| 173 | var mirror = function (row, item) { |
| 174 | if (row.disabled !== item.disabled) row.disabled = item.disabled; |
| 175 | if (row.hasAttribute("aria-pressed")) { |
| 176 | var p = item.getAttribute("aria-pressed"); |
| 177 | if (p && row.getAttribute("aria-pressed") !== p) row.setAttribute("aria-pressed", p); |
| 178 | } |
| 179 | }; |
| 180 | Array.prototype.forEach.call(more.querySelectorAll("[data-docs-proxy]"), function (row) { |
| 181 | var item = target(row); |
| 182 | if (!item) return; |
| 183 | mirror(row, item); |
| 184 | new MutationObserver(function () { mirror(row, item); }).observe(item, { attributes: true, attributeFilter: ["disabled", "aria-pressed"] }); |
| 185 | }); |
| 186 | more.addEventListener("click", function (e) { |
| 187 | var rename = e.target.closest("[data-docs-rename]"); |
| 188 | if (rename && form) { |
| 189 | e.preventDefault(); |
| 190 | close(); |
| 191 | startRename(); |
| 192 | return; |
| 193 | } |
| 194 | var row = e.target.closest("[data-docs-proxy]"); |
| 195 | if (!row || row.disabled) return; |
| 196 | var item = target(row); |
| 197 | if (!item) return; |
| 198 | e.preventDefault(); |
| 199 | close(); |
| 200 | item.click(); |
| 201 | // Focus comes back to •••, where the person was: the menu bar's item |
| 202 | // the command ran from is not drawn on a phone, so focus would fall |
| 203 | // to the page's top. A command that opened a dialog (word count, the |
| 204 | // shortcuts sheet) brings it back when the dialog closes. |
| 205 | var dialog = document.querySelector("dialog[open]"); |
| 206 | if (dialog && summary) dialog.addEventListener("close", function () { summary.focus(); }, { once: true }); |
| 207 | else if (summary) summary.focus(); |
| 208 | }); |
| 209 | } |
| 210 | |
| 211 | // The Format sheet. Open is data-docs-sheet="open" on <html>, which |
| 212 | // the page's stylesheet draws as the sheet below 704px; every Aa says |
| 213 | // so in aria-expanded. The editor is told, so it can take the caret |
| 214 | // out of the document and keep the selection drawn. |
| 215 | var root = document.documentElement; |
| 216 | var sheet = document.querySelector("[docs-inspector]"); |
| 217 | var aas = Array.prototype.slice.call(document.querySelectorAll("[docs-aa], [docs-quickbar-aa]")); |
| 218 | var sheetOpen = function () { return root.getAttribute("data-docs-sheet") === "open"; }; |
| 219 | var openedFrom = null; |
| 220 | var setSheet = function (open, from) { |
| 221 | if (open === sheetOpen()) return; |
| 222 | if (open) root.setAttribute("data-docs-sheet", "open"); |
| 223 | else root.removeAttribute("data-docs-sheet"); |
| 224 | aas.forEach(function (a) { a.setAttribute("aria-expanded", open ? "true" : "false"); }); |
| 225 | var height = open && sheet ? sheet.getBoundingClientRect().height : 0; |
| 226 | document.dispatchEvent(new CustomEvent("docs:format-sheet", { detail: { open: open, height: height } })); |
| 227 | place(); |
| 228 | if (open) { |
| 229 | openedFrom = from || null; |
| 230 | // from is the Aa a keyboard opened it from (Enter or Space, or an |
| 231 | // assistive technology's click; see below): focus goes to the |
| 232 | // sheet's title, which names it, and Tab goes on to its controls. |
| 233 | // A tap leaves focus where it was. |
| 234 | var title = from && sheet && sheet.querySelector("[docs-sheet-title]"); |
| 235 | if (title) title.focus(); |
| 236 | } else if (openedFrom && sheet && sheet.contains(document.activeElement)) { |
| 237 | openedFrom.focus(); |
| 238 | } |
| 239 | }; |
| 240 | if (sheet) { |
| 241 | aas.forEach(function (a) { |
| 242 | a.disabled = false; |
| 243 | a.addEventListener("mousedown", function (e) { e.preventDefault(); }); |
| 244 | // A tap or a click is a pointer on Aa just before; anything else |
| 245 | // (Enter or Space, whose click has detail 0, or an assistive |
| 246 | // technology's click, whose detail need not be) is a keyboard's, |
| 247 | // and takes focus into the sheet. |
| 248 | var pointedAt = 0; |
| 249 | a.addEventListener("pointerdown", function () { pointedAt = Date.now(); }); |
| 250 | a.addEventListener("click", function (e) { |
| 251 | var byPointer = e.detail > 0 && Date.now() - pointedAt < 1500; |
| 252 | pointedAt = 0; |
| 253 | setSheet(!sheetOpen(), byPointer ? null : a); |
| 254 | }); |
| 255 | }); |
| 256 | var closeButton = sheet.querySelector("[docs-sheet-close]"); |
| 257 | if (closeButton) closeButton.addEventListener("click", function () { setSheet(false); }); |
| 258 | sheet.addEventListener("keydown", function (e) { |
| 259 | if (e.key === "Escape" && sheetOpen() && !e.defaultPrevented) { e.preventDefault(); setSheet(false); } |
| 260 | }); |
| 261 | // Escape anywhere closes it, unless something nearer has used the |
| 262 | // key: a picker's list says so with preventDefault; a modal dialog |
| 263 | // closes itself on Escape without saying so, so Escape from inside |
| 264 | // one, or while one is open, is the dialog's. |
| 265 | document.addEventListener("keydown", function (e) { |
| 266 | if (e.key !== "Escape" || !sheetOpen() || e.defaultPrevented) return; |
| 267 | var t = e.target; |
| 268 | if ((t && t.closest && t.closest("dialog")) || document.querySelector("dialog[open]")) return; |
| 269 | setSheet(false); |
| 270 | }); |
| 271 | // A swipe down on the grabber or the title closes it, when the |
| 272 | // finger lifts: a sheet that left under a finger still down would |
| 273 | // hand the rest of the touch to the page (on iOS, a tap in the |
| 274 | // document, and the keyboard). |
| 275 | var head = sheet.querySelector("[docs-sheet-head]"); |
| 276 | if (head) { |
| 277 | var startY = null; |
| 278 | head.addEventListener("pointerdown", function (e) { startY = e.clientY; }); |
| 279 | head.addEventListener("pointerup", function (e) { |
| 280 | if (startY !== null && e.clientY - startY > 60) setSheet(false); |
| 281 | startY = null; |
| 282 | }); |
| 283 | head.addEventListener("pointercancel", function () { startY = null; }); |
| 284 | } |
| 285 | // The editor closes it when what it acts on is gone (after a resync |
| 286 | // that found the kept words nowhere). |
| 287 | document.addEventListener("docs:format-sheet-close", function () { setSheet(false); }); |
| 288 | // From 704px there is no sheet to keep open. |
| 289 | if (compact && compact.addEventListener) compact.addEventListener("change", function () { if (!compact.matches) setSheet(false); }); |
| 290 | } |
| 291 | |
| 292 | // The quick bar, and the bar kept in view, on visualViewport. The |
| 293 | // keyboard is "up" while the document has the caret and the visual |
| 294 | // viewport is shorter than the layout one by more than 150px (a |
| 295 | // hardware keyboard raises none; its ⌘B and ⌘I work as ever). |
| 296 | var quick = document.querySelector("[docs-quickbar]"); |
| 297 | var editor = document.querySelector("[docs-editor]"); |
| 298 | var vv = window.visualViewport; |
| 299 | var hide = quick && quick.querySelector("[docs-quickbar-hide]"); |
| 300 | if (hide) { |
| 301 | hide.disabled = false; |
| 302 | hide.addEventListener("mousedown", function (e) { e.preventDefault(); }); |
| 303 | hide.addEventListener("click", function () { |
| 304 | var a = document.activeElement; |
| 305 | if (a && editor && editor.contains(a)) a.blur(); |
| 306 | }); |
| 307 | } |
| 308 | var lastHeight = -1, capsuleHeight = 0; |
| 309 | // The layout viewport's tallest height at this width: a keyboard that |
| 310 | // shrinks the layout viewport itself (interactive-widget= |
| 311 | // resizes-content, Firefox for Android) leaves the visual viewport as |
| 312 | // tall as it, and shows only as the layout viewport falling short of |
| 313 | // this. Keyed on the layout viewport's width (clientWidth), not |
| 314 | // innerWidth, which Mobile Safari long reported as the visual |
| 315 | // viewport's, so a pinch-zoom would have reset it. The edges: |
| 316 | // - A turn of the phone, or an iPad window resized, changes the |
| 317 | // width and starts over: the first height seen at the new width |
| 318 | // is the tallest until a taller one comes. |
| 319 | // - A page that loads with a keyboard already up (a reload while |
| 320 | // typing) starts from the short height, so the strip shows only |
| 321 | // once the keyboard has been down at this width. |
| 322 | // - A browser toolbar that hides on scroll changes the height by |
| 323 | // well under 150px, so it is never taken for a keyboard. |
| 324 | var tallest = 0, tallestAt = -1; |
| 325 | function place() { |
| 326 | var phone = !compact || compact.matches; |
| 327 | var focused = !!editor && editor.contains(document.activeElement); |
| 328 | var layout = document.documentElement.clientHeight; |
| 329 | var width = document.documentElement.clientWidth; |
| 330 | if (width !== tallestAt) { tallestAt = width; tallest = 0; } |
| 331 | if (layout > tallest) tallest = layout; |
| 332 | // In layout pixels (height times scale), so a pinch-zoom, which |
| 333 | // shrinks the visual viewport too, is not a keyboard. |
| 334 | var up = phone && focused && !!vv && vv.height * (vv.scale || 1) < layout - 150; |
| 335 | var shrunk = phone && focused && !up && tallest - layout > 150; |
| 336 | if (up !== keyboardUp) { keyboardUp = up; fold(); } |
| 337 | // The bar stays at the top of what can be seen while the keyboard |
| 338 | // is up. Pinch-zoomed (scale not 1), the page is being looked at, |
| 339 | // not written in, and the bar stays where the page has it. |
| 340 | if (bar) { |
| 341 | var zoomed = !!vv && Math.abs((vv.scale || 1) - 1) > 0.01; |
| 342 | var off = up && !zoomed ? Math.max(0, vv.offsetTop) : 0; |
| 343 | var t = off > 0.5 ? "translateY(" + off + "px)" : ""; |
| 344 | if (bar.style.transform !== t) bar.style.transform = t; |
| 345 | } |
| 346 | if (!quick) return; |
| 347 | var mode = ""; |
| 348 | if (phone && focused && !sheetOpen()) mode = vv && !shrunk ? (up ? "keyboard" : "") : "strip"; |
| 349 | if ((quick.getAttribute("data-shown") || "") !== mode) { |
| 350 | if (mode) quick.setAttribute("data-shown", mode); |
| 351 | else quick.removeAttribute("data-shown"); |
| 352 | } |
| 353 | // Measured from the top, as the bar is: Mobile Safari's innerHeight |
| 354 | // can read short of the layout viewport once a short document is |
| 355 | // scrolled with the keyboard up, so a distance from the bottom lands |
| 356 | // the capsule behind its accessory bar. |
| 357 | if (mode === "keyboard") { |
| 358 | // Read once, the first time it shows: the stylesheet fixes its |
| 359 | // height, and a read per event would force a layout on every |
| 360 | // visual viewport scroll. |
| 361 | if (!capsuleHeight) capsuleHeight = quick.offsetHeight || 48; |
| 362 | var top = vv.offsetTop + vv.height - capsuleHeight - 8; |
| 363 | var at = Math.round(top) + "px"; |
| 364 | if (quick.style.insetBlockStart !== at) quick.style.insetBlockStart = at; |
| 365 | if (quick.style.insetBlockEnd !== "auto") quick.style.insetBlockEnd = "auto"; |
| 366 | } else if (quick.style.insetBlockStart) { |
| 367 | quick.style.insetBlockStart = ""; |
| 368 | quick.style.insetBlockEnd = ""; |
| 369 | } |
| 370 | var h = mode === "keyboard" ? capsuleHeight : 0; |
| 371 | if (h !== lastHeight) { |
| 372 | lastHeight = h; |
| 373 | document.dispatchEvent(new CustomEvent("docs:quickbar", { detail: { height: h } })); |
| 374 | } |
| 375 | } |
| 376 | if (vv) { |
| 377 | vv.addEventListener("resize", place); |
| 378 | vv.addEventListener("scroll", place); |
| 379 | } |
| 380 | if (editor) { |
| 381 | // A tap in the document while the sheet is open is a return to |
| 382 | // writing: the sheet goes, so the keyboard and the capsule come up |
| 383 | // and the kept selection gives way to the live one. |
| 384 | editor.addEventListener("focusin", function () { |
| 385 | if (sheetOpen()) setSheet(false); |
| 386 | place(); |
| 387 | }); |
| 388 | editor.addEventListener("focusout", function () { setTimeout(place, 0); }); |
| 389 | } |
| 390 | if (compact && compact.addEventListener) compact.addEventListener("change", place); |
| 391 | place(); |
| 392 | |
| 393 | // This script is here, so the phone's own lines (the subtitle at rest, |
| 394 | // the folded bar's inline line) can say the save state, which the |
| 395 | // editor drives itself; only now may the corner toast give way to them |
| 396 | // on a phone (views/show.html). Without this script the bar never folds, |
| 397 | // and the toast keeps saying a failure. |
| 398 | document.documentElement.setAttribute("data-docs-phone", ""); |
| 399 | })(); |
| 400 | |