From ceac53b6d052fa6dbe379af95bb81abf7ad1ee97 Mon Sep 17 00:00:00 2001 From: boojack Date: Mon, 14 Nov 2022 22:21:19 +0800 Subject: [PATCH] feat: additional script system setting (#467) --- api/system.go | 2 + api/system_setting.go | 10 +++++ server/system.go | 11 ++++-- web/src/App.tsx | 5 +++ .../Settings/PreferencesSection.tsx | 8 ++-- web/src/components/Settings/SystemSection.tsx | 38 ++++++++++++++++++- web/src/locales/en.json | 2 +- web/src/locales/vi.json | 2 +- web/src/locales/zh.json | 2 +- web/src/types/modules/system.d.ts | 1 + 10 files changed, 68 insertions(+), 13 deletions(-) diff --git a/api/system.go b/api/system.go index 31fe135d..41220741 100644 --- a/api/system.go +++ b/api/system.go @@ -10,4 +10,6 @@ type SystemStatus struct { AllowSignUp bool `json:"allowSignUp"` // Additional style. AdditionalStyle string `json:"additionalStyle"` + // Additional script. + AdditionalScript string `json:"additionalScript"` } diff --git a/api/system_setting.go b/api/system_setting.go index 6327a0ff..ce35bb8a 100644 --- a/api/system_setting.go +++ b/api/system_setting.go @@ -12,6 +12,8 @@ const ( SystemSettingAllowSignUpName SystemSettingName = "allowSignUp" // SystemSettingAdditionalStyleName is the key type of additional style. SystemSettingAdditionalStyleName SystemSettingName = "additionalStyle" + // SystemSettingAdditionalScriptName is the key type of additional script. + SystemSettingAdditionalScriptName SystemSettingName = "additionalScript" ) func (key SystemSettingName) String() string { @@ -20,6 +22,8 @@ func (key SystemSettingName) String() string { return "allowSignUp" case SystemSettingAdditionalStyleName: return "additionalStyle" + case SystemSettingAdditionalScriptName: + return "additionalScript" } return "" } @@ -65,6 +69,12 @@ func (upsert SystemSettingUpsert) Validate() error { if err != nil { return fmt.Errorf("failed to unmarshal system setting additional style value") } + } else if upsert.Name == SystemSettingAdditionalScriptName { + value := "" + err := json.Unmarshal([]byte(upsert.Value), &value) + if err != nil { + return fmt.Errorf("failed to unmarshal system setting additional script value") + } } else { return fmt.Errorf("invalid system setting name") } diff --git a/server/system.go b/server/system.go index dad8fffa..64ea9d14 100644 --- a/server/system.go +++ b/server/system.go @@ -38,10 +38,11 @@ func (s *Server) registerSystemRoutes(g *echo.Group) { } systemStatus := api.SystemStatus{ - Host: hostUser, - Profile: s.Profile, - AllowSignUp: false, - AdditionalStyle: "", + Host: hostUser, + Profile: s.Profile, + AllowSignUp: false, + AdditionalStyle: "", + AdditionalScript: "", } systemSettingList, err := s.Store.FindSystemSettingList(ctx, &api.SystemSettingFind{}) @@ -59,6 +60,8 @@ func (s *Server) registerSystemRoutes(g *echo.Group) { systemStatus.AllowSignUp = value.(bool) } else if systemSetting.Name == api.SystemSettingAdditionalStyleName { systemStatus.AdditionalStyle = value.(string) + } else if systemSetting.Name == api.SystemSettingAdditionalScriptName { + systemStatus.AdditionalScript = value.(string) } } diff --git a/web/src/App.tsx b/web/src/App.tsx index 7a59fa85..2a94cf54 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -30,6 +30,11 @@ function App() { styleEl.setAttribute("type", "text/css"); document.head.appendChild(styleEl); } + if (status.additionalScript) { + const scriptEl = document.createElement("script"); + scriptEl.innerHTML = status.additionalScript; + document.head.appendChild(scriptEl); + } }); }, []); diff --git a/web/src/components/Settings/PreferencesSection.tsx b/web/src/components/Settings/PreferencesSection.tsx index 8c276657..5293340d 100644 --- a/web/src/components/Settings/PreferencesSection.tsx +++ b/web/src/components/Settings/PreferencesSection.tsx @@ -80,10 +80,6 @@ const PreferencesSection = () => { handleValueChanged={handleDefaultMemoVisibilityChanged} /> - + ); }; diff --git a/web/src/components/Settings/SystemSection.tsx b/web/src/components/Settings/SystemSection.tsx index 683617e1..0eeec04b 100644 --- a/web/src/components/Settings/SystemSection.tsx +++ b/web/src/components/Settings/SystemSection.tsx @@ -7,6 +7,7 @@ import "../../less/settings/preferences-section.less"; interface State { allowSignUp: boolean; additionalStyle: string; + additionalScript: string; } const SystemSection = () => { @@ -14,6 +15,7 @@ const SystemSection = () => { const [state, setState] = useState({ allowSignUp: false, additionalStyle: "", + additionalScript: "", }); useEffect(() => { @@ -22,6 +24,7 @@ const SystemSection = () => { setState({ allowSignUp: status.allowSignUp, additionalStyle: status.additionalStyle, + additionalScript: status.additionalScript, }); }); }, []); @@ -51,6 +54,20 @@ const SystemSection = () => { }); }; + const handleAdditionalScriptChanged = (value: string) => { + setState({ + ...state, + additionalScript: value, + }); + }; + + const handleSaveAdditionalScript = async () => { + await api.upsertSystemSetting({ + name: "additionalScript", + value: JSON.stringify(state.additionalScript), + }); + }; + return (

{t("common.basic")}

@@ -58,12 +75,12 @@ const SystemSection = () => { Allow user signup handleAllowSignUpChanged(event.target.checked)} /> -