oficina / docs Public

Rename the large title in place: a fetch, so the page never leaves mid-save

Fable on #179 (minor): a blur rename was the form's full-page POST, so
tapping back into the text reloaded the page, and iOS does not reliably
send what is pending as a page goes, so words typed just before could be
lost. With script the rename is now a fetch of the same POST (same
origin, so the same CSRF checks), and the title changes in place: the
large title, the folded bar's inline title, the bar's own field and the
tab's title. A refusal, or no answer, falls back to the form's ordinary
POST; with no script the form posts as before.

TestThePhoneDocumentScreen types, renames at once, and asserts the page
stayed and both the words and the new title survive a reload; before this
the page navigated away.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 9f03f5fc90fe9da3cafe569cdbe78a7317754480 parent c903a8e
2 files changed, +52 −4
  • internal/docs/brand/phone.js +28 −4
  • internal/docs/phone_browser_test.go +24 −0
diff --git a/internal/docs/brand/phone.js b/internal/docs/brand/phone.js
index 7ac430b..a9224c1 100644
--- a/internal/docs/brand/phone.js
+++ b/internal/docs/brand/phone.js
@@ -13,8 +13,8 @@
command table (menu.go), and mirrors its disabled and pressed state.
Rename turns the large title into its field.
- The large title renames in place: a tap on it shows the field;
- Return renames through the form's POST, Esc puts it back, and
- leaving a changed field renames, as the bar's title does.
+ Return renames (a fetch of the form's POST, so the page stays), Esc
+ or Cancel puts it back, and leaving a changed field renames.
- Aa shows the formatting strip under the bar (until slice P2 makes it
the Format sheet).
- It marks the page (data-docs-phone on <html>), so on a phone the
@@ -68,6 +68,30 @@
var v = input.value.trim();
return v !== "" && v !== input.defaultValue.trim();
};
+ // Renaming is a fetch of the form's own POST, and the title changes in
+ // place: the page never leaves while the editor may still be saving
+ // what was just typed (Fable on #179; iOS does not reliably send what
+ // is pending as a page goes). The server makes a title one line
+ // (handlers.go's cleanTitle), and so does this, to show what it keeps.
+ // A refusal, or no answer, falls back to the form's ordinary POST.
+ var barTitle = document.querySelector("[estilo-bar-title-input]");
+ var inlineTitle = document.querySelector("[docs-inline-title] > b");
+ var text = form.querySelector("[docs-title-text]");
+ var rename = function () {
+ var title = input.value.replace(/[\s\u0000-\u001f\u007f-\u009f\u2028\u2029]+/g, " ").trim();
+ var old = input.defaultValue;
+ stop(false);
+ fetch(form.action, { method: "POST", body: new URLSearchParams(new FormData(form)), credentials: "same-origin", redirect: "manual" })
+ .then(function (r) {
+ if (r.type !== "opaqueredirect" && !r.ok) throw new Error("refused");
+ input.value = input.defaultValue = title;
+ if (text) text.textContent = title;
+ if (inlineTitle) inlineTitle.textContent = title;
+ if (barTitle) barTitle.value = barTitle.defaultValue = title;
+ if (old && document.title.indexOf(old) === 0) document.title = title + document.title.slice(old.length);
+ })
+ .catch(function () { form.submit(); });
+ };
startRename = function () {
form.setAttribute("data-editing", "");
input.focus();
@@ -77,7 +101,7 @@
input.addEventListener("keydown", function (e) {
if (e.key === "Enter") {
e.preventDefault();
- if (changed()) form.requestSubmit();
+ if (changed()) rename();
else putBack(true);
} else if (e.key === "Escape") {
e.preventDefault();
@@ -86,7 +110,7 @@
});
input.addEventListener("blur", function () {
if (!form.hasAttribute("data-editing")) return;
- if (changed()) form.requestSubmit();
+ if (changed()) rename();
else putBack(false);
});
if (cancel) {
diff --git a/internal/docs/phone_browser_test.go b/internal/docs/phone_browser_test.go
index 2d99af2..8009df6 100644
--- a/internal/docs/phone_browser_test.go
+++ b/internal/docs/phone_browser_test.go
@@ -190,6 +190,30 @@ func TestThePhoneDocumentScreen(t *testing.T) {
t.Errorf("after clearing the field and leaving it: editing %v, the field %q, the title %q, still the same page %v; want it put back", r.Editing, r.Value, r.Title, r.Stayed)
}
+ // Typing, then renaming at once: the rename is a fetch that changes the
+ // title in place, so the page never leaves while an edit is still
+ // being saved (Fable on #179: a blur rename was a full-page POST, and
+ // iOS does not reliably send what is pending as a page goes). The
+ // words and the new title are both there after a reload.
+ var kept struct {
+ Stayed bool
+ Title, Inline string
+ }
+ run("type, then rename", chromedp.Evaluate[chromedp.Void](`window.__stayed = true; (() => { const el = document.querySelector('[docs-text]'); el.focus(); document.execCommand('insertText', false, 'zebra crossing'); return true })()`),
+ chromedp.Click(chromedp.CSS(`[docs-title-edit]`)),
+ chromedp.Evaluate[chromedp.Void](`(() => { const i = document.querySelector('[docs-title-input]'); i.value = 'Renamed at once'; i.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })); return true })()`),
+ chromedp.Sleep(1500*time.Millisecond),
+ evalInto(&kept, `({ Stayed: window.__stayed === true, Title: document.querySelector('[docs-title-text]').textContent, Inline: document.querySelector('[docs-inline-title] b').textContent })`))
+ if !kept.Stayed || kept.Title != "Renamed at once" || kept.Inline != "Renamed at once" {
+ t.Errorf("type then rename: same page %v, title %q, inline title %q; want it renamed in place", kept.Stayed, kept.Title, kept.Inline)
+ }
+ var after struct{ Text, Title string }
+ run("reload", chromedp.Reload(), chromedp.WaitVisible(chromedp.CSS(`[contenteditable="true"]`)), settle,
+ evalInto(&after, `({ Text: document.querySelector('[docs-text]').textContent, Title: document.querySelector('[docs-title-text]').textContent })`))
+ if !strings.Contains(after.Text, "zebra crossing") || after.Title != "Renamed at once" {
+ t.Errorf("after a reload: the text %q, the title %q; an edit or the rename was lost", after.Text, after.Title)
+ }
+
// Aa shows the strip.
run("Aa", chromedp.Click(chromedp.CSS(`[docs-aa]`)), settle)
if p = probe("Aa"); !p.Strip {