@@ -57,7 +57,7 @@ var codeInput = {
5757 // Update code
5858 result_element . innerHTML = this . escape_html ( text ) ;
5959 this . plugin_evt ( "beforeHighlight" ) ;
60-
60+
6161 // Syntax Highlight
6262 if ( this . template . includeCodeInputInHighlightFunc ) this . template . highlight ( result_element , this ) ;
6363 else this . template . highlight ( result_element ) ;
@@ -74,127 +74,6 @@ var codeInput = {
7474 result_element . scrollLeft = input_element . scrollLeft ;
7575 }
7676
77- check_tab ( event ) {
78- if ( event . key != "Tab" || ! this . template . isCode ) {
79- return ;
80- }
81- let input_element = this . querySelector ( "textarea" ) ;
82- let code = input_element . value ;
83- event . preventDefault ( ) ; // stop normal
84-
85- if ( ! event . shiftKey && input_element . selectionStart == input_element . selectionEnd ) {
86- // Shift always means dedent - this places a tab here.
87- let before_selection = code . slice ( 0 , input_element . selectionStart ) ; // text before tab
88- let after_selection = code . slice ( input_element . selectionEnd , input_element . value . length ) ; // text after tab
89-
90- let cursor_pos = input_element . selectionEnd + 1 ; // where cursor moves after tab - moving forward by 1 char to after tab
91- input_element . value = before_selection + "\t" + after_selection ; // add tab char
92-
93- // move cursor
94- input_element . selectionStart = cursor_pos ;
95- input_element . selectionEnd = cursor_pos ;
96-
97- } else {
98- let lines = input_element . value . split ( "\n" ) ;
99- let letter_i = 0 ;
100-
101- let selection_start = input_element . selectionStart ; // where cursor moves after tab - moving forward by 1 indent
102- let selection_end = input_element . selectionEnd ; // where cursor moves after tab - moving forward by 1 indent
103-
104- let number_indents = 0 ;
105- let first_line_indents = 0 ;
106-
107- for ( let i = 0 ; i < lines . length ; i ++ ) {
108- letter_i += lines [ i ] . length + 1 ; // newline counted
109-
110- console . log ( lines [ i ] , ": start" , input_element . selectionStart , letter_i , "&& end" , input_element . selectionEnd , letter_i - lines [ i ] . length )
111- if ( input_element . selectionStart <= letter_i && input_element . selectionEnd >= letter_i - lines [ i ] . length ) {
112- // Starts before or at last char and ends after or at first char
113- if ( event . shiftKey ) {
114- if ( lines [ i ] [ 0 ] == "\t" ) {
115- // Remove first tab
116- lines [ i ] = lines [ i ] . slice ( 1 ) ;
117- if ( number_indents == 0 ) first_line_indents -- ;
118- number_indents -- ;
119- }
120- } else {
121- lines [ i ] = "\t" + lines [ i ] ;
122- if ( number_indents == 0 ) first_line_indents ++ ;
123- number_indents ++ ;
124- }
125-
126- }
127- }
128- input_element . value = lines . join ( "\n" ) ;
129-
130- // move cursor
131- input_element . selectionStart = selection_start + first_line_indents ;
132- input_element . selectionEnd = selection_end + number_indents ;
133- }
134-
135- this . update ( input_element . value ) ;
136- }
137-
138- check_enter ( event ) {
139- if ( event . key != "Enter" || ! this . template . isCode ) {
140- return ;
141- }
142- event . preventDefault ( ) ; // stop normal
143-
144- let input_element = this . querySelector ( "textarea" ) ;
145- let lines = input_element . value . split ( "\n" ) ;
146- let letter_i = 0 ;
147- let current_line = lines . length - 1 ;
148- let new_line = "" ;
149- let number_indents = 0 ;
150-
151- // find the index of the line our cursor is currently on
152- for ( let i = 0 ; i < lines . length ; i ++ ) {
153- letter_i += lines [ i ] . length + 1 ;
154- if ( input_element . selectionEnd <= letter_i ) {
155- current_line = i ;
156- break ;
157- }
158- }
159-
160- // count the number of indents the current line starts with (up to our cursor position in the line)
161- let cursor_pos_in_line = lines [ current_line ] . length - ( letter_i - input_element . selectionEnd ) + 1 ;
162- for ( let i = 0 ; i < cursor_pos_in_line ; i ++ ) {
163- if ( lines [ current_line ] [ i ] == "\t" ) {
164- number_indents ++ ;
165- } else {
166- break ;
167- }
168- }
169-
170- // determine the text before and after the cursor and chop the current line at the new line break
171- let text_after_cursor = "" ;
172- if ( cursor_pos_in_line != lines [ current_line ] . length ) {
173- text_after_cursor = lines [ current_line ] . substring ( cursor_pos_in_line ) ;
174- lines [ current_line ] = lines [ current_line ] . substring ( 0 , cursor_pos_in_line ) ;
175- }
176-
177- // insert our indents and any text from the previous line that might have been after the line break
178- for ( let i = 0 ; i < number_indents ; i ++ ) {
179- new_line += "\t" ;
180- }
181- new_line += text_after_cursor ;
182-
183- // save the current cursor position
184- let selection_start = input_element . selectionStart ;
185- let selection_end = input_element . selectionEnd ;
186-
187- // splice our new line into the list of existing lines and join them all back up
188- lines . splice ( current_line + 1 , 0 , new_line ) ;
189- input_element . value = lines . join ( "\n" ) ;
190-
191- // move cursor to new position
192- input_element . selectionStart = selection_start + number_indents + 1 ; // count the indent level and the newline character
193- input_element . selectionEnd = selection_end + number_indents + 1 ;
194-
195- this . update ( input_element . value ) ;
196- }
197-
19877 escape_html ( text ) {
19978 return text . replace ( new RegExp ( "&" , "g" ) , "&" ) . replace ( new RegExp ( "<" , "g" ) , "<" ) ; /* Global RegExp */
20079 }
@@ -227,7 +106,6 @@ var codeInput = {
227106
228107 textarea . setAttribute ( "oninput" , "this.parentElement.update(this.value); this.parentElement.sync_scroll();" ) ;
229108 textarea . setAttribute ( "onscroll" , "this.parentElement.sync_scroll();" ) ;
230- textarea . setAttribute ( "onkeydown" , "this.parentElement.check_tab(event); this.parentElement.check_enter(event);" ) ;
231109 this . append ( textarea ) ;
232110
233111 /* Create pre code */
0 commit comments