Skip to content

Commit 66e8994

Browse files
committed
separate key handlers into different hooks
1 parent f99a3e2 commit 66e8994

File tree

1 file changed

+81
-40
lines changed

1 file changed

+81
-40
lines changed

client/modules/IDE/components/ConsoleInput.jsx

Lines changed: 81 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,75 +24,116 @@ function ConsoleInput({ theme, dispatchConsoleEvent, fontSize }) {
2424
inputStyle: 'contenteditable'
2525
});
2626

27-
cmInstance.current.on('keydown', (cm, e) => {
27+
cmInstance.current.getWrapperElement().style['font-size'] = `${fontSize}px`;
28+
29+
return () => {
30+
if (cmInstance.current) {
31+
cmInstance.current = null;
32+
}
33+
};
34+
}, []);
35+
36+
useEffect(() => {
37+
if (cmInstance.current) {
38+
cmInstance.current.setOption('theme', `p5-${theme}`);
39+
cmInstance.current.getWrapperElement().style[
40+
'font-size'
41+
] = `${fontSize}px`;
42+
cmInstance.current.refresh();
43+
}
44+
}, [theme, fontSize]);
45+
46+
useEffect(() => {
47+
const handleEnterKey = (cm, e) => {
2848
if (e.key === 'Enter' && !e.shiftKey) {
2949
e.preventDefault();
3050
e.stopPropagation();
31-
const value = cm.getValue();
32-
if (value.trim(' ') === '') {
33-
return false;
34-
}
51+
52+
const value = cm.getValue().trim();
53+
if (value === '') return;
54+
3555
const messages = [
3656
{ log: Encode({ method: 'command', data: [value] }) }
3757
];
3858
const consoleEvent = [{ method: 'command', data: [value] }];
59+
3960
dispatchMessage({
4061
type: MessageTypes.EXECUTE,
41-
payload: {
42-
source: 'console',
43-
messages
44-
}
62+
payload: { source: 'console', messages }
4563
});
64+
4665
dispatchConsoleEvent(consoleEvent);
4766
cm.setValue('');
4867
setCommandHistory([value, ...commandHistory]);
4968
setCommandCursor(-1);
50-
} else if (e.key === 'ArrowUp') {
51-
const lineNumber = cmInstance.current.getDoc().getCursor().line;
52-
if (lineNumber !== 0) {
53-
return false;
54-
}
69+
}
70+
};
71+
72+
if (cmInstance.current) {
73+
cmInstance.current.on('keydown', handleEnterKey);
74+
}
75+
76+
return () => {
77+
if (cmInstance.current) {
78+
cmInstance.current.off('keydown', handleEnterKey);
79+
}
80+
};
81+
}, [commandHistory, dispatchConsoleEvent]);
82+
83+
useEffect(() => {
84+
const handleUpArrowKey = (cm, e) => {
85+
if (e.key === 'ArrowUp') {
86+
const lineNumber = cm.getDoc().getCursor().line;
87+
if (lineNumber !== 0) return;
5588

5689
const newCursor = Math.min(
5790
commandCursor + 1,
5891
commandHistory.length - 1
5992
);
60-
cmInstance.current.getDoc().setValue(commandHistory[newCursor] || '');
61-
const cursorPos = cmInstance.current.getDoc().getLine(0).length - 1;
62-
cmInstance.current.getDoc().setCursor({ line: 0, ch: cursorPos });
93+
cm.setValue(commandHistory[newCursor] || '');
94+
const cursorPos = cm.current.getDoc().getLine(0).length - 1;
95+
cm.getDoc().setCursor({ line: 0, ch: cursorPos });
6396
setCommandCursor(newCursor);
64-
} else if (e.key === 'ArrowDown') {
65-
const lineNumber = cmInstance.current.getDoc().getCursor().line;
66-
const lineCount = cmInstance.current.getValue().split('\n').length;
67-
if (lineNumber + 1 !== lineCount) {
68-
return false;
69-
}
97+
}
98+
};
99+
100+
if (cmInstance.current) {
101+
cmInstance.current.on('keydown', handleUpArrowKey);
102+
}
103+
104+
return () => {
105+
if (cmInstance.current) {
106+
cmInstance.current.off('keydown', handleUpArrowKey);
107+
}
108+
};
109+
}, [commandCursor, commandHistory]);
110+
111+
useEffect(() => {
112+
const handleArrowDownKey = (cm, e) => {
113+
if (e.key === 'ArrowDown') {
114+
const lineNumber = cm.getDoc().getCursor().line;
115+
const lineCount = cm.lineCount();
116+
if (lineNumber + 1 !== lineCount) return;
70117

71118
const newCursor = Math.max(commandCursor - 1, -1);
72-
cmInstance.current.getDoc().setValue(commandHistory[newCursor] || '');
73-
const newLineCount = cmInstance.current.getValue().split('\n').length;
74-
const newLine = cmInstance.current.getDoc().getLine(newLineCount);
119+
cm.setValue(commandHistory[newCursor] || '');
120+
const newLine = cm.getDoc().getLine(lineCount - 1);
75121
const cursorPos = newLine ? newLine.length - 1 : 1;
76-
cmInstance.current
77-
.getDoc()
78-
.setCursor({ line: lineCount, ch: cursorPos });
122+
cm.getDoc().setCursor({ line: lineCount - 1, ch: cursorPos });
79123
setCommandCursor(newCursor);
80124
}
81-
return true;
82-
});
125+
};
83126

84-
cmInstance.current.getWrapperElement().style['font-size'] = `${fontSize}px`;
127+
if (cmInstance.current) {
128+
cmInstance.current.on('keydown', handleArrowDownKey);
129+
}
85130

86131
return () => {
87-
cmInstance.current = null;
132+
if (cmInstance.current) {
133+
cmInstance.current.off('keydown', handleArrowDownKey);
134+
}
88135
};
89-
}, []);
90-
91-
useEffect(() => {
92-
cmInstance.current.setOption('theme', `p5-${theme}`);
93-
cmInstance.current.getWrapperElement().style['font-size'] = `${fontSize}px`;
94-
cmInstance.current.refresh();
95-
}, [theme, fontSize]);
136+
}, [commandCursor, commandHistory]);
96137

97138
return (
98139
<div className="console__input">

0 commit comments

Comments
 (0)