@@ -586,6 +586,52 @@ define([
586586 return text ;
587587 } ;
588588
589+
590+ /**
591+ * @return {Array } - the text between cursors and within selections (multicursor/sorted)
592+ * @method get_split_text
593+ **/
594+ Cell . prototype . get_split_text = function ( ) {
595+ var ranges = this . code_mirror . listSelections ( ) ;
596+
597+ var cursors = [ { line : 0 , ch : 0 } ] ;
598+
599+ for ( var i = 0 ; i < ranges . length ; i ++ ) {
600+ // append both to handle selections
601+ if ( ranges [ i ] . head . sticky == 'before' ) {
602+ cursors . push ( ranges [ i ] . anchor ) ;
603+ cursors . push ( ranges [ i ] . head ) ;
604+ } else {
605+ cursors . push ( ranges [ i ] . head ) ;
606+ cursors . push ( ranges [ i ] . anchor ) ;
607+ }
608+ }
609+
610+ var last_line_num = this . code_mirror . lineCount ( ) - 1 ;
611+ var last_line_len = this . code_mirror . getLine ( last_line_num ) . length ;
612+ var end = { line :last_line_num , ch :last_line_len } ;
613+ cursors . push ( end ) ;
614+
615+ // Cursors is now sorted, but likely has duplicates due to anchor and head being the same for cursors
616+ var locations = [ cursors [ 0 ] ] ;
617+ for ( var i = 1 ; i < cursors . length ; i ++ ) {
618+ var last = locations [ locations . length - 1 ] ;
619+ var current = cursors [ i ] ;
620+ if ( ( last . line != current . line ) || ( last . ch != current . ch ) ) {
621+ locations . push ( cursors [ i ] ) ;
622+ }
623+ }
624+
625+ // Split text
626+ var text_list = [ ] ;
627+ for ( var i = 1 ; i < locations . length ; i ++ ) {
628+ var text = this . code_mirror . getRange ( locations [ i - 1 ] , locations [ i ] ) ;
629+ text = text . replace ( / ^ \n + / , '' ) . replace ( / \n + $ / , '' ) ; // removes newlines at beginning and end
630+ text_list . push ( text ) ;
631+ }
632+ return text_list ;
633+ } ;
634+
589635 /**
590636 * Show/Hide CodeMirror LineNumber
591637 * @method show_line_numbers
0 commit comments