(e, consoleInput)
| 889 | * @param {HTMLElement} consoleInput the console input element |
| 890 | */ |
| 891 | const OnConsoleCommand = (e, consoleInput) => { |
| 892 | if (e.key === "Enter") { |
| 893 | const [cmd, ...args] = consoleInput.value.split(" "); |
| 894 | |
| 895 | let command = CommandsList[cmd]; |
| 896 | |
| 897 | if (command) |
| 898 | command.action(args); |
| 899 | else |
| 900 | AddConsoleLine(consoleInput.value, GetLocale("cmd_unknown")); |
| 901 | |
| 902 | if (consoleInput.value != "") |
| 903 | cmdHistory.push(consoleInput.value); |
| 904 | cmdHistIndex = 0; |
| 905 | } |
| 906 | else if (e.key === "ArrowUp") { |
| 907 | e.preventDefault(); |
| 908 | let newIndex = cmdHistory.length - (cmdHistIndex + 1); |
| 909 | if (newIndex > 0) cmdHistIndex += 1; |
| 910 | |
| 911 | let cmd = cmdHistory[newIndex > 0 ? newIndex : 0]; |
| 912 | consoleInput.value = cmd || ""; |
| 913 | let cursorPosition = cmd ? cmd.length : 0; |
| 914 | consoleInput.setSelectionRange(cursorPosition, cursorPosition); |
| 915 | } |
| 916 | else if (e.key === "ArrowDown") { |
| 917 | e.preventDefault(); |
| 918 | let newIndex = cmdHistory.length - 1 - (cmdHistIndex - 1); |
| 919 | if (newIndex < cmdHistory.length) cmdHistIndex -= 1; |
| 920 | |
| 921 | let cmd = cmdHistory[newIndex < cmdHistory.length ? newIndex : cmdHistory.length]; |
| 922 | consoleInput.value = cmd || ""; |
| 923 | let cursorPosition = cmd ? cmd.length : 0; |
| 924 | consoleInput.setSelectionRange(cursorPosition, cursorPosition); |
| 925 | } |
| 926 | else if (e.key === "Tab") { |
| 927 | e.preventDefault(); |
| 928 | const [cmd, ...args] = consoleInput.value.split(" "); |
| 929 | |
| 930 | if (args.length > 0) |
| 931 | return; |
| 932 | |
| 933 | let nbStartingBy = 0; |
| 934 | let lastStartingBy = ""; |
| 935 | for (const command in CommandsList) { |
| 936 | if (command.startsWith(cmd)) { |
| 937 | nbStartingBy++; |
| 938 | lastStartingBy = command; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | if (nbStartingBy === 1) { |
| 943 | consoleInput.value = lastStartingBy; |
| 944 | let cmdLength = lastStartingBy.length; |
| 945 | consoleInput.setSelectionRange(cmdLength, cmdLength); |
| 946 | } |
| 947 | } |
| 948 | }; |
no test coverage detected