1
- import React , { useRef , useState , useEffect } from 'react' ;
1
+ import { useRef , useState , useEffect , KeyboardEvent } from 'react' ;
2
2
import Form from 'react-bootstrap/Form' ;
3
- import Dropdown from 'react-bootstrap/Dropdown' ;
4
3
import { Light as SyntaxHighlighter } from 'react-syntax-highlighter' ;
5
4
import { atomOneLight } from 'react-syntax-highlighter/dist/esm/styles/hljs' ;
6
5
7
6
const TAB = " " ;
8
7
9
- SyntaxHighlighter . registerLanguage ( 'foo' , ( hljs ) => ( {
8
+ SyntaxHighlighter . registerLanguage ( 'foo' , ( hljs : any ) => ( {
10
9
case_insensitive : true , // language is case-insensitive
11
10
keywords : {
12
11
keyword : [ 'GET' , 'POST' , 'PUT' , 'PATCH' , 'DELETE' ] ,
@@ -42,11 +41,14 @@ type EditorProps = {
42
41
} ;
43
42
44
43
export default function Editor ( { className, id, value, onChange } : EditorProps ) {
45
- const editorRef = useRef ( null ) ;
46
- const highlightRef = useRef ( null ) ;
44
+ const editorRef = useRef < HTMLTextAreaElement | null > ( null ) ;
45
+ const highlightRef = useRef < HTMLDivElement | null > ( null ) ;
47
46
const [ width , setWidth ] = useState ( 0 ) ;
48
47
49
48
const updateWidth = ( ) => {
49
+ if ( ! editorRef . current || ! editorRef . current . parentElement ) {
50
+ return ;
51
+ }
50
52
if ( width != editorRef . current . parentElement . clientWidth - 1 ) {
51
53
setWidth ( editorRef . current . parentElement . clientWidth - 1 ) ;
52
54
}
@@ -64,16 +66,22 @@ export default function Editor({ className, id, value, onChange }: EditorProps)
64
66
let height = Math . min ( 15 , Math . max ( 6 , value . split ( '\n' ) . length ) ) + 1 ;
65
67
66
68
const syncScroll = ( ) => {
69
+ if ( ! editorRef . current || ! highlightRef . current ) {
70
+ return ;
71
+ }
67
72
highlightRef . current . style . height = editorRef . current . clientHeight + "px" ;
68
73
highlightRef . current . scrollTop = editorRef . current . scrollTop ;
69
74
highlightRef . current . scrollLeft = editorRef . current . scrollLeft ;
70
75
} ;
71
76
72
- const handleKeyDown = ev => {
73
- const before = editorRef . current . value . slice ( 0 , editorRef . current . selectionStart ) ;
74
- const after = editorRef . current . value . slice ( editorRef . current . selectionEnd , editorRef . current . value . length ) ;
77
+ const handleKeyDown = ( ev : KeyboardEvent ) => {
78
+ if ( ! editorRef . current ) {
79
+ return ;
80
+ }
81
+ const before = editorRef . current . value . slice ( 0 , editorRef . current . selectionStart || undefined ) ;
82
+ const after = editorRef . current . value . slice ( editorRef . current . selectionEnd || undefined , editorRef . current . value . length ) ;
75
83
const atEol = ( after === "" || after . startsWith ( "\n" ) ) ;
76
- const cursor_pos = editorRef . current . selectionEnd ;
84
+ const cursor_pos = editorRef . current . selectionEnd || 0 ;
77
85
if ( ev . key == "{" && atEol ) {
78
86
ev . preventDefault ( ) ;
79
87
editorRef . current . value = before + "{}" + after ;
@@ -159,7 +167,7 @@ export default function Editor({ className, id, value, onChange }: EditorProps)
159
167
}
160
168
} ;
161
169
162
- const changedEvent = ev => {
170
+ const changedEvent = ( ev : any ) => {
163
171
if ( onChange ) {
164
172
onChange ( ev ) ;
165
173
}
0 commit comments