11import classnames from 'classnames' ;
22import CodeMirror from 'codemirror' ;
3+ import get from 'lodash-es/get' ;
34import isNil from 'lodash-es/isNil' ;
45import LRU from 'lru-cache' ;
56import map from 'lodash-es/map' ;
@@ -14,6 +15,7 @@ import 'codemirror/addon/lint/lint';
1415import 'codemirror/addon/selection/active-line' ;
1516
1617import bowser from '../services/bowser' ;
18+ import { EditorLocation } from '../records' ;
1719
1820const CODEMIRROR_MODES_MAP = {
1921 html : 'htmlmixed' ,
@@ -25,10 +27,12 @@ export default function CodeMirrorEditor({
2527 errors,
2628 language,
2729 projectKey,
30+ requestedFocusedLine,
2831 source,
2932 textSizeIsLarge,
3033 onAutoFormat,
3134 onInput,
35+ onRequestedLineFocused,
3236} ) {
3337 const containerRef = useRef ( ) ;
3438 const editorRef = useRef ( ) ;
@@ -115,6 +119,23 @@ export default function CodeMirrorEditor({
115119 editor . setOption ( 'extraKeys' , { [ keyBinding ] : onAutoFormat } ) ;
116120 } , [ onAutoFormat ] ) ;
117121
122+ useEffect ( ( ) => {
123+ if ( get ( requestedFocusedLine , 'component' ) !==
124+ `editor.${ language } ` ) {
125+ return ;
126+ }
127+
128+ const editor = editorRef . current ;
129+ const position = {
130+ line : requestedFocusedLine . line ,
131+ ch : requestedFocusedLine . column ,
132+ } ;
133+ editor . getDoc ( ) . setCursor ( position ) ;
134+ editor . scrollIntoView ( position ) ;
135+ editor . focus ( ) ;
136+ onRequestedLineFocused ( ) ;
137+ } , [ language , onRequestedLineFocused , requestedFocusedLine ] ) ;
138+
118139 return (
119140 < div
120141 className = { classnames (
@@ -130,8 +151,14 @@ CodeMirrorEditor.propTypes = {
130151 errors : PropTypes . array . isRequired ,
131152 language : PropTypes . string . isRequired ,
132153 projectKey : PropTypes . string . isRequired ,
154+ requestedFocusedLine : PropTypes . instanceOf ( EditorLocation ) ,
133155 source : PropTypes . string . isRequired ,
134156 textSizeIsLarge : PropTypes . bool . isRequired ,
135157 onAutoFormat : PropTypes . func . isRequired ,
136158 onInput : PropTypes . func . isRequired ,
159+ onRequestedLineFocused : PropTypes . func . isRequired ,
160+ } ;
161+
162+ CodeMirrorEditor . defaultProps = {
163+ requestedFocusedLine : null ,
137164} ;
0 commit comments