code-editor: make Backspace remove full indents at the start of a line
This commit is contained in:
parent
10d384f3d7
commit
dd955b0649
|
@ -26,6 +26,8 @@ export class CodeEditor extends HTMLElement {
|
||||||
this.textArea.rows = 1;
|
this.textArea.rows = 1;
|
||||||
|
|
||||||
this.keyShortcuts = {
|
this.keyShortcuts = {
|
||||||
|
Backspace: () => this.backspace(),
|
||||||
|
|
||||||
Enter: () => this.enter(),
|
Enter: () => this.enter(),
|
||||||
|
|
||||||
Tab: () => this.tab(),
|
Tab: () => this.tab(),
|
||||||
|
@ -251,6 +253,28 @@ export class CodeEditor extends HTMLElement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
backspace() {
|
||||||
|
this.pushHistory({ allowMerge: true });
|
||||||
|
|
||||||
|
let selection = this.getSelection();
|
||||||
|
selection.collapse();
|
||||||
|
|
||||||
|
let start = getLineStart(this.code, selection.start);
|
||||||
|
let leading = this.code.substring(start, selection.cursor);
|
||||||
|
let isAtIndent = /^ +$/.test(leading);
|
||||||
|
let positionInLine = selection.cursor - start;
|
||||||
|
let charactersToRemove = isAtIndent
|
||||||
|
? this.indentWidth - (positionInLine % this.indentWidth)
|
||||||
|
: 1;
|
||||||
|
|
||||||
|
selection.cursor -= charactersToRemove;
|
||||||
|
selection.clampCursor(this.code);
|
||||||
|
this.replace(selection, "");
|
||||||
|
selection.collapse();
|
||||||
|
|
||||||
|
this.setSelection(selection);
|
||||||
|
}
|
||||||
|
|
||||||
enter() {
|
enter() {
|
||||||
this.pushHistory({ allowMerge: false });
|
this.pushHistory({ allowMerge: false });
|
||||||
|
|
||||||
|
@ -335,16 +359,20 @@ export class Selection {
|
||||||
this.cursor = Math.max(0, Math.min(this.cursor, text.length));
|
this.cursor = Math.max(0, Math.min(this.cursor, text.length));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
collapse() {
|
||||||
|
this.anchor = this.cursor;
|
||||||
|
}
|
||||||
|
|
||||||
set(text, n) {
|
set(text, n) {
|
||||||
this.cursor = n;
|
this.cursor = n;
|
||||||
this.clampCursor(text);
|
this.clampCursor(text);
|
||||||
this.anchor = this.cursor;
|
this.collapse();
|
||||||
}
|
}
|
||||||
|
|
||||||
advance(text, n) {
|
advance(text, n) {
|
||||||
this.cursor += n;
|
this.cursor += n;
|
||||||
this.clampCursor(text);
|
this.clampCursor(text);
|
||||||
this.anchor = this.cursor;
|
this.collapse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue