refactor: JsonEditor
This commit is contained in:
@@ -108,6 +108,21 @@ const DynamicJsonForm = ({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const formatJson = () => {
|
||||||
|
try {
|
||||||
|
const jsonStr = rawJsonValue.trim();
|
||||||
|
if (!jsonStr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const formatted = JSON.stringify(JSON.parse(jsonStr), null, 2);
|
||||||
|
setRawJsonValue(formatted);
|
||||||
|
debouncedUpdateParent(formatted);
|
||||||
|
setJsonError(undefined);
|
||||||
|
} catch (err) {
|
||||||
|
setJsonError(err instanceof Error ? err.message : "Invalid JSON");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const renderFormFields = (
|
const renderFormFields = (
|
||||||
propSchema: JsonSchemaType,
|
propSchema: JsonSchemaType,
|
||||||
currentValue: JsonValue,
|
currentValue: JsonValue,
|
||||||
@@ -353,7 +368,12 @@ const DynamicJsonForm = ({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
<div className="flex justify-end">
|
<div className="flex justify-end space-x-2">
|
||||||
|
{isJsonMode && (
|
||||||
|
<Button variant="outline" size="sm" onClick={formatJson}>
|
||||||
|
Format JSON
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
<Button variant="outline" size="sm" onClick={handleSwitchToFormMode}>
|
<Button variant="outline" size="sm" onClick={handleSwitchToFormMode}>
|
||||||
{isJsonMode ? "Switch to Form" : "Switch to JSON"}
|
{isJsonMode ? "Switch to Form" : "Switch to JSON"}
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import Editor from "react-simple-code-editor";
|
|||||||
import Prism from "prismjs";
|
import Prism from "prismjs";
|
||||||
import "prismjs/components/prism-json";
|
import "prismjs/components/prism-json";
|
||||||
import "prismjs/themes/prism.css";
|
import "prismjs/themes/prism.css";
|
||||||
import { Button } from "@/components/ui/button";
|
|
||||||
|
|
||||||
interface JsonEditorProps {
|
interface JsonEditorProps {
|
||||||
value: string;
|
value: string;
|
||||||
@@ -16,49 +15,25 @@ const JsonEditor = ({
|
|||||||
onChange,
|
onChange,
|
||||||
error: externalError,
|
error: externalError,
|
||||||
}: JsonEditorProps) => {
|
}: JsonEditorProps) => {
|
||||||
const [editorContent, setEditorContent] = useState(value);
|
const [editorContent, setEditorContent] = useState(value || "");
|
||||||
const [internalError, setInternalError] = useState<string | undefined>(
|
const [internalError, setInternalError] = useState<string | undefined>(
|
||||||
undefined,
|
undefined,
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setEditorContent(value);
|
setEditorContent(value || "");
|
||||||
}, [value]);
|
}, [value]);
|
||||||
|
|
||||||
const formatJson = (json: string): string => {
|
|
||||||
try {
|
|
||||||
return JSON.stringify(JSON.parse(json), null, 2);
|
|
||||||
} catch {
|
|
||||||
return json;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleEditorChange = (newContent: string) => {
|
const handleEditorChange = (newContent: string) => {
|
||||||
setEditorContent(newContent);
|
setEditorContent(newContent);
|
||||||
setInternalError(undefined);
|
setInternalError(undefined);
|
||||||
onChange(newContent);
|
onChange(newContent);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleFormatJson = () => {
|
|
||||||
try {
|
|
||||||
const formatted = formatJson(editorContent);
|
|
||||||
setEditorContent(formatted);
|
|
||||||
onChange(formatted);
|
|
||||||
setInternalError(undefined);
|
|
||||||
} catch (err) {
|
|
||||||
setInternalError(err instanceof Error ? err.message : "Invalid JSON");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const displayError = internalError || externalError;
|
const displayError = internalError || externalError;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="relative space-y-2">
|
<div className="relative">
|
||||||
<div className="flex justify-end">
|
|
||||||
<Button variant="outline" size="sm" onClick={handleFormatJson}>
|
|
||||||
Format JSON
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
<div
|
<div
|
||||||
className={`border rounded-md ${
|
className={`border rounded-md ${
|
||||||
displayError
|
displayError
|
||||||
|
|||||||
Reference in New Issue
Block a user