Library sidebar: folders, pinned-top, per-page menus, and an all-pages view
- Page.isFolder (migration 027): folders are content-less pages reusing parentId; folder delete reparents children one level up - Sidebar: Pages group becomes Library; Pinned at top; folder tree; page menu (duplicate/pin/copy link/rename/move/trash); folder menu - /pages index: filterable Library table (all/recent/pinned/untagged) with folder path + last edit (components/library-index.tsx) - Docs: API.md isFolder + folder-delete semantics
This commit is contained in:
@@ -54,12 +54,12 @@ func HandlePagesList(w http.ResponseWriter, r *http.Request) {
|
||||
respond(w, 200, v, e)
|
||||
}
|
||||
func pageData(b object) (object, error) {
|
||||
d := fields(b, "title content icon pinned parentId")
|
||||
d := fields(b, "title content icon pinned parentId isFolder")
|
||||
// parentId is nullable: an explicit null promotes the page to the root.
|
||||
if e := stringFields(d, "icon parentId", "title content"); e != nil {
|
||||
return nil, e
|
||||
}
|
||||
if e := boolFields(d, "pinned"); e != nil {
|
||||
if e := boolFields(d, "pinned isFolder"); e != nil {
|
||||
return nil, e
|
||||
}
|
||||
if e := limitText(d, "content", maxPageContentLen); e != nil {
|
||||
@@ -313,7 +313,24 @@ func HandlePagesDelete(w http.ResponseWriter, r *http.Request) {
|
||||
if e := lockUser(r, tx); e != nil {
|
||||
return e
|
||||
}
|
||||
return trashPage(r, tx, r.PathValue("id"))
|
||||
id := r.PathValue("id")
|
||||
// Library folders (027): deleting a folder reparents its children
|
||||
// (pages AND subfolders) to the folder's own parent, keeping the tree
|
||||
// intact one level up — the mockup's "folder removed, pages kept".
|
||||
// Plain pages keep the SET NULL promote-to-top behavior.
|
||||
row, e := owned(r, tx, "Page", id, "ownerId")
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if isFolder, _ := row["isFolder"].(bool); isFolder {
|
||||
parent := row["parentId"] // nil for a top-level folder
|
||||
if _, e := tx.ExecContext(r.Context(),
|
||||
`UPDATE "Page" SET "parentId"=$1 WHERE "parentId"=$2 AND "ownerId"=$3`,
|
||||
parent, id, user(r)); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
return trashPage(r, tx, id)
|
||||
})
|
||||
respond(w, 200, OK{true}, e)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package http
|
||||
|
||||
import "testing"
|
||||
|
||||
// Library folders (migration 027): a folder is a content-less Page (isFolder)
|
||||
// reusing the parentId hierarchy. Deleting a folder reparents its children
|
||||
// (pages AND subfolders) to the folder's own parent, keeping them one level up.
|
||||
|
||||
func TestPageFolders(t *testing.T) {
|
||||
srv := testServer(t)
|
||||
alice := newAPIUser(t, srv, "folders@superbrain.test")
|
||||
|
||||
top := alice.request("POST", "/api/pages", map[string]any{"title": "Personal", "isFolder": true}, 201)
|
||||
topID := top["id"].(string)
|
||||
if top["isFolder"] != true {
|
||||
t.Fatalf("folder flag not stored: %v", top)
|
||||
}
|
||||
sub := alice.request("POST", "/api/pages", map[string]any{"title": "Travel", "isFolder": true, "parentId": topID}, 201)["id"].(string)
|
||||
page := alice.request("POST", "/api/pages", map[string]any{"title": "Lisbon", "parentId": sub}, 201)["id"].(string)
|
||||
loose := alice.request("POST", "/api/pages", map[string]any{"title": "Loose", "parentId": topID}, 201)["id"].(string)
|
||||
|
||||
// isFolder is exposed in the list projection.
|
||||
found := false
|
||||
for _, p := range alice.getJSON("/api/pages", 200) {
|
||||
if p["id"] == topID {
|
||||
found = p["isFolder"] == true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("isFolder missing from list projection")
|
||||
}
|
||||
|
||||
// Deleting the nested folder promotes its page and keeps it under the
|
||||
// grandparent (top), not at the root.
|
||||
alice.request("DELETE", "/api/pages/"+sub, nil, 200)
|
||||
after := map[string]string{}
|
||||
for _, p := range alice.getJSON("/api/pages", 200) {
|
||||
parent, _ := p["parentId"].(string)
|
||||
after[p["id"].(string)] = parent
|
||||
}
|
||||
if after[page] != topID {
|
||||
t.Fatalf("page not reparented to grandparent: %v", after)
|
||||
}
|
||||
if _, ok := after[sub]; ok {
|
||||
t.Fatal("deleted folder still listed")
|
||||
}
|
||||
|
||||
// Deleting the top folder promotes remaining children to the root.
|
||||
alice.request("DELETE", "/api/pages/"+topID, nil, 200)
|
||||
after = map[string]string{}
|
||||
for _, p := range alice.getJSON("/api/pages", 200) {
|
||||
parent, _ := p["parentId"].(string)
|
||||
after[p["id"].(string)] = parent
|
||||
}
|
||||
if after[page] != "" || after[loose] != "" {
|
||||
t.Fatalf("children not promoted to root: %v", after)
|
||||
}
|
||||
|
||||
// A plain page delete still promotes children to the root (SET NULL).
|
||||
p1 := alice.request("POST", "/api/pages", map[string]any{"title": "P1"}, 201)["id"].(string)
|
||||
p2 := alice.request("POST", "/api/pages", map[string]any{"title": "P2", "parentId": p1}, 201)["id"].(string)
|
||||
alice.request("DELETE", "/api/pages/"+p1, nil, 200)
|
||||
for _, p := range alice.getJSON("/api/pages", 200) {
|
||||
if p["id"] == p2 && p["parentId"] != nil {
|
||||
t.Fatalf("plain page delete did not promote child: %v", p)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user