1
- import React , { forwardRef , useImperativeHandle , useMemo , useRef } from "react" ;
1
+ import React , { useImperativeHandle , useMemo , useRef } from "react" ;
2
2
import CodeMirror from "@uiw/react-codemirror" ;
3
3
import { history } from "@codemirror/history" ;
4
4
import { bracketMatching } from "@codemirror/matchbrackets" ;
@@ -10,65 +10,63 @@ import {
10
10
} from "../utils/codemirror-theme" ;
11
11
import "../scss/editor.scss" ;
12
12
13
- const CodeEditor = forwardRef (
14
- ( { codeValue, onUpdate, eslintOptions, eslintInstance } , ref ) => {
15
- const editorRef = useRef ( null ) ;
13
+ export default function CodeEditor ( {
14
+ codeValue,
15
+ onUpdate,
16
+ eslintOptions,
17
+ eslintInstance,
18
+ ref,
19
+ } ) {
20
+ const editorRef = useRef ( null ) ;
16
21
17
- const extensions = useMemo (
18
- ( ) => [
19
- history ( ) ,
20
- bracketMatching ( ) ,
21
- linter ( esLint ( eslintInstance , eslintOptions ) , { delay : 0 } ) ,
22
- javascript ( { typescript : true } ) ,
23
- ESLintPlaygroundTheme ,
24
- ESLintPlaygroundHighlightStyle ,
25
- ] ,
26
- [ eslintOptions , eslintInstance ] ,
27
- ) ;
22
+ const extensions = useMemo (
23
+ ( ) => [
24
+ history ( ) ,
25
+ bracketMatching ( ) ,
26
+ linter ( esLint ( eslintInstance , eslintOptions ) , { delay : 0 } ) ,
27
+ javascript ( { typescript : true } ) ,
28
+ ESLintPlaygroundTheme ,
29
+ ESLintPlaygroundHighlightStyle ,
30
+ ] ,
31
+ [ eslintOptions , eslintInstance ] ,
32
+ ) ;
28
33
29
- useImperativeHandle ( ref , ( ) => ( {
30
- scrollToPosition ( line , col ) {
31
- const editorView = editorRef . current . view ;
32
- const { state } = editorView ;
33
- const pos = state . doc . line ( line ) . from + col ;
34
+ useImperativeHandle ( ref , ( ) => ( {
35
+ scrollToPosition ( line , col ) {
36
+ const editorView = editorRef . current . view ;
37
+ const { state } = editorView ;
38
+ const pos = state . doc . line ( line ) . from + col ;
34
39
35
- // Set the cursor selection to the position
36
- editorView . dispatch ( {
37
- selection : { anchor : pos } ,
38
- scrollIntoView : true ,
39
- } ) ;
40
+ // Set the cursor selection to the position
41
+ editorView . dispatch ( {
42
+ selection : { anchor : pos } ,
43
+ scrollIntoView : true ,
44
+ } ) ;
40
45
41
- const linePos = editorView . coordsAtPos (
42
- state . doc . line ( line ) . from ,
43
- ) ;
46
+ const linePos = editorView . coordsAtPos ( state . doc . line ( line ) . from ) ;
44
47
45
- // Calculate to try to center the line in the editor
46
- if ( linePos ) {
47
- const editorRect = editorView . dom . getBoundingClientRect ( ) ;
48
- const offset =
49
- linePos . top - editorRect . top - editorRect . height / 2 ;
50
- editorView . scrollDOM . scrollTop += offset ;
51
- }
48
+ // Calculate to try to center the line in the editor
49
+ if ( linePos ) {
50
+ const editorRect = editorView . dom . getBoundingClientRect ( ) ;
51
+ const offset =
52
+ linePos . top - editorRect . top - editorRect . height / 2 ;
53
+ editorView . scrollDOM . scrollTop += offset ;
54
+ }
52
55
53
- editorView . focus ( ) ;
54
- } ,
55
- } ) ) ;
56
+ editorView . focus ( ) ;
57
+ } ,
58
+ } ) ) ;
56
59
57
- return (
58
- < CodeMirror
59
- value = { codeValue }
60
- minWidth = "100%"
61
- height = "100%"
62
- ref = { editorRef }
63
- extensions = { extensions }
64
- onChange = { value => {
65
- onUpdate ( value ) ;
66
- } }
67
- />
68
- ) ;
69
- } ,
70
- ) ;
71
-
72
- CodeEditor . displayName = "CodeEditor" ;
73
-
74
- export default CodeEditor ;
60
+ return (
61
+ < CodeMirror
62
+ value = { codeValue }
63
+ minWidth = "100%"
64
+ height = "100%"
65
+ ref = { editorRef }
66
+ extensions = { extensions }
67
+ onChange = { value => {
68
+ onUpdate ( value ) ;
69
+ } }
70
+ />
71
+ ) ;
72
+ }
0 commit comments