Skip to content

Commit 82563f5

Browse files
syntax highlighting in demo app code editor
1 parent 44e0253 commit 82563f5

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

demo/src/Editor.tsx

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import React, { useRef, useState, useEffect } from 'react';
1+
import { useRef, useState, useEffect, KeyboardEvent } from 'react';
22
import Form from 'react-bootstrap/Form';
3-
import Dropdown from 'react-bootstrap/Dropdown';
43
import {Light as SyntaxHighlighter} from 'react-syntax-highlighter';
54
import { atomOneLight } from 'react-syntax-highlighter/dist/esm/styles/hljs';
65

76
const TAB = " ";
87

9-
SyntaxHighlighter.registerLanguage('foo', (hljs) => ({
8+
SyntaxHighlighter.registerLanguage('foo', (hljs: any) => ({
109
case_insensitive: true, // language is case-insensitive
1110
keywords: {
1211
keyword: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'],
@@ -42,11 +41,14 @@ type EditorProps = {
4241
};
4342

4443
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);
4746
const [width, setWidth] = useState(0);
4847

4948
const updateWidth = () => {
49+
if (!editorRef.current || !editorRef.current.parentElement) {
50+
return;
51+
}
5052
if (width != editorRef.current.parentElement.clientWidth - 1) {
5153
setWidth(editorRef.current.parentElement.clientWidth - 1);
5254
}
@@ -64,16 +66,22 @@ export default function Editor({ className, id, value, onChange }: EditorProps)
6466
let height = Math.min(15, Math.max(6, value.split('\n').length)) + 1;
6567

6668
const syncScroll = () => {
69+
if (!editorRef.current || !highlightRef.current) {
70+
return;
71+
}
6772
highlightRef.current.style.height = editorRef.current.clientHeight + "px";
6873
highlightRef.current.scrollTop = editorRef.current.scrollTop;
6974
highlightRef.current.scrollLeft = editorRef.current.scrollLeft;
7075
};
7176

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);
7583
const atEol = (after === "" || after.startsWith("\n"));
76-
const cursor_pos = editorRef.current.selectionEnd;
84+
const cursor_pos = editorRef.current.selectionEnd || 0;
7785
if (ev.key == "{" && atEol) {
7886
ev.preventDefault();
7987
editorRef.current.value = before + "{}" + after;
@@ -159,7 +167,7 @@ export default function Editor({ className, id, value, onChange }: EditorProps)
159167
}
160168
};
161169

162-
const changedEvent = ev => {
170+
const changedEvent = (ev: any) => {
163171
if (onChange) {
164172
onChange(ev);
165173
}

0 commit comments

Comments
 (0)