Files
kiss-translator/src/views/Options/Setting.js

236 lines
6.7 KiB
JavaScript
Raw Normal View History

2023-07-20 13:45:41 +08:00
import Box from "@mui/material/Box";
import Stack from "@mui/material/Stack";
import InputLabel from "@mui/material/InputLabel";
import TextField from "@mui/material/TextField";
import MenuItem from "@mui/material/MenuItem";
import FormControl from "@mui/material/FormControl";
import Select from "@mui/material/Select";
2023-09-01 11:03:53 +08:00
import Link from "@mui/material/Link";
2023-09-03 13:11:04 +08:00
import FormHelperText from "@mui/material/FormHelperText";
2023-08-30 18:05:37 +08:00
import { useSetting } from "../../hooks/Setting";
2023-08-31 00:18:57 +08:00
import { limitNumber } from "../../libs/utils";
2023-07-20 13:45:41 +08:00
import { useI18n } from "../../hooks/I18n";
2023-09-03 13:11:04 +08:00
import { useAlert } from "../../hooks/Alert";
2023-09-06 23:39:11 +08:00
import { isExt } from "../../libs/client";
2023-09-07 18:12:45 +08:00
import Grid from "@mui/material/Grid";
2023-09-06 18:00:18 +08:00
import {
UI_LANGS,
TRANS_NEWLINE_LENGTH,
CACHE_NAME,
OPT_MOUSEKEY_ALL,
OPT_MOUSEKEY_DISABLE,
2023-09-07 18:12:45 +08:00
OPT_SHORTCUT_TRANSLATE,
OPT_SHORTCUT_STYLE,
OPT_SHORTCUT_POPUP,
2023-09-09 15:08:34 +08:00
OPT_SHORTCUT_SETTING,
2023-09-06 18:00:18 +08:00
} from "../../config";
2023-09-07 18:12:45 +08:00
import { useShortcut } from "../../hooks/Shortcut";
2023-09-13 15:53:40 +08:00
import ShortcutInput from "./ShortcutInput";
2023-09-07 18:12:45 +08:00
function ShortcutItem({ action, label }) {
const { shortcut, setShortcut } = useShortcut(action);
return (
2023-09-13 15:53:40 +08:00
<ShortcutInput value={shortcut} onChange={setShortcut} label={label} />
2023-09-07 18:12:45 +08:00
);
}
2023-07-20 13:45:41 +08:00
export default function Settings() {
const i18n = useI18n();
2023-08-30 18:05:37 +08:00
const { setting, updateSetting } = useSetting();
2023-09-03 13:11:04 +08:00
const alert = useAlert();
2023-07-20 13:45:41 +08:00
2023-08-31 00:18:57 +08:00
const handleChange = (e) => {
e.preventDefault();
let { name, value } = e.target;
switch (name) {
case "fetchLimit":
value = limitNumber(value, 1, 100);
break;
case "fetchInterval":
value = limitNumber(value, 0, 5000);
break;
case "minLength":
value = limitNumber(value, 1, 100);
break;
case "maxLength":
value = limitNumber(value, 100, 10000);
break;
2023-09-01 11:21:06 +08:00
case "newlineLength":
value = limitNumber(value, 1, 1000);
break;
2023-08-31 00:18:57 +08:00
default:
}
updateSetting({
[name]: value,
});
};
2023-08-17 13:27:22 +08:00
2023-09-03 13:11:04 +08:00
const handleClearCache = () => {
try {
caches.delete(CACHE_NAME);
alert.success(i18n("clear_success"));
} catch (err) {
console.log("[clear cache]", err);
}
};
2023-07-20 13:45:41 +08:00
const {
uiLang,
fetchLimit,
2023-08-04 16:05:14 +08:00
fetchInterval,
2023-08-22 21:45:23 +08:00
minLength,
maxLength,
2023-07-20 13:45:41 +08:00
clearCache,
2023-09-01 11:21:06 +08:00
newlineLength = TRANS_NEWLINE_LENGTH,
2023-09-06 18:00:18 +08:00
mouseKey = OPT_MOUSEKEY_DISABLE,
2023-09-09 15:08:34 +08:00
hideFab = false,
2023-07-20 13:45:41 +08:00
} = setting;
return (
<Box>
<Stack spacing={3}>
<FormControl size="small">
<InputLabel>{i18n("ui_lang")}</InputLabel>
<Select
2023-08-17 13:27:22 +08:00
name="uiLang"
2023-07-20 13:45:41 +08:00
value={uiLang}
label={i18n("ui_lang")}
2023-08-17 13:27:22 +08:00
onChange={handleChange}
2023-07-20 13:45:41 +08:00
>
{UI_LANGS.map(([lang, name]) => (
2023-08-04 10:08:54 +08:00
<MenuItem key={lang} value={lang}>
{name}
</MenuItem>
2023-07-20 13:45:41 +08:00
))}
</Select>
</FormControl>
<TextField
size="small"
label={i18n("fetch_limit")}
type="number"
2023-08-17 13:27:22 +08:00
name="fetchLimit"
2023-09-15 20:44:01 +08:00
defaultValue={fetchLimit}
2023-08-17 13:27:22 +08:00
onChange={handleChange}
2023-08-04 16:05:14 +08:00
/>
<TextField
size="small"
label={i18n("fetch_interval")}
type="number"
2023-08-17 13:27:22 +08:00
name="fetchInterval"
2023-09-15 20:44:01 +08:00
defaultValue={fetchInterval}
2023-08-17 13:27:22 +08:00
onChange={handleChange}
2023-07-20 13:45:41 +08:00
/>
2023-08-22 21:45:23 +08:00
<TextField
size="small"
label={i18n("min_translate_length")}
type="number"
name="minLength"
2023-09-15 20:44:01 +08:00
defaultValue={minLength}
2023-08-22 21:45:23 +08:00
onChange={handleChange}
/>
<TextField
size="small"
label={i18n("max_translate_length")}
type="number"
name="maxLength"
2023-09-15 20:44:01 +08:00
defaultValue={maxLength}
2023-08-22 21:45:23 +08:00
onChange={handleChange}
/>
2023-09-01 11:21:06 +08:00
<TextField
size="small"
label={i18n("num_of_newline_characters")}
type="number"
name="newlineLength"
2023-09-15 20:44:01 +08:00
defaultValue={newlineLength}
2023-09-01 11:21:06 +08:00
onChange={handleChange}
/>
2023-09-06 18:00:18 +08:00
<FormControl size="small">
<InputLabel>{i18n("mouseover_translation")}</InputLabel>
<Select
name="mouseKey"
value={mouseKey}
label={i18n("mouseover_translation")}
onChange={handleChange}
>
{OPT_MOUSEKEY_ALL.map((item) => (
<MenuItem key={item} value={item}>
{i18n(item)}
</MenuItem>
))}
</Select>
</FormControl>
2023-09-07 18:12:45 +08:00
{isExt ? (
2023-09-06 23:39:11 +08:00
<FormControl size="small">
<InputLabel>{i18n("if_clear_cache")}</InputLabel>
<Select
name="clearCache"
value={clearCache}
label={i18n("if_clear_cache")}
onChange={handleChange}
>
<MenuItem value={false}>{i18n("clear_cache_never")}</MenuItem>
<MenuItem value={true}>{i18n("clear_cache_restart")}</MenuItem>
</Select>
<FormHelperText>
<Link component="button" onClick={handleClearCache}>
{i18n("clear_all_cache_now")}
</Link>
</FormHelperText>
</FormControl>
2023-09-07 18:12:45 +08:00
) : (
2023-09-09 15:08:34 +08:00
<>
<FormControl size="small">
<InputLabel>{i18n("hide_fab_button")}</InputLabel>
<Select
name="hideFab"
value={hideFab}
label={i18n("hide_fab_button")}
onChange={handleChange}
>
<MenuItem value={false}>{i18n("show")}</MenuItem>
<MenuItem value={true}>{i18n("hide")}</MenuItem>
</Select>
</FormControl>
2023-09-13 11:16:56 +08:00
<Box>
2023-09-15 20:44:01 +08:00
<Grid container spacing={2} columns={12}>
2023-09-13 11:16:56 +08:00
<Grid item xs={12} sm={12} md={3} lg={3}>
<ShortcutItem
action={OPT_SHORTCUT_TRANSLATE}
label={i18n("toggle_translate_shortcut")}
/>
</Grid>
<Grid item xs={12} sm={12} md={3} lg={3}>
<ShortcutItem
action={OPT_SHORTCUT_STYLE}
label={i18n("toggle_style_shortcut")}
/>
</Grid>
<Grid item xs={12} sm={12} md={3} lg={3}>
<ShortcutItem
action={OPT_SHORTCUT_POPUP}
label={i18n("toggle_popup_shortcut")}
/>
</Grid>
<Grid item xs={12} sm={12} md={3} lg={3}>
<ShortcutItem
action={OPT_SHORTCUT_SETTING}
label={i18n("open_setting_shortcut")}
/>
</Grid>
2023-09-09 15:08:34 +08:00
</Grid>
2023-09-13 11:16:56 +08:00
</Box>
2023-09-09 15:08:34 +08:00
</>
2023-09-06 23:39:11 +08:00
)}
2023-07-20 13:45:41 +08:00
</Stack>
</Box>
);
}