Add ability to set CSS classes instead of inline styles (#103)
* Add ability to set CSS classes instead of inline styles This allows to set a `ClassName` field in `Style` structs. Setting this field to anything but "" will cause all other styles to be ignored. The element will then have a `class=` tag instead with the corresponding name. Possible reasons to use this: * Including multiple SVGs on the same webside, using the same styles * Desire to use strict CSP headers * Add warning that setting `ClassName` will drop all other inline styles
This commit is contained in:
parent
6735e8990a
commit
f97f94425f
6 changed files with 120 additions and 7 deletions
31
style.go
31
style.go
|
|
@ -39,6 +39,8 @@ type Style struct {
|
|||
Show bool
|
||||
Padding Box
|
||||
|
||||
ClassName string
|
||||
|
||||
StrokeWidth float64
|
||||
StrokeColor drawing.Color
|
||||
StrokeDashArray []float64
|
||||
|
|
@ -71,7 +73,8 @@ func (s Style) IsZero() bool {
|
|||
s.FillColor.IsZero() &&
|
||||
s.FontColor.IsZero() &&
|
||||
s.FontSize == 0 &&
|
||||
s.Font == nil
|
||||
s.Font == nil &&
|
||||
s.ClassName == ""
|
||||
}
|
||||
|
||||
// String returns a text representation of the style.
|
||||
|
|
@ -87,6 +90,12 @@ func (s Style) String() string {
|
|||
output = []string{"\"show\": false"}
|
||||
}
|
||||
|
||||
if s.ClassName != "" {
|
||||
output = append(output, fmt.Sprintf("\"class_name\": %s", s.ClassName))
|
||||
} else {
|
||||
output = append(output, "\"class_name\": null")
|
||||
}
|
||||
|
||||
if !s.Padding.IsZero() {
|
||||
output = append(output, fmt.Sprintf("\"padding\": %s", s.Padding.String()))
|
||||
} else {
|
||||
|
|
@ -155,6 +164,16 @@ func (s Style) String() string {
|
|||
return "{" + strings.Join(output, ", ") + "}"
|
||||
}
|
||||
|
||||
func (s Style) GetClassName(defaults ...string) string {
|
||||
if s.ClassName == "" {
|
||||
if len(defaults) > 0 {
|
||||
return defaults[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
return s.ClassName
|
||||
}
|
||||
|
||||
// GetStrokeColor returns the stroke color.
|
||||
func (s Style) GetStrokeColor(defaults ...drawing.Color) drawing.Color {
|
||||
if s.StrokeColor.IsZero() {
|
||||
|
|
@ -321,6 +340,7 @@ func (s Style) GetTextRotationDegrees(defaults ...float64) float64 {
|
|||
|
||||
// WriteToRenderer passes the style's options to a renderer.
|
||||
func (s Style) WriteToRenderer(r Renderer) {
|
||||
r.SetClassName(s.GetClassName())
|
||||
r.SetStrokeColor(s.GetStrokeColor())
|
||||
r.SetStrokeWidth(s.GetStrokeWidth())
|
||||
r.SetStrokeDashArray(s.GetStrokeDashArray())
|
||||
|
|
@ -337,6 +357,7 @@ func (s Style) WriteToRenderer(r Renderer) {
|
|||
|
||||
// WriteDrawingOptionsToRenderer passes just the drawing style options to a renderer.
|
||||
func (s Style) WriteDrawingOptionsToRenderer(r Renderer) {
|
||||
r.SetClassName(s.GetClassName())
|
||||
r.SetStrokeColor(s.GetStrokeColor())
|
||||
r.SetStrokeWidth(s.GetStrokeWidth())
|
||||
r.SetStrokeDashArray(s.GetStrokeDashArray())
|
||||
|
|
@ -345,6 +366,7 @@ func (s Style) WriteDrawingOptionsToRenderer(r Renderer) {
|
|||
|
||||
// WriteTextOptionsToRenderer passes just the text style options to a renderer.
|
||||
func (s Style) WriteTextOptionsToRenderer(r Renderer) {
|
||||
r.SetClassName(s.GetClassName())
|
||||
r.SetFont(s.GetFont())
|
||||
r.SetFontColor(s.GetFontColor())
|
||||
r.SetFontSize(s.GetFontSize())
|
||||
|
|
@ -352,6 +374,8 @@ func (s Style) WriteTextOptionsToRenderer(r Renderer) {
|
|||
|
||||
// InheritFrom coalesces two styles into a new style.
|
||||
func (s Style) InheritFrom(defaults Style) (final Style) {
|
||||
final.ClassName = s.GetClassName(defaults.ClassName)
|
||||
|
||||
final.StrokeColor = s.GetStrokeColor(defaults.StrokeColor)
|
||||
final.StrokeWidth = s.GetStrokeWidth(defaults.StrokeWidth)
|
||||
final.StrokeDashArray = s.GetStrokeDashArray(defaults.StrokeDashArray)
|
||||
|
|
@ -379,6 +403,7 @@ func (s Style) InheritFrom(defaults Style) (final Style) {
|
|||
// GetStrokeOptions returns the stroke components.
|
||||
func (s Style) GetStrokeOptions() Style {
|
||||
return Style{
|
||||
ClassName: s.ClassName,
|
||||
StrokeDashArray: s.StrokeDashArray,
|
||||
StrokeColor: s.StrokeColor,
|
||||
StrokeWidth: s.StrokeWidth,
|
||||
|
|
@ -388,6 +413,7 @@ func (s Style) GetStrokeOptions() Style {
|
|||
// GetFillOptions returns the fill components.
|
||||
func (s Style) GetFillOptions() Style {
|
||||
return Style{
|
||||
ClassName: s.ClassName,
|
||||
FillColor: s.FillColor,
|
||||
}
|
||||
}
|
||||
|
|
@ -395,6 +421,7 @@ func (s Style) GetFillOptions() Style {
|
|||
// GetDotOptions returns the dot components.
|
||||
func (s Style) GetDotOptions() Style {
|
||||
return Style{
|
||||
ClassName: s.ClassName,
|
||||
StrokeDashArray: nil,
|
||||
FillColor: s.DotColor,
|
||||
StrokeColor: s.DotColor,
|
||||
|
|
@ -405,6 +432,7 @@ func (s Style) GetDotOptions() Style {
|
|||
// GetFillAndStrokeOptions returns the fill and stroke components.
|
||||
func (s Style) GetFillAndStrokeOptions() Style {
|
||||
return Style{
|
||||
ClassName: s.ClassName,
|
||||
StrokeDashArray: s.StrokeDashArray,
|
||||
FillColor: s.FillColor,
|
||||
StrokeColor: s.StrokeColor,
|
||||
|
|
@ -415,6 +443,7 @@ func (s Style) GetFillAndStrokeOptions() Style {
|
|||
// GetTextOptions returns just the text components of the style.
|
||||
func (s Style) GetTextOptions() Style {
|
||||
return Style{
|
||||
ClassName: s.ClassName,
|
||||
FontColor: s.FontColor,
|
||||
FontSize: s.FontSize,
|
||||
Font: s.Font,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue