Files
Docwell/Server/internal/convert/database.go
T
tajniak81andClaude Opus 5.5 3102a476a6
CI / go (Server) (push) Failing after 4m1s
CI / go (Web-App) (push) Successful in 10m50s
CI / frontend (push) Failing after 6m46s
CI / desktop (ubuntu-latest) (push) Successful in 1h10m39s
CI / mobile (push) Failing after 3m2s
CI / docker (push) Successful in 35m59s
Docs / build (push) Successful in 13m26s
Docs / deploy (push) Skipped
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
Add databases: table, kanban and calendar views with rows as pages
Phase C of docs/AFFINE.md (ADR-13).

A database is a block whose records live in the page's Y.Doc, in
ydoc.getMap("blocks") under the block's id: columns, rows (each cell its
own map entry, so concurrent edits to one row merge) and views. Order is
a fractional position per item, so moving a row keeps edits made to it at
the same time. Database edits share the page's undo history.

- Thirteen property types: title, text, number, checkbox, select,
  multi-select, date, progress, link, attachment, member, created time
  and created by. The column menu renames, changes type (converting the
  values), sorts, filters, hides, inserts, moves, duplicates and deletes;
  columns resize and drag.
- Table view: keyboard navigation, range selection, typing to edit,
  copy and paste with spreadsheets (pasting adds rows as needed), row
  dragging, bulk delete, grouping.
- Views in tabs, each with its own filters (all/any), sorts, grouping
  and visible properties; kanban grouped by select, multi-select,
  checkbox or member with cards dragged between columns; calendar by a
  date property with records dragged between days.
- A record opens beside the page with its properties and a body of its
  own (a Y.XmlFragment in the row, edited by a second editor).
- /database, Ctrl+G or "Group as database" turns a list into a database;
  CSV export and import; a copied, pasted or duplicated database gets
  its own copy of the records.

Server: yrs writes each database's records into its node as attrs.data
when it turns a document into JSON, adds them to the search text, and
writes them back on imports, restores and templates. Markdown, HTML,
PDF and Word exports show a database as a table of its first view's
columns. Notion database CSVs import as database blocks with guessed
types. Share links and history previews show databases read-only.

Strings are translated into all five languages, the user guide covers
databases, and the mobile editor bundle is rebuilt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 20:06:18 +01:00

168 lines
3.9 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package convert
import (
"strings"
"time"
)
// Databases (ADR-13) reach the converters as "database" nodes whose
// attrs.data holds the records, written by yrs from the page's Y.Doc. Every
// export draws one as its title and a table of the first view's visible
// columns, in the rows' own order.
// expandDatabases replaces database nodes, at any depth, with a title and a table.
func expandDatabases(n Node) Node {
if len(n.Content) == 0 {
return n
}
out := make([]Node, 0, len(n.Content))
for _, c := range n.Content {
if c.Type == "database" {
out = append(out, databaseNodes(c)...)
} else {
out = append(out, expandDatabases(c))
}
}
n.Content = out
return n
}
type dbColumn struct {
id, name, typ string
options map[string]string
}
func databaseNodes(n Node) []Node {
data, _ := n.Attrs["data"].(map[string]any)
if data == nil {
return nil
}
var cols []dbColumn
for _, raw := range list(data["columns"]) {
c, _ := raw.(map[string]any)
id, _ := c["id"].(string)
if id == "" {
continue
}
col := dbColumn{id: id, typ: str(c["type"]), name: str(c["name"]), options: map[string]string{}}
for _, o := range list(c["options"]) {
if om, ok := o.(map[string]any); ok {
col.options[str(om["id"])] = str(om["name"])
}
}
cols = append(cols, col)
}
// The first view decides which columns show.
hidden := map[string]bool{}
if views := list(data["views"]); len(views) > 0 {
if v, ok := views[0].(map[string]any); ok {
for _, h := range list(v["hidden"]) {
hidden[str(h)] = true
}
}
}
shown := cols[:0:0]
for _, c := range cols {
if c.typ == "title" || !hidden[c.id] {
shown = append(shown, c)
}
}
if len(shown) == 0 {
return nil
}
cell := func(typ, text string) Node {
p := Node{Type: "paragraph"}
if text != "" {
p.Content = []Node{{Type: "text", Text: text}}
}
return Node{Type: typ, Content: []Node{p}}
}
head := Node{Type: "tableRow"}
for _, c := range shown {
head.Content = append(head.Content, cell("tableHeader", c.name))
}
table := Node{Type: "table", Content: []Node{head}}
for _, raw := range list(data["rows"]) {
r, _ := raw.(map[string]any)
if r == nil {
continue
}
row := Node{Type: "tableRow"}
for _, c := range shown {
row.Content = append(row.Content, cell("tableCell", cellText(c, r)))
}
table.Content = append(table.Content, row)
}
var out []Node
if title := str(data["title"]); title != "" {
out = append(out, Node{Type: "paragraph", Content: []Node{{Type: "text", Text: title, Marks: []Mark{{Type: "bold"}}}}})
}
return append(out, table)
}
// cellText is a cell as text, like cellText in Packages/editor/src/database/model.ts.
func cellText(c dbColumn, row map[string]any) string {
cells, _ := row["cells"].(map[string]any)
v := cells[c.id]
switch c.typ {
case "createdTime":
if ms, ok := row["createdAt"].(float64); ok && ms > 0 {
return time.UnixMilli(int64(ms)).UTC().Format("2006-01-02")
}
return ""
case "createdBy":
if p, ok := row["createdBy"].(map[string]any); ok {
return str(p["name"])
}
return ""
case "checkbox":
if v == true {
return "✓"
}
return ""
case "select":
return c.options[str(v)]
case "multiSelect":
var names []string
for _, id := range list(v) {
if name := c.options[str(id)]; name != "" {
names = append(names, name)
}
}
return strings.Join(names, ", ")
case "member", "attachment":
var names []string
for _, x := range list(v) {
if m, ok := x.(map[string]any); ok && str(m["name"]) != "" {
names = append(names, str(m["name"]))
}
}
return strings.Join(names, ", ")
case "progress":
if f, ok := v.(float64); ok {
return formatNumber(f) + "%"
}
return ""
}
switch x := v.(type) {
case string:
return x
case float64:
return formatNumber(x)
}
return ""
}
func list(v any) []any {
l, _ := v.([]any)
return l
}
func str(v any) string {
s, _ := v.(string)
return s
}