1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package element
- import (
- "golang.org/x/net/html/atom"
- )
- const (
- TextThemeSuccess = "success"
- TextThemeDanger = "danger"
- )
- type (
- TextOption func(t *Text)
- Text struct {
- Tag atom.Atom
- Content string
- Theme string
- Color string
- Style Style
- }
- )
- func WithTextTheme(s string) TextOption {
- return func(t *Text) {
- t.Theme = s
- }
- }
- func WithTextStyle(ms map[string]string) TextOption {
- return func(t *Text) {
- t.Style = ms
- }
- }
- func WithTextColor(color string) TextOption {
- return func(t *Text) {
- t.Color = color
- }
- }
- func (text *Text) Html() string {
- return text.String()
- }
- func (text *Text) String() string {
- var className string
- if text.Style == nil {
- text.Style = make(map[string]string)
- }
- if text.Color != "" {
- text.Style["color"] = text.Color
- }
- if text.Tag == 0 {
- text.Tag = atom.Span
- }
- if text.Theme != "" {
- className = "text-" + text.Theme
- }
- return NewElement(
- text.Tag,
- text.Content,
- WithAttribute("class", className),
- WithAttribute("style", text.Style.String()),
- ).String()
- }
- func NewText(s string, opts ...TextOption) *Text {
- txt := &Text{Content: s, Tag: atom.Span}
- for _, cb := range opts {
- cb(txt)
- }
- return txt
- }
|