From 4b7643d8338636828629be76f241e42c286cfb67 Mon Sep 17 00:00:00 2001 From: cflmy Date: Fri, 18 Sep 2026 12:58:24 +0800 Subject: [PATCH] fix(web): prefer form redirect and soft-default now_if_empty MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Desk 文案 cancel links stamp redirect so POST leaves the compose page; empty date fields can opt into 空则现在 without blocking custom values. Co-authored-by: Cursor --- plugins/web/internal/form/form.go | 14 +++++++++++- plugins/web/internal/form/form_test.go | 3 +++ plugins/web/internal/form/source.go | 27 ++++++++++++++++++------ plugins/web/internal/form/source_test.go | 17 +++++++++++++++ plugins/web/internal/httpx/listen.go | 4 +++- 5 files changed, 56 insertions(+), 9 deletions(-) diff --git a/plugins/web/internal/form/form.go b/plugins/web/internal/form/form.go index b74ff27..eec01c8 100644 --- a/plugins/web/internal/form/form.go +++ b/plugins/web/internal/form/form.go @@ -69,6 +69,8 @@ func SetRules(formBag map[string]any, rules any) map[string]any { } // SetLabels stamps localized submit / cancel button copy (and optional cancel href). +// When cancelHref is set, also stamps redirect so successful POST lands on the list +// (or other cancel target) instead of the composing page's _mq_return path. func SetLabels(formBag map[string]any, submit, cancel, cancelHref string) map[string]any { out := cloneMap(formBag) if s := strings.TrimSpace(submit); s != "" { @@ -79,6 +81,7 @@ func SetLabels(formBag map[string]any, submit, cancel, cancelHref string) map[st } if s := strings.TrimSpace(cancelHref); s != "" { out["cancel_href"] = s + out["redirect"] = s } return out } @@ -725,7 +728,16 @@ func RenderBodyCtx(formBag map[string]any, formID string, data, errors any, csrf if csrf != "" { fmt.Fprintf(&body, ``, esc(csrf)) } - if action == "update" && rowID != "" { + hasIDField := false + for _, f := range fields { + if fm, ok := f.(map[string]any); ok { + if strOf(fm, "name", "") == "id" && IsClientSource(strOf(fm, "source", "client")) { + hasIDField = true + break + } + } + } + if action == "update" && rowID != "" && !hasIDField { fmt.Fprintf(&body, ``, esc(rowID)) } if ctx != nil { diff --git a/plugins/web/internal/form/form_test.go b/plugins/web/internal/form/form_test.go index d4df58c..c63fc6d 100644 --- a/plugins/web/internal/form/form_test.go +++ b/plugins/web/internal/form/form_test.go @@ -75,6 +75,9 @@ func TestRenderMarkdownAndLabels(t *testing.T) { map[string]any{"字段": "content", "标签": "正文", "类型": "markdown", "必填": true}, }) f = form.SetLabels(f, "发布文章", "返回列表", "/desk/posts") + if f["redirect"] != "/desk/posts" || f["cancel_href"] != "/desk/posts" { + t.Fatalf("labels should stamp redirect/cancel_href, got %#v %#v", f["redirect"], f["cancel_href"]) + } html := form.Render(f, "post", nil, nil, "") for _, want := range []string{ `data-mq-field="markdown"`, diff --git a/plugins/web/internal/form/source.go b/plugins/web/internal/form/source.go index b733558..18b7668 100644 --- a/plugins/web/internal/form/source.go +++ b/plugins/web/internal/form/source.go @@ -18,6 +18,8 @@ func normalizeSource(raw string) string { return "session.username" case "now", "utc.now", "server.now", "时间", "utc": return "now" + case "now_if_empty", "空则现在", "now?": + return "now_if_empty" default: return s } @@ -25,7 +27,8 @@ func normalizeSource(raw string) string { // IsClientSource reports whether the field is filled by the browser. func IsClientSource(source string) bool { - return normalizeSource(source) == "client" + src := normalizeSource(source) + return src == "client" || src == "now_if_empty" } // RequestContext carries session user and route params for form source stamping @@ -39,7 +42,8 @@ type RequestContext struct { } // ApplySources strips client-supplied values for non-client fields, then stamps -// from ctx. Always overwrites server-owned keys. +// from ctx. Always overwrites server-owned keys. `now_if_empty` keeps a non-empty +// client value and only stamps UTC now when the field was left blank. func ApplySources(formBag map[string]any, data map[string]any, ctx *RequestContext) { if data == nil { return @@ -57,12 +61,21 @@ func ApplySources(formBag map[string]any, data map[string]any, ctx *RequestConte continue } src := normalizeSource(strOf(fm, "source", "client")) - if src == "client" { + switch src { + case "client": continue - } - delete(data, name) - if v, ok := resolveSource(src, ctx); ok { - data[name] = v + case "now_if_empty": + if data[name] != nil && strings.TrimSpace(cellStr(data[name])) != "" { + continue + } + if v, ok := resolveSource("now", ctx); ok { + data[name] = v + } + default: + delete(data, name) + if v, ok := resolveSource(src, ctx); ok { + data[name] = v + } } } } diff --git a/plugins/web/internal/form/source_test.go b/plugins/web/internal/form/source_test.go index e99f82c..27c5c54 100644 --- a/plugins/web/internal/form/source_test.go +++ b/plugins/web/internal/form/source_test.go @@ -32,6 +32,23 @@ func TestApplySourcesOverwritesClient(t *testing.T) { } } +func TestApplySourcesNowIfEmpty(t *testing.T) { + frm := SetFields(New("news", "insert", "n"), []any{ + map[string]any{"字段": "title", "标签": "t", "类型": "text", "必填": true}, + map[string]any{"字段": "published_at", "标签": "d", "类型": "text", "来源": "空则现在"}, + }) + empty := map[string]any{"title": "x", "published_at": ""} + ApplySources(frm, empty, &RequestContext{}) + if empty["published_at"] == nil || empty["published_at"] == "" { + t.Fatalf("expected stamp, got %#v", empty["published_at"]) + } + kept := map[string]any{"title": "x", "published_at": "2020-01-02"} + ApplySources(frm, kept, &RequestContext{}) + if kept["published_at"] != "2020-01-02" { + t.Fatalf("should keep client date, got %#v", kept["published_at"]) + } +} + func TestRenderSkipsServerSources(t *testing.T) { frm := SetFields(New("comments", "insert", "c"), []any{ map[string]any{"字段": "body", "标签": "Body", "类型": "textarea", "必填": true}, diff --git a/plugins/web/internal/httpx/listen.go b/plugins/web/internal/httpx/listen.go index f1fcd92..b2339a6 100644 --- a/plugins/web/internal/httpx/listen.go +++ b/plugins/web/internal/httpx/listen.go @@ -504,8 +504,10 @@ func (st *state) handleFormPost(w http.ResponseWriter, r *http.Request) { return } if ok, _ := res["ok"].(bool); ok { + // Prefer an explicit form redirect (e.g. cancel_href list URL) over the + // composing page path stamped as _mq_return. redir := strOpt(res, "redirect", "/") - if ctx.ReturnPath != "" { + if (redir == "" || redir == "/") && ctx.ReturnPath != "" { redir = ctx.ReturnPath } http.Redirect(w, r, redir, http.StatusSeeOther)