Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: folding options #459

Merged
merged 6 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions web/src/components/MemoContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { marked } from "../labs/marked";
import Icon from "./Icon";
import {
SETTING_IS_FOLDING_ENABLED_KEY,
IS_FOLDING_ENABLED_DEFAULT_VALUE,
SETTING_IS_FOLDING_DEFAULT_KEY,
IS_FOLDING_DEFAULT_DEFAULT_VALUE,
} from "../helpers/consts";
import useLocalStorage from "../hooks/useLocalStorage";
import "../less/memo-content.less";

export interface DisplayConfig {
Expand Down Expand Up @@ -31,6 +38,8 @@ const MAX_MEMO_CONTAINER_HEIGHT = 384;
const MemoContent: React.FC<Props> = (props: Props) => {
const { className, content, onMemoContentClick, onMemoContentDoubleClick } = props;
const { t } = useTranslation();
const [isFoldingEnabled] = useLocalStorage(SETTING_IS_FOLDING_ENABLED_KEY, IS_FOLDING_ENABLED_DEFAULT_VALUE);
const [isFoldingDefault] = useLocalStorage(SETTING_IS_FOLDING_DEFAULT_KEY, IS_FOLDING_DEFAULT_DEFAULT_VALUE);
const [state, setState] = useState<State>({
expandButtonStatus: -1,
});
Expand All @@ -45,11 +54,11 @@ const MemoContent: React.FC<Props> = (props: Props) => {
return;
}

if (displayConfig.enableExpand) {
if (displayConfig.enableExpand && isFoldingEnabled) {
if (Number(memoContentContainerRef.current?.clientHeight) > MAX_MEMO_CONTAINER_HEIGHT) {
setState({
...state,
expandButtonStatus: 0,
expandButtonStatus: isFoldingDefault ? 0 : 1,
});
}
}
Expand Down
56 changes: 55 additions & 1 deletion web/src/components/Settings/PreferencesSection.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { useTranslation } from "react-i18next";
import { globalService, userService } from "../../services";
import { useAppSelector } from "../../store";
import { VISIBILITY_SELECTOR_ITEMS, MEMO_DISPLAY_TS_OPTION_SELECTOR_ITEMS } from "../../helpers/consts";
import {
VISIBILITY_SELECTOR_ITEMS,
MEMO_DISPLAY_TS_OPTION_SELECTOR_ITEMS,
IS_FOLDING_DEFAULT_SELECTOR_ITEMS,
IS_FOLDING_ENABLED_SELECTOR_ITEMS,
SETTING_IS_FOLDING_ENABLED_KEY,
IS_FOLDING_ENABLED_DEFAULT_VALUE,
SETTING_IS_FOLDING_DEFAULT_KEY,
IS_FOLDING_DEFAULT_DEFAULT_VALUE,
} from "../../helpers/consts";
import useLocalStorage from "../../hooks/useLocalStorage";
import Selector from "../common/Selector";
import "../../less/settings/preferences-section.less";

Expand Down Expand Up @@ -37,6 +47,22 @@ const PreferencesSection = () => {
};
});

const isFoldingEnabledSelectorItems = IS_FOLDING_ENABLED_SELECTOR_ITEMS.map((item) => {
return {
value: String(item.value),
text: t(`common.${item.text}`),
};
});
const [isFoldingEnabled, setIsFoldingEnabled] = useLocalStorage(SETTING_IS_FOLDING_ENABLED_KEY, IS_FOLDING_ENABLED_DEFAULT_VALUE);

const isFoldingDefaultSelectorItems = IS_FOLDING_DEFAULT_SELECTOR_ITEMS.map((item) => {
return {
value: String(item.value),
text: t(`common.${item.text}`),
};
});
const [isFoldingDefault, setIsFoldingDefault] = useLocalStorage(SETTING_IS_FOLDING_DEFAULT_KEY, IS_FOLDING_DEFAULT_DEFAULT_VALUE);

const handleLocaleChanged = async (value: string) => {
await userService.upsertUserSetting("locale", value);
globalService.setLocale(value as Locale);
Expand All @@ -50,6 +76,14 @@ const PreferencesSection = () => {
await userService.upsertUserSetting("memoDisplayTsOption", value);
};

const handleIsFoldingEnabledChanged = (value: string) => {
setIsFoldingEnabled(value === "true");
};

const handleIsFoldingDefaultChanged = async (value: string) => {
setIsFoldingDefault(value === "true");
};

return (
<div className="section-container preferences-section-container">
<p className="title-text">{t("common.basic")}</p>
Expand All @@ -67,6 +101,26 @@ const PreferencesSection = () => {
handleValueChanged={handleDefaultMemoVisibilityChanged}
/>
</label>
<label className="form-label selector">
<span className="normal-text">{t("setting.preference-section.is-folding-fuction-enabled")}</span>
<Selector
className="ml-2 w-32"
value={String(isFoldingEnabled)}
dataSource={isFoldingEnabledSelectorItems}
handleValueChanged={handleIsFoldingEnabledChanged}
/>
</label>
{isFoldingEnabled && (
<label className="form-label selector">
<span className="normal-text">{t("setting.preference-section.is-folding-default")}</span>
<Selector
className="ml-2 w-32"
value={String(isFoldingDefault)}
dataSource={isFoldingDefaultSelectorItems}
handleValueChanged={handleIsFoldingDefaultChanged}
/>
</label>
)}
<label className="form-label selector">
<span className="normal-text">{t("setting.preference-section.default-memo-sort-option")}</span>
<Selector
Expand Down
14 changes: 14 additions & 0 deletions web/src/helpers/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,18 @@ export const MEMO_DISPLAY_TS_OPTION_SELECTOR_ITEMS = [
{ text: "created_ts", value: "updated_ts" },
];

export const IS_FOLDING_ENABLED_SELECTOR_ITEMS = [
{ text: "true", value: true },
{ text: "false", value: false },
];
export const IS_FOLDING_ENABLED_DEFAULT_VALUE = true;
export const SETTING_IS_FOLDING_ENABLED_KEY = "setting_IS_FOLDING_ENABLED";

export const IS_FOLDING_DEFAULT_SELECTOR_ITEMS = [
{ text: "true", value: true },
{ text: "false", value: false },
];
export const IS_FOLDING_DEFAULT_DEFAULT_VALUE = true;
export const SETTING_IS_FOLDING_DEFAULT_KEY = "setting_IS_FOLDING_DEFAULT";

export const TAB_SPACE_WIDTH = 2;
26 changes: 26 additions & 0 deletions web/src/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useState } from "react";

const useLocalStorage = <T>(key: string, initialValue: T) => {
const [storedValue, setStoredValue] = useState<T>(() => {
try {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
} catch (error) {
return initialValue;
}
});

const setValue = (value: T | ((val: T) => T)) => {
try {
const valueToStore = value instanceof Function ? value(storedValue) : value;
setStoredValue(valueToStore);
window.localStorage.setItem(key, JSON.stringify(valueToStore));
} catch (error) {
console.log(error);
}
};

return [storedValue, setValue] as const;
};

export default useLocalStorage;
6 changes: 5 additions & 1 deletion web/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
"changed": "changed",
"update-on": "Update on",
"fold": "Fold",
"expand": "Expand"
"expand": "Expand",
"true": "True",
"false": "False"
},
"slogan": "An open-source, self-hosted memo hub for knowledge management and collaboration.",
"auth": {
Expand Down Expand Up @@ -139,6 +141,8 @@
},
"preference-section": {
"default-memo-visibility": "Default memo visibility",
"is-folding-fuction-enabled": "Is folding function enabled",
hyoban marked this conversation as resolved.
Show resolved Hide resolved
"is-folding-default": "Is folding default",
"editor-font-style": "Editor font style",
"mobile-editor-style": "Mobile editor style",
"default-memo-sort-option": "Display by created/updated time",
Expand Down
6 changes: 5 additions & 1 deletion web/src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
"changed": "已更改",
"update-on": "更新于",
"fold": "折叠",
"expand": "展开"
"expand": "展开",
"true": "是",
"false": "否"
},
"slogan": "An open-source, self-hosted memo hub for knowledge management and collaboration.",
"auth": {
Expand Down Expand Up @@ -139,6 +141,8 @@
},
"preference-section": {
"default-memo-visibility": "默认 Memo 可见性",
"is-folding-fuction-enabled": "开启折叠功能",
"is-folding-default": "默认折叠",
"editor-font-style": "编辑器字体样式",
"mobile-editor-style": "移动端编辑器样式",
"default-memo-sort-option": "按创建时间/更新时间显示",
Expand Down