后台管理重塑 + 主题 / 皮肤双轴 + 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:
+100
-20
@@ -29,6 +29,7 @@ func (a *API) Routes() http.Handler {
|
||||
mux.HandleFunc("/api/admin/logout", a.logout)
|
||||
mux.HandleFunc("/api/admin/me", a.guard(a.me))
|
||||
|
||||
mux.HandleFunc("/api/admin/dashboard", a.guard(a.dashboard))
|
||||
mux.HandleFunc("/api/admin/posts", a.guard(a.listPosts))
|
||||
mux.HandleFunc("/api/admin/posts/", a.guard(a.postByID))
|
||||
mux.HandleFunc("/api/admin/tags", a.guard(a.listTags))
|
||||
@@ -128,12 +129,13 @@ func (a *API) listPosts(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
o := store.ListOptions{
|
||||
Kind: httpx.QueryString(r, "kind"),
|
||||
Tag: httpx.QueryString(r, "tag"),
|
||||
Query: httpx.QueryString(r, "q"),
|
||||
Status: httpx.QueryString(r, "status"),
|
||||
Page: httpx.QueryInt(r, "page", 1),
|
||||
Size: httpx.QueryInt(r, "size", 20),
|
||||
Kind: httpx.QueryString(r, "kind"),
|
||||
Tag: httpx.QueryString(r, "tag"),
|
||||
Query: httpx.QueryString(r, "q"),
|
||||
Status: httpx.QueryString(r, "status"),
|
||||
OrderBy: httpx.QueryString(r, "order"),
|
||||
Page: httpx.QueryInt(r, "page", 1),
|
||||
Size: httpx.QueryInt(r, "size", 20),
|
||||
}
|
||||
if o.Status == "" {
|
||||
o.Status = "any"
|
||||
@@ -150,7 +152,7 @@ func (a *API) listPosts(w http.ResponseWriter, r *http.Request) {
|
||||
httpx.BadRequest(w, "invalid body")
|
||||
return
|
||||
}
|
||||
in.Status = normalizeStatus(in.Status)
|
||||
in.Status = store.NormalizeStatus(in.Status)
|
||||
p, err := a.Store.Create(in)
|
||||
if err != nil {
|
||||
httpx.ServerError(w, err)
|
||||
@@ -162,21 +164,16 @@ func (a *API) listPosts(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeStatus(s string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(s)) {
|
||||
case model.StatusDraft, model.StatusPublished:
|
||||
return strings.ToLower(strings.TrimSpace(s))
|
||||
default:
|
||||
return model.StatusDraft
|
||||
}
|
||||
}
|
||||
|
||||
func (a *API) postByID(w http.ResponseWriter, r *http.Request) {
|
||||
rest := strings.Trim(strings.TrimPrefix(r.URL.Path, "/api/admin/posts/"), "/")
|
||||
if rest == "" {
|
||||
a.listPosts(w, r)
|
||||
return
|
||||
}
|
||||
if rest == "bulk" {
|
||||
a.bulkPosts(w, r)
|
||||
return
|
||||
}
|
||||
id, err := parseInt(rest)
|
||||
if err != nil {
|
||||
httpx.BadRequest(w, "bad post id")
|
||||
@@ -193,7 +190,7 @@ func (a *API) postByID(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
if in.Status != "" {
|
||||
in.Status = normalizeStatus(in.Status)
|
||||
in.Status = store.NormalizeStatus(in.Status)
|
||||
}
|
||||
p, err := a.Store.Update(id, in)
|
||||
writeOne(w, p, err)
|
||||
@@ -264,21 +261,27 @@ func (a *API) listTags(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (a *API) tagByID(w http.ResponseWriter, r *http.Request) {
|
||||
rest := strings.Trim(strings.TrimPrefix(r.URL.Path, "/api/admin/tags/"), "/")
|
||||
id, err := parseInt(rest)
|
||||
parts := strings.SplitN(rest, "/", 2)
|
||||
id, err := parseInt(parts[0])
|
||||
if err != nil {
|
||||
httpx.BadRequest(w, "bad tag id")
|
||||
return
|
||||
}
|
||||
if len(parts) == 2 && parts[1] == "merge" {
|
||||
a.mergeTag(w, r, id)
|
||||
return
|
||||
}
|
||||
switch r.Method {
|
||||
case http.MethodPut, http.MethodPatch:
|
||||
var in struct {
|
||||
Name string `json:"name"`
|
||||
Name string `json:"name"`
|
||||
Color string `json:"color"`
|
||||
}
|
||||
if err := httpx.Decode(r, &in); err != nil {
|
||||
httpx.BadRequest(w, "invalid body")
|
||||
return
|
||||
}
|
||||
t, err := a.Store.RenameTag(id, in.Name)
|
||||
t, err := a.Store.UpdateTag(id, in.Name, in.Color)
|
||||
writeTag(w, t, err)
|
||||
case http.MethodDelete:
|
||||
if err := a.Store.DeleteTag(id); err != nil {
|
||||
@@ -291,6 +294,26 @@ func (a *API) tagByID(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *API) mergeTag(w http.ResponseWriter, r *http.Request, fromID int64) {
|
||||
if r.Method != http.MethodPost {
|
||||
httpx.Error(w, http.StatusMethodNotAllowed, "POST required")
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
ToID int64 `json:"to_id"`
|
||||
}
|
||||
if err := httpx.Decode(r, &in); err != nil {
|
||||
httpx.BadRequest(w, "invalid body")
|
||||
return
|
||||
}
|
||||
if in.ToID == 0 {
|
||||
httpx.BadRequest(w, "to_id required")
|
||||
return
|
||||
}
|
||||
t, err := a.Store.MergeTags(fromID, in.ToID)
|
||||
writeTag(w, t, err)
|
||||
}
|
||||
|
||||
func writeTag(w http.ResponseWriter, t model.Tag, err error) {
|
||||
if err != nil {
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
@@ -348,3 +371,60 @@ func parseInt(s string) (int64, error) {
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// ---------- dashboard ----------
|
||||
|
||||
func (a *API) dashboard(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
httpx.Error(w, http.StatusMethodNotAllowed, "GET required")
|
||||
return
|
||||
}
|
||||
d, err := a.Store.Dashboard()
|
||||
if err != nil {
|
||||
httpx.ServerError(w, err)
|
||||
return
|
||||
}
|
||||
httpx.OK(w, d)
|
||||
}
|
||||
|
||||
// ---------- bulk posts ----------
|
||||
|
||||
type bulkPostsRequest struct {
|
||||
IDs []int64 `json:"ids"`
|
||||
Action string `json:"action"` // "publish" | "draft" | "delete"
|
||||
}
|
||||
|
||||
func (a *API) bulkPosts(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
httpx.Error(w, http.StatusMethodNotAllowed, "POST required")
|
||||
return
|
||||
}
|
||||
var in bulkPostsRequest
|
||||
if err := httpx.Decode(r, &in); err != nil {
|
||||
httpx.BadRequest(w, "invalid body")
|
||||
return
|
||||
}
|
||||
if len(in.IDs) == 0 {
|
||||
httpx.BadRequest(w, "ids required")
|
||||
return
|
||||
}
|
||||
switch in.Action {
|
||||
case "publish", "draft":
|
||||
n, err := a.Store.BulkUpdateStatus(in.IDs, store.NormalizeStatus(in.Action))
|
||||
if err != nil {
|
||||
httpx.ServerError(w, err)
|
||||
return
|
||||
}
|
||||
httpx.OK(w, map[string]any{"ok": true, "updated": n})
|
||||
case "delete":
|
||||
var failed int
|
||||
for _, id := range in.IDs {
|
||||
if err := a.Store.Delete(id); err != nil {
|
||||
failed++
|
||||
}
|
||||
}
|
||||
httpx.OK(w, map[string]any{"ok": true, "deleted": len(in.IDs) - failed, "failed": failed})
|
||||
default:
|
||||
httpx.BadRequest(w, "action must be one of publish|draft|delete")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user