Skip to content

Commit 095a8ff

Browse files
authored
Add smart list handling on text selection (#53)
1 parent 74a097e commit 095a8ff

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

index.html

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -616,29 +616,49 @@
616616
}
617617

618618
handleSmartTab(e) {
619-
const { value, selectionStart } = this.editor;
619+
const { value, selectionStart, selectionEnd } = this.editor;
620620
const lineStart = value.lastIndexOf('\n', selectionStart - 1) + 1;
621+
622+
// Multi-line selection: indent/dedent every line that is selected
623+
if (selectionStart !== selectionEnd) {
624+
e.preventDefault();
625+
const lineEnd = selectionEnd === value.length ? value.length : value.indexOf('\n', selectionEnd - 1);
626+
const block = value.substring(lineStart, lineEnd === -1 ? value.length : lineEnd);
627+
628+
const updated = block.split('\n').map(line => {
629+
if (e.shiftKey) return line.replace(/^\t/, '');
630+
return '\t' + line;
631+
}).join('\n');
632+
633+
this.editor.setSelectionRange(lineStart, lineEnd === -1 ? value.length : lineEnd);
634+
document.execCommand('insertText', false, updated);
635+
this.editor.setSelectionRange(lineStart, lineStart + updated.length);
636+
return;
637+
}
638+
621639
const lineEnd = value.indexOf('\n', selectionStart);
622640
const line = value.substring(lineStart, lineEnd === -1 ? value.length : lineEnd);
623-
624641
const isList = line.match(/^(\s*)([-*]|\d+\.)\s/);
625642

626643
e.preventDefault();
627-
if (isList && e.shiftKey) {
644+
// Plain text: insert tab or no-op on shift
645+
if (!isList) {
646+
if (!e.shiftKey) document.execCommand('insertText', false, '\t');
647+
return;
648+
}
649+
650+
// Indent/dedent list items
651+
if (e.shiftKey) {
628652
const outdented = line.replace(/^\t/, '');
629653
if (outdented === line) return;
630654
this.editor.setSelectionRange(lineStart, lineStart + line.length);
631655
document.execCommand('insertText', false, outdented);
632656
this.editor.setSelectionRange(selectionStart - 1, selectionStart - 1);
633-
} else if (isList && !e.shiftKey) {
657+
} else {
634658
this.editor.setSelectionRange(lineStart, lineStart);
635659
document.execCommand('insertText', false, '\t');
636660
this.editor.setSelectionRange(selectionStart + 1, selectionStart + 1);
637-
} else if (!e.shiftKey) {
638-
// Plain text: insert tab
639-
document.execCommand('insertText', false, '\t');
640661
}
641-
// Shift+Tab on plain text: no-op (but still preventDefault to avoid focus jump)
642662
}
643663

644664
about() {

0 commit comments

Comments
 (0)