后台管理重塑 + 主题 / 皮肤双轴 + a11y 与编辑器增强

Backend
- Post 加 cover_url,Tag 加 color;老库 ALTER 兼容
- admin API: GET /api/admin/dashboard,POST /bulk,POST /tags/:id/merge,
  PUT /tags/:id 接 {name,color},Settings 加 light_skin_id(兼容老 theme_id)
- 白名单兜底(非法 light_skin_id 落 paper)
- 新增合并 / Dashboard / Theme 测试覆盖

Frontend
- AdminLayout 改为侧边栏布局;新增 Dashboard 总览页
- PostsView 卡片化预览 + 筛选 chip + 批量操作 + 排序
- EditorView 增强:封面图 / WYSIWYG↔MD 双模式 / 实时大纲 / 字数统计 /
  自动保存条 / ⌘S / unsaved 守卫(beforeunload + beforeRouteLeave)
- TagsView 加色板 + 合并到目标
- SettingsView 主题外观改为「亮色皮肤」三选一 + 「暗色皮肤」固定墨色说明
- 新增 CommandPalette(⌘K)+ ShortcutsPanel(?)
- api.js 接新接口;router.js 拆 /admin → /admin/posts

主题 / 皮肤双轴
- site.js: themeMode(light/dark/auto)+ light_skin_id;data-theme 前台,
  data-admin-theme 后台(仅跟随 mode 切明暗)
- styles.css: :root[data-theme] 四套皮肤 + :root[data-admin-theme=dark] 后台墨感
- 修 sticky head / ThemeSwitcher / 表单 / 批量条 等 token 引用
- 后台用独立 --admin-* 系列,与前台主题解耦
- 左栏底部放 inline 形态 ThemeSwitcher(图标按钮 36×36),mobile 保留浮窗

