@@ -375,39 +375,42 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
375375
376376 const onInput = useCallback (
377377 async ( code : string ) => {
378- const newOutput = [ ...( outputRef . current ?? [ ] ) ] ;
379- const newHistory = [ ...( historyRef . current ?? [ ] ) ] ;
378+ const newOutputBeforeEval = [ ...( outputRef . current ?? [ ] ) ] ;
380379
381380 // don't evaluate empty input, but do add it to the output
382381 if ( ! code || code . trim ( ) === '' ) {
383- newOutput . push ( {
382+ newOutputBeforeEval . push ( {
384383 format : 'input' ,
385384 value : ' ' ,
386385 } ) ;
387- capLengthEnd ( newOutput , maxOutputLength ) ;
388- outputRef . current = newOutput ;
389- onOutputChanged ?.( newOutput ) ;
386+ capLengthEnd ( newOutputBeforeEval , maxOutputLength ) ;
387+ outputRef . current = newOutputBeforeEval ;
388+ onOutputChanged ?.( newOutputBeforeEval ) ;
390389 return ;
391390 }
392391
393392 // add input to output
394- newOutput . push ( {
393+ newOutputBeforeEval . push ( {
395394 format : 'input' ,
396395 value : code ,
397396 } ) ;
398- capLengthEnd ( newOutput , maxOutputLength ) ;
399- outputRef . current = newOutput ;
400- onOutputChanged ?.( newOutput ) ;
397+ capLengthEnd ( newOutputBeforeEval , maxOutputLength ) ;
398+ outputRef . current = newOutputBeforeEval ;
399+ onOutputChanged ?.( newOutputBeforeEval ) ;
401400
402401 const outputLine = await evaluate ( code ) ;
403402
403+ // outputRef.current could have changed if evaluate() used onPrint
404+ const newOutputAfterEval = [ ...( outputRef . current ?? [ ] ) ] ;
405+
404406 // add output to output
405- newOutput . push ( outputLine ) ;
406- capLengthEnd ( newOutput , maxOutputLength ) ;
407- outputRef . current = newOutput ;
408- onOutputChanged ?.( newOutput ) ;
407+ newOutputAfterEval . push ( outputLine ) ;
408+ capLengthEnd ( newOutputAfterEval , maxOutputLength ) ;
409+ outputRef . current = newOutputAfterEval ;
410+ onOutputChanged ?.( newOutputAfterEval ) ;
409411
410412 // update history
413+ const newHistory = [ ...( historyRef . current ?? [ ] ) ] ;
411414 newHistory . unshift ( code ) ;
412415 capLengthStart ( newHistory , maxHistoryLength ) ;
413416 changeHistory (
0 commit comments