-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathExampleCodeMirrorEditor.tsx
More file actions
52 lines (45 loc) · 1.23 KB
/
ExampleCodeMirrorEditor.tsx
File metadata and controls
52 lines (45 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import type { EditorDocument, EditorUpdate, ScrollPosition } from '@tutorialkit/react/core';
import CodeMirrorEditor from '@tutorialkit/react/core/CodeMirrorEditor';
import { useState } from 'react';
import { useTheme } from './hooks/useTheme';
export default function ExampleCodeMirrorEditor() {
const { editorDocument, theme, onChange, onScroll } = useEditorDocument();
return (
<CodeMirrorEditor
theme={theme}
doc={editorDocument}
onChange={onChange}
onScroll={onScroll}
debounceChange={500}
debounceScroll={500}
className="h-full text-sm"
/>
);
}
function useEditorDocument() {
const theme = useTheme();
const [editorDocument, setEditorDocument] = useState<EditorDocument>(DEFAULT_DOCUMENT);
function onChange({ content }: EditorUpdate) {
setEditorDocument((prev) => ({
...prev,
value: content,
}));
}
function onScroll(scroll: ScrollPosition) {
setEditorDocument((prev) => ({
...prev,
scroll,
}));
}
return {
theme,
editorDocument,
onChange,
onScroll,
};
}
const DEFAULT_DOCUMENT: EditorDocument = {
filePath: 'index.js',
loading: false,
value: 'function hello() {\n console.log("Hello, world!");\n}\n\nhello();',
};