11/*
22
33 codeit.js
4- v3.1.1
4+ v3.1.2
55 MIT License
66
77 https://codeit.codes
@@ -583,30 +583,46 @@ class CodeitElement extends HTMLElement {
583583
584584 if ( event . shiftKey ) {
585585
586- const before = cd . beforeCursor ( ) ;
587-
588- // get padding of line
589- let [ padding , start ] = getPadding ( before ) ;
586+ // delete a tab from selection
590587
591588 // get caret pos in text
592- let pos = cd . getSelection ( ) ;
593-
594- if ( padding . length > 0 ) {
595-
596- const tabLength = cd . options . tab . length ;
589+ const pos = cd . getSelection ( ) ;
597590
598- // remove full length tab
591+ let startPos = Math . min ( pos . start , pos . end ) ;
592+ let endPos = Math . max ( pos . start , pos . end ) ;
593+
594+ // get text before cursor
595+
596+ cd . setSelection ( endPos ) ;
597+
598+ const before = cd . beforeCursor ( ) ;
599599
600- cd . setSelection ( start + tabLength ) ;
600+ // get padding of line
601+ let [ linePadding , lineStartPos ] = getPadding ( before ) ;
602+
603+ // if line has tabs
604+ if ( linePadding . length > 0 ) {
601605
602- for ( let i = 0 ; i < tabLength ; i ++ ) cd . deleteCurrentSelection ( ) ;
606+ // delete a tab
607+
608+ const tabLength = cd . options . tab . length ;
603609
604- pos . start -= tabLength ;
605- pos . end -= tabLength ;
610+ cd . setSelection ( lineStartPos , lineStartPos + tabLength ) ;
606611
612+ cd . deleteCurrentSelection ( ) ;
613+
607614 // restore pos in text
608- cd . setSelection ( pos . start , pos . end ) ;
615+ cd . setSelection ( lineStartPos + linePadding . length - tabLength , endPos - tabLength ) ;
609616
617+ const selectedTab = window . getSelection ( ) . toString ( ) . startsWith ( cd . options . tab ) ;
618+
619+ if ( selectedTab ) {
620+
621+ // restore pos in text
622+ cd . setSelection ( lineStartPos + linePadding . length , endPos - tabLength ) ;
623+
624+ }
625+
610626 }
611627
612628 } else {
@@ -621,29 +637,38 @@ class CodeitElement extends HTMLElement {
621637
622638 if ( selContents . includes ( '\n' ) ) {
623639
624- // tab lines in selection
640+ // add tabs to selection string
625641 selContents = cd . options . tab + selContents . split ( '\n' ) . join ( '\n' + cd . options . tab ) ;
626642
627- // insert tabbed selection
643+ // delete selection
628644 cd . deleteCurrentSelection ( ) ;
629- cd . insert ( selContents ) ;
645+
646+ // insert tabbed selection
647+ cd . insert ( selContents , { moveToEnd : false } ) ;
648+
649+ // get caret pos in text
650+ const pos = cd . getSelection ( ) ;
651+
652+ // restore pos in text
653+ cd . setSelection ( pos . start , ( pos . start + selContents . length ) ) ;
630654
631655 } else {
632656
633657 // tab selection
634- const sel = cd . getSelection ( ) ;
635658
636- cd . setSelection ( Math . min ( sel . start , sel . end ) ) ;
659+ // get caret pos in text
660+ const pos = cd . getSelection ( ) ;
637661
638- // insert tab at start of selection
639- cd . insert ( cd . options . tab ) ;
662+ const start = Math . min ( pos . start , pos . end ) ;
663+ const end = Math . max ( pos . start , pos . end ) ;
640664
641- // reselect text
665+ cd . setSelection ( start ) ;
642666
643- sel . start += cd . options . tab . length ;
644- sel . end += cd . options . tab . length ;
667+ // insert tab at start of selection
668+ cd . insert ( cd . options . tab , { moveToEnd : false } ) ;
645669
646- cd . setSelection ( sel . start , sel . end ) ;
670+ // restore pos in text
671+ cd . setSelection ( start , end + cd . options . tab . length ) ;
647672
648673 }
649674
0 commit comments