@@ -22,7 +22,9 @@ var dis = require("../../dispatcher");
2222var KeyCode = {
2323 ENTER : 13 ,
2424 TAB : 9 ,
25- SHIFT : 16
25+ SHIFT : 16 ,
26+ UP : 38 ,
27+ DOWN : 40
2628} ;
2729
2830module . exports = {
@@ -33,10 +35,105 @@ module.exports = {
3335 original : null ,
3436 index : 0
3537 } ;
38+ this . sentHistory = {
39+ // The list of typed messages. Index 0 is more recent
40+ data : [ ] ,
41+ // The position in data currently displayed
42+ position : - 1 ,
43+ // The room the history is for.
44+ roomId : null ,
45+ // The original text before they hit UP
46+ originalText : null ,
47+ // The textarea element to set text to.
48+ element : null ,
49+
50+ init : function ( element , roomId ) {
51+ this . roomId = roomId ;
52+ this . element = element ;
53+ this . position = - 1 ;
54+ var storedData = window . sessionStorage . getItem (
55+ "history_" + roomId
56+ ) ;
57+ if ( storedData ) {
58+ this . data = JSON . parse ( storedData ) ;
59+ }
60+ if ( this . roomId ) {
61+ this . setLastTextEntry ( ) ;
62+ }
63+ } ,
64+
65+ push : function ( text ) {
66+ // store a message in the sent history
67+ this . data . unshift ( text ) ;
68+ window . sessionStorage . setItem (
69+ "history_" + this . roomId ,
70+ JSON . stringify ( this . data )
71+ ) ;
72+ // reset history position
73+ this . position = - 1 ;
74+ this . originalText = null ;
75+ } ,
76+
77+ // move in the history. Returns true if we managed to move.
78+ next : function ( offset ) {
79+ if ( this . position === - 1 ) {
80+ // user is going into the history, save the current line.
81+ this . originalText = this . element . value ;
82+ }
83+ else {
84+ // user may have modified this line in the history; remember it.
85+ this . data [ this . position ] = this . element . value ;
86+ }
87+
88+ if ( offset > 0 && this . position === ( this . data . length - 1 ) ) {
89+ // we've run out of history
90+ return false ;
91+ }
92+
93+ // retrieve the next item (bounded).
94+ var newPosition = this . position + offset ;
95+ newPosition = Math . max ( - 1 , newPosition ) ;
96+ newPosition = Math . min ( newPosition , this . data . length - 1 ) ;
97+ this . position = newPosition ;
98+
99+ if ( this . position !== - 1 ) {
100+ // show the message
101+ this . element . value = this . data [ this . position ] ;
102+ }
103+ else if ( this . originalText ) {
104+ // restore the original text the user was typing.
105+ this . element . value = this . originalText ;
106+ }
107+ return true ;
108+ } ,
109+
110+ saveLastTextEntry : function ( ) {
111+ // save the currently entered text in order to restore it later.
112+ // NB: This isn't 'originalText' because we want to restore
113+ // sent history items too!
114+ var text = this . element . value ;
115+ window . sessionStorage . setItem ( "input_" + this . roomId , text ) ;
116+ } ,
117+
118+ setLastTextEntry : function ( ) {
119+ var text = window . sessionStorage . getItem ( "input_" + this . roomId ) ;
120+ if ( text ) {
121+ this . element . value = text ;
122+ }
123+ }
124+ } ;
125+ } ,
126+
127+ componentDidMount : function ( ) {
128+ this . sentHistory . init (
129+ this . refs . textarea . getDOMNode ( ) ,
130+ this . props . room . roomId
131+ ) ;
36132 } ,
37133
38134 componentWillUnmount : function ( ) {
39135 dis . unregister ( this . dispatcherRef ) ;
136+ this . sentHistory . saveLastTextEntry ( ) ;
40137 } ,
41138
42139 onAction : function ( payload ) {
@@ -49,6 +146,7 @@ module.exports = {
49146
50147 onKeyDown : function ( ev ) {
51148 if ( ev . keyCode === KeyCode . ENTER ) {
149+ this . sentHistory . push ( this . refs . textarea . getDOMNode ( ) . value ) ;
52150 this . onEnter ( ev ) ;
53151 }
54152 else if ( ev . keyCode === KeyCode . TAB ) {
@@ -58,6 +156,12 @@ module.exports = {
58156 }
59157 this . onTab ( ev , members ) ;
60158 }
159+ else if ( ev . keyCode === KeyCode . UP || ev . keyCode === KeyCode . DOWN ) {
160+ this . sentHistory . next (
161+ ev . keyCode === KeyCode . UP ? 1 : - 1
162+ ) ;
163+ ev . preventDefault ( ) ;
164+ }
61165 else if ( ev . keyCode !== KeyCode . SHIFT && this . tabStruct . completing ) {
62166 // they're resuming typing; reset tab complete state vars.
63167 this . tabStruct . completing = false ;
0 commit comments