Edit page content with the Docwell editor
PageView now mounts @docwell/editor and autosaves the document through
GET/PUT /api/v1/pages/{id}/content. The server stores Tiptap JSON in
pages.content_json and derives content_text and word_count itself.
Saves are last-write-wins until the collab relay replaces them.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a96c6ad3ce
commit
abf55a426b
13 files changed
+420
-9
No files matched your search
@@ -140,7 +140,12 @@ func (a *api) routes(mux *http.ServeMux) {
|
||||
}
|
||||
|
||||
func (a *api) decode(w http.ResponseWriter, r *http.Request, v any) bool {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 1<<20)
|
||||
return a.decodeMax(w, r, v, 1<<20)
|
||||
}
|
||||
|
||||
// decodeMax is decode with a larger body limit, for page content.
|
||||
func (a *api) decodeMax(w http.ResponseWriter, r *http.Request, v any, limit int64) bool {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, limit)
|
||||
if err := json.NewDecoder(r.Body).Decode(v); err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid_json"})
|
||||
return false
|
||||
|
||||
@@ -23,6 +23,8 @@ type SpaceService interface {
|
||||
CreatePage(ctx context.Context, c access.Caller, spaceID string, in directory.CreatePageInput) (directory.Page, error)
|
||||
GetPage(ctx context.Context, c access.Caller, id string) (directory.Page, error)
|
||||
UpdatePage(ctx context.Context, c access.Caller, id string, in directory.UpdatePageInput) (directory.Page, error)
|
||||
GetPageContent(ctx context.Context, c access.Caller, id string) (directory.PageContent, error)
|
||||
SavePageContent(ctx context.Context, c access.Caller, id string, in directory.SavePageContentInput) (directory.PageContent, error)
|
||||
MovePage(ctx context.Context, c access.Caller, id string, in directory.MovePageInput) (directory.Page, error)
|
||||
TrashPage(ctx context.Context, c access.Caller, id string) error
|
||||
RestorePage(ctx context.Context, c access.Caller, id string) (directory.Page, error)
|
||||
@@ -104,6 +106,18 @@ func (a *api) spaceRoutes(mux *http.ServeMux) {
|
||||
p, err := a.dir.UpdatePage(r.Context(), caller(r), r.PathValue("id"), in)
|
||||
a.respond(w, p, err)
|
||||
}))
|
||||
mux.HandleFunc("GET /api/v1/pages/{id}/content", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
||||
c, err := a.dir.GetPageContent(r.Context(), caller(r), r.PathValue("id"))
|
||||
a.respond(w, c, err)
|
||||
}))
|
||||
mux.HandleFunc("PUT /api/v1/pages/{id}/content", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
||||
var in directory.SavePageContentInput
|
||||
if !a.decodeMax(w, r, &in, 10<<20) {
|
||||
return
|
||||
}
|
||||
c, err := a.dir.SavePageContent(r.Context(), caller(r), r.PathValue("id"), in)
|
||||
a.respond(w, c, err)
|
||||
}))
|
||||
mux.HandleFunc("POST /api/v1/pages/{id}/move", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
||||
var in directory.MovePageInput
|
||||
if !a.decode(w, r, &in) {
|
||||
|
||||
@@ -29,6 +29,11 @@ func (p pageDir) MovePage(_ context.Context, _ access.Caller, id string, in dire
|
||||
return directory.Page{ID: id, Parent: in.Parent}, nil
|
||||
}
|
||||
|
||||
func (p pageDir) SavePageContent(_ context.Context, _ access.Caller, id string, in directory.SavePageContentInput) (directory.PageContent, error) {
|
||||
*p.calls = append(*p.calls, "save "+id+" "+string(in.Content))
|
||||
return directory.PageContent{Content: in.Content}, nil
|
||||
}
|
||||
|
||||
func TestPageRoutes(t *testing.T) {
|
||||
var calls []string
|
||||
h := NewRouter(Deps{
|
||||
@@ -40,12 +45,13 @@ func TestPageRoutes(t *testing.T) {
|
||||
{"DELETE", "/api/v1/pages/p1", ""},
|
||||
{"DELETE", "/api/v1/pages/p1?permanent=true", ""},
|
||||
{"POST", "/api/v1/pages/p2/move", `{"parent":"p1","index":0}`},
|
||||
{"PUT", "/api/v1/pages/p2/content", `{"content":{"type":"doc"}}`},
|
||||
} {
|
||||
if rec := send(h, req.method, req.path, "alice", req.body); rec.Code >= 300 {
|
||||
t.Fatalf("%s %s = %d %s", req.method, req.path, rec.Code, rec.Body)
|
||||
}
|
||||
}
|
||||
want := []string{"trash p1", "purge p1", "move p2 under p1"}
|
||||
want := []string{"trash p1", "purge p1", "move p2 under p1", `save p2 {"type":"doc"}`}
|
||||
if len(calls) != len(want) {
|
||||
t.Fatalf("calls = %v", calls)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user