Commit aea634bf authored by GuiΩ's avatar GuiΩ
Browse files

feat(CodeEditor): upgrade tabulations features

(shift +) Tab: (remove or) add tabulation
Delete: before the first non empty char remove a tabulation
        before the new line char remove the char and the following indentation
parent 3ed34ec0
......@@ -25,49 +25,71 @@ export default {
this.$emit('input', event.target.value)
},
handleKeyDown(event) {
let key = event.key;
let shiftKey = event.shiftKey;
let textarea = event.target;
let start = textarea.selectionStart;
let end = textarea.selectionEnd;
let lineStart = textarea.value.substring(0,start).lastIndexOf("\n")
switch (event.key) {
let lineStart = textarea.value.substring(0,start).lastIndexOf("\n");
let lineIndentSize = textarea.value.substr(lineStart + 1).search(/[^ ]/);
// let lineIndentSize = 0
// while (textarea.value.charAt(lineStart + lineIndentSize + 1) === ' ')
// lineIndentSize++;
if (key === 'Backspace' && start > lineStart+1 && start <= lineStart + lineIndentSize + 1) {
key = 'Tab';
shiftKey = true;
}
switch (key) {
case 'Delete':
if (start === end && textarea.value.charAt(start ) === "\n") {
textarea.selectionEnd = start + textarea.value.substr(start + 1).search(/[^ ]/);
}
break;
case 'Enter':
event.preventDefault();
let spaces = "\n" + " ".repeat(lineIndentSize);
let nextCharIndex = end + textarea.value.substr(end).search(/[^ ]/)
if (start <= lineStart + lineIndentSize) { // cursor before the first non empty char
end = lineStart + lineIndentSize + end - start + 1;
start = lineStart + lineIndentSize + 1;
} else {
end = nextCharIndex;
}
textarea.value = textarea.value.substring(0, start) + spaces + textarea.value.substring(end);
textarea.selectionStart = start + lineIndentSize + 1;
textarea.selectionEnd = textarea.selectionStart;
this.$emit('input', textarea.value);
break;
case 'Tab':
event.preventDefault();
// event.stopPropagation()
if (event.shiftKey) { // Shift + Tab
for (let i=0; i < this.tabsize; i++) {
if (textarea.value.charAt(lineStart + 1) === ' ') {
textarea.value = textarea.value.substr(0, lineStart + 1) + textarea.value.substr(lineStart + 2);
if (start > lineStart + 1)
start -= 1;
if (end > lineStart + 1)
end -= 1;
}
let nbspaces = this.tabsize;
if (start <= lineStart + lineIndentSize + 1 || shiftKey) {
nbspaces = lineIndentSize % this.tabsize;
if (nbspaces === 0)
nbspaces = this.tabsize;
}
if (start <= lineStart + lineIndentSize) {
end = lineStart + lineIndentSize + end - start + 1;
start = lineStart + lineIndentSize + 1;
}
if (shiftKey) { // Shift + Tab
if (lineIndentSize > 0) {
textarea.value = textarea.value.substr(0, lineStart + 1)
+ textarea.value.substr(lineStart + nbspaces + 1 );
start -= nbspaces;
end -= nbspaces;
}
textarea.selectionStart = start;
textarea.selectionEnd = end;
} else { // Tab
let newVal = textarea.value.substring(0, start);
for (let i = 0; i < this.tabsize; i++)
newVal += ' ';
newVal += textarea.value.substring(end);
textarea.value = newVal;
textarea.selectionStart = start + this.tabsize;
textarea.value = textarea.value.substring(0, start)
+ " ".repeat(nbspaces)
+ textarea.value.substring(end);
textarea.selectionStart = start + nbspaces;
textarea.selectionEnd = textarea.selectionStart;
}
this.$emit('input', textarea.value);
break;
case 'Enter':
event.preventDefault();
let spaces = "\n";
let length = 1;
while (textarea.value.charAt(lineStart + length) === ' ') {
length += 1;
spaces += ' ';
}
textarea.value = textarea.value.substring(0, start) + spaces + textarea.value.substring(end);
textarea.selectionStart = start + length;
textarea.selectionEnd = textarea.selectionStart;
this.$emit('input', textarea.value);
}
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment