|
24 | 24 | event.preventDefault(); // stop normal |
25 | 25 |
|
26 | 26 | if(!event.shiftKey && input_element.selectionStart == input_element.selectionEnd) { |
27 | | - // Shift always means dedent - this places a tab here. |
28 | | - let before_selection = code.slice(0, input_element.selectionStart); // text before tab |
29 | | - let after_selection = code.slice(input_element.selectionEnd, input_element.value.length); // text after tab |
30 | | - |
31 | | - let cursor_pos = input_element.selectionEnd + 1; // where cursor moves after tab - moving forward by 1 char to after tab |
32 | | - input_element.value = before_selection + "\t" + after_selection; // add tab char |
33 | | - |
34 | | - // move cursor |
35 | | - input_element.selectionStart = cursor_pos; |
36 | | - input_element.selectionEnd = cursor_pos; |
| 27 | + // Just place a tab here. |
| 28 | + document.execCommand("insertText", false, "\t"); |
37 | 29 |
|
38 | 30 | } else { |
39 | 31 | let lines = input_element.value.split("\n"); |
|
45 | 37 | let number_indents = 0; |
46 | 38 | let first_line_indents = 0; |
47 | 39 |
|
48 | | - for (let i = 0; i < lines.length; i++) { |
49 | | - letter_i += lines[i].length+1; // newline counted |
50 | | - |
51 | | - console.log(lines[i], ": start", input_element.selectionStart, letter_i, "&& end", input_element.selectionEnd , letter_i - lines[i].length) |
52 | | - if(input_element.selectionStart <= letter_i && input_element.selectionEnd >= letter_i - lines[i].length) { |
| 40 | + for (let i = 0; i < lines.length; i++) { |
| 41 | + console.log(lines[i], ": start", selection_start, letter_i + lines[i].length + 1, "&& end", selection_end , letter_i + 1) |
| 42 | + if(selection_start <= letter_i+lines[i].length && selection_end >= letter_i + 1) { // + 1 so newlines counted |
53 | 43 | // Starts before or at last char and ends after or at first char |
54 | 44 | if(event.shiftKey) { |
55 | 45 | if(lines[i][0] == "\t") { |
56 | 46 | // Remove first tab |
57 | | - lines[i] = lines[i].slice(1); |
58 | | - if(number_indents == 0) first_line_indents--; |
59 | | - number_indents--; |
| 47 | + input_element.selectionStart = letter_i; |
| 48 | + input_element.selectionEnd = letter_i+1; |
| 49 | + document.execCommand("delete", false, ""); |
| 50 | + |
| 51 | + // Change selection |
| 52 | + if(selection_start > letter_i) { // Indented outside selection |
| 53 | + selection_start--; |
| 54 | + } |
| 55 | + selection_end--; |
| 56 | + letter_i--; |
60 | 57 | } |
61 | 58 | } else { |
62 | | - lines[i] = "\t" + lines[i]; |
63 | | - if(number_indents == 0) first_line_indents++; |
64 | | - number_indents++; |
65 | | - } |
66 | | - |
| 59 | + // Add tab at start |
| 60 | + input_element.selectionStart = letter_i; |
| 61 | + input_element.selectionEnd = letter_i; |
| 62 | + document.execCommand("insertText", false, "\t"); |
| 63 | + |
| 64 | + // Change selection |
| 65 | + if(selection_start > letter_i) { // Indented outside selection |
| 66 | + selection_start++; |
| 67 | + } |
| 68 | + selection_end++; |
| 69 | + letter_i++; |
| 70 | + } |
67 | 71 | } |
| 72 | + |
| 73 | + letter_i += lines[i].length+1; // newline counted |
68 | 74 | } |
69 | | - input_element.value = lines.join("\n"); |
| 75 | + // input_element.value = lines.join("\n"); |
70 | 76 |
|
71 | 77 | // move cursor |
72 | | - input_element.selectionStart = selection_start + first_line_indents; |
73 | | - input_element.selectionEnd = selection_end + number_indents; |
| 78 | + input_element.selectionStart = selection_start; |
| 79 | + input_element.selectionEnd = selection_end; |
74 | 80 | } |
75 | 81 |
|
76 | 82 | codeInput.update(input_element.value); |
|
119 | 125 | for (let i = 0; i < number_indents; i++) { |
120 | 126 | new_line += "\t"; |
121 | 127 | } |
122 | | - new_line += text_after_cursor; |
123 | 128 |
|
124 | 129 | // save the current cursor position |
125 | 130 | let selection_start = input_element.selectionStart; |
126 | 131 | let selection_end = input_element.selectionEnd; |
127 | 132 |
|
128 | | - // splice our new line into the list of existing lines and join them all back up |
129 | | - lines.splice(current_line + 1, 0, new_line); |
130 | | - input_element.value = lines.join("\n"); |
| 133 | + document.execCommand("insertText", false, "\n" + new_line); // Write new line, including auto-indentation |
131 | 134 |
|
132 | 135 | // move cursor to new position |
133 | 136 | input_element.selectionStart = selection_start + number_indents + 1; // count the indent level and the newline character |
|
0 commit comments