Editor:
- Toggles (Tiptap Details). Open state is per reader, not saved.
- Embeds for YouTube, Vimeo, Loom, Figma, CodePen, Spotify, Google Docs
and Maps, in sandboxed frames. Other addresses become link cards.
- Link cards with title, description and picture read by docwell-server
(GET /link-preview). The fetcher refuses private, loopback, link-local
and CGNAT addresses after DNS resolution, redirects included.
- Block links: a margin button gives a block a short id and copies
/p/<id>#b-<block>. Opening one jumps to the block and flashes it.
- Markdown, HTML and PDF exports handle the new blocks; HTML import reads
<details> as a toggle.
Pages and spaces:
- History gets a Changes view: block and word diff against the previous
version or the page as it is now.
- Archive: archived pages leave the tree and become read-only but stay
readable; a space's Archive lists them with Unarchive. New migration
(pages.archived_at) and page.archived / page.unarchived webhook events.
- Full space export (GET /spaces/{id}/export): a zip of Markdown or HTML
files with subpages in folders, an index, and attachments under _files/.
Links between exported pages and to attachments become relative paths.
Desktop 1.0:
- Several servers, each with its own sign-in; old settings files carry over.
- Session tokens in the system keychain (go-keyring), moved out of
localStorage on first start.
- Native menu bar (File, Edit, View, Window, Help) with a Server submenu.
- docwell:// links may point at a block.
- Release workflow builds Windows NSIS, macOS DMG and Linux deb, rpm and
AppImage installers and attaches them to the release;
build/set-version.mjs stamps the version into their metadata.
Not yet run: the release workflow, the desktop menus by eye, and macOS or
Linux builds.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
229 lines
5.8 KiB
Go
229 lines
5.8 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package main
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
// Server is one docwell-server the app can sign in to.
|
|
type Server struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
URL string `json:"url"`
|
|
}
|
|
|
|
// Settings are stored per user in the OS config directory. Sign-in tokens
|
|
// are not: they live in the system keychain (see keychain.go).
|
|
type Settings struct {
|
|
Servers []Server `json:"servers"`
|
|
// Active is the id of the server the window shows.
|
|
Active string `json:"active"`
|
|
// ServerURL is the active server's address (and, in files written by
|
|
// older versions, the only server).
|
|
ServerURL string `json:"serverUrl"`
|
|
}
|
|
|
|
// SettingsService is bound to the frontend through Wails bindings.
|
|
type SettingsService struct {
|
|
path string
|
|
mu sync.Mutex
|
|
// changed is called after the server list or the active server changes,
|
|
// with the ids of removed servers, e.g. to rebuild the menu and delete
|
|
// their tokens.
|
|
changed func(s Settings, removed []string)
|
|
}
|
|
|
|
func NewSettingsService() (*SettingsService, error) {
|
|
dir, err := os.UserConfigDir()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &SettingsService{path: filepath.Join(dir, "Docwell", "settings.json")}, nil
|
|
}
|
|
|
|
const defaultServerURL = "http://localhost:8080"
|
|
|
|
// Get returns the saved settings, or one local server when none are saved.
|
|
func (s *SettingsService) Get() (Settings, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
return s.load()
|
|
}
|
|
|
|
func (s *SettingsService) load() (Settings, error) {
|
|
var out Settings
|
|
data, err := os.ReadFile(s.path)
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
return normalize(out), err
|
|
}
|
|
if err == nil {
|
|
if err := json.Unmarshal(data, &out); err != nil {
|
|
return normalize(Settings{}), err
|
|
}
|
|
}
|
|
return normalize(out), nil
|
|
}
|
|
|
|
// normalize fills in what older or empty files lack: a server for a lone
|
|
// serverUrl, a default server, an active one, and ServerURL.
|
|
func normalize(s Settings) Settings {
|
|
if len(s.Servers) == 0 {
|
|
u := s.ServerURL
|
|
if u == "" {
|
|
u = defaultServerURL
|
|
}
|
|
s.Servers = []Server{{ID: "default", Name: hostName(u), URL: u}}
|
|
}
|
|
if s.server(s.Active) == nil {
|
|
s.Active = s.Servers[0].ID
|
|
}
|
|
s.ServerURL = s.server(s.Active).URL
|
|
return s
|
|
}
|
|
|
|
func (s Settings) server(id string) *Server {
|
|
for i := range s.Servers {
|
|
if s.Servers[i].ID == id {
|
|
return &s.Servers[i]
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *SettingsService) save(settings Settings, removed ...string) (Settings, error) {
|
|
settings = normalize(settings)
|
|
if err := os.MkdirAll(filepath.Dir(s.path), 0o700); err != nil {
|
|
return Settings{}, err
|
|
}
|
|
data, _ := json.MarshalIndent(settings, "", " ")
|
|
if err := os.WriteFile(s.path, data, 0o600); err != nil {
|
|
return Settings{}, err
|
|
}
|
|
if s.changed != nil {
|
|
go s.changed(settings, removed)
|
|
}
|
|
return settings, nil
|
|
}
|
|
|
|
// cleanServerURL validates a docwell-server address.
|
|
func cleanServerURL(raw string) (string, error) {
|
|
u, err := url.Parse(strings.TrimSpace(raw))
|
|
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
|
|
return "", errors.New("server URL must be an http(s) address")
|
|
}
|
|
u.RawQuery, u.Fragment = "", ""
|
|
return strings.TrimRight(u.String(), "/"), nil
|
|
}
|
|
|
|
func hostName(raw string) string {
|
|
if u, err := url.Parse(raw); err == nil && u.Host != "" {
|
|
return u.Host
|
|
}
|
|
return raw
|
|
}
|
|
|
|
func newServerID() string {
|
|
b := make([]byte, 6)
|
|
_, _ = rand.Read(b)
|
|
return hex.EncodeToString(b)
|
|
}
|
|
|
|
// AddServer saves a server and makes it the active one. A server that is
|
|
// already in the list (same address) is made active instead of added twice.
|
|
func (s *SettingsService) AddServer(name, rawURL string) (Settings, error) {
|
|
u, err := cleanServerURL(rawURL)
|
|
if err != nil {
|
|
return Settings{}, err
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
settings, _ := s.load()
|
|
for _, sv := range settings.Servers {
|
|
if strings.EqualFold(sv.URL, u) {
|
|
settings.Active = sv.ID
|
|
return s.save(settings)
|
|
}
|
|
}
|
|
name = strings.TrimSpace(name)
|
|
if name == "" {
|
|
name = hostName(u)
|
|
}
|
|
if len(name) > 80 {
|
|
name = name[:80]
|
|
}
|
|
sv := Server{ID: newServerID(), Name: name, URL: u}
|
|
settings.Servers = append(settings.Servers, sv)
|
|
settings.Active = sv.ID
|
|
return s.save(settings)
|
|
}
|
|
|
|
// RenameServer changes how a server is listed.
|
|
func (s *SettingsService) RenameServer(id, name string) (Settings, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
settings, _ := s.load()
|
|
sv := settings.server(id)
|
|
if sv == nil {
|
|
return Settings{}, errors.New("no such server")
|
|
}
|
|
if name = strings.TrimSpace(name); name != "" {
|
|
sv.Name = name
|
|
}
|
|
return s.save(settings)
|
|
}
|
|
|
|
// RemoveServer forgets a server. The caller signs out of it first (its
|
|
// token is in the keychain). The last server cannot be removed.
|
|
func (s *SettingsService) RemoveServer(id string) (Settings, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
settings, _ := s.load()
|
|
if len(settings.Servers) <= 1 {
|
|
return Settings{}, errors.New("keep at least one server")
|
|
}
|
|
kept := settings.Servers[:0]
|
|
for _, sv := range settings.Servers {
|
|
if sv.ID != id {
|
|
kept = append(kept, sv)
|
|
}
|
|
}
|
|
settings.Servers = kept
|
|
return s.save(settings, id)
|
|
}
|
|
|
|
// SetActive switches the window to another saved server; the frontend
|
|
// reloads afterwards.
|
|
func (s *SettingsService) SetActive(id string) (Settings, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
settings, _ := s.load()
|
|
if settings.server(id) == nil {
|
|
return Settings{}, errors.New("no such server")
|
|
}
|
|
settings.Active = id
|
|
return s.save(settings)
|
|
}
|
|
|
|
// SetServerURL changes the active server's address (settings from the
|
|
// first desktop builds had only this).
|
|
func (s *SettingsService) SetServerURL(raw string) (Settings, error) {
|
|
u, err := cleanServerURL(raw)
|
|
if err != nil {
|
|
return Settings{}, err
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
settings, _ := s.load()
|
|
settings.server(settings.Active).URL = u
|
|
return s.save(settings)
|
|
}
|