11import PropTypes from 'prop-types' ;
2- import React , { useState , useEffect , useRef } from 'react' ;
2+ import React , { useRef , useEffect , useState } from 'react' ;
33import CodeMirror from 'codemirror' ;
44import { Encode } from 'console-feed' ;
5+
56import RightArrowIcon from '../../../images/right-arrow.svg' ;
67import { dispatchMessage , MessageTypes } from '../../../utils/dispatcher' ;
78
8- const ConsoleInput = ( { theme, dispatchConsoleEvent, fontSize } ) => {
9+ // heavily inspired by
10+ // https://github.com/codesandbox/codesandbox-client/blob/92a1131f4ded6f7d9c16945dc7c18aa97c8ada27/packages/app/src/app/components/Preview/DevTools/Console/Input/index.tsx
11+
12+ function ConsoleInput ( { theme, dispatchConsoleEvent, fontSize } ) {
913 const [ commandHistory , setCommandHistory ] = useState ( [ ] ) ;
1014 const [ commandCursor , setCommandCursor ] = useState ( - 1 ) ;
1115 const codemirrorContainer = useRef ( null ) ;
@@ -25,10 +29,9 @@ const ConsoleInput = ({ theme, dispatchConsoleEvent, fontSize }) => {
2529 e . preventDefault ( ) ;
2630 e . stopPropagation ( ) ;
2731 const value = cm . getValue ( ) ;
28- if ( value . trim ( ) === '' ) {
32+ if ( value . trim ( ' ' ) === '' ) {
2933 return false ;
3034 }
31-
3235 const messages = [
3336 { log : Encode ( { method : 'command' , data : [ value ] } ) }
3437 ] ;
@@ -42,58 +45,53 @@ const ConsoleInput = ({ theme, dispatchConsoleEvent, fontSize }) => {
4245 } ) ;
4346 dispatchConsoleEvent ( consoleEvent ) ;
4447 cm . setValue ( '' ) ;
45- setCommandHistory ( ( prevHistory ) => [ value , ...prevHistory ] ) ;
48+ setCommandHistory ( [ value , ...commandHistory ] ) ;
4649 setCommandCursor ( - 1 ) ;
4750 } else if ( e . key === 'ArrowUp' ) {
4851 const lineNumber = cmInstance . current . getDoc ( ) . getCursor ( ) . line ;
4952 if ( lineNumber !== 0 ) {
5053 return false ;
5154 }
5255
53- setCommandCursor ( ( prevCursor ) => {
54- const newCursor = Math . min ( prevCursor + 1 , commandHistory . length - 1 ) ;
55- cmInstance . current . getDoc ( ) . setValue ( commandHistory [ newCursor ] || '' ) ;
56- const cursorPos = cmInstance . current . getDoc ( ) . getLine ( 0 ) . length - 1 ;
57- cmInstance . current . getDoc ( ) . setCursor ( { line : 0 , ch : cursorPos } ) ;
58- return newCursor ;
59- } ) ;
56+ const newCursor = Math . min (
57+ commandCursor + 1 ,
58+ commandHistory . length - 1
59+ ) ;
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 } ) ;
63+ setCommandCursor ( newCursor ) ;
6064 } else if ( e . key === 'ArrowDown' ) {
6165 const lineNumber = cmInstance . current . getDoc ( ) . getCursor ( ) . line ;
6266 const lineCount = cmInstance . current . getValue ( ) . split ( '\n' ) . length ;
6367 if ( lineNumber + 1 !== lineCount ) {
6468 return false ;
6569 }
6670
67- setCommandCursor ( ( prevCursor ) => {
68- const newCursor = Math . max ( prevCursor - 1 , - 1 ) ;
69- cmInstance . current . getDoc ( ) . setValue ( commandHistory [ newCursor ] || '' ) ;
70- const newLineCount = cmInstance . current . getValue ( ) . split ( '\n' ) . length ;
71- const newLine = cmInstance . current . getDoc ( ) . getLine ( newLineCount ) ;
72- const cursorPos = newLine ? newLine . length - 1 : 1 ;
73- cmInstance . current
74- . getDoc ( )
75- . setCursor ( { line : lineCount , ch : cursorPos } ) ;
76- return newCursor ;
77- } ) ;
71+ 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 ) ;
75+ const cursorPos = newLine ? newLine . length - 1 : 1 ;
76+ cmInstance . current
77+ . getDoc ( )
78+ . setCursor ( { line : lineCount , ch : cursorPos } ) ;
79+ setCommandCursor ( newCursor ) ;
7880 }
7981 return true ;
8082 } ) ;
8183
8284 cmInstance . current . getWrapperElement ( ) . style [ 'font-size' ] = `${ fontSize } px` ;
8385
8486 return ( ) => {
85- cmInstance . current = null ; // Cleanup when the component unmounts
87+ cmInstance . current = null ;
8688 } ;
87- } , [ theme , dispatchConsoleEvent , fontSize , commandHistory ] ) ;
89+ } , [ ] ) ;
8890
8991 useEffect ( ( ) => {
90- if ( cmInstance . current ) {
91- cmInstance . current . setOption ( 'theme' , `p5-${ theme } ` ) ;
92- cmInstance . current . getWrapperElement ( ) . style [
93- 'font-size'
94- ] = `${ fontSize } px` ;
95- cmInstance . current . refresh ( ) ;
96- }
92+ cmInstance . current . setOption ( 'theme' , `p5-${ theme } ` ) ;
93+ cmInstance . current . getWrapperElement ( ) . style [ 'font-size' ] = `${ fontSize } px` ;
94+ cmInstance . current . refresh ( ) ;
9795 } , [ theme , fontSize ] ) ;
9896
9997 return (
@@ -115,7 +113,7 @@ const ConsoleInput = ({ theme, dispatchConsoleEvent, fontSize }) => {
115113 < div ref = { codemirrorContainer } className = "console__editor" />
116114 </ div >
117115 ) ;
118- } ;
116+ }
119117
120118ConsoleInput . propTypes = {
121119 theme : PropTypes . string . isRequired ,
0 commit comments