2023-01-08 13:26:35 +08:00
|
|
|
import React, { useEffect, useRef } from 'react';
|
2024-12-04 23:56:09 +08:00
|
|
|
import type { JsonEditor, JSONEditorPropsOptional } from 'vanilla-jsoneditor';
|
|
|
|
import { createJSONEditor, Mode } 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) => {
|
2024-12-04 23:56:09 +08:00
|
|
|
const editorRef = useRef<JsonEditor>();
|
2023-01-08 13:26:35 +08:00
|
|
|
const container = useRef<HTMLDivElement>(null);
|
2022-12-27 15:16:14 +08:00
|
|
|
|
2023-01-08 13:26:35 +08:00
|
|
|
useEffect(() => {
|
2024-12-04 23:56:09 +08:00
|
|
|
if (container.current) {
|
|
|
|
editorRef.current = createJSONEditor({
|
|
|
|
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);
|
2023-05-22 09:59:29 +08:00
|
|
|
}, [props.content]);
|
2022-12-27 15:16:14 +08:00
|
|
|
|
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;
|