Files
Docwell/Server/internal/directory/content.go
T
tajniak81andClaude Opus 5.5 abf55a426b 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>
2026-10-31 11:24:36 +01:00

145 lines
4.2 KiB
Go

package directory
import (
"context"
"encoding/json"
"strings"
"unicode"
"docwell/server/internal/access"
"docwell/server/internal/apperr"
"docwell/server/internal/store/pocketbase"
)
// ---- Page content ------------------------------------------------------------
//
// Until the collab relay lands, a page body is saved whole as Tiptap JSON
// (pages.content_json), last write wins. The server derives content_text and
// word_count from it, so clients cannot make search text disagree with the
// document.
// maxContentBytes caps one saved document (the PocketBase field allows 20 MB).
const maxContentBytes = 8 << 20
type contentRecord struct {
ID string `json:"id"`
Space string `json:"space"`
ContentJSON json.RawMessage `json:"content_json"`
Updated string `json:"updated"`
}
// PageContent is a page's document as Tiptap JSON; Content is null for a page
// that was never edited.
type PageContent struct {
Content json.RawMessage `json:"content"`
Updated string `json:"updated"`
}
// GetPageContent returns a page's document (viewer).
func (s *Service) GetPageContent(ctx context.Context, c access.Caller, id string) (PageContent, error) {
p, _, err := s.pageFor(ctx, c, id)
if err != nil {
return PageContent{}, err
}
rec, err := pocketbase.Get[contentRecord](ctx, s.pb, "pages", p.ID)
if err != nil {
return PageContent{}, mapPB(err, "page not found")
}
return PageContent{Content: nullIfEmpty(rec.ContentJSON), Updated: rec.Updated}, nil
}
type SavePageContentInput struct {
Content json.RawMessage `json:"content"`
}
// SavePageContent replaces a page's document (editor).
func (s *Service) SavePageContent(ctx context.Context, c access.Caller, id string, in SavePageContentInput) (PageContent, error) {
p, a, err := s.pageFor(ctx, c, id)
if err != nil {
return PageContent{}, err
}
if err := access.EditPages(a.role); err != nil {
return PageContent{}, err
}
if p.DeletedAt != "" {
return PageContent{}, apperr.Invalid("restore the page from the trash first")
}
if len(in.Content) > maxContentBytes {
return PageContent{}, apperr.InvalidFields(map[string]string{"content": "The page is too large to save."})
}
var doc docNode
if err := json.Unmarshal(in.Content, &doc); err != nil || doc.Type != "doc" {
return PageContent{}, apperr.InvalidFields(map[string]string{"content": "Content must be a Tiptap document."})
}
text := doc.plainText()
rec, err := pocketbase.Update[contentRecord](ctx, s.pb, "pages", p.ID, map[string]any{
"content_json": in.Content, "content_text": text, "word_count": wordCount(text), "last_editor": c.ID,
})
if err != nil {
return PageContent{}, mapPB(err, "page not found")
}
return PageContent{Content: nullIfEmpty(rec.ContentJSON), Updated: rec.Updated}, nil
}
func nullIfEmpty(raw json.RawMessage) json.RawMessage {
if len(raw) == 0 || string(raw) == `""` {
return json.RawMessage("null")
}
return raw
}
// docNode is the part of a ProseMirror/Tiptap JSON node needed for text extraction.
type docNode struct {
Type string `json:"type"`
Text string `json:"text"`
Content []docNode `json:"content"`
}
// blockTypes end with a line break in the plain text; everything else is inline.
var blockTypes = map[string]bool{
"paragraph": true, "heading": true, "codeBlock": true, "blockquote": true,
"listItem": true, "taskItem": true, "horizontalRule": true, "tableRow": true,
}
// plainText flattens the document to text, one line per block.
func (n docNode) plainText() string {
var b strings.Builder
lineEnded := true
write := func(s string) {
if s != "" {
b.WriteString(s)
lineEnded = s[len(s)-1] == '\n'
}
}
var walk func(n docNode, depth int)
walk = func(n docNode, depth int) {
if depth > 200 {
return
}
switch n.Type {
case "text":
write(n.Text)
case "hardBreak":
write("\n")
case "tableCell", "tableHeader":
for _, c := range n.Content {
walk(c, depth+1)
}
write("\t")
return
}
for _, c := range n.Content {
walk(c, depth+1)
}
if blockTypes[n.Type] && !lineEnded {
write("\n")
}
}
walk(n, 0)
return strings.TrimSpace(b.String())
}
func wordCount(text string) int {
return len(strings.FieldsFunc(text, func(r rune) bool { return unicode.IsSpace(r) }))
}