11/*
22
33 codeit.js
4- v3.1.1
4+ v3.1.2
55 MIT License
66
77 https://codeit.codes
@@ -582,33 +582,72 @@ class CodeitElement extends HTMLElement {
582582 event . preventDefault ( ) ;
583583
584584 if ( event . shiftKey ) {
585-
586- const before = cd . beforeCursor ( ) ;
587-
588- // get padding of line
589- let [ padding , start ] = getPadding ( before ) ;
590-
591- // get caret pos in text
592- let pos = cd . getSelection ( ) ;
593-
594- if ( padding . length > 0 ) {
595-
596- const tabLength = cd . options . tab . length ;
597-
598- // remove full length tab
599-
600- cd . setSelection ( start + tabLength ) ;
601-
602- for ( let i = 0 ; i < tabLength ; i ++ ) cd . deleteCurrentSelection ( ) ;
603-
604- pos . start -= tabLength ;
605- pos . end -= tabLength ;
606-
585+
586+ // get current selection
587+ const s = window . getSelection ( ) ;
588+
589+ let selContents = s . toString ( ) ;
590+
591+ // if selection exists
592+ if ( ! s . isCollapsed ) {
593+
594+ let lines = selContents . split ( '\n' ) ;
595+
596+ // run on all lines
597+ lines . forEach ( ( line , index ) => {
598+
599+ // if line contains a tab
600+ if ( line . startsWith ( cd . options . tab ) ) {
601+
602+ // remove tab from line
603+ lines [ index ] = line . slice ( cd . options . tab . length ) ;
604+
605+ }
606+
607+ } ) ;
608+
609+ // join lines
610+ selContents = lines . join ( '\n' ) ;
611+
612+
613+ // delete selection
614+ cd . deleteCurrentSelection ( ) ;
615+
616+ // insert un-tabbed selection
617+ cd . insert ( selContents , { moveToEnd : false } ) ;
618+
619+ // get caret pos in text
620+ const pos = cd . getSelection ( ) ;
621+
607622 // restore pos in text
608- cd . setSelection ( pos . start , pos . end ) ;
609-
623+ cd . setSelection ( pos . start , ( pos . start + selContents . length ) ) ;
624+
625+ } else {
626+
627+ let lastLine = cd . beforeCursor ( ) . split ( '\n' ) ;
628+ lastLine = lastLine [ lastLine . length - 1 ] ;
629+
630+ // if current line contains a tab
631+ if ( lastLine . startsWith ( cd . options . tab ) ) {
632+
633+ // remove tab from line
634+
635+ // get caret pos in text
636+ const pos = cd . getSelection ( ) ;
637+
638+ // select the tab
639+ cd . setSelection ( ( pos . start - lastLine . length ) , ( pos . start - lastLine . length + cd . options . tab . length ) ) ;
640+
641+ // delete selection
642+ cd . deleteCurrentSelection ( ) ;
643+
644+ // restore pos in text
645+ cd . setSelection ( pos . start - cd . options . tab . length ) ;
646+
647+ }
648+
610649 }
611-
650+
612651 } else {
613652
614653 // get current selection
@@ -621,29 +660,38 @@ class CodeitElement extends HTMLElement {
621660
622661 if ( selContents . includes ( '\n' ) ) {
623662
624- // tab lines in selection
663+ // add tabs to selection string
625664 selContents = cd . options . tab + selContents . split ( '\n' ) . join ( '\n' + cd . options . tab ) ;
626665
627- // insert tabbed selection
666+ // delete selection
628667 cd . deleteCurrentSelection ( ) ;
629- cd . insert ( selContents ) ;
668+
669+ // insert tabbed selection
670+ cd . insert ( selContents , { moveToEnd : false } ) ;
671+
672+ // get caret pos in text
673+ const pos = cd . getSelection ( ) ;
674+
675+ // restore pos in text
676+ cd . setSelection ( pos . start , ( pos . start + selContents . length ) ) ;
630677
631678 } else {
632679
633680 // tab selection
634- const sel = cd . getSelection ( ) ;
635681
636- cd . setSelection ( Math . min ( sel . start , sel . end ) ) ;
682+ // get caret pos in text
683+ const pos = cd . getSelection ( ) ;
637684
638- // insert tab at start of selection
639- cd . insert ( cd . options . tab ) ;
685+ const start = Math . min ( pos . start , pos . end ) ;
686+ const end = Math . max ( pos . start , pos . end ) ;
640687
641- // reselect text
688+ cd . setSelection ( start ) ;
642689
643- sel . start += cd . options . tab . length ;
644- sel . end += cd . options . tab . length ;
690+ // insert tab at start of selection
691+ cd . insert ( cd . options . tab , { moveToEnd : false } ) ;
645692
646- cd . setSelection ( sel . start , sel . end ) ;
693+ // restore pos in text
694+ cd . setSelection ( start , end + cd . options . tab . length ) ;
647695
648696 }
649697
0 commit comments