@@ -24,75 +24,116 @@ function ConsoleInput({ theme, dispatchConsoleEvent, fontSize }) {
24
24
inputStyle : 'contenteditable'
25
25
} ) ;
26
26
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 ) => {
28
48
if ( e . key === 'Enter' && ! e . shiftKey ) {
29
49
e . preventDefault ( ) ;
30
50
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
+
35
55
const messages = [
36
56
{ log : Encode ( { method : 'command' , data : [ value ] } ) }
37
57
] ;
38
58
const consoleEvent = [ { method : 'command' , data : [ value ] } ] ;
59
+
39
60
dispatchMessage ( {
40
61
type : MessageTypes . EXECUTE ,
41
- payload : {
42
- source : 'console' ,
43
- messages
44
- }
62
+ payload : { source : 'console' , messages }
45
63
} ) ;
64
+
46
65
dispatchConsoleEvent ( consoleEvent ) ;
47
66
cm . setValue ( '' ) ;
48
67
setCommandHistory ( [ value , ...commandHistory ] ) ;
49
68
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 ;
55
88
56
89
const newCursor = Math . min (
57
90
commandCursor + 1 ,
58
91
commandHistory . length - 1
59
92
) ;
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 } ) ;
63
96
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 ;
70
117
71
118
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 ) ;
75
121
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 } ) ;
79
123
setCommandCursor ( newCursor ) ;
80
124
}
81
- return true ;
82
- } ) ;
125
+ } ;
83
126
84
- cmInstance . current . getWrapperElement ( ) . style [ 'font-size' ] = `${ fontSize } px` ;
127
+ if ( cmInstance . current ) {
128
+ cmInstance . current . on ( 'keydown' , handleArrowDownKey ) ;
129
+ }
85
130
86
131
return ( ) => {
87
- cmInstance . current = null ;
132
+ if ( cmInstance . current ) {
133
+ cmInstance . current . off ( 'keydown' , handleArrowDownKey ) ;
134
+ }
88
135
} ;
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 ] ) ;
96
137
97
138
return (
98
139
< div className = "console__input" >
0 commit comments