Skip to content

Commit 3887b22

Browse files
committed
Update codeit.js
1 parent f275fed commit 3887b22

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

lib/codeit.js

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -582,49 +582,57 @@ class CodeitElement extends HTMLElement {
582582
event.preventDefault();
583583

584584
if (event.shiftKey) {
585-
586-
// delete a tab from selection
587-
588-
// get caret pos in text
589-
const pos = cd.getSelection();
590-
591-
let startPos = Math.min(pos.start, pos.end);
592-
let endPos = Math.max(pos.start, pos.end);
593585

594-
// get text before cursor
595-
596-
cd.setSelection(endPos);
597-
598-
const before = cd.beforeCursor();
599-
600-
// get padding of line
601-
let [linePadding, lineStartPos] = getPadding(before);
586+
// get current selection
587+
const s = window.getSelection();
602588

603-
// if line has tabs
604-
if (linePadding.length > 0) {
605-
606-
// delete a tab
589+
// if selection is collapsed
590+
if (s.isCollapsed) {
607591

608-
const tabLength = cd.options.tab.length;
609-
610-
cd.setSelection(lineStartPos, lineStartPos + tabLength);
611-
612-
cd.deleteCurrentSelection();
592+
// delete a tab from selection
593+
594+
// get caret pos in text
595+
const pos = cd.getSelection();
596+
597+
let startPos = Math.min(pos.start, pos.end);
598+
let endPos = Math.max(pos.start, pos.end);
613599

614-
// restore pos in text
615-
cd.setSelection(lineStartPos + linePadding.length - tabLength, endPos - tabLength);
616-
617-
const selectedTab = window.getSelection().toString().startsWith(cd.options.tab);
600+
// get text before cursor
601+
602+
cd.setSelection(endPos);
618603

619-
if (selectedTab) {
604+
const before = cd.beforeCursor();
605+
606+
// get padding of line
607+
let [linePadding, lineStartPos] = getPadding(before);
608+
609+
// if line has tabs
610+
if (linePadding.length > 0) {
611+
612+
// delete a tab
613+
614+
const tabLength = cd.options.tab.length;
615+
616+
cd.setSelection(lineStartPos, lineStartPos + tabLength);
617+
618+
cd.deleteCurrentSelection();
620619

621620
// restore pos in text
622-
cd.setSelection(lineStartPos + linePadding.length, endPos - tabLength);
621+
cd.setSelection(lineStartPos + linePadding.length - tabLength, endPos - tabLength);
622+
623+
const selectedTab = window.getSelection().toString().startsWith(cd.options.tab);
624+
625+
if (selectedTab) {
626+
627+
// restore pos in text
628+
cd.setSelection(lineStartPos + linePadding.length, endPos - tabLength);
629+
630+
}
623631

624632
}
625633

626634
}
627-
635+
628636
} else {
629637

630638
// get current selection

0 commit comments

Comments
 (0)