Support color customization from the admin (#2338)

* Add user-customizable theming. Closes #1915

* Prettified Code!

* Add user-customizable theming. Closes #1915

* Add explicit color for page content background

* Prettified Code!

Co-authored-by: gabek <gabek@users.noreply.github.com>
This commit is contained in:
Gabe Kangas
2022-11-12 20:26:55 -08:00
committed by GitHub
parent 5bbf9db84a
commit 813f8692f0
33 changed files with 499 additions and 65 deletions

View File

@@ -67,6 +67,7 @@ const (
hasConfiguredInitialNotificationsKey = "has_configured_initial_notifications"
hideViewerCountKey = "hide_viewer_count"
customOfflineMessageKey = "custom_offline_message"
customColorVariableValuesKey = "custom_color_variable_values"
)
// GetExtraPageBodyContent will return the user-supplied body content.
@@ -932,3 +933,14 @@ func GetCustomOfflineMessage() string {
func SetCustomOfflineMessage(message string) error {
return _datastore.SetString(customOfflineMessageKey, message)
}
// SetCustomColorVariableValues sets CSS variable names and values.
func SetCustomColorVariableValues(variables map[string]string) error {
return _datastore.SetStringMap(customColorVariableValuesKey, variables)
}
// GetCustomColorVariableValues gets CSS variable names and values.
func GetCustomColorVariableValues() map[string]string {
values, _ := _datastore.GetStringMap(customColorVariableValuesKey)
return values
}

View File

@@ -19,6 +19,13 @@ func (c *ConfigEntry) getStringSlice() ([]string, error) {
return result, err
}
func (c *ConfigEntry) getStringMap() (map[string]string, error) {
decoder := c.getDecoder()
var result map[string]string
err := decoder.Decode(&result)
return result, err
}
func (c *ConfigEntry) getString() (string, error) {
decoder := c.getDecoder()
var result string

View File

@@ -110,6 +110,40 @@ func TestCustomType(t *testing.T) {
}
}
func TestStringMap(t *testing.T) {
const testKey = "test string map key"
testMap := map[string]string{
"test string 1": "test string 2",
"test string 3": "test string 4",
}
// Save config entry to the database
if err := _datastore.Save(ConfigEntry{testKey, &testMap}); err != nil {
t.Error(err)
}
// Get the config entry from the database
entryResult, err := _datastore.Get(testKey)
if err != nil {
t.Error(err)
}
testResult, err := entryResult.getStringMap()
if err != nil {
t.Error(err)
}
fmt.Printf("%+v", testResult)
if testResult["test string 1"] != testMap["test string 1"] {
t.Error("expected", testMap["test string 1"], "but test returned", testResult["test string 1"])
}
if testResult["test string 3"] != testMap["test string 3"] {
t.Error("expected", testMap["test string 3"], "but test returned", testResult["test string 3"])
}
}
// Custom type for testing
type TestStruct struct {
Test string

View File

@@ -59,3 +59,18 @@ func (ds *Datastore) SetBool(key string, value bool) error {
configEntry := ConfigEntry{key, value}
return ds.Save(configEntry)
}
// GetStringMap will return the string map value for a key.
func (ds *Datastore) GetStringMap(key string) (map[string]string, error) {
configEntry, err := ds.Get(key)
if err != nil {
return map[string]string{}, err
}
return configEntry.getStringMap()
}
// SetStringMap will set the string map value for a key.
func (ds *Datastore) SetStringMap(key string, value map[string]string) error {
configEntry := ConfigEntry{key, value}
return ds.Save(configEntry)
}