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 IconButton from "@mui/material/IconButton";
|
|
|
|
|
import EditIcon from "@mui/icons-material/Edit";
|
|
|
|
|
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-06 18:00:18 +08:00
|
|
|
} from "../../config";
|
2023-09-07 18:12:45 +08:00
|
|
|
import { useEffect, useState, useRef } from "react";
|
|
|
|
|
import { useShortcut } from "../../hooks/Shortcut";
|
|
|
|
|
|
|
|
|
|
function ShortcutItem({ action, label }) {
|
|
|
|
|
const { shortcut, setShortcut } = useShortcut(action);
|
|
|
|
|
const [disabled, setDisabled] = useState(true);
|
|
|
|
|
const [focused, setFocus] = useState(false);
|
|
|
|
|
const [formval, setFormval] = useState(shortcut);
|
|
|
|
|
const inputRef = useRef(null);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!disabled) {
|
|
|
|
|
inputRef.current.focus();
|
|
|
|
|
setFormval([]);
|
|
|
|
|
}
|
|
|
|
|
}, [disabled]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!focused) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const keys = new Set();
|
|
|
|
|
const handleKeydown = (e) => {
|
|
|
|
|
// console.log("keydown", e);
|
|
|
|
|
e.code && keys.add(e.key);
|
|
|
|
|
setFormval([...keys]);
|
|
|
|
|
};
|
|
|
|
|
const handleKeyup = (e) => {
|
|
|
|
|
// console.log("keyup", e);
|
|
|
|
|
keys.delete(e.key);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
window.addEventListener("keydown", handleKeydown);
|
|
|
|
|
window.addEventListener("keyup", handleKeyup);
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener("keydown", handleKeydown);
|
|
|
|
|
window.removeEventListener("keyup", handleKeyup);
|
|
|
|
|
};
|
|
|
|
|
}, [focused]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Stack direction="row">
|
|
|
|
|
<TextField
|
|
|
|
|
size="small"
|
|
|
|
|
label={label}
|
|
|
|
|
name={label}
|
|
|
|
|
value={formval.join(" + ")}
|
|
|
|
|
fullWidth
|
|
|
|
|
inputRef={inputRef}
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
onFocus={(e) => {
|
|
|
|
|
setFocus(true);
|
|
|
|
|
}}
|
|
|
|
|
onBlur={(e) => {
|
|
|
|
|
setFocus(false);
|
|
|
|
|
setDisabled(true);
|
|
|
|
|
setShortcut(formval);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<IconButton
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setDisabled(false);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{<EditIcon />}
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Stack>
|
|
|
|
|
);
|
|
|
|
|
}
|
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-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-08-31 00:18:57 +08:00
|
|
|
value={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-08-31 00:18:57 +08:00
|
|
|
value={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-08-31 00:18:57 +08:00
|
|
|
value={minLength}
|
2023-08-22 21:45:23 +08:00
|
|
|
onChange={handleChange}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<TextField
|
|
|
|
|
size="small"
|
|
|
|
|
label={i18n("max_translate_length")}
|
|
|
|
|
type="number"
|
|
|
|
|
name="maxLength"
|
2023-08-31 00:18:57 +08:00
|
|
|
value={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"
|
|
|
|
|
value={newlineLength}
|
|
|
|
|
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
|
|
|
) : (
|
|
|
|
|
<Grid container rowSpacing={2} columns={12}>
|
|
|
|
|
<Grid item xs={12} sm={12} md={4} lg={4}>
|
|
|
|
|
<ShortcutItem
|
|
|
|
|
action={OPT_SHORTCUT_TRANSLATE}
|
|
|
|
|
label={i18n("toggle_translate_shortcut")}
|
|
|
|
|
/>
|
|
|
|
|
</Grid>
|
|
|
|
|
<Grid item xs={12} sm={12} md={4} lg={4}>
|
|
|
|
|
<ShortcutItem
|
|
|
|
|
action={OPT_SHORTCUT_STYLE}
|
|
|
|
|
label={i18n("toggle_style_shortcut")}
|
|
|
|
|
/>
|
|
|
|
|
</Grid>
|
|
|
|
|
<Grid item xs={12} sm={12} md={4} lg={4}>
|
|
|
|
|
<ShortcutItem
|
|
|
|
|
action={OPT_SHORTCUT_POPUP}
|
|
|
|
|
label={i18n("toggle_popup_shortcut")}
|
|
|
|
|
/>
|
|
|
|
|
</Grid>
|
|
|
|
|
</Grid>
|
2023-09-06 23:39:11 +08:00
|
|
|
)}
|
2023-07-20 13:45:41 +08:00
|
|
|
</Stack>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|