fix shortcut

This commit is contained in:
Gabe Yuan
2023-09-08 13:53:33 +08:00
parent 850dc0e83b
commit 56350de2cf
4 changed files with 44 additions and 13 deletions

View File

@@ -12,7 +12,10 @@ const SettingContext = createContext({
}); });
export function SettingProvider({ children }) { export function SettingProvider({ children }) {
const { data, update, reload } = useStorage(STOKEY_SETTING, DEFAULT_SETTING); const { data, update, reload, loading } = useStorage(
STOKEY_SETTING,
DEFAULT_SETTING
);
const { const {
sync: { settingUpdateAt }, sync: { settingUpdateAt },
updateSync, updateSync,
@@ -36,6 +39,10 @@ export function SettingProvider({ children }) {
[settingUpdateAt, update, updateSync, syncSetting] [settingUpdateAt, update, updateSync, syncSetting]
); );
if (loading) {
return;
}
return ( return (
<SettingContext.Provider <SettingContext.Provider
value={{ value={{

View File

@@ -2,6 +2,7 @@ import { useCallback, useEffect, useState } from "react";
import { storage } from "../libs/storage"; import { storage } from "../libs/storage";
export function useStorage(key, defaultVal = null) { export function useStorage(key, defaultVal = null) {
const [loading, setLoading] = useState(true);
const [data, setData] = useState(defaultVal); const [data, setData] = useState(defaultVal);
const save = useCallback( const save = useCallback(
@@ -36,9 +37,16 @@ export function useStorage(key, defaultVal = null) {
useEffect(() => { useEffect(() => {
(async () => { (async () => {
await reload(); try {
setLoading(true);
await reload();
} catch (err) {
//
} finally {
setLoading(false);
}
})(); })();
}, [reload]); }, [reload]);
return { data, save, update, remove, reload }; return { data, save, update, remove, reload, loading };
} }

View File

@@ -4,24 +4,34 @@ import { isSameSet } from "./utils";
* 键盘快捷键监听 * 键盘快捷键监听
* @param {*} fn * @param {*} fn
* @param {*} target * @param {*} target
* @param {*} timeout
* @returns * @returns
*/ */
export const shortcutListener = (fn, target = document) => { export const shortcutListener = (fn, target = document, timeout = 3000) => {
// todo: let done = false;
const allkeys = new Set(); const allkeys = new Set();
const curkeys = new Set(); const curkeys = new Set();
let timer = null;
const handleKeydown = (e) => { const handleKeydown = (e) => {
timer && clearTimeout(timer);
timer = setTimeout(() => {
allkeys.clear();
curkeys.clear();
clearTimeout(timer);
timer = null;
}, timeout);
if (e.code) { if (e.code) {
allkeys.add(e.key); allkeys.add(e.key);
curkeys.add(e.key); curkeys.add(e.key);
fn([...curkeys], [...allkeys]);
} }
}; };
const handleKeyup = (e) => { const handleKeyup = (e) => {
curkeys.delete(e.key); curkeys.delete(e.key);
if (curkeys.size === 0) { if (curkeys.size === 0) {
fn([...allkeys]); fn([...curkeys], [...allkeys]);
allkeys.clear(); allkeys.clear();
} }
}; };
@@ -42,8 +52,8 @@ export const shortcutListener = (fn, target = document) => {
* @returns * @returns
*/ */
export const shortcutRegister = (targetKeys, fn, target = document) => { export const shortcutRegister = (targetKeys, fn, target = document) => {
return shortcutListener((keys) => { return shortcutListener((curkeys) => {
if (isSameSet(new Set(targetKeys), new Set(keys))) { if (isSameSet(new Set(targetKeys), new Set(curkeys))) {
fn(); fn();
} }
}, target); }, target);

View File

@@ -33,23 +33,29 @@ function ShortcutItem({ action, label }) {
const { shortcut, setShortcut } = useShortcut(action); const { shortcut, setShortcut } = useShortcut(action);
const [disabled, setDisabled] = useState(true); const [disabled, setDisabled] = useState(true);
const inputRef = useRef(null); const inputRef = useRef(null);
const [formval, setFormval] = useState(shortcut);
useEffect(() => { useEffect(() => {
if (disabled) { if (disabled) {
setFormval(shortcut);
return; return;
} }
inputRef.current.focus(); inputRef.current.focus();
setShortcut([]); setFormval([]);
const clearShortcut = shortcutListener((keys) => { const clearShortcut = shortcutListener((curkeys, allkeys) => {
setShortcut(keys); setFormval(allkeys);
if (curkeys.length === 0) {
setDisabled(true);
setShortcut(allkeys);
}
}, inputRef.current); }, inputRef.current);
return () => { return () => {
clearShortcut(); clearShortcut();
}; };
}, [disabled, setShortcut]); }, [disabled, setShortcut, shortcut]);
return ( return (
<Stack direction="row"> <Stack direction="row">
@@ -57,7 +63,7 @@ function ShortcutItem({ action, label }) {
size="small" size="small"
label={label} label={label}
name={label} name={label}
value={shortcut.join(" + ")} value={formval.join(" + ")}
fullWidth fullWidth
inputRef={inputRef} inputRef={inputRef}
disabled={disabled} disabled={disabled}