Editor: Find & Replace, read-aloud, backup, typography, phonetic, org niceties

Writer power-ups (Phase 11), plus the selection-bubble vs copy/paste fix.

- Find & Replace (Ctrl/Cmd+F): SearchHighlight decoration extension +
  FindReplace bar (match-case, replace-all back-to-front, scroll without
  popping the selection bubble).
- Read-aloud (Web Speech, offline) on the word card and selection bubble.
- Keyboard/touch access to the ESL helpers: Ctrl/Cmd+D look up word at caret,
  Ctrl/Cmd+J rewrite selection, touch long-press lookup. Refactored the
  right-click handler into a shared openWordLookup(pos).
- Whole-corpus backup: GET /api/docs/export-all zips every doc (md/docx),
  de-dupes filenames, dated name; sidebar download links. TestExportAll.
- Smart typography input rules (curly quotes/em-dash/ellipsis), ASCII-only so
  CJK is untouched.
- Duplicate doc, sidebar sort (Recent/Title/Longest), toolbar outline popover.
- English phonetic (chosen over pinyin for an English learner): ECDICT-built
  phonetic.json.gz (46,579 words) + Result.Phonetic + WordCard IPA line;
  scripts/build_phonetic.py (full build + --seed fallback).
- Selection bubble no longer blocks copy/paste: deferred to pointer-up and made
  click-through except on its buttons.

Claude-Session: https://claude.ai/code/session_016Yr6jELuRc7hyzYLccQKZd
This commit is contained in:
prosolis
2026-06-26 09:47:26 -07:00
parent 6e6e4edce7
commit db737fa612
20 changed files with 1039 additions and 22 deletions

View File

@@ -9,15 +9,101 @@ import (
"fmt"
"net/http"
"strings"
"time"
"github.com/go-chi/chi/v5"
"gitea.parodia.dev/drwily/petal/internal/db"
)
// exportRoutes registers the download endpoint: GET /api/docs/{id}/export?format=md|html|txt|docx
// exportRoutes registers the download endpoints: one document
// (GET /api/docs/{id}/export?format=…) and a whole-corpus backup zip
// (GET /api/docs/export-all?format=…). The static "export-all" segment takes
// priority over the {id} param in chi's router, so the two don't collide.
func (h *Handler) exportRoutes(r chi.Router) {
r.Get("/{id}/export", h.export)
r.Get("/export-all", h.exportAll)
}
// exportAll streams every document the user owns, each rendered in the requested
// format, bundled into a single zip — a one-click "download all my writing"
// backup. Per-doc version history guards against bad edits; this guards against
// a lost disk. Reuses the same renderers as the single-document export.
func (h *Handler) exportAll(w http.ResponseWriter, r *http.Request) {
formatKey := r.URL.Query().Get("format")
if formatKey == "" {
formatKey = "md"
}
format, ok := exportFormats[formatKey]
if !ok {
badRequest(w, "unsupported export format")
return
}
rows, err := h.DB.Query(
`SELECT id, user_id, title, content, content_text, tone, word_count, created_at, updated_at
FROM documents
WHERE user_id = ?
ORDER BY updated_at DESC`,
db.LocalUserID,
)
if err != nil {
serverError(w, err)
return
}
defer rows.Close()
var buf bytes.Buffer
zw := zip.NewWriter(&buf)
seen := map[string]int{} // de-duplicate filenames from same-titled docs
for rows.Next() {
var doc db.Document
if err := rows.Scan(
&doc.ID, &doc.UserID, &doc.Title, &doc.Content, &doc.ContentText,
&doc.Tone, &doc.WordCount, &doc.CreatedAt, &doc.UpdatedAt,
); err != nil {
serverError(w, err)
return
}
body, err := format.render(doc)
if err != nil {
serverError(w, err)
return
}
base := sanitizeFilename(doc.Title)
if base == "" {
base = "untitled"
}
key := base + "." + format.ext
name := key
if c := seen[key]; c > 0 {
name = fmt.Sprintf("%s (%d).%s", base, c, format.ext)
}
seen[key]++
f, err := zw.Create(name)
if err != nil {
serverError(w, err)
return
}
if _, err := f.Write(body); err != nil {
serverError(w, err)
return
}
}
if err := rows.Err(); err != nil {
serverError(w, err)
return
}
if err := zw.Close(); err != nil {
serverError(w, err)
return
}
filename := "petal-backup-" + time.Now().Format("2006-01-02") + ".zip"
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename*=UTF-8''%s", urlEscapeFilename(filename)))
w.Header().Set("Content-Length", fmt.Sprintf("%d", buf.Len()))
_, _ = w.Write(buf.Bytes())
}
// exportFormat describes one downloadable format: how to render it and how to

View File

@@ -61,6 +61,45 @@ func TestExportMarkdown(t *testing.T) {
}
}
func TestExportAll(t *testing.T) {
srv := newTestServer(t)
// Two docs, one with a duplicate title to exercise filename de-duplication.
seedRichDoc(t, srv)
id2 := newDoc(t, srv)
if rec := do(t, srv, http.MethodPut, "/"+id2, `{"title":"日记 Diary","content":`+jsonString(richDocJSON)+`,"content_text":"x","word_count":1}`); rec.Code != http.StatusOK {
t.Fatalf("seed second doc: %d %s", rec.Code, rec.Body)
}
rec := do(t, srv, http.MethodGet, "/export-all?format=md", "")
if rec.Code != http.StatusOK {
t.Fatalf("export-all: code=%d body=%s", rec.Code, rec.Body)
}
if ct := rec.Header().Get("Content-Type"); ct != "application/zip" {
t.Fatalf("unexpected content-type: %q", ct)
}
zr, err := zip.NewReader(bytes.NewReader(rec.Body.Bytes()), int64(rec.Body.Len()))
if err != nil {
t.Fatalf("zip open: %v", err)
}
if len(zr.File) != 2 {
t.Fatalf("expected 2 files in backup, got %d", len(zr.File))
}
names := map[string]bool{}
for _, f := range zr.File {
names[f.Name] = true
rc, _ := f.Open()
b, _ := io.ReadAll(rc)
rc.Close()
if !strings.Contains(string(b), "你好") {
t.Fatalf("backup entry %q missing CJK body", f.Name)
}
}
// The duplicate title must have been disambiguated, not overwritten.
if !names["日记 Diary.md"] || !names["日记 Diary (1).md"] {
t.Fatalf("expected de-duplicated filenames, got %v", names)
}
}
func TestExportHTML(t *testing.T) {
srv := newTestServer(t)
id := seedRichDoc(t, srv)