@@ -75,7 +75,6 @@ const PyodideRunner = ({ active, consoleMode = false, autoRun = false }) => {
75
75
const showVisualTab = queryParams . get ( "show_visual_tab" ) === "true" ;
76
76
const [ hasVisual , setHasVisual ] = useState ( showVisualTab || senseHatAlways ) ;
77
77
const [ visuals , setVisuals ] = useState ( [ ] ) ;
78
- const [ showRunner , setShowRunner ] = useState ( active ) ;
79
78
const [ inputStack , setInputStack ] = useState ( [ ] ) ;
80
79
const [ indentationLevel , setIndentationLevel ] = useState ( 0 ) ;
81
80
const [ awaitingInput , setAwaitingInput ] = useState ( false ) ;
@@ -88,30 +87,41 @@ const PyodideRunner = ({ active, consoleMode = false, autoRun = false }) => {
88
87
const prependToInputStack = ( input ) => {
89
88
setInputStack ( ( prevInputStack ) => {
90
89
if ( prevInputStack [ 0 ] === "" ) {
91
- console . log ( "overwriting..." ) ;
92
90
const newStack = [ ...prevInputStack ] ;
93
91
newStack [ 0 ] = input ;
94
92
return newStack ;
95
93
} else {
96
- console . log ( "prepending..." ) ;
97
- console . log ( prevInputStack ) ;
98
94
return [ input , ...prevInputStack ] ;
99
95
}
100
96
} ) ;
101
97
} ;
102
98
const [ inputStackIndex , setInputStackIndex ] = useState ( 0 ) ;
103
99
104
- const incrementIndentationLevel = ( ) => {
105
- setIndentationLevel ( ( prevIndentationLevel ) => prevIndentationLevel + 1 ) ;
100
+ const incrementIndentationLevel = ( prevLine ) => {
101
+ // console.log("prevLine", prevLine);
102
+ // console.log(prevLine.match(/^\s*/)[0].length);
103
+ const prevLevel = prevLine
104
+ ? Math . floor ( prevLine . match ( / ^ \s * / ) [ 0 ] . length / 4 )
105
+ : 0 ;
106
+ setIndentationLevel ( prevLevel + 1 ) ;
106
107
} ;
107
108
108
- useEffect ( ( ) => {
109
- console . log ( "indentationLevel" , indentationLevel ) ;
110
- } , [ indentationLevel ] ) ;
109
+ const keepSameIndentationLevel = ( prevLine ) => {
110
+ const prevLevel = prevLine
111
+ ? Math . floor ( prevLine . match ( / ^ \s * / ) [ 0 ] . length / 4 )
112
+ : 0 ;
113
+ setIndentationLevel ( prevLevel ) ;
114
+ } ;
111
115
112
- useEffect ( ( ) => {
113
- console . log ( "isOutputOnly" , isOutputOnly ) ;
114
- } ) ;
116
+ const handleIndentationLevel = ( prevLine ) => {
117
+ if ( prevLine . trimEnd ( ) . slice ( - 1 ) === ":" ) {
118
+ incrementIndentationLevel ( prevLine ) ;
119
+ } else if ( prevLine . trimEnd ( ) === "" ) {
120
+ setIndentationLevel ( 0 ) ;
121
+ } else {
122
+ keepSameIndentationLevel ( prevLine ) ;
123
+ }
124
+ } ;
115
125
116
126
useEffect ( ( ) => {
117
127
const handleKeyDown = ( event ) => {
@@ -149,14 +159,16 @@ const PyodideRunner = ({ active, consoleMode = false, autoRun = false }) => {
149
159
} , [ awaitingInput , indentationLevel , consoleMode ] ) ;
150
160
151
161
useEffect ( ( ) => {
152
- console . log ( "inputStack" , inputStack ) ;
153
- } , [ inputStack ] ) ;
154
-
155
- useEffect ( ( ) => {
156
- console . log ( "inputStackIndex" , inputStackIndex ) ;
157
162
const inputElement = getInputElement ( ) ;
158
163
if ( inputElement ) {
159
164
inputElement . innerText = inputStack [ inputStackIndex ] ;
165
+ // move cursor to end of text
166
+ const range = document . createRange ( ) ;
167
+ const selection = window . getSelection ( ) ;
168
+ range . selectNodeContents ( inputElement ) ;
169
+ range . collapse ( false ) ;
170
+ selection . removeAllRanges ( ) ;
171
+ selection . addRange ( range ) ;
160
172
}
161
173
} , [ inputStackIndex ] ) ;
162
174
@@ -199,10 +211,10 @@ const PyodideRunner = ({ active, consoleMode = false, autoRun = false }) => {
199
211
} , [ pyodideWorker ] ) ;
200
212
201
213
useEffect ( ( ) => {
202
- if ( autoRun ) {
214
+ if ( autoRun && active && pyodideWorker ) {
203
215
dispatch ( triggerCodeRun ( ) ) ;
204
216
}
205
- } , [ ] ) ;
217
+ } , [ active , pyodideWorker ] ) ;
206
218
207
219
useEffect ( ( ) => {
208
220
if ( codeRunTriggered && active && output . current ) {
@@ -254,13 +266,19 @@ const PyodideRunner = ({ active, consoleMode = false, autoRun = false }) => {
254
266
setAwaitingInput ( false ) ;
255
267
256
268
prependToInputStack ( content ) ;
257
- if ( content . trimEnd ( ) . slice ( - 1 ) === ":" ) {
258
- incrementIndentationLevel ( ) ;
259
- } else if ( content . trimEnd ( ) === "" ) {
260
- console . log ( "the content is" ) ;
261
- console . log ( content ) ;
262
- setIndentationLevel ( 0 ) ;
263
- }
269
+ // if (content.trimEnd().slice(-1) === ":") {
270
+ // incrementIndentationLevel(content);
271
+ // } else if (content.trimEnd() === "") {
272
+ // // console.log("the content is");
273
+ // // console.log(content);
274
+ // setIndentationLevel(0);
275
+ // }
276
+
277
+ console . log (
278
+ "indentedLine" ,
279
+ getInputElement ( ) . parentElement . innerText . matches ( / ...: / ) ,
280
+ ) ;
281
+ handleIndentationLevel ( content ) ;
264
282
265
283
const encoder = new TextEncoder ( ) ;
266
284
const bytes = encoder . encode ( content + "\n" ) ;
0 commit comments