-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblocks_core.go
More file actions
114 lines (95 loc) · 3.48 KB
/
Copy pathblocks_core.go
File metadata and controls
114 lines (95 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package myrtle
import (
"strings"
"github.com/gzuidhof/myrtle/theme"
)
// RenderContext contains resolved email values available to block text renderers.
type RenderContext struct {
Preheader string
Values theme.Values
}
// InsetMode controls how a block is horizontally inset within the main content container.
// For example, an image with InsetModeDefault keeps the theme's normal side padding,
// InsetModeNone spans edge-to-edge inside the container, and InsetModeCustom uses
// LayoutSpec.CustomInset as the per-block inset value.
type InsetMode string
const (
// InsetModeDefault uses the theme default content inset for the block.
InsetModeDefault InsetMode = "default"
// InsetModeNone removes side insets so the block renders full-bleed within the container.
InsetModeNone InsetMode = "none"
// InsetModeCustom uses LayoutSpec.CustomInset to set a custom side inset for the block.
InsetModeCustom InsetMode = "custom"
)
// Tone defines semantic visual emphasis used by tone-aware blocks.
type Tone string
const (
// ToneDefault renders neutral/default styling.
ToneDefault Tone = "default"
// TonePrimary renders primary accent styling.
TonePrimary Tone = "primary"
// ToneSecondary renders secondary accent styling.
ToneSecondary Tone = "secondary"
// ToneMuted renders de-emphasized styling.
ToneMuted Tone = "muted"
// ToneInfo renders informational semantic styling.
ToneInfo Tone = "info"
// ToneSuccess renders success semantic styling.
ToneSuccess Tone = "success"
// ToneWarning renders warning semantic styling.
ToneWarning Tone = "warning"
// ToneDanger renders danger/error semantic styling.
ToneDanger Tone = "danger"
// ToneDark renders high-contrast dark styling.
ToneDark Tone = "dark"
)
func normalizedTone(value Tone) Tone {
switch value {
case TonePrimary, ToneSecondary, ToneMuted, ToneInfo, ToneSuccess, ToneWarning, ToneDanger, ToneDark:
return value
default:
return ToneDefault
}
}
// LayoutSpec describes per-block layout behavior that themes can use when placing the block.
type LayoutSpec struct {
InsetMode InsetMode
CustomInset string
}
// Block is the core content unit in an email, with HTML template data and text fallback rendering behavior.
type Block interface {
// Kind returns the theme block kind used to select the HTML template for this block.
Kind() theme.BlockKind
// TemplateData returns the normalized data payload passed into the block's HTML template.
TemplateData() any
// RenderText returns the plain-text representation of the block for text-only email output.
RenderText(context RenderContext) (string, error)
// LayoutSpec returns layout metadata, such as inset behavior, used by theme layout templates.
LayoutSpec() LayoutSpec
}
func normalizedLayoutSpec(spec LayoutSpec) LayoutSpec {
if spec.InsetMode != InsetModeNone && spec.InsetMode != InsetModeCustom {
spec.InsetMode = InsetModeDefault
}
spec.CustomInset = strings.TrimSpace(spec.CustomInset)
if spec.InsetMode == InsetModeCustom && spec.CustomInset == "" {
spec.InsetMode = InsetModeDefault
}
return spec
}
func defaultLayoutSpec() LayoutSpec {
return LayoutSpec{InsetMode: InsetModeDefault}
}
func (group *Group) Kind() theme.BlockKind {
return theme.BlockKind("group")
}
func (group *Group) TemplateData() any {
return group
}
func (group *Group) RenderText(context RenderContext) (string, error) {
if group == nil {
return "", nil
}
return renderColumnText(group.Blocks(), context)
}
func (group *Group) LayoutSpec() LayoutSpec { return defaultLayoutSpec() }