@@ -6,11 +6,19 @@ function styleSelectedText(textarea, prepend, append) {
6
6
$start = textarea . prop ( 'selectionStart' ) ;
7
7
$end = textarea . prop ( 'selectionEnd' ) ;
8
8
$content = textarea . val ( ) ;
9
- $content = $content . slice ( 0 , $start ) + prepend + $content . slice ( $start , $end ) + append + $content . slice ( $end , $content . length ) ;
10
- textarea . val ( $content ) ;
11
- textarea . focus ( ) ;
12
- textarea . prop ( 'selectionStart' , $start + prepend . length ) ;
13
- textarea . prop ( 'selectionEnd' , $end + append . length ) ;
9
+ if ( $content . slice ( 0 , $start ) . endsWith ( prepend ) && $content . slice ( $end , $content . length ) . replace ( "\n" , '' ) . startsWith ( append . replace ( "\n" , '' ) ) ) {
10
+ $content = $content . slice ( 0 , $start - prepend . length ) + $content . slice ( $start , $end ) + $content . slice ( $end + append . length , $content . length ) ;
11
+ textarea . val ( $content ) ;
12
+ textarea . focus ( ) ;
13
+ textarea . prop ( 'selectionStart' , $start - prepend . length ) ;
14
+ textarea . prop ( 'selectionEnd' , $end - append . length ) ;
15
+ } else {
16
+ $content = $content . slice ( 0 , $start ) + prepend + $content . slice ( $start , $end ) + append + $content . slice ( $end , $content . length ) ;
17
+ textarea . val ( $content ) ;
18
+ textarea . focus ( ) ;
19
+ textarea . prop ( 'selectionStart' , $start + prepend . length ) ;
20
+ textarea . prop ( 'selectionEnd' , $end + append . length ) ;
21
+ }
14
22
}
15
23
16
24
$ ( document ) . keydown ( function ( e ) {
0 commit comments