feat(cabana): code editor, colour and mail image fields, mail branding and the image library

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-13 12:53:09 +01:00
parent b996a8fc83
commit 4fd2ed07eb
34 changed files with 1691 additions and 41 deletions

View File

@@ -2,6 +2,7 @@ package cabana
import (
"context"
"encoding/json"
"errors"
"fmt"
"regexp"
@@ -13,6 +14,7 @@ import (
"time"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/lagoon/attach"
"git.golem15.com/golem15/summercms/modules/pact"
"git.golem15.com/golem15/summercms/modules/phrasebook"
"git.golem15.com/golem15/summercms/modules/postcard"
@@ -44,7 +46,12 @@ func mailPermissions() []pact.Permission {
}
func (m *mailScreens) controllers() []pact.AdminController {
return []pact.AdminController{mailTemplatesAdmin{mail: m}, mailLayoutsAdmin{mail: m}, mailPartialsAdmin{mail: m}}
return []pact.AdminController{
mailTemplatesAdmin{mail: m},
mailLayoutsAdmin{mail: m},
mailPartialsAdmin{mail: m},
mailImagesAdmin{mail: m},
}
}
// settings are the Settings → Mail entries.
@@ -80,6 +87,27 @@ func (m *mailScreens) settings() []pact.SettingsItem {
Permissions: []string{PermissionManageMailLayouts},
Controller: MailPartialsControllerID,
},
{
Code: "mail_brand",
Label: mailLang + "brand.menu_label",
Description: mailLang + "brand.menu_description",
Category: mailCategory,
Icon: "palette",
Order: 230,
Permissions: []string{PermissionManageMailLayouts},
Form: "models/mailbrandsetting/fields.yaml",
NewModel: func() any { return &MailBrandSetting{} },
},
{
Code: "mail_images",
Label: mailLang + "images.menu_label",
Description: mailLang + "images.menu_description",
Category: mailCategory,
Icon: "image",
Order: 240,
Permissions: []string{PermissionManageMailLayouts},
Controller: MailImagesControllerID,
},
}
}
@@ -1441,3 +1469,199 @@ func partialUsage(code, sections string) string {
b.WriteString(" }}")
return b.String()
}
// MailBrandSetting is the settings form over postcard's branding row (id 1).
// Saving it switches mail from mail.brandCss to the compiled colours.
type MailBrandSetting struct {
ID uint `gorm:"column:id;primaryKey"`
LogoFileID *uint `gorm:"column:logo_file_id"`
BodyBg string `gorm:"column:body_bg"`
ContentBg string `gorm:"column:content_bg"`
HeaderColor string `gorm:"column:header_color"`
FooterColor string `gorm:"column:footer_color"`
HeadingColor string `gorm:"column:heading_color"`
TextColor string `gorm:"column:text_color"`
LinkColor string `gorm:"column:link_color"`
ButtonTextColor string `gorm:"column:button_text_color"`
ButtonPrimaryBg string `gorm:"column:button_primary_bg"`
ButtonPositiveBg string `gorm:"column:button_positive_bg"`
ButtonNegativeBg string `gorm:"column:button_negative_bg"`
PanelBg string `gorm:"column:panel_bg"`
TableBorderColor string `gorm:"column:table_border_color"`
CreatedAt time.Time `gorm:"column:created_at"`
UpdatedAt time.Time `gorm:"column:updated_at"`
}
func (MailBrandSetting) TableName() string { return postcard.BrandTable }
func (MailBrandSetting) Fillable() []string {
return []string{
"logo_file_id", "body_bg", "content_bg", "header_color", "footer_color",
"heading_color", "text_color", "link_color", "button_text_color",
"button_primary_bg", "button_positive_bg", "button_negative_bg",
"panel_bg", "table_border_color",
}
}
func (MailBrandSetting) Rules() map[string]string {
return map[string]string{
"logo_file_id": "nullable",
"body_bg": "nullable",
"content_bg": "nullable",
"header_color": "nullable",
"footer_color": "nullable",
"heading_color": "nullable",
"text_color": "nullable",
"link_color": "nullable",
"button_text_color": "nullable",
"button_primary_bg": "nullable",
"button_positive_bg": "nullable",
"button_negative_bg": "nullable",
"panel_bg": "nullable",
"table_border_color": "nullable",
}
}
var brandColourPattern = regexp.MustCompile(`^#[0-9a-f]{6}$`)
// BeforeValidate lower-cases colours, refuses anything but #rrggbb or empty,
// and checks logo_file_id is null or a library image.
func (b *MailBrandSetting) BeforeValidate(tx *gorm.DB) error {
details := map[string]any{}
for _, col := range []struct {
name string
value *string
}{
{"body_bg", &b.BodyBg},
{"content_bg", &b.ContentBg},
{"header_color", &b.HeaderColor},
{"footer_color", &b.FooterColor},
{"heading_color", &b.HeadingColor},
{"text_color", &b.TextColor},
{"link_color", &b.LinkColor},
{"button_text_color", &b.ButtonTextColor},
{"button_primary_bg", &b.ButtonPrimaryBg},
{"button_positive_bg", &b.ButtonPositiveBg},
{"button_negative_bg", &b.ButtonNegativeBg},
{"panel_bg", &b.PanelBg},
{"table_border_color", &b.TableBorderColor},
} {
colour := strings.ToLower(strings.TrimSpace(*col.value))
*col.value = colour
if colour == "" {
continue
}
if !brandColourPattern.MatchString(colour) {
details[col.name] = []string{"backend::lang.colorpicker.invalid"}
}
}
if b.LogoFileID != nil {
var n int64
if err := tx.Session(&gorm.Session{NewDB: true}).
Table("system_files").
Where("id = ? AND attachment_type = ? AND field = ?", *b.LogoFileID, mailImageLibraryMorph, "images").
Count(&n).Error; err != nil {
return err
}
if n == 0 {
details["logo_file_id"] = []string{"backend::lang.mailimage.missing"}
}
}
if len(details) > 0 {
return &ValidationError{Details: details}
}
return nil
}
// mailImageLibraryMorph is the attachment_type of the mail image library host.
const mailImageLibraryMorph = `System\Models\MailImageLibrary`
// MailImageLibrary is the one-row host of the mail image library's attachMany
// images relation.
type MailImageLibrary struct {
ID uint `gorm:"column:id;primaryKey"`
CreatedAt time.Time `gorm:"column:created_at"`
UpdatedAt time.Time `gorm:"column:updated_at"`
}
func (MailImageLibrary) TableName() string { return postcard.ImageLibraryTable }
func (MailImageLibrary) MorphName() string { return mailImageLibraryMorph }
func (MailImageLibrary) AttachRelations() []attach.Relation {
return []attach.Relation{{Name: "images", Many: true, Public: true}}
}
func (MailImageLibrary) Fillable() []string { return nil }
func (MailImageLibrary) Rules() map[string]string { return nil }
// mailImagesAdmin is the Mail images singleton screen.
type mailImagesAdmin struct {
mail *mailScreens
}
var (
_ pact.AdminController = mailImagesAdmin{}
_ pact.AdminPermissioned = mailImagesAdmin{}
_ pact.AdminSingleton = mailImagesAdmin{}
)
func (mailImagesAdmin) ID() string { return MailImagesControllerID }
func (mailImagesAdmin) ModelName() string { return `System\Models\MailImageLibrary` }
func (mailImagesAdmin) ConfigDir() string { return "controllers/mailimages" }
func (mailImagesAdmin) RequiredPermissions() []string {
return []string{PermissionManageMailLayouts}
}
func (mailImagesAdmin) NewRecord() any { return &MailImageLibrary{} }
// SingletonRecord returns the library host row, creating it when absent.
func (mailImagesAdmin) SingletonRecord(ctx context.Context, db *gorm.DB) (any, error) {
var row MailImageLibrary
err := db.WithContext(ctx).Order("id").Limit(1).Take(&row).Error
if err == nil {
return &row, nil
}
if !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
row = MailImageLibrary{}
if err := db.WithContext(ctx).Create(&row).Error; err != nil {
return nil, err
}
return &row, nil
}
// injectLayoutCreateDefaults copies postcard's default layout into the create
// form defaults of the layouts controller, so the YAML carries no copy of it.
func injectLayoutCreateDefaults(reg *Registry, editor *postcard.Editor) {
if reg == nil || editor == nil {
return
}
cc, ok := reg.Get(MailLayoutsControllerID)
if !ok || cc == nil || cc.Form == nil {
return
}
file, ok := editor.Catalog().Layout(mailDefaultLayout)
if !ok {
return
}
set := map[string]string{
"content_html": file.HTML,
"content_text": file.Text,
"content_css": "",
}
for i := range cc.Form.Fields {
if raw, ok := set[cc.Form.Fields[i].Name]; ok {
encoded, err := json.Marshal(raw)
if err != nil {
continue
}
cc.Form.Fields[i].Default = &jsonScalar{raw: encoded}
}
}
}