-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCodeEditor.tsx
More file actions
105 lines (93 loc) · 2.84 KB
/
CodeEditor.tsx
File metadata and controls
105 lines (93 loc) · 2.84 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import React, { useEffect, useRef } from 'react'
import MonacoEditor, { useMonaco } from '@monaco-editor/react'
import { MarkerSeverity } from './Editor/types'
// @ts-ignore
import sdkTypeDefs from '!raw-loader!generated/cryptostats-sdk.d.ts'
// @ts-ignore
import graphTypeDefs from '!raw-loader!generated/graph-ts.d.ts'
interface EditorProps {
onValidated: (code: string, markers: any[]) => void
onChange?: (code: string) => void
defaultValue: string
fileId: string
onMount?: (editor: any, monaco: any) => void
isSubgraph: boolean
}
const Editor: React.FC<EditorProps> = ({
onValidated,
onChange,
defaultValue,
fileId,
onMount,
isSubgraph,
}) => {
const code = useRef(defaultValue)
const monaco = useMonaco()
useEffect(() => {
if (monaco) {
// validation settings
monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
noSemanticValidation: false,
noSyntaxValidation: false,
})
monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
noSemanticValidation: false,
noSyntaxValidation: false,
})
// compiler options
monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
target: monaco.languages.typescript.ScriptTarget.ES2019,
allowNonTsExtensions: true,
lib: ['es2018'],
})
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
target: monaco.languages.typescript.ScriptTarget.ES2019,
allowNonTsExtensions: true,
lib: ['es2018'],
})
var sdkUri = 'ts:filename/sdk.d.ts'
monaco.languages.typescript.javascriptDefaults.addExtraLib(
isSubgraph ? graphTypeDefs : sdkTypeDefs,
sdkUri
)
// When resolving definitions and references, the editor will try to use created models.
// Creating a model for the library allows "peek definition/references" commands to work with the library.
monaco.editor.createModel(
isSubgraph ? graphTypeDefs : sdkTypeDefs,
'typescript',
monaco.Uri.parse(sdkUri)
)
return () => monaco.editor.getModels().forEach((model: any) => model.dispose())
}
}, [monaco])
useEffect(() => {
code.current = defaultValue
}, [fileId])
return (
<MonacoEditor
theme="vs-dark"
defaultLanguage="typescript"
defaultValue={defaultValue}
path={fileId}
options={{
tabSize: 2,
insertSpaces: true,
}}
onMount={(editor: any) => {
if (onMount) {
onMount(editor, monaco)
}
}}
onChange={(newCode?: string) => {
code.current = newCode || ''
if (onChange && newCode) {
onChange(newCode)
}
}}
onValidate={(markers: MarkerSeverity[]) => {
onValidated(code.current, markers)
}}
/>
)
}
export default Editor