@@ -63,6 +63,9 @@ const PyodideRunner = (props) => {
6363 const [ visuals , setVisuals ] = useState ( [ ] ) ;
6464 const [ showRunner , setShowRunner ] = useState ( active ) ;
6565 const [ inputStack , setInputStack ] = useState ( [ ] ) ;
66+ const [ indentationLevel , setIndentationLevel ] = useState ( 0 ) ;
67+ const [ awaitingInput , setAwaitingInput ] = useState ( false ) ;
68+
6669 const prependToInputStack = ( input ) => {
6770 setInputStack ( ( prevInputStack ) => {
6871 if ( prevInputStack [ 0 ] === "" ) {
@@ -79,6 +82,20 @@ const PyodideRunner = (props) => {
7982 } ;
8083 const [ inputStackIndex , setInputStackIndex ] = useState ( 0 ) ;
8184
85+ const incrementIndentationLevel = ( ) => {
86+ setIndentationLevel ( ( prevIndentationLevel ) => prevIndentationLevel + 1 ) ;
87+ } ;
88+
89+ const decrementIndentationLevel = ( ) => {
90+ setIndentationLevel ( ( prevIndentationLevel ) => {
91+ return Math . max ( 0 , prevIndentationLevel - 1 ) ;
92+ } ) ;
93+ } ;
94+
95+ useEffect ( ( ) => {
96+ console . log ( "indentationLevel" , indentationLevel ) ;
97+ } , [ indentationLevel ] ) ;
98+
8299 useEffect ( ( ) => {
83100 console . log ( "isOutputOnly" , isOutputOnly ) ;
84101 } ) ;
@@ -102,6 +119,20 @@ const PyodideRunner = (props) => {
102119 }
103120 } , [ inputStack , inputStackIndex , consoleMode ] ) ;
104121
122+ useEffect ( ( ) => {
123+ if ( awaitingInput && consoleMode ) {
124+ const inputElement = getInputElement ( ) ;
125+ inputElement . innerText = " " . repeat ( indentationLevel * 4 ) ;
126+ // move cursor to end of text
127+ const range = document . createRange ( ) ;
128+ const selection = window . getSelection ( ) ;
129+ range . selectNodeContents ( inputElement ) ;
130+ range . collapse ( false ) ;
131+ selection . removeAllRanges ( ) ;
132+ selection . addRange ( range ) ;
133+ }
134+ } , [ awaitingInput , indentationLevel , consoleMode ] ) ;
135+
105136 useEffect ( ( ) => {
106137 console . log ( "inputStack" , inputStack ) ;
107138 } , [ inputStack ] ) ;
@@ -208,7 +239,16 @@ const PyodideRunner = (props) => {
208239
209240 const element = getInputElement ( ) ;
210241 const { content, ctrlD } = await getInputContent ( element ) ;
242+ setAwaitingInput ( false ) ;
243+
211244 prependToInputStack ( content ) ;
245+ if ( content . trimEnd ( ) . slice ( - 1 ) === ":" ) {
246+ incrementIndentationLevel ( ) ;
247+ } else if ( content . trimEnd ( ) === "" ) {
248+ console . log ( "the content is" ) ;
249+ console . log ( content ) ;
250+ decrementIndentationLevel ( ) ;
251+ }
212252
213253 const encoder = new TextEncoder ( ) ;
214254 const bytes = encoder . encode ( content + "\n" ) ;
@@ -310,6 +350,7 @@ const PyodideRunner = (props) => {
310350 span . setAttribute ( "spellCheck" , "false" ) ;
311351 span . setAttribute ( "class" , "pythonrunner-input" ) ;
312352 span . setAttribute ( "contentEditable" , "true" ) ;
353+ setAwaitingInput ( true ) ;
313354 return span ;
314355 } ;
315356
0 commit comments