make Backspace delete the current selection correctly (#108)

This commit is contained in:
りき萌 2025-05-26 18:54:14 +02:00
parent 7727e62655
commit c4f6f80166

View file

@ -257,21 +257,25 @@ export class CodeEditor extends HTMLElement {
this.pushHistory({ allowMerge: true });
let selection = this.getSelection();
if (selection.start != selection.end) {
this.replace(selection, "");
} else {
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();
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);
}