mirror of
https://github.com/usememos/memos.git
synced 2024-11-11 07:24:18 +03:00
feat: additional style system setting (#444)
* feat: additional style system setting * feat: remove editor font setting
This commit is contained in:
parent
67691d1e99
commit
a142d975d7
@ -6,5 +6,8 @@ type SystemStatus struct {
|
|||||||
Host *User `json:"host"`
|
Host *User `json:"host"`
|
||||||
Profile *profile.Profile `json:"profile"`
|
Profile *profile.Profile `json:"profile"`
|
||||||
// System settings
|
// System settings
|
||||||
|
// Allow sign up.
|
||||||
AllowSignUp bool `json:"allowSignUp"`
|
AllowSignUp bool `json:"allowSignUp"`
|
||||||
|
// Additional style.
|
||||||
|
AdditionalStyle string `json:"additionalStyle"`
|
||||||
}
|
}
|
||||||
|
@ -10,15 +10,16 @@ type SystemSettingName string
|
|||||||
const (
|
const (
|
||||||
// SystemSettingAllowSignUpName is the key type of allow signup setting.
|
// SystemSettingAllowSignUpName is the key type of allow signup setting.
|
||||||
SystemSettingAllowSignUpName SystemSettingName = "allowSignUp"
|
SystemSettingAllowSignUpName SystemSettingName = "allowSignUp"
|
||||||
SystemSettingPlaceholderName SystemSettingName = "placeholder"
|
// SystemSettingAdditionalStyleName is the key type of additional style.
|
||||||
|
SystemSettingAdditionalStyleName SystemSettingName = "additionalStyle"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (key SystemSettingName) String() string {
|
func (key SystemSettingName) String() string {
|
||||||
switch key {
|
switch key {
|
||||||
case SystemSettingAllowSignUpName:
|
case SystemSettingAllowSignUpName:
|
||||||
return "allowSignUp"
|
return "allowSignUp"
|
||||||
case SystemSettingPlaceholderName:
|
case SystemSettingAdditionalStyleName:
|
||||||
return "placeholder"
|
return "additionalStyle"
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@ -58,6 +59,12 @@ func (upsert SystemSettingUpsert) Validate() error {
|
|||||||
if invalid {
|
if invalid {
|
||||||
return fmt.Errorf("invalid system setting allow signup value")
|
return fmt.Errorf("invalid system setting allow signup value")
|
||||||
}
|
}
|
||||||
|
} else if upsert.Name == SystemSettingAdditionalStyleName {
|
||||||
|
value := ""
|
||||||
|
err := json.Unmarshal([]byte(upsert.Value), &value)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to unmarshal system setting additional style value")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("invalid system setting name")
|
return fmt.Errorf("invalid system setting name")
|
||||||
}
|
}
|
||||||
|
@ -12,9 +12,7 @@ const (
|
|||||||
UserSettingLocaleKey UserSettingKey = "locale"
|
UserSettingLocaleKey UserSettingKey = "locale"
|
||||||
// UserSettingMemoVisibilityKey is the key type for user preference memo default visibility.
|
// UserSettingMemoVisibilityKey is the key type for user preference memo default visibility.
|
||||||
UserSettingMemoVisibilityKey UserSettingKey = "memoVisibility"
|
UserSettingMemoVisibilityKey UserSettingKey = "memoVisibility"
|
||||||
// UserSettingEditorFontStyleKey is the key type for editor font style.
|
// UserSettingMobileEditorStyleKey is the key type for mobile editor style.
|
||||||
UserSettingEditorFontStyleKey UserSettingKey = "editorFontStyle"
|
|
||||||
// UserSettingEditorFontStyleKey is the key type for mobile editor style.
|
|
||||||
UserSettingMobileEditorStyleKey UserSettingKey = "mobileEditorStyle"
|
UserSettingMobileEditorStyleKey UserSettingKey = "mobileEditorStyle"
|
||||||
// UserSettingMemoDisplayTsOptionKey is the key type for memo display ts option.
|
// UserSettingMemoDisplayTsOptionKey is the key type for memo display ts option.
|
||||||
UserSettingMemoDisplayTsOptionKey UserSettingKey = "memoDisplayTsOption"
|
UserSettingMemoDisplayTsOptionKey UserSettingKey = "memoDisplayTsOption"
|
||||||
@ -27,8 +25,6 @@ func (key UserSettingKey) String() string {
|
|||||||
return "locale"
|
return "locale"
|
||||||
case UserSettingMemoVisibilityKey:
|
case UserSettingMemoVisibilityKey:
|
||||||
return "memoVisibility"
|
return "memoVisibility"
|
||||||
case UserSettingEditorFontStyleKey:
|
|
||||||
return "editorFontFamily"
|
|
||||||
case UserSettingMobileEditorStyleKey:
|
case UserSettingMobileEditorStyleKey:
|
||||||
return "mobileEditorStyle"
|
return "mobileEditorStyle"
|
||||||
case UserSettingMemoDisplayTsOptionKey:
|
case UserSettingMemoDisplayTsOptionKey:
|
||||||
@ -93,23 +89,6 @@ func (upsert UserSettingUpsert) Validate() error {
|
|||||||
if invalid {
|
if invalid {
|
||||||
return fmt.Errorf("invalid user setting memo visibility value")
|
return fmt.Errorf("invalid user setting memo visibility value")
|
||||||
}
|
}
|
||||||
} else if upsert.Key == UserSettingEditorFontStyleKey {
|
|
||||||
editorFontStyleValue := "normal"
|
|
||||||
err := json.Unmarshal([]byte(upsert.Value), &editorFontStyleValue)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to unmarshal user setting editor font style")
|
|
||||||
}
|
|
||||||
|
|
||||||
invalid := true
|
|
||||||
for _, value := range UserSettingEditorFontStyleValue {
|
|
||||||
if editorFontStyleValue == value {
|
|
||||||
invalid = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if invalid {
|
|
||||||
return fmt.Errorf("invalid user setting editor font style value")
|
|
||||||
}
|
|
||||||
} else if upsert.Key == UserSettingMobileEditorStyleKey {
|
} else if upsert.Key == UserSettingMobileEditorStyleKey {
|
||||||
mobileEditorStyleValue := "normal"
|
mobileEditorStyleValue := "normal"
|
||||||
err := json.Unmarshal([]byte(upsert.Value), &mobileEditorStyleValue)
|
err := json.Unmarshal([]byte(upsert.Value), &mobileEditorStyleValue)
|
||||||
|
@ -38,9 +38,10 @@ func (s *Server) registerSystemRoutes(g *echo.Group) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
systemStatus := api.SystemStatus{
|
systemStatus := api.SystemStatus{
|
||||||
Host: hostUser,
|
Host: hostUser,
|
||||||
Profile: s.Profile,
|
Profile: s.Profile,
|
||||||
AllowSignUp: false,
|
AllowSignUp: false,
|
||||||
|
AdditionalStyle: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
systemSettingList, err := s.Store.FindSystemSettingList(ctx, &api.SystemSettingFind{})
|
systemSettingList, err := s.Store.FindSystemSettingList(ctx, &api.SystemSettingFind{})
|
||||||
@ -56,6 +57,8 @@ func (s *Server) registerSystemRoutes(g *echo.Group) {
|
|||||||
|
|
||||||
if systemSetting.Name == api.SystemSettingAllowSignUpName {
|
if systemSetting.Name == api.SystemSettingAllowSignUpName {
|
||||||
systemStatus.AllowSignUp = value.(bool)
|
systemStatus.AllowSignUp = value.(bool)
|
||||||
|
} else if systemSetting.Name == api.SystemSettingAdditionalStyleName {
|
||||||
|
systemStatus.AdditionalStyle = value.(string)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import { RouterProvider } from "react-router-dom";
|
|||||||
import { globalService, locationService } from "./services";
|
import { globalService, locationService } from "./services";
|
||||||
import { useAppSelector } from "./store";
|
import { useAppSelector } from "./store";
|
||||||
import router from "./router";
|
import router from "./router";
|
||||||
|
import * as api from "./helpers/api";
|
||||||
import * as storage from "./helpers/storage";
|
import * as storage from "./helpers/storage";
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
@ -20,6 +21,18 @@ function App() {
|
|||||||
globalService.initialState();
|
globalService.initialState();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
api.getSystemStatus().then(({ data }) => {
|
||||||
|
const { data: status } = data;
|
||||||
|
if (status.additionalStyle) {
|
||||||
|
const styleEl = document.createElement("style");
|
||||||
|
styleEl.innerHTML = status.additionalStyle;
|
||||||
|
styleEl.setAttribute("type", "text/css");
|
||||||
|
document.head.appendChild(styleEl);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
i18n.changeLanguage(global.locale);
|
i18n.changeLanguage(global.locale);
|
||||||
storage.set({
|
storage.set({
|
||||||
|
@ -55,7 +55,6 @@ const MemoEditor: React.FC = () => {
|
|||||||
const prevGlobalStateRef = useRef(editorState);
|
const prevGlobalStateRef = useRef(editorState);
|
||||||
const editorRef = useRef<EditorRefActions>(null);
|
const editorRef = useRef<EditorRefActions>(null);
|
||||||
const tagSeletorRef = useRef<HTMLDivElement>(null);
|
const tagSeletorRef = useRef<HTMLDivElement>(null);
|
||||||
const editorFontStyle = user?.setting.editorFontStyle || "normal";
|
|
||||||
const mobileEditorStyle = user?.setting.mobileEditorStyle || "normal";
|
const mobileEditorStyle = user?.setting.mobileEditorStyle || "normal";
|
||||||
const memoVisibilityOptionSelectorItems = VISIBILITY_SELECTOR_ITEMS.map((item) => {
|
const memoVisibilityOptionSelectorItems = VISIBILITY_SELECTOR_ITEMS.map((item) => {
|
||||||
return {
|
return {
|
||||||
@ -395,14 +394,14 @@ const MemoEditor: React.FC = () => {
|
|||||||
|
|
||||||
const editorConfig = useMemo(
|
const editorConfig = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
className: `memo-editor ${editorFontStyle}`,
|
className: `memo-editor`,
|
||||||
initialContent: getEditorContentCache(),
|
initialContent: getEditorContentCache(),
|
||||||
placeholder: t("editor.placeholder"),
|
placeholder: t("editor.placeholder"),
|
||||||
fullscreen: state.fullscreen,
|
fullscreen: state.fullscreen,
|
||||||
onContentChange: handleContentChange,
|
onContentChange: handleContentChange,
|
||||||
onPaste: handlePasteEvent,
|
onPaste: handlePasteEvent,
|
||||||
}),
|
}),
|
||||||
[state.fullscreen, i18n.language, editorFontStyle]
|
[state.fullscreen, i18n.language]
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -20,17 +20,6 @@ const localeSelectorItems = [
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const editorFontStyleSelectorItems = [
|
|
||||||
{
|
|
||||||
text: "Normal",
|
|
||||||
value: "normal",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: "Mono",
|
|
||||||
value: "mono",
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const mobileEditorStyleSelectorItems = [
|
const mobileEditorStyleSelectorItems = [
|
||||||
{
|
{
|
||||||
text: "Normal",
|
text: "Normal",
|
||||||
@ -68,10 +57,6 @@ const PreferencesSection = () => {
|
|||||||
await userService.upsertUserSetting("memoVisibility", value);
|
await userService.upsertUserSetting("memoVisibility", value);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleEditorFontStyleChanged = async (value: string) => {
|
|
||||||
await userService.upsertUserSetting("editorFontStyle", value);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleMobileEditorStyleChanged = async (value: string) => {
|
const handleMobileEditorStyleChanged = async (value: string) => {
|
||||||
await userService.upsertUserSetting("mobileEditorStyle", value);
|
await userService.upsertUserSetting("mobileEditorStyle", value);
|
||||||
};
|
};
|
||||||
@ -97,15 +82,6 @@ const PreferencesSection = () => {
|
|||||||
handleValueChanged={handleDefaultMemoVisibilityChanged}
|
handleValueChanged={handleDefaultMemoVisibilityChanged}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
<label className="form-label selector">
|
|
||||||
<span className="normal-text">{t("setting.preference-section.editor-font-style")}</span>
|
|
||||||
<Selector
|
|
||||||
className="ml-2 w-32"
|
|
||||||
value={setting.editorFontStyle}
|
|
||||||
dataSource={editorFontStyleSelectorItems}
|
|
||||||
handleValueChanged={handleEditorFontStyleChanged}
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label className="form-label selector">
|
<label className="form-label selector">
|
||||||
<span className="normal-text">{t("setting.preference-section.mobile-editor-style")}</span>
|
<span className="normal-text">{t("setting.preference-section.mobile-editor-style")}</span>
|
||||||
<Selector
|
<Selector
|
||||||
|
@ -1,17 +1,19 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import Switch from "@mui/joy/Switch";
|
import { Button, Switch, Textarea } from "@mui/joy";
|
||||||
import * as api from "../../helpers/api";
|
import * as api from "../../helpers/api";
|
||||||
import "../../less/settings/preferences-section.less";
|
import "../../less/settings/preferences-section.less";
|
||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
allowSignUp: boolean;
|
allowSignUp: boolean;
|
||||||
|
additionalStyle: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const SystemSection = () => {
|
const SystemSection = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [state, setState] = useState<State>({
|
const [state, setState] = useState<State>({
|
||||||
allowSignUp: false,
|
allowSignUp: false,
|
||||||
|
additionalStyle: "",
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -19,6 +21,7 @@ const SystemSection = () => {
|
|||||||
const { data: status } = data;
|
const { data: status } = data;
|
||||||
setState({
|
setState({
|
||||||
allowSignUp: status.allowSignUp,
|
allowSignUp: status.allowSignUp,
|
||||||
|
additionalStyle: status.additionalStyle,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
@ -34,6 +37,20 @@ const SystemSection = () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleAdditionalStyleChanged = (value: string) => {
|
||||||
|
setState({
|
||||||
|
...state,
|
||||||
|
additionalStyle: value,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSaveAdditionalStyle = async () => {
|
||||||
|
await api.upsertSystemSetting({
|
||||||
|
name: "additionalStyle",
|
||||||
|
value: JSON.stringify(state.additionalStyle),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="section-container preferences-section-container">
|
<div className="section-container preferences-section-container">
|
||||||
<p className="title-text">{t("common.basic")}</p>
|
<p className="title-text">{t("common.basic")}</p>
|
||||||
@ -41,6 +58,21 @@ const SystemSection = () => {
|
|||||||
<span className="normal-text">Allow user signup</span>
|
<span className="normal-text">Allow user signup</span>
|
||||||
<Switch size="sm" checked={state.allowSignUp} onChange={(event) => handleAllowSignUpChanged(event.target.checked)} />
|
<Switch size="sm" checked={state.allowSignUp} onChange={(event) => handleAllowSignUpChanged(event.target.checked)} />
|
||||||
</label>
|
</label>
|
||||||
|
<label className="form-label selector">
|
||||||
|
<span className="normal-text">Additional style</span>
|
||||||
|
<Button size="sm" onClick={handleSaveAdditionalStyle}>
|
||||||
|
Save
|
||||||
|
</Button>
|
||||||
|
</label>
|
||||||
|
<Textarea
|
||||||
|
className="w-full"
|
||||||
|
sx={{
|
||||||
|
fontFamily: "monospace",
|
||||||
|
}}
|
||||||
|
minRows={5}
|
||||||
|
defaultValue={state.additionalStyle}
|
||||||
|
onChange={(event) => handleAdditionalStyleChanged(event.target.value)}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -63,14 +63,8 @@ const UserBanner = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleSignOutBtnClick = async () => {
|
const handleSignOutBtnClick = async () => {
|
||||||
userService
|
userService.doSignOut().catch();
|
||||||
.doSignOut()
|
navigate("/auth");
|
||||||
.then(() => {
|
|
||||||
navigate("/auth");
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
// do nth
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -3,12 +3,6 @@
|
|||||||
.common-editor-wrapper {
|
.common-editor-wrapper {
|
||||||
@apply flex flex-col justify-start items-start relative w-full h-auto bg-white;
|
@apply flex flex-col justify-start items-start relative w-full h-auto bg-white;
|
||||||
|
|
||||||
&.mono {
|
|
||||||
> .common-editor-inputer {
|
|
||||||
@apply font-mono;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
> .common-editor-inputer {
|
> .common-editor-inputer {
|
||||||
@apply w-full h-full mt-2 mb-1 text-base resize-none overflow-x-hidden overflow-y-auto bg-transparent whitespace-pre-wrap;
|
@apply w-full h-full mt-2 mb-1 text-base resize-none overflow-x-hidden overflow-y-auto bg-transparent whitespace-pre-wrap;
|
||||||
max-height: 300px;
|
max-height: 300px;
|
||||||
|
@ -7,7 +7,6 @@ import { setUser, patchUser, setHost, setOwner } from "../store/modules/user";
|
|||||||
const defauleSetting: Setting = {
|
const defauleSetting: Setting = {
|
||||||
locale: "en",
|
locale: "en",
|
||||||
memoVisibility: "PRIVATE",
|
memoVisibility: "PRIVATE",
|
||||||
editorFontStyle: "normal",
|
|
||||||
mobileEditorStyle: "normal",
|
mobileEditorStyle: "normal",
|
||||||
memoDisplayTsOption: "created_ts",
|
memoDisplayTsOption: "created_ts",
|
||||||
};
|
};
|
||||||
|
8
web/src/types/modules/setting.d.ts
vendored
8
web/src/types/modules/setting.d.ts
vendored
@ -1,7 +1,6 @@
|
|||||||
interface Setting {
|
interface Setting {
|
||||||
locale: Locale;
|
locale: Locale;
|
||||||
memoVisibility: Visibility;
|
memoVisibility: Visibility;
|
||||||
editorFontStyle: "normal" | "mono";
|
|
||||||
mobileEditorStyle: "normal" | "float";
|
mobileEditorStyle: "normal" | "float";
|
||||||
memoDisplayTsOption: "created_ts" | "updated_ts";
|
memoDisplayTsOption: "created_ts" | "updated_ts";
|
||||||
}
|
}
|
||||||
@ -16,12 +15,7 @@ interface UserMemoVisibilitySetting {
|
|||||||
value: Visibility;
|
value: Visibility;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface UserEditorFontStyleSetting {
|
type UserSetting = UserLocaleSetting | UserMemoVisibilitySetting;
|
||||||
key: "editorFontStyle";
|
|
||||||
value: "normal" | "mono";
|
|
||||||
}
|
|
||||||
|
|
||||||
type UserSetting = UserLocaleSetting | UserMemoVisibilitySetting | UserEditorFontStyleSetting;
|
|
||||||
|
|
||||||
interface UserSettingUpsert {
|
interface UserSettingUpsert {
|
||||||
key: keyof Setting;
|
key: keyof Setting;
|
||||||
|
1
web/src/types/modules/system.d.ts
vendored
1
web/src/types/modules/system.d.ts
vendored
@ -8,6 +8,7 @@ interface SystemStatus {
|
|||||||
profile: Profile;
|
profile: Profile;
|
||||||
// System settings
|
// System settings
|
||||||
allowSignUp: boolean;
|
allowSignUp: boolean;
|
||||||
|
additionalStyle: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SystemSetting {
|
interface SystemSetting {
|
||||||
|
Loading…
Reference in New Issue
Block a user