@@ -19,10 +19,20 @@ limitations under the License.
1919var MatrixClientPeg = require ( "../../MatrixClientPeg" ) ;
2020
2121var dis = require ( "../../dispatcher" ) ;
22+ var KeyCode = {
23+ ENTER : 13 ,
24+ TAB : 9 ,
25+ SHIFT : 16
26+ } ;
2227
2328module . exports = {
24- componentDidMount : function ( ) {
29+ componentWillMount : function ( ) {
2530 this . dispatcherRef = dis . register ( this . onAction ) ;
31+ this . tabStruct = {
32+ completing : false ,
33+ original : null ,
34+ index : 0
35+ } ;
2636 } ,
2737
2838 componentWillUnmount : function ( ) {
@@ -38,30 +48,143 @@ module.exports = {
3848 } ,
3949
4050 onKeyDown : function ( ev ) {
41- if ( ev . keyCode == 13 ) {
42- var contentText = this . refs . textarea . getDOMNode ( ) . value ;
43-
44- var content = null ;
45- if ( / ^ \/ m e / i. test ( contentText ) ) {
46- content = {
47- msgtype : 'm.emote' ,
48- body : contentText . substring ( 4 )
49- } ;
50- } else {
51- content = {
52- msgtype : 'm.text' ,
53- body : contentText
54- } ;
51+ if ( ev . keyCode === KeyCode . ENTER ) {
52+ this . onEnter ( ev ) ;
53+ }
54+ else if ( ev . keyCode === KeyCode . TAB ) {
55+ var members = [ ] ;
56+ if ( this . props . room ) {
57+ members = this . props . room . getJoinedMembers ( ) ;
5558 }
59+ this . onTab ( ev , members ) ;
60+ }
61+ else if ( ev . keyCode !== KeyCode . SHIFT && this . tabStruct . completing ) {
62+ // they're resuming typing; reset tab complete state vars.
63+ this . tabStruct . completing = false ;
64+ this . tabStruct . index = 0 ;
65+ }
66+ } ,
5667
57- MatrixClientPeg . get ( ) . sendMessage ( this . props . room . roomId , content ) . then ( function ( ) {
58- dis . dispatch ( {
59- action : 'message_sent'
60- } ) ;
61- } ) ;
62- this . refs . textarea . getDOMNode ( ) . value = '' ;
63- ev . preventDefault ( ) ;
68+ onEnter : function ( ev ) {
69+ var contentText = this . refs . textarea . getDOMNode ( ) . value ;
70+ var content = null ;
71+ if ( / ^ \/ m e / i. test ( contentText ) ) {
72+ content = {
73+ msgtype : 'm.emote' ,
74+ body : contentText . substring ( 4 )
75+ } ;
76+ } else {
77+ content = {
78+ msgtype : 'm.text' ,
79+ body : contentText
80+ } ;
6481 }
82+
83+ MatrixClientPeg . get ( ) . sendMessage ( this . props . room . roomId , content ) . then ( function ( ) {
84+ dis . dispatch ( {
85+ action : 'message_sent'
86+ } ) ;
87+ } ) ;
88+ this . refs . textarea . getDOMNode ( ) . value = '' ;
89+ ev . preventDefault ( ) ;
6590 } ,
91+
92+ onTab : function ( ev , sortedMembers ) {
93+ var textArea = this . refs . textarea . getDOMNode ( ) ;
94+ if ( ! this . tabStruct . completing ) {
95+ this . tabStruct . completing = true ;
96+ this . tabStruct . index = 0 ;
97+ // cache starting text
98+ this . tabStruct . original = textArea . value ;
99+ }
100+
101+ // loop in the right direction
102+ if ( ev . shiftKey ) {
103+ this . tabStruct . index -- ;
104+ if ( this . tabStruct . index < 0 ) {
105+ // wrap to the last search match, and fix up to a real index
106+ // value after we've matched.
107+ this . tabStruct . index = Number . MAX_VALUE ;
108+ }
109+ }
110+ else {
111+ this . tabStruct . index ++ ;
112+ }
113+
114+ var searchIndex = 0 ;
115+ var targetIndex = this . tabStruct . index ;
116+ var text = this . tabStruct . original ;
117+
118+ var search = / @ ? ( [ a - z A - Z 0 - 9 _ \- : \. ] + ) $ / . exec ( text ) ;
119+ // console.log("Searched in '%s' - got %s", text, search);
120+ if ( targetIndex === 0 ) { // 0 is always the original text
121+ textArea . value = text ;
122+ }
123+ else if ( search && search [ 1 ] ) {
124+ // console.log("search found: " + search+" from "+text);
125+ var expansion ;
126+
127+ // FIXME: could do better than linear search here
128+ for ( var i = 0 ; i < sortedMembers . length ; i ++ ) {
129+ var member = sortedMembers [ i ] ;
130+ if ( member . name && searchIndex < targetIndex ) {
131+ if ( member . name . toLowerCase ( ) . indexOf ( search [ 1 ] . toLowerCase ( ) ) === 0 ) {
132+ expansion = member . name ;
133+ searchIndex ++ ;
134+ }
135+ }
136+ }
137+
138+ if ( searchIndex < targetIndex ) { // then search raw mxids
139+ for ( var i = 0 ; i < sortedMembers . length ; i ++ ) {
140+ if ( searchIndex >= targetIndex ) {
141+ break ;
142+ }
143+ var userId = sortedMembers [ i ] . userId ;
144+ // === 1 because mxids are @username
145+ if ( userId . toLowerCase ( ) . indexOf ( search [ 1 ] . toLowerCase ( ) ) === 1 ) {
146+ expansion = userId ;
147+ searchIndex ++ ;
148+ }
149+ }
150+ }
151+
152+ if ( searchIndex === targetIndex ||
153+ targetIndex === Number . MAX_VALUE ) {
154+ // xchat-style tab complete, add a colon if tab
155+ // completing at the start of the text
156+ if ( search [ 0 ] . length === text . length ) {
157+ expansion += ": " ;
158+ }
159+ else {
160+ expansion += " " ;
161+ }
162+ textArea . value = text . replace (
163+ / @ ? ( [ a - z A - Z 0 - 9 _ \- : \. ] + ) $ / , expansion
164+ ) ;
165+ // cancel blink
166+ textArea . style [ "background-color" ] = "" ;
167+ if ( targetIndex === Number . MAX_VALUE ) {
168+ // wrap the index around to the last index found
169+ this . tabStruct . index = searchIndex ;
170+ targetIndex = searchIndex ;
171+ }
172+ }
173+ else {
174+ // console.log("wrapped!");
175+ textArea . style [ "background-color" ] = "#faa" ;
176+ setTimeout ( function ( ) {
177+ textArea . style [ "background-color" ] = "" ;
178+ } , 150 ) ;
179+ textArea . value = text ;
180+ this . tabStruct . index = 0 ;
181+ }
182+ }
183+ else {
184+ this . tabStruct . index = 0 ;
185+ }
186+ // prevent the default TAB operation (typically focus shifting)
187+ ev . preventDefault ( ) ;
188+ }
66189} ;
67190
0 commit comments