oficina / mapa Public

Rename answers JSON for the in-page title, and carries the draft with it

POST /p/{id}/rename and /p/{id}/links answer JSON when asked, as Make
team page and Resolve do: the title, the revisions the rename was made
over and minted, and the confirm page's honest count; a refusal is the
form's sentence as 422 {error, taken}. A rename moves every draft based
on the revision it was made over to the one it minted (they hold the
same body), so a draft's next commit is Saved, not a spurious merge.
A team page the editor mounts on marks its H1 renamable; daily notes,
scratch pages, History, the saved version and the Markdown editor do
not. Gate 1 compares the JSON answers too.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 425b4637e57023854e3109612351c1cd0f336dcf parent 1e92f29
9 files changed, +290 −11
  • internal/mapa/handlers_names.go +12 −1
  • internal/mapa/handlers_pages.go +12 −1
  • internal/mapa/handlers_title.go +56 −0
  • internal/mapa/templates/page.html +1 −1
  • internal/mapa/templates/partials.html +8 −3
  • internal/mapa/verbs.go +38 −4
  • internal/mapatest/gate1_test.go +8 −0
  • internal/mapatest/pages_test.go +1 −1
  • internal/mapatest/title_test.go +154 −0
diff --git a/internal/mapa/handlers_names.go b/internal/mapa/handlers_names.go
index 67b0685..8f60f77 100644
--- a/internal/mapa/handlers_names.go
+++ b/internal/mapa/handlers_names.go
@@ -52,7 +52,12 @@ func (app *app) rename(writer http.ResponseWriter, request *http.Request) {
}
address, _ := app.identity(request)
title := request.PostFormValue("title")
- err := app.store.Rename(request.Context(), address, page.ID, title)
+ done, err := app.store.Retitle(request.Context(), address, page.ID,
+ title)
+ if wantsJSON(request) {
+ app.renamedJSON(writer, request, page, done, err)
+ return
+ }
if sayable(err) {
app.showForm(writer, request, http.StatusUnprocessableEntity,
"rename", pageForm{Page: page, Title: title, Label: "Rename",
@@ -120,6 +125,12 @@ func (app *app) updateLinks(writer http.ResponseWriter,
app.refuse(writer, request, err)
return
}
+ if wantsJSON(request) {
+ answer(writer, map[string]any{"changed": changed,
+ "notice": "Updated links in " +
+ pagesCount(strconv.Itoa(changed)) + "."})
+ return
+ }
seeOther(writer, request, pageHref(page.Public, "links")+"&n="+
strconv.Itoa(changed))
}
diff --git a/internal/mapa/handlers_pages.go b/internal/mapa/handlers_pages.go
index 596ffee..497cca1 100644
--- a/internal/mapa/handlers_pages.go
+++ b/internal/mapa/handlers_pages.go
@@ -92,7 +92,8 @@ func (app *app) showPage(writer http.ResponseWriter,
app.fail(writer, request, err)
return
}
- data["Editing"] = editing
+ data["Editing"], data["Rename"] = editing, renameIn(request, page,
+ state.Island != "")
if last, ok, err := app.store.LastEditOf(request.Context(), viewer,
page.ID); err == nil && ok {
data["Last"] = &last
@@ -103,6 +104,16 @@ func (app *app) showPage(writer http.ResponseWriter,
Writable: state.Island != ""}})
}
+// renameIn is the rename POST the page's in-page title sends (level-up
+// §4.1), or "": only a team page, only where the editor mounts (its
+// script makes the title editable), and never on the history routes.
+func renameIn(request *http.Request, page PageView, editor bool) string {
+ if page.Kind != KindPage || !editor || historyIn(request) != nil {
+ return ""
+ }
+ return pageHref(page.Public, "") + "/rename"
+}
+
// navFor is the rail entry a page belongs under.
func navFor(kind string) string {
switch kind {
diff --git a/internal/mapa/handlers_title.go b/internal/mapa/handlers_title.go
new file mode 100644
index 0000000..b6abdeb
--- /dev/null
+++ b/internal/mapa/handlers_title.go
@@ -0,0 +1,56 @@
+package mapa
+
+import (
+ "errors"
+ "net/http"
+ "strconv"
+)
+
+// The in-page title's answers (level-up §4.1, build note 77). The H1
+// and the header's title field rename through the same POST
+// /p/{id}/rename as the form, asking for JSON; the form's redirect to
+// the Update links confirm page is unchanged, and stays the floor.
+
+// renamedJSON answers a rename the script sent. A refusal is 422
+// {error, taken}, the form's own sentence and the page holding the
+// title, if one does. A rename is {title, href, from, to, links, count,
+// aliases}: from and to are the revisions it was made over and minted
+// ("0" both when the title was already the page's), links and count the
+// confirm page's honest count of pages the viewer may edit that link by
+// an old title (base §3.4), and aliases the page's old titles now.
+func (app *app) renamedJSON(writer http.ResponseWriter,
+ request *http.Request, page PageView, done Renamed, err error) {
+ if sayable(err) {
+ writer.Header().Set("Content-Type",
+ "application/json; charset=utf-8")
+ writer.WriteHeader(http.StatusUnprocessableEntity)
+ answer(writer, map[string]any{"error": err.Error(),
+ "taken": app.index.TitleHref(request.Context(),
+ request.PostFormValue("title"))})
+ return
+ }
+ viewer, viewErr := app.viewer(request)
+ if err != nil || viewErr != nil {
+ app.refuse(writer, request, errors.Join(err, viewErr))
+ return
+ }
+ scope := ScopeOf(viewer)
+ now, err := app.index.View(request.Context(), scope, page.ID)
+ if err != nil {
+ app.refuse(writer, request, err)
+ return
+ }
+ links, err := app.index.AliasLinkers(request.Context(), scope, page.ID)
+ if err != nil {
+ app.refuse(writer, request, err)
+ return
+ }
+ aliases := make([]string, 0, len(now.Aliases))
+ for _, alias := range now.Aliases {
+ aliases = append(aliases, alias.Title)
+ }
+ answer(writer, map[string]any{"title": now.Title, "href": now.Href,
+ "from": idText(done.From), "to": idText(done.To),
+ "links": len(links), "count": pagesCount(strconv.Itoa(len(links))),
+ "aliases": aliases})
+}
diff --git a/internal/mapa/templates/page.html b/internal/mapa/templates/page.html
index 65e9bf7..c503841 100644
--- a/internal/mapa/templates/page.html
+++ b/internal/mapa/templates/page.html
@@ -1,5 +1,5 @@
{{define "content"}}{{$d := .Data}}{{$page := $d.Page}}
-{{template "mapa-doc-head" dict "Page" $page "Last" $d.Last "Edit" (printf "%s/edit" $page.Href)}}
+{{template "mapa-doc-head" dict "Page" $page "Last" $d.Last "Edit" (printf "%s/edit" $page.Href) "Rename" $d.Rename}}
{{template "notice" $d.Notice}}
{{with $d.Draft}}{{template "mapa-unsaved" dict "Draft" . "Save" (printf "/p/%s/draft/save" $page.Public) "Discard" (printf "/p/%s/draft/discard" $page.Public) "Href" $page.Href "Saved" $d.Saved}}{{end}}
{{template "mapa-island" $d.Island}}<article mapa-body>
diff --git a/internal/mapa/templates/partials.html b/internal/mapa/templates/partials.html
index cf0a514..423688f 100644
--- a/internal/mapa/templates/partials.html
+++ b/internal/mapa/templates/partials.html
@@ -57,16 +57,21 @@
it; Edit as Markdown opens the textarea, the floor without script
(§6.10).
Keys: Page (PageView), Last (*LastEdit or nil), Edit (href, or ""
- on the editor itself), Day (*dayPill, daily notes). */}}
+ on the editor itself), Day (*dayPill, daily notes), Rename (the
+ rename POST, on a team page the editor mounts on; "" elsewhere).
+ With Rename, the editor's script makes the title's text editable
+ in place (editor/title.mjs, build note 77); without script it is
+ the plain heading it always was. */}}
{{define "mapa-doc-head"}}{{$page := .Page}}<header mapa-page-head>
<div mapa-doc-kicker>
{{- if eq $page.Kind "page"}}<span>{{icon "users"}}Team page</span>
{{- else if eq $page.Kind "daily"}}<span>{{icon "lock"}}Your note. Only you can open it.</span>
{{- else}}<span>{{icon "lock"}}Scratch. Only you can open it.</span>{{end}}
{{- with .Last}}<span>Edited by {{.Who}} {{editedWhen .At}}</span>{{end}}
-{{- with $page.Aliases}}<span>Also called {{range $i, $a := .}}{{if $i}}, {{end}}{{$a.Title}}{{end}}</span>{{end}}
+{{- with $page.Aliases}}<span mapa-also>Also called {{range $i, $a := .}}{{if $i}}, {{end}}{{$a.Title}}{{end}}</span>{{end}}
{{- with .Edit}}<span mapa-actions><a mapa-doc-edit href="{{.}}">{{icon "pencil"}}Edit as Markdown</a></span>{{end}}
{{- with .Day}}<span mapa-phone-only>{{template "mapa-day-pill" .}}</span>{{end}}
</div>
-<h1 mapa-title>{{if eq $page.Kind "daily"}}{{dayTitle $page.Date}}<span mapa-title-faint> {{dayYear $page.Date}}</span>{{else}}{{$page.Label}}{{end}}</h1>
+{{if .Rename}}<h1 mapa-title mapa-rename="{{.Rename}}"><span mapa-title-text>{{$page.Label}}</span></h1>
+{{- else}}<h1 mapa-title>{{if eq $page.Kind "daily"}}{{dayTitle $page.Date}}<span mapa-title-faint> {{dayYear $page.Date}}</span>{{else}}{{$page.Label}}{{end}}</h1>{{end}}
</header>{{end}}
diff --git a/internal/mapa/verbs.go b/internal/mapa/verbs.go
index ce6da48..ae4ebb3 100644
--- a/internal/mapa/verbs.go
+++ b/internal/mapa/verbs.go
@@ -47,11 +47,28 @@ func (tx *Tx) teamPage(memberID, pageID int64) (Page, Record, error) {
// revision (decision 9, gate 3).
func (store *Store) Rename(ctx context.Context, who string, pageID int64,
rawTitle string) error {
+ _, err := store.Retitle(ctx, who, pageID, rawTitle)
+ return err
+}
+
+// Renamed is what a rename did: the revision it was made over (From)
+// and the one it minted (To). Both are 0 when the title was already the
+// page's, and nothing was written.
+type Renamed struct {
+ Title string
+ From, To int64
+}
+
+// Retitle is Rename, answering what it did, for the in-page title
+// (level-up §4.1): the editor moves its base from From to To.
+func (store *Store) Retitle(ctx context.Context, who string, pageID int64,
+ rawTitle string) (Renamed, error) {
title, err := CleanTitle(rawTitle)
if err != nil {
- return err
+ return Renamed{}, err
}
- return quiet(store.write(ctx, func(tx *Tx) error {
+ done := Renamed{Title: title}
+ err = quiet(store.write(ctx, func(tx *Tx) error {
actor, err := tx.member(who)
if err != nil {
return err
@@ -68,9 +85,26 @@ func (store *Store) Rename(ctx context.Context, who string, pageID int64,
return err
}
}
- record.Title = title
- return tx.commit(&page, record, actor, VerbRenamed)
+ record.Title, done.From = title, page.Revision
+ if err := tx.commit(&page, record, actor, VerbRenamed); err != nil {
+ return err
+ }
+ done.To = page.Revision
+ return tx.carryDrafts(page.ID, done.From, done.To)
}))
+ return done, err
+}
+
+// carryDrafts moves every draft on the page based on from to to. A
+// rename's revision holds exactly from's body, so a draft based on one
+// is based on the other; left behind, the draft's next commit would
+// merge against a "teammate's" revision that changed nothing, and say
+// so. Only the base moves: not the text, nor the version a tab holds,
+// nor the time it was written (presence and the draft window read it).
+func (tx *Tx) carryDrafts(pageID, from, to int64) error {
+ return tx.db.Model(&Draft{}).
+ Where("page_id = ? AND base_revision = ?", pageID, from).
+ UpdateColumn("base_revision", to).Error
}
// retitle claims the new title and keeps the old as an alias. Taking
diff --git a/internal/mapatest/gate1_test.go b/internal/mapatest/gate1_test.go
index 2f4306f..f4c9a57 100644
--- a/internal/mapatest/gate1_test.go
+++ b/internal/mapatest/gate1_test.go
@@ -130,6 +130,14 @@ func boSees(t *testing.T, rig *rig, acme string) map[string]string {
updated := rig.post(t, Bo, acme+"/links", nil)
seen["links report"] = updated.Header().Get("Location")
seen["after"] = rig.page(t, Bo, acme)
+ // The in-page title's answers (build note 77): the offer's count and
+ // the old titles, as JSON. Revisions are random in both worlds.
+ _, inPlace := rig.postJSON(Bo, acme+"/rename",
+ url.Values{"title": {"Acme Group"}})
+ seen["rename in place"] = fmt.Sprint(inPlace["title"], inPlace["links"],
+ inPlace["count"], inPlace["aliases"], inPlace["href"])
+ _, relinked := rig.postJSON(Bo, acme+"/links", nil)
+ seen["links in place"] = fmt.Sprint(relinked)
return seen
}
diff --git a/internal/mapatest/pages_test.go b/internal/mapatest/pages_test.go
index 04a8709..64777cd 100644
--- a/internal/mapatest/pages_test.go
+++ b/internal/mapatest/pages_test.go
@@ -33,7 +33,7 @@ func TestATeamPageIsCreatedReadAndEditedByEveryone(t *testing.T) {
rig := newRig(t)
acme := rig.createPage(t, Ana, "Acme", "Customer since 2024.")
body := rig.page(t, Bo, acme)
- if !strings.Contains(body, "<h1 mapa-title>Acme</h1>") ||
+ if !strings.Contains(body, "<span mapa-title-text>Acme</span></h1>") ||
!strings.Contains(body, "Customer since 2024.") {
t.Fatalf("Bo cannot read Ana's team page:\n%s", body)
}
diff --git a/internal/mapatest/title_test.go b/internal/mapatest/title_test.go
new file mode 100644
index 0000000..b7748f4
--- /dev/null
+++ b/internal/mapatest/title_test.go
@@ -0,0 +1,154 @@
+package mapatest
+
+import (
+ "net/http"
+ "net/url"
+ "strings"
+ "testing"
+)
+
+// The in-page title (level-up §4.1, build note 77): the H1 and the
+// header rename through POST /p/{id}/rename asking for JSON, which
+// answers the title, the revisions, and the confirm page's honest
+// count; a refusal is the form's sentence and writes nothing; Update
+// links answers JSON too; and a rename carries the person's draft with
+// it, so their next commit is theirs alone, not a merge.
+
+func TestARenameAnswersTheTitleInPlace(t *testing.T) {
+ rig := newRig(t)
+ halden := rig.createPage(t, Ana, "Halden", "A robotics customer.")
+ rig.createPage(t, Bo, "Plan", "Call [[Halden]].")
+ before := revisionOf(t, rig, halden)
+ since := rig.lastEvent(t)
+ code, reply := rig.postJSON(Ana, halden+"/rename",
+ url.Values{"title": {" Halden Robotics "}})
+ if code != http.StatusOK || reply["title"] != "Halden Robotics" ||
+ reply["href"] != halden {
+ t.Fatalf("rename answered %d %v", code, reply)
+ }
+ if got := rig.verbsSince(t, since); got != "renamed" {
+ t.Fatalf("the rename logged %q", got)
+ }
+ if reply["from"] != before || reply["to"] != revisionOf(t, rig, halden) {
+ t.Fatalf("revisions %v, %v: want from %s to the page's now",
+ reply["from"], reply["to"], before)
+ }
+ if reply["links"] != 1.0 || reply["count"] != "1 page" ||
+ len(reply["aliases"].([]any)) != 1 {
+ t.Fatalf("the offer: %v", reply)
+ }
+ if offer := rig.page(t, Ana, halden+"/links"); !strings.Contains(offer,
+ "1 page you can edit link here") {
+ t.Fatal("the confirm page's count differs from the answer's")
+ }
+ since = rig.lastEvent(t)
+ code, same := rig.postJSON(Ana, halden+"/rename",
+ url.Values{"title": {"Halden Robotics"}})
+ if code != http.StatusOK || same["from"] != "0" ||
+ rig.lastEvent(t) != since {
+ t.Fatalf("an unchanged title answered %d %v, or wrote", code, same)
+ }
+}
+
+func TestARefusedRenameIsTheSentenceAndWritesNothing(t *testing.T) {
+ rig := newRig(t)
+ halden := rig.createPage(t, Ana, "Halden", "One.")
+ acme := rig.createPage(t, Bo, "Acme", "Two.")
+ since := rig.lastEvent(t)
+ for title, want := range map[string]string{
+ "acme": "already exists",
+ "2026-11-12": "A date can't be a page title",
+ strings.Repeat("x", 121): "too long",
+ "A [[b]]": "link syntax",
+ } {
+ code, reply := rig.postJSON(Ana, halden+"/rename",
+ url.Values{"title": {title}})
+ said, _ := reply["error"].(string)
+ if code != http.StatusUnprocessableEntity ||
+ !strings.Contains(said, want) {
+ t.Errorf("%q answered %d %v", title, code, reply)
+ }
+ if title == "acme" && reply["taken"] != acme {
+ t.Errorf("a taken title names %v, not %s", reply["taken"], acme)
+ }
+ }
+ if rig.lastEvent(t) != since {
+ t.Fatal("a refused rename wrote")
+ }
+ if code, _ := rig.postJSON(Ana, "/p/nothere/rename",
+ url.Values{"title": {"X"}}); code != http.StatusNotFound {
+ t.Fatalf("a page that is not there answered %d", code)
+ }
+}
+
+func TestUpdateLinksAnswersTheEditor(t *testing.T) {
+ rig := newRig(t)
+ acme := rig.createPage(t, Ana, "Acme", "A customer.")
+ plan := rig.createPage(t, Bo, "Plan", "Call [[Acme]].")
+ rig.postJSON(Ana, acme+"/rename", url.Values{"title": {"Acme Ltd"}})
+ code, reply := rig.postJSON(Ana, acme+"/links", nil)
+ if code != http.StatusOK || reply["changed"] != 1.0 ||
+ reply["notice"] != "Updated links in 1 page." {
+ t.Fatalf("Update links answered %d %v", code, reply)
+ }
+ if !strings.Contains(rig.page(t, Bo, plan+"/edit"), "[[Acme Ltd]]") {
+ t.Fatal("the link was not updated")
+ }
+}
+
+// A rename while a draft is open (§3.2: a rename mints a revision for
+// that page only, holding the same body): the draft's base moves with
+// it, so committing the draft is Saved, not merged against a
+// "teammate's" revision that changed nothing, and its text is intact.
+func TestARenameCarriesTheDraftWithIt(t *testing.T) {
+ rig := newRig(t)
+ acme := rig.createPage(t, Ana, "Acme", "One.")
+ _, saved := rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0",
+ "One, and mine.")
+ version, _ := saved["version"].(string)
+ rig.postJSON(Ana, acme+"/rename", url.Values{"title": {"Acme Ltd"}})
+ code, again := rig.autosave(t, Ana, acme, revisionOf(t, rig, acme),
+ version, "One, and mine, still.")
+ if code != http.StatusOK || again["merge"] != nil {
+ t.Fatalf("the autosave after a rename answered %d %v", code, again)
+ }
+ code, committed := rig.postJSON(Ana, acme+"/draft/save",
+ url.Values{"version": {again["version"].(string)}})
+ if code != http.StatusOK || committed["saved"] != true ||
+ committed["merged"] != false {
+ t.Fatalf("the commit after a rename answered %d %v", code, committed)
+ }
+ if body := islandOf(t, rig.page(t, Bo, acme)).Source; body !=
+ "One, and mine, still." {
+ t.Fatalf("the page holds %q", body)
+ }
+}
+
+// Only a team page's title is editable in place, and only where the
+// editor mounts: never a daily note, a scratch page, History, the saved
+// version beside a draft, or the Markdown editor; and the server never
+// draws it editable (scriptless, it is a plain heading).
+func TestOnlyATeamPagesTitleIsRenamableInPlace(t *testing.T) {
+ rig := newRig(t)
+ acme := rig.createPage(t, Ana, "Acme", "One.")
+ rig.post(t, Ana, "/d/2026-11-12", url.Values{"body": {"Day."},
+ "revision": {"0"}})
+ scratch := rig.post(t, Ana, "/scratch", url.Values{"body": {"Mine."}}).
+ Header().Get("Location")
+ page := rig.page(t, Ana, acme)
+ if !strings.Contains(page, `<h1 mapa-title mapa-rename="`+acme+
+ `/rename"><span mapa-title-text>Acme</span></h1>`) {
+ t.Fatalf("the team page's title is not renamable:\n%s",
+ between(page, "<header mapa-page-head>", "</header>"))
+ }
+ rig.autosave(t, Ana, acme, revisionOf(t, rig, acme), "0", "Draft.")
+ for _, path := range []string{"/d/2026-11-12", "/", scratch,
+ acme + "/history", acme + "?saved=1", acme + "/edit"} {
+ if got := rig.page(t, Ana, path); strings.Contains(got, "mapa-rename") {
+ t.Errorf("%s: the title is renamable in place", path)
+ }
+ }
+ if strings.Contains(page, "contenteditable") {
+ t.Fatal("the server drew something editable")
+ }
+}