Refactor: Improve code check to prevent bugs
This commit is contained in:
parent
fc4791526b
commit
40e977cc3c
1 changed files with 11 additions and 5 deletions
|
|
@ -123,13 +123,18 @@ export default function EditShortcutButton({
|
||||||
return `${oldKey.length > 0 ? toTitleCase(oldKey) : oldKey.toUpperCase()} + ${key.length > 0 ? toTitleCase(key) : key.toUpperCase()}`;
|
return `${oldKey.length > 0 ? toTitleCase(oldKey) : oldKey.toUpperCase()} + ${key.length > 0 ? toTitleCase(key) : key.toUpperCase()}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function checkForKeys(keys: string, keyToCompare: string): boolean {
|
||||||
|
const keysArr = keys.split(" ");
|
||||||
|
let hasNewKey = false;
|
||||||
|
return keysArr.some(
|
||||||
|
(k) => k.toLowerCase().trim() === keyToCompare.toLowerCase().trim(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
function onKeyDown(e: KeyboardEvent) {
|
function onKeyDown(e: KeyboardEvent) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
let fixedKey = e.key;
|
let fixedKey = e.key;
|
||||||
if (key) {
|
|
||||||
if (key.toUpperCase().includes(e.key.toUpperCase())) return;
|
|
||||||
}
|
|
||||||
if (e.key?.toLowerCase() === "control") {
|
if (e.key?.toLowerCase() === "control") {
|
||||||
fixedKey = "Ctrl";
|
fixedKey = "Ctrl";
|
||||||
}
|
}
|
||||||
|
|
@ -139,8 +144,9 @@ export default function EditShortcutButton({
|
||||||
if (e.key?.toLowerCase() === " ") {
|
if (e.key?.toLowerCase() === " ") {
|
||||||
fixedKey = "Space";
|
fixedKey = "Space";
|
||||||
}
|
}
|
||||||
if (shortcutInitialValue?.toUpperCase().includes(fixedKey.toUpperCase()))
|
if (key) {
|
||||||
return;
|
if (checkForKeys(key, fixedKey)) return;
|
||||||
|
}
|
||||||
setKey((oldKey) =>
|
setKey((oldKey) =>
|
||||||
getFixedCombination({ oldKey: oldKey!, key: fixedKey }),
|
getFixedCombination({ oldKey: oldKey!, key: fixedKey }),
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue