package web import ( "net/http" "time" "github.com/gramanas/blogspace/internal/store" ) func (s *Server) handleDesignForm(w http.ResponseWriter, r *http.Request) { blog := currentBlog(r) images, _, err := blogStore(r).ListFiles(r.Context(), "image", "", 0, 0) if err != nil { s.serverError(w, err) return } s.render(w, r, "dashboard/design.html", designData(ParseTheme(blog.ThemeJSON), images)) } // designData is what design.html shows; "today" is the sample for the date formats. func designData(theme Theme, images []store.File) map[string]any { return map[string]any{"theme": theme, "images": images, "presets": Presets(), "today": time.Now()} } func (s *Server) handleDesign(w http.ResponseWriter, r *http.Request) { blog := currentBlog(r) theme := ThemeFromForm(ParseTheme(blog.ThemeJSON), r.Form) // Optional direct uploads from the design form. for field, dst := range map[string]*string{"bg_image_file": &theme.BgImage, "header_image_file": &theme.HeaderImage, "logo_file": &theme.Logo, "favicon_file": &theme.Favicon} { img, err := s.readUpload(r, field, true) if err != nil { images, _, _ := blogStore(r).ListFiles(r.Context(), "image", "", 0, 0) d := designData(theme, images) d["error"] = err.Error() s.renderStatus(w, r, http.StatusBadRequest, "dashboard/design.html", d) return } if img != nil { *dst = img.ID.String() } } if err := blogStore(r).UpdateTheme(r.Context(), theme.JSON()); err != nil { s.serverError(w, err) return } redirectOK(w, r, "/b/"+blog.Subdomain+"/design", s.tr(r, "Design saved. Refresh your blog to see it.")) } // handleDesignPreset applies a colour scheme straight away; it is a separate // form so it cannot be confused with the unsaved edits of the main one. func (s *Server) handleDesignPreset(w http.ResponseWriter, r *http.Request) { blog := currentBlog(r) p, ok := PresetByKey(r.FormValue("preset")) if !ok { s.plainError(w, http.StatusBadRequest, s.tr(r, "Unknown colour scheme.")) return } theme := ParseTheme(blog.ThemeJSON).WithPreset(p) if err := blogStore(r).UpdateTheme(r.Context(), theme.JSON()); err != nil { s.serverError(w, err) return } redirectOK(w, r, "/b/"+blog.Subdomain+"/design", s.trf(r, "Applied the %s scheme. Refresh your blog to see it.", s.tr(r, p.Name))) } // handleDesignReset restores the defaults, layout modules included; images // stay in the library and the menu is left alone. func (s *Server) handleDesignReset(w http.ResponseWriter, r *http.Request) { blog := currentBlog(r) if err := blogStore(r).UpdateTheme(r.Context(), DefaultTheme().JSON()); err != nil { s.serverError(w, err) return } if err := blogStore(r).ResetModules(r.Context()); err != nil { s.serverError(w, err) return } redirectOK(w, r, "/b/"+blog.Subdomain+"/design", s.tr(r, "Design and layout reset to the defaults.")) }