-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example.tsx
75 lines (69 loc) · 2.24 KB
/
Example.tsx
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
/*
* @Author: legends-killer
* @Date: 2023-12-27 20:10:41
* @LastEditors: legends-killer
* @LastEditTime: 2024-01-17 22:50:48
* @Description: Example App
*/
import { useEffect, useRef, useState } from 'react'
import { JsonEditor } from './JsonEditor'
import { languages as Languages } from 'monaco-editor'
import { ISuggestionItem } from './JsonEditor/helper/jsonSchemaProcessor'
export const Example = () => {
const [jsonSchema, setJsonSchema] = useState()
const [loading, setLoading] = useState(true)
const [valueSuggestions, setValueSuggestions] = useState<ISuggestionItem[]>([])
const ref = useRef<any>()
// to fix the issue that monaco editor will add repeated suggestions
// maintain the suggestions that have been added in a outter component by user
const addedKeySuggestions = useRef<ISuggestionItem[]>([])
const addedValueSuggestions = useRef<ISuggestionItem[]>([])
const init = async () => {
const jsonSchema = await (
await fetch('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/schemas/v3.0/schema.json')
).json()
const myValueSuggestions: ISuggestionItem[] = []
Object.entries(jsonSchema.definitions).forEach((entity) => {
const [key, _] = entity
myValueSuggestions.push({
title: `@${key}`,
suggestion: `"@${key}"`,
kind: Languages.CompletionItemKind.Reference,
})
})
setValueSuggestions(myValueSuggestions)
addedValueSuggestions.current = addedValueSuggestions.current.concat(myValueSuggestions)
setJsonSchema(jsonSchema)
setLoading(false)
}
useEffect(() => {
init()
}, [])
const getEditorValue = () => {
return ref.current?.getEditorValue()
}
return (
<div>
{!loading && (
<JsonEditor
addedKeySuggestions={addedKeySuggestions}
addedValueSuggestions={addedValueSuggestions}
ref={ref}
keySuggesions={[]}
valueSuggestions={valueSuggestions}
height={'90vh'}
promptJsonSchema={jsonSchema}
userDefinedItemCompleteProviders={[]}
triggerCharacters={['{']}
/>
)}
<button
onClick={() => {
console.log(getEditorValue())
}}
>
getValue
</button>
</div>
)
}