MISC: Add key binding feature (#1830)

This commit is contained in:
catloversg
2025-02-28 13:59:12 +07:00
committed by GitHub
parent 3ba89eb388
commit 8ed83f3d37
44 changed files with 1201 additions and 305 deletions
+14 -8
View File
@@ -38,6 +38,12 @@ import { RamCalculationErrorCode } from "../../Script/RamCalculationErrorCodes";
import { hasScriptExtension, isLegacyScript, type ScriptFilePath } from "../../Paths/ScriptFilePath";
import { exceptionAlert } from "../../utils/helpers/exceptionAlert";
import type { BaseServer } from "../../Server/BaseServer";
import {
convertKeyboardEventToKeyCombination,
CurrentKeyBindings,
determineKeyBindingTypes,
ScriptEditorAction,
} from "../../utils/KeyBindingUtils";
import { SpecialServers } from "../../Server/data/SpecialServers";
import { SnackbarEvents } from "../../ui/React/Snackbar";
import { ToastVariant } from "@enums";
@@ -215,19 +221,19 @@ function Root(props: IProps): React.ReactElement {
useEffect(() => {
function keydown(event: KeyboardEvent): void {
if (Settings.DisableHotkeys) return;
//Ctrl + b
if (event.code == "KeyB" && (event.ctrlKey || event.metaKey)) {
event.preventDefault();
Router.toPage(Page.Terminal);
if (Settings.DisableHotkeys) {
return;
}
// CTRL/CMD + S
if (event.code == "KeyS" && (event.ctrlKey || event.metaKey)) {
const keyBindingTypes = determineKeyBindingTypes(CurrentKeyBindings, convertKeyboardEventToKeyCombination(event));
if (keyBindingTypes.has(ScriptEditorAction.Save)) {
event.preventDefault();
event.stopPropagation();
save();
}
if (keyBindingTypes.has(ScriptEditorAction.GoToTerminal)) {
event.preventDefault();
Router.toPage(Page.Terminal);
}
}
document.addEventListener("keydown", keydown);
return () => document.removeEventListener("keydown", keydown);
+9 -4
View File
@@ -22,6 +22,7 @@ import { Settings } from "../../Settings/Settings";
import { OptionsModal, OptionsModalProps } from "./OptionsModal";
import { useScriptEditorContext } from "./ScriptEditorContext";
import { NsApiDocumentationLink } from "../../ui/React/NsApiDocumentationLink";
import { CurrentKeyBindings, parseKeyCombinationsToString, ScriptEditorAction } from "../../utils/KeyBindingUtils";
type IStandaloneCodeEditor = monaco.editor.IStandaloneCodeEditor;
@@ -67,10 +68,14 @@ export function Toolbar({ editor, onSave }: IProps) {
<Button color={isUpdatingRAM ? "secondary" : "primary"} sx={{ mx: 1 }} onClick={openRAMInfo}>
{ram}
</Button>
<Button onClick={onSave}>Save (Ctrl/Cmd + s)</Button>
<Button sx={{ mx: 1 }} onClick={() => Router.toPage(Page.Terminal)}>
Terminal (Ctrl/Cmd + b)
</Button>
<Tooltip title={parseKeyCombinationsToString(CurrentKeyBindings[ScriptEditorAction.Save])}>
<Button onClick={onSave}>Save</Button>
</Tooltip>
<Tooltip title={parseKeyCombinationsToString(CurrentKeyBindings[ScriptEditorAction.GoToTerminal])}>
<Button sx={{ mx: 1 }} onClick={() => Router.toPage(Page.Terminal)}>
Terminal
</Button>
</Tooltip>
<Typography>
<NsApiDocumentationLink />
</Typography>