a11y
- 全局 :focus-visible 描边(前后台跟随 token)
- prefers-reduced-motion 全局降级
- color-scheme 随主题切
- meta theme-color 双套(light/dark)
- 命令面板 / 快捷键面板:role=dialog aria-modal aria-label + 搜索框 aria-label
- icon-only 按钮加 aria-label(tag × / 清除选择 / 移除封面等)
- 输入框 aria-label + spellcheck=false
- PostsView 筛选同步 URL(可分享、可刷新保留)
- img width/height + loading=lazy
- utils.js 用 Intl.DateTimeFormat(zh-CN / sv-SE)
This commit is contained in:
cjun
2026-09-21 22:14:58 +08:00
parent 4d8f2de3a4
commit a721e26e45
32 changed files with 5823 additions and 976 deletions
+224
View File
@@ -40,3 +40,227 @@ func TestPostInputDefaults(t *testing.T) {
t.Errorf("empty status means draft is applied by the API layer, got %q", in.Status)
}
}
func TestNormalizeStatus(t *testing.T) {
cases := map[string]string{
"": model.StatusDraft,
"draft": model.StatusDraft,
"published": model.StatusPublished,
" Published ": model.StatusPublished,
"pending": model.StatusDraft, // unknown → draft
}
for in, want := range cases {
if got := NormalizeStatus(in); got != want {
t.Errorf("NormalizeStatus(%q) = %q, want %q", in, got, want)
}
}
}
func TestRebindBulk(t *testing.T) {
// BulkUpdateStatus uses an IN clause with len(ids) placeholders + 1 for status.
d := &db.DB{Dialect: db.Postgres}
q := d.Q(`UPDATE posts SET status=? WHERE id IN (?,?,?)`)
if got := strings.Count(q, "$"); got != 4 {
t.Errorf("expected 4 placeholders, got %d", got)
}
}
func TestSettingsThemeID(t *testing.T) {
s := openTestStore(t)
st, err := s.GetSettings()
if err != nil {
t.Fatalf("get: %v", err)
}
if st.LightSkinID != "paper" {
t.Errorf("default light_skin_id should be 'paper', got %q", st.LightSkinID)
}
st.LightSkinID = "sage"
st.SiteTitle = "ONE"
if err := s.UpdateSettings(st); err != nil {
t.Fatalf("update: %v", err)
}
got, err := s.GetSettings()
if err != nil {
t.Fatalf("get again: %v", err)
}
if got.LightSkinID != "sage" {
t.Errorf("light_skin_id should persist, got %q", got.LightSkinID)
}
if got.ThemeID != "sage" {
t.Errorf("legacy theme_id should mirror light_skin_id, got %q", got.ThemeID)
}
if got.SiteTitle != "ONE" {
t.Errorf("site_title should persist, got %q", got.SiteTitle)
}
}
func TestSettingsLightSkinFallback(t *testing.T) {
s := openTestStore(t)
// Simulate an older DB that only has theme_id set.
if _, err := s.db.Exec(s.db.Q(`INSERT INTO settings(key,value) VALUES (?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value`),
"theme_id", "rose"); err != nil {
t.Fatalf("seed theme_id: %v", err)
}
st, err := s.GetSettings()
if err != nil {
t.Fatalf("get: %v", err)
}
if st.LightSkinID != "rose" {
t.Errorf("expected fallback to 'rose' from legacy theme_id, got %q", st.LightSkinID)
}
}
// TestStoreSQLite runs a small integration pass on top of an in-memory SQLite
// database to exercise the new fields (cover_url, tag color) and the merge
// + bulk + dashboard paths.
func TestStoreSQLite(t *testing.T) {
s := openTestStore(t)
p1, err := s.Create(model.PostInput{
Kind: model.KindLong,
Title: "第一篇",
Slug: "first",
Summary: "first summary",
CoverURL: "https://example.com/a.png",
ContentMd: "# hi\n这是一段正文。",
Status: model.StatusPublished,
Tags: []string{"Go", "博客"},
})
if err != nil {
t.Fatalf("create p1: %v", err)
}
if p1.CoverURL == "" {
t.Errorf("cover_url not persisted")
}
if len(p1.Tags) != 2 {
t.Errorf("expected 2 tags, got %v", p1.Tags)
}
p2, err := s.Create(model.PostInput{
Kind: model.KindShort, ContentMd: "碎片想法。", Status: model.StatusDraft,
Tags: []string{"碎片"},
})
if err != nil {
t.Fatalf("create p2: %v", err)
}
// Update cover_url and tags. Frontend sends the current CoverURL back so
// the value is preserved across updates.
upd, err := s.Update(p1.ID, model.PostInput{
Kind: model.KindLong, Title: "第一篇",
CoverURL: p1.CoverURL,
ContentMd: "# hi\n这是新版本。",
Tags: []string{"Go"},
})
if err != nil {
t.Fatalf("update: %v", err)
}
if upd.CoverURL != p1.CoverURL {
t.Errorf("cover_url not preserved when echoed back, got %q", upd.CoverURL)
}
// An empty CoverURL is treated as an explicit clear.
upd2, err := s.Update(p1.ID, model.PostInput{
Kind: model.KindLong, Title: "第一篇", CoverURL: "",
Tags: []string{"Go"},
})
if err != nil {
t.Fatalf("update clear: %v", err)
}
if upd2.CoverURL != "" {
t.Errorf("empty cover_url should clear, got %q", upd2.CoverURL)
}
// restore for downstream tests
if _, err := s.Update(p1.ID, model.PostInput{
Kind: model.KindLong, Title: "第一篇", CoverURL: p1.CoverURL,
Tags: []string{"Go"},
}); err != nil {
t.Fatalf("restore: %v", err)
}
if len(upd.Tags) != 1 || upd.Tags[0] != "Go" {
t.Errorf("tags after update: %v", upd.Tags)
}
// BulkUpdateStatus flips drafts to published
n, err := s.BulkUpdateStatus([]int64{p2.ID}, model.StatusPublished)
if err != nil {
t.Fatalf("bulk: %v", err)
}
if n != 1 {
t.Errorf("expected 1 updated, got %d", n)
}
// Tag merge: create two distinct slug tags, attach both to p1, merge.
if _, err := s.UpdateTag(0, "", ""); err == nil {
t.Errorf("expected error for empty name")
}
tags, err := s.ListTags()
if err != nil {
t.Fatalf("list tags: %v", err)
}
var goID int64
for _, tg := range tags {
if tg.Name == "Go" {
goID = tg.ID
}
}
if goID == 0 {
t.Fatalf("Go tag not found")
}
// Give the surviving target tag a color so we can confirm it sticks.
if _, err := s.UpdateTag(goID, "Go", "#3d7f9c"); err != nil {
t.Fatalf("color target: %v", err)
}
// CreateTagFull uses upsertTag so "GoLang" with its own slug is a fresh row.
golang, err := s.CreateTagFull("GoLang", "")
if err != nil {
t.Fatalf("create golang: %v", err)
}
if _, err := s.db.Exec(s.db.Q(`INSERT INTO post_tags(post_id, tag_id) VALUES (?, ?)`), p1.ID, golang.ID); err != nil {
t.Fatalf("attach golang: %v", err)
}
merged, err := s.MergeTags(golang.ID, goID)
if err != nil {
t.Fatalf("merge: %v", err)
}
if merged.ID != goID {
t.Errorf("merge should return target tag, got %d want %d", merged.ID, goID)
}
if merged.Color != "#3d7f9c" {
t.Errorf("target tag color lost, got %q", merged.Color)
}
// Dashboard
d, err := s.Dashboard()
if err != nil {
t.Fatalf("dashboard: %v", err)
}
if d.TotalPosts != 2 {
t.Errorf("total_posts = %d", d.TotalPosts)
}
if d.PublishedPosts != 2 {
t.Errorf("published_posts = %d", d.PublishedPosts)
}
if len(d.RecentPosts) == 0 {
t.Errorf("recent posts empty")
}
if len(d.TopTags) == 0 {
t.Errorf("top tags empty")
}
}
func openTestStore(t *testing.T) *Store {
t.Helper()
d, err := db.Open("sqlite", ":memory:")
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
s, err := New(d)
if err != nil {
t.Fatalf("new store: %v", err)
}
return s
}