Refactor: Add the possibility to don't use ctrl/cmd
This commit is contained in:
parent
b698ca3fd2
commit
c2ae5e8831
1 changed files with 59 additions and 36 deletions
|
|
@ -25,7 +25,7 @@ export default function EditShortcutButton({
|
||||||
disable?: boolean;
|
disable?: boolean;
|
||||||
}): JSX.Element {
|
}): JSX.Element {
|
||||||
const isMac = navigator.userAgent.toUpperCase().includes("MAC");
|
const isMac = navigator.userAgent.toUpperCase().includes("MAC");
|
||||||
const [key, setKey] = useState<string>(isMac ? "Cmd" : "Ctrl");
|
const [key, setKey] = useState<string | null>("");
|
||||||
const setSuccessData = useAlertStore((state) => state.setSuccessData);
|
const setSuccessData = useAlertStore((state) => state.setSuccessData);
|
||||||
const setShortcuts = useShortcutsStore((state) => state.setShortcuts);
|
const setShortcuts = useShortcutsStore((state) => state.setShortcuts);
|
||||||
const unavaliableShortcuts = useShortcutsStore(
|
const unavaliableShortcuts = useShortcutsStore(
|
||||||
|
|
@ -48,7 +48,7 @@ export default function EditShortcutButton({
|
||||||
);
|
);
|
||||||
|
|
||||||
function editCombination(): void {
|
function editCombination(): void {
|
||||||
console.log(canEditCombination(key));
|
if (key) {
|
||||||
if (canEditCombination(key)) {
|
if (canEditCombination(key)) {
|
||||||
const newCombination = defaultShortcuts.map((s) => {
|
const newCombination = defaultShortcuts.map((s) => {
|
||||||
if (s.name === shortcut[0]) {
|
if (s.name === shortcut[0]) {
|
||||||
|
|
@ -62,20 +62,32 @@ export default function EditShortcutButton({
|
||||||
return s;
|
return s;
|
||||||
});
|
});
|
||||||
const fixCombination = key.split(" ");
|
const fixCombination = key.split(" ");
|
||||||
|
if (
|
||||||
|
fixCombination[0].toLowerCase().includes("ctrl") ||
|
||||||
|
fixCombination[0].toLowerCase().includes("cmd")
|
||||||
|
) {
|
||||||
fixCombination[0] = "mod";
|
fixCombination[0] = "mod";
|
||||||
|
}
|
||||||
const shortcutName = shortcut[0].split(" ")[0].toLowerCase();
|
const shortcutName = shortcut[0].split(" ")[0].toLowerCase();
|
||||||
setUniqueShortcut(shortcutName, fixCombination.join("").toLowerCase());
|
setUniqueShortcut(shortcutName, fixCombination.join("").toLowerCase());
|
||||||
|
console.log(newCombination);
|
||||||
setShortcuts(newCombination, unavailable);
|
setShortcuts(newCombination, unavailable);
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
setSuccessData({ title: `${shortcut[0]} shortcut successfully changed` });
|
setSuccessData({
|
||||||
setKey(isMac ? "META" : "CTRL");
|
title: `${shortcut[0]} shortcut successfully changed`,
|
||||||
|
});
|
||||||
|
setKey(null);
|
||||||
localStorage.setItem(
|
localStorage.setItem(
|
||||||
"langflow-shortcuts",
|
"langflow-shortcuts",
|
||||||
JSON.stringify(newCombination),
|
JSON.stringify(newCombination),
|
||||||
);
|
);
|
||||||
localStorage.setItem("langflow-UShortcuts", JSON.stringify(unavailable));
|
localStorage.setItem(
|
||||||
|
"langflow-UShortcuts",
|
||||||
|
JSON.stringify(unavailable),
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
setErrorData({
|
setErrorData({
|
||||||
title: "Error saving key combination",
|
title: "Error saving key combination",
|
||||||
list: ["This combination already exists!"],
|
list: ["This combination already exists!"],
|
||||||
|
|
@ -83,18 +95,29 @@ export default function EditShortcutButton({
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!open) setKey(isMac ? "Cmd" : "Ctrl");
|
if (!open) setKey(null);
|
||||||
console.log(key);
|
|
||||||
}, [open, setOpen, key]);
|
}, [open, setOpen, key]);
|
||||||
|
|
||||||
|
function getFixedCombination({
|
||||||
|
oldKey,
|
||||||
|
key,
|
||||||
|
}: {
|
||||||
|
oldKey: string;
|
||||||
|
key: string;
|
||||||
|
}): string {
|
||||||
|
if (oldKey === null) {
|
||||||
|
return `${key.length > 0 ? toTitleCase(key) : toTitleCase(key)}`;
|
||||||
|
}
|
||||||
|
return `${oldKey.length > 0 ? toTitleCase(oldKey) : oldKey.toUpperCase()} + ${key.length > 0 ? toTitleCase(key) : key.toUpperCase()}`;
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
function onKeyDown(e: KeyboardEvent) {
|
function onKeyDown(e: KeyboardEvent) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
if (key) {
|
||||||
if (key.toUpperCase().includes(e.key.toUpperCase())) return;
|
if (key.toUpperCase().includes(e.key.toUpperCase())) return;
|
||||||
setKey(
|
}
|
||||||
(oldKey) =>
|
setKey((oldKey) => getFixedCombination({ oldKey: oldKey!, key: e.key }));
|
||||||
`${oldKey.length > 0 ? toTitleCase(oldKey) : oldKey.toUpperCase()} + ${e.key.length > 0 ? toTitleCase(e.key) : e.key.toUpperCase()}`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener("keydown", onKeyDown);
|
document.addEventListener("keydown", onKeyDown);
|
||||||
|
|
@ -118,7 +141,7 @@ export default function EditShortcutButton({
|
||||||
<BaseModal.Content>
|
<BaseModal.Content>
|
||||||
<div className="align-center flex h-full w-full justify-center gap-4">
|
<div className="align-center flex h-full w-full justify-center gap-4">
|
||||||
<div className="flex items-center justify-center text-center text-lg font-bold">
|
<div className="flex items-center justify-center text-center text-lg font-bold">
|
||||||
{key.toUpperCase()}
|
{key && key.toUpperCase()}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</BaseModal.Content>
|
</BaseModal.Content>
|
||||||
|
|
@ -127,7 +150,7 @@ export default function EditShortcutButton({
|
||||||
<Button
|
<Button
|
||||||
className="mr-5"
|
className="mr-5"
|
||||||
variant={"destructive"}
|
variant={"destructive"}
|
||||||
onClick={() => setKey(isMac ? "META" : "CTRL")}
|
onClick={() => setKey(null)}
|
||||||
>
|
>
|
||||||
Reset Combination
|
Reset Combination
|
||||||
</Button>
|
</Button>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue