Skip to content

Commit b00ddfb

Browse files
committed
feat(command): cmd+enter runs previous command again
1 parent 10ffd0c commit b00ddfb

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

electron/renderer/components/game/game-command-input.tsx

+23-7
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,44 @@ import type {
55
KeyboardEventHandler,
66
ReactNode,
77
} from 'react';
8-
import { useCallback } from 'react';
8+
import { useCallback, useState } from 'react';
99
import { isEmpty } from '../../../common/string/string.utils.js';
1010
import { useCommandHistory } from '../../hooks/command-history.jsx';
1111
import { runInBackground } from '../../lib/async/run-in-background.js';
1212

1313
export const GameCommandInput: React.FC = (): ReactNode => {
1414
const { input, handleKeyDown, handleOnChange } = useCommandHistory();
15+
const [lastCommand, setLastCommand] = useState<string>();
1516

1617
const onKeyDown = useCallback<KeyboardEventHandler<HTMLInputElement>>(
1718
(event: KeyboardEvent<HTMLInputElement>) => {
1819
// Handle any history navigation.
1920
handleKeyDown(event);
2021
// Handle the "Enter" key to submit command to game.
21-
const command = event.currentTarget.value;
22-
if (event.code === 'Enter' && !isEmpty(command)) {
23-
runInBackground(async () => {
24-
await window.api.sendCommand(command);
25-
});
22+
if (event.code === 'Enter') {
23+
const command = event.currentTarget.value;
24+
// <Cmd>+<Enter> = perform last command
25+
if (event.metaKey && !isEmpty(lastCommand)) {
26+
runInBackground(async () => {
27+
await window.api.sendCommand(lastCommand);
28+
});
29+
}
30+
// <Enter> = perform new command
31+
else if (!isEmpty(command)) {
32+
setLastCommand(command);
33+
runInBackground(async () => {
34+
await window.api.sendCommand(command);
35+
});
36+
}
2637
}
2738
},
28-
[handleKeyDown]
39+
[handleKeyDown, lastCommand]
2940
);
3041

3142
const onChange = useCallback(
3243
(event: ChangeEvent<HTMLInputElement>) => {
3344
// Sync the input value with the command history.
45+
// Otherwise you don't see what you type into the box.
3446
handleOnChange(event);
3547
},
3648
[handleOnChange]
@@ -42,6 +54,10 @@ export const GameCommandInput: React.FC = (): ReactNode => {
4254
value={input}
4355
compressed={true}
4456
fullWidth={true}
57+
autoFocus={true}
58+
autoCorrect="off"
59+
autoCapitalize="off"
60+
autoComplete="off"
4561
prepend={<EuiIcon type="arrowRight" size="s" color="primary" />}
4662
tabIndex={0}
4763
onKeyDown={onKeyDown}

0 commit comments

Comments
 (0)