1
1
import PropTypes from 'prop-types' ;
2
- import React , { useState , useEffect , useRef } from 'react' ;
2
+ import React , { useRef , useEffect , useState } from 'react' ;
3
3
import CodeMirror from 'codemirror' ;
4
4
import { Encode } from 'console-feed' ;
5
+
5
6
import RightArrowIcon from '../../../images/right-arrow.svg' ;
6
7
import { dispatchMessage , MessageTypes } from '../../../utils/dispatcher' ;
7
8
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 } ) {
9
13
const [ commandHistory , setCommandHistory ] = useState ( [ ] ) ;
10
14
const [ commandCursor , setCommandCursor ] = useState ( - 1 ) ;
11
15
const codemirrorContainer = useRef ( null ) ;
@@ -25,10 +29,9 @@ const ConsoleInput = ({ theme, dispatchConsoleEvent, fontSize }) => {
25
29
e . preventDefault ( ) ;
26
30
e . stopPropagation ( ) ;
27
31
const value = cm . getValue ( ) ;
28
- if ( value . trim ( ) === '' ) {
32
+ if ( value . trim ( ' ' ) === '' ) {
29
33
return false ;
30
34
}
31
-
32
35
const messages = [
33
36
{ log : Encode ( { method : 'command' , data : [ value ] } ) }
34
37
] ;
@@ -42,58 +45,53 @@ const ConsoleInput = ({ theme, dispatchConsoleEvent, fontSize }) => {
42
45
} ) ;
43
46
dispatchConsoleEvent ( consoleEvent ) ;
44
47
cm . setValue ( '' ) ;
45
- setCommandHistory ( ( prevHistory ) => [ value , ...prevHistory ] ) ;
48
+ setCommandHistory ( [ value , ...commandHistory ] ) ;
46
49
setCommandCursor ( - 1 ) ;
47
50
} else if ( e . key === 'ArrowUp' ) {
48
51
const lineNumber = cmInstance . current . getDoc ( ) . getCursor ( ) . line ;
49
52
if ( lineNumber !== 0 ) {
50
53
return false ;
51
54
}
52
55
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 ) ;
60
64
} else if ( e . key === 'ArrowDown' ) {
61
65
const lineNumber = cmInstance . current . getDoc ( ) . getCursor ( ) . line ;
62
66
const lineCount = cmInstance . current . getValue ( ) . split ( '\n' ) . length ;
63
67
if ( lineNumber + 1 !== lineCount ) {
64
68
return false ;
65
69
}
66
70
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 ) ;
78
80
}
79
81
return true ;
80
82
} ) ;
81
83
82
84
cmInstance . current . getWrapperElement ( ) . style [ 'font-size' ] = `${ fontSize } px` ;
83
85
84
86
return ( ) => {
85
- cmInstance . current = null ; // Cleanup when the component unmounts
87
+ cmInstance . current = null ;
86
88
} ;
87
- } , [ theme , dispatchConsoleEvent , fontSize , commandHistory ] ) ;
89
+ } , [ ] ) ;
88
90
89
91
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 ( ) ;
97
95
} , [ theme , fontSize ] ) ;
98
96
99
97
return (
@@ -115,7 +113,7 @@ const ConsoleInput = ({ theme, dispatchConsoleEvent, fontSize }) => {
115
113
< div ref = { codemirrorContainer } className = "console__editor" />
116
114
</ div >
117
115
) ;
118
- } ;
116
+ }
119
117
120
118
ConsoleInput . propTypes = {
121
119
theme : PropTypes . string . isRequired ,
0 commit comments