@@ -3,7 +3,7 @@ import { useSelector, useDispatch } from 'react-redux';
33
44import * as actions from './actions' ;
55import AdvancedEditor from './AdvancedEditor' ;
6- import { CommonEditorProps , Editor as EditorType , Position } from './types' ;
6+ import { CommonEditorProps , Editor as EditorType , Position , Selection } from './types' ;
77import { State } from './reducers' ;
88
99class CodeByteOffsets {
@@ -24,6 +24,17 @@ class CodeByteOffsets {
2424 return [ precedingBytes , precedingBytes + highlightedBytes ] ;
2525 }
2626
27+ public rangeToOffsets ( start : Position , end : Position ) {
28+ const startBytes = this . positionToBytes ( start ) ;
29+ const endBytes = this . positionToBytes ( end ) ;
30+ return [ startBytes , endBytes ] ;
31+ }
32+
33+ private positionToBytes ( position : Position ) {
34+ // Subtract one as this logic is zero-based and the columns are one-based
35+ return this . bytesBeforeLine ( position . line ) + position . column - 1 ;
36+ }
37+
2738 private bytesBeforeLine ( line : number ) {
2839 // Subtract one as this logic is zero-based and the lines are one-based
2940 line -= 1 ;
@@ -64,6 +75,7 @@ class SimpleEditor extends React.PureComponent<CommonEditorProps> {
6475
6576 public componentDidUpdate ( prevProps , _prevState ) {
6677 this . gotoPosition ( prevProps . position , this . props . position ) ;
78+ this . setSelection ( prevProps . selection , this . props . selection ) ;
6779 }
6880
6981 private gotoPosition ( oldPosition : Position , newPosition : Position ) {
@@ -78,12 +90,26 @@ class SimpleEditor extends React.PureComponent<CommonEditorProps> {
7890 editor . focus ( ) ;
7991 editor . setSelectionRange ( startBytes , endBytes ) ;
8092 }
93+
94+ private setSelection ( oldSelection : Selection , newSelection : Selection ) {
95+ const editor = this . _editor ;
96+
97+ if ( ! newSelection || ! editor ) { return ; }
98+ if ( newSelection === oldSelection ) { return ; }
99+
100+ const offsets = new CodeByteOffsets ( this . props . code ) ;
101+ const [ startBytes , endBytes ] = offsets . rangeToOffsets ( newSelection . start , newSelection . end ) ;
102+
103+ editor . focus ( ) ;
104+ editor . setSelectionRange ( startBytes , endBytes ) ;
105+ }
81106}
82107
83108const Editor : React . SFC = ( ) => {
84109 const code = useSelector ( ( state : State ) => state . code ) ;
85110 const editor = useSelector ( ( state : State ) => state . configuration . editor ) ;
86111 const position = useSelector ( ( state : State ) => state . position ) ;
112+ const selection = useSelector ( ( state : State ) => state . selection ) ;
87113 const crates = useSelector ( ( state : State ) => state . crates ) ;
88114
89115 const dispatch = useDispatch ( ) ;
@@ -96,6 +122,7 @@ const Editor: React.SFC = () => {
96122 < div className = "editor" >
97123 < SelectedEditor code = { code }
98124 position = { position }
125+ selection = { selection }
99126 crates = { crates }
100127 onEditCode = { onEditCode }
101128 execute = { execute } />
0 commit comments