2023-01-08 13:26:35 +08:00
|
|
|
import React, { useEffect, useRef } from 'react';
|
|
|
|
import { JSONEditor, Mode } from 'vanilla-jsoneditor';
|
|
|
|
import type { JSONEditorPropsOptional } from 'vanilla-jsoneditor';
|
2022-12-27 15:16:14 +08:00
|
|
|
|
2023-01-08 13:26:35 +08:00
|
|
|
const Editor: React.FC<JSONEditorPropsOptional> = (props) => {
|
|
|
|
const editorRef = useRef<JSONEditor>(null);
|
|
|
|
const container = useRef<HTMLDivElement>(null);
|
2022-12-27 15:16:14 +08:00
|
|
|
|
2023-01-08 13:26:35 +08:00
|
|
|
useEffect(() => {
|
|
|
|
editorRef.current = new JSONEditor({
|
|
|
|
target: container.current,
|
|
|
|
props: { mode: Mode.text },
|
2022-12-27 15:16:14 +08:00
|
|
|
});
|
|
|
|
return () => {
|
2023-01-08 15:36:50 +08:00
|
|
|
editorRef.current?.destroy();
|
2022-12-27 15:16:14 +08:00
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
2023-01-08 13:26:35 +08:00
|
|
|
useEffect(() => {
|
2023-01-08 15:36:50 +08:00
|
|
|
editorRef.current?.updateProps(props);
|
2022-12-27 15:16:14 +08:00
|
|
|
}, [props]);
|
|
|
|
|
2023-01-08 13:26:35 +08:00
|
|
|
return <div ref={container} className="vanilla-jsoneditor-react" />;
|
2022-12-27 15:16:14 +08:00
|
|
|
};
|
|
|
|
|
2023-01-08 13:26:35 +08:00
|
|
|
export default Editor;
|