11import React from 'react' ;
22import { IndexLink } from 'react-router' ;
3- import Console from 'react-console-component'
43import Socket from '../../socket.js' ;
54
65class ConsoleContent extends React . Component {
@@ -10,20 +9,37 @@ class ConsoleContent extends React.Component {
109 this . connectWebsocket = this . connectWebsocket . bind ( this ) ;
1110 this . handleCommand = this . handleCommand . bind ( this ) ;
1211 this . onConnect = this . onConnect . bind ( this ) ;
13- this . onNewLogLine = this . onNewLogLine . bind ( this ) ;
14- this . state = { }
12+ this . handleInput = this . handleInput . bind ( this ) ;
13+ this . clearInput = this . clearInput . bind ( this ) ;
14+ this . clearHistory = this . clearHistory . bind ( this ) ;
15+ this . addHistory = this . addHistory . bind ( this ) ;
16+ this . handleClick = this . handleClick . bind ( this ) ;
17+ this . newLogLine = this . newLogLine . bind ( this ) ;
18+ this . state = {
19+ commands : { } ,
20+ history : [ ] ,
21+ prompt : '$ ' ,
22+ }
1523 }
1624
1725 componentDidMount ( ) {
1826 this . connectWebsocket ( ) ;
1927 }
2028
29+ componentDidUpdate ( ) {
30+ var el = this . refs . output ;
31+ console . log ( this . refs ) ;
32+ var container = document . getElementById ( "console-output" ) ;
33+ console . log ( container )
34+ container . scrollTop = this . refs . output . scrollHeight ;
35+ }
36+
2137 connectWebsocket ( ) {
2238 var ws_scheme = window . location . protocol == "https:" ? "wss" : "ws" ;
2339 let ws = new WebSocket ( ws_scheme + "://" + window . location . host + "/ws" ) ;
2440 let socket = this . socket = new Socket ( ws ) ;
2541 socket . on ( 'connect' , this . onConnect . bind ( this ) ) ;
26- socket . on ( 'log update' , this . onNewLogLine . bind ( this ) ) ;
42+ socket . on ( 'log update' , this . newLogLine . bind ( this ) ) ;
2743 }
2844
2945 handleCommand ( command ) {
@@ -34,17 +50,52 @@ class ConsoleContent extends React.Component {
3450 onConnect ( ) {
3551 this . setState ( { connected : true } ) ;
3652 this . socket . emit ( "log subscribe" ) ;
37- this . refs . console . log ( "connected to Factorio Server" )
3853 }
3954
40- onNewLogLine ( logline ) {
41- console . log ( logline ) ;
42- console . log ( this . refs . console ) ;
43- this . refs . console . log ( { message : logline } ) ;
44- this . refs . console . return ( ) ;
55+ handleInput ( e ) {
56+ if ( e . key === "Enter" ) {
57+ var input_text = this . refs . term . value ;
58+
59+ this . addHistory ( this . state . prompt + " " + input_text ) ;
60+
61+ this . clearInput ( ) ;
62+ }
63+ }
64+
65+ clearInput ( ) {
66+ this . refs . term . value = "" ;
67+ }
68+
69+ clearHistory ( ) {
70+ ths . setState ( { history : [ ] } ) ;
71+ }
72+
73+ addHistory ( output ) {
74+ var history = this . state . history ;
75+ history . push ( output ) ;
76+ this . setState ( {
77+ 'history' : history
78+ } ) ;
79+ }
80+
81+ handleClick ( ) {
82+ var term = this . refs . term ;
83+ term . focus ( ) ;
84+ }
85+
86+ newLogLine ( logline ) {
87+ var history = this . state . history ;
88+ history . push ( logline ) ;
89+ this . setState ( {
90+ 'history' : history
91+ } ) ;
4592 }
4693
4794 render ( ) {
95+ var output = this . state . history . map ( ( op , i ) => {
96+ return < p key = { i } > { op } </ p >
97+ } ) ;
98+
4899 return (
49100 < div className = "content-wrapper" >
50101 < section className = "content-header" >
@@ -59,11 +110,14 @@ class ConsoleContent extends React.Component {
59110 </ section >
60111
61112 < section className = "content" >
62-
63- < Console ref = "console"
64- autofocus = { true }
65- handler = { this . handleCommand }
66- />
113+
114+ < div id = 'console-output' className = 'console-container' onClick = { this . handleClick } ref = "output" >
115+ { output }
116+ </ div >
117+ < p >
118+ < span className = "console-prompt-box" > { this . state . prompt } </ span >
119+ < input type = "text" onKeyPress = { this . handleInput } ref = "term" />
120+ </ p >
67121
68122 </ section >
69123 </ div >
0 commit comments