Skip to content

Commit 53df0b3

Browse files
author
WebCoder49
committed
Replace Enter and Tab actions with execCommand + test
1 parent d95cb9b commit 53df0b3

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

plugins/indent.js

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,8 @@
2424
event.preventDefault(); // stop normal
2525

2626
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");
3729

3830
} else {
3931
let lines = input_element.value.split("\n");
@@ -45,32 +37,46 @@
4537
let number_indents = 0;
4638
let first_line_indents = 0;
4739

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
5343
// Starts before or at last char and ends after or at first char
5444
if(event.shiftKey) {
5545
if(lines[i][0] == "\t") {
5646
// 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--;
6057
}
6158
} 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+
}
6771
}
72+
73+
letter_i += lines[i].length+1; // newline counted
6874
}
69-
input_element.value = lines.join("\n");
75+
// input_element.value = lines.join("\n");
7076

7177
// 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;
7480
}
7581

7682
codeInput.update(input_element.value);
@@ -119,15 +125,12 @@
119125
for (let i = 0; i < number_indents; i++) {
120126
new_line += "\t";
121127
}
122-
new_line += text_after_cursor;
123128

124129
// save the current cursor position
125130
let selection_start = input_element.selectionStart;
126131
let selection_end = input_element.selectionEnd;
127132

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
131134

132135
// move cursor to new position
133136
input_element.selectionStart = selection_start + number_indents + 1; // count the indent level and the newline character

0 commit comments

Comments
 (0)