This commit is contained in:
h z
2025-05-05 22:35:22 +01:00
commit f5b3b54a5a
22 changed files with 1196 additions and 0 deletions

24
.gitignore vendored Normal file
View File

@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

28
eslint.config.js Normal file
View File

@@ -0,0 +1,28 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'
export default tseslint.config(
{ ignores: ['dist'] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ['**/*.{ts,tsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
)

13
index.html Normal file
View File

@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>

46
package.json Normal file
View File

@@ -0,0 +1,46 @@
{
"name": "json-schema-editor",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"@monaco-editor/react": "^4.7.0",
"@reduxjs/toolkit": "^2.7.0",
"@tailwindcss/vite": "^4.1.5",
"antd": "^5.24.9",
"avj": "^0.0.0",
"immer": "^10.1.1",
"lodash": "^4.17.21",
"lucide-react": "^0.507.0",
"react": "^19.1.0",
"react-dom": "^19.1.0",
"react-redux": "^9.2.0",
"zustand": "^5.0.4"
},
"devDependencies": {
"@eslint/js": "^9.25.0",
"@tailwindcss/postcss": "^4.1.5",
"@types/lodash": "^4.17.16",
"@types/node": "^22.15.3",
"@types/react": "^19.1.2",
"@types/react-dom": "^19.1.2",
"@types/tailwindcss": "^3.1.0",
"@vitejs/plugin-react": "^4.4.1",
"autoprefixer": "^10.4.21",
"eslint": "^9.25.0",
"eslint-plugin-react-hooks": "^5.2.0",
"eslint-plugin-react-refresh": "^0.4.19",
"globals": "^16.0.0",
"postcss": "^8.5.3",
"tailwindcss": "^4.1.5",
"typescript": "~5.8.3",
"typescript-eslint": "^8.30.1",
"vite": "^6.3.5"
}
}

6
postcss.config.js Normal file
View File

@@ -0,0 +1,6 @@
export default {
plugins: {
"@tailwindcss/postcss": {},
autoprefixer: {},
},
}

0
src/App.css Normal file
View File

13
src/App.tsx Normal file
View File

@@ -0,0 +1,13 @@
import './App.css'
import JSONSchemaEditor from "./components/JSONSchemaEditor.tsx";
function App() {
return (
<>
<JSONSchemaEditor />
</>
);
}
export default App;

1
src/assets/react.svg Normal file
View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>

After

Width:  |  Height:  |  Size: 4.0 KiB

View File

@@ -0,0 +1,100 @@
import {useState} from "react";
import {Check, Plus, Trash2, X} from "lucide-react";
import type { JSONSchemaType } from "../defs/types";
import PropertyEditor from "./PropertyEditor.tsx";
import styles from "../defs/styles.tsx";
import type {FieldVisibility} from "../defs/interfaces.tsx";
export const DefsEditor = ({
defs,
onChange,
rootSchema,
visibility,
}: {
defs: Record<string, JSONSchemaType>,
onChange: (defs: Record<string, JSONSchemaType>) => void,
rootSchema: JSONSchemaType,
visibility: FieldVisibility,
}) => {
const [isAddingDef, setIsAddingDef] = useState(false);
const [newDefName, setNewDefName] = useState('');
const handleAddDef = () => {
if (newDefName && !defs[newDefName]) {
const newDefs = { ...defs };
newDefs[newDefName] = { type: 'object', properties: {} };
onChange(newDefs);
setNewDefName('');
setIsAddingDef(false);
}
};
const handleDeleteDef = (defName: string) => {
const newDefs = { ...defs };
delete newDefs[defName];
onChange(Object.keys(newDefs).length ? newDefs : {});
};
return (
<div>
<div className="flex items-center justify-between mb-4">
<h2 className="text-lg font-semibold">Definitions ($defs)</h2>
{!isAddingDef ? (
<button
onClick={() => setIsAddingDef(true)}
className="text-blue-500 hover:text-blue-700 flex items-center"
>
<Plus size={16} className="mr-1" /> Add Definition
</button>
) : (
<div className="flex items-center">
<input
type="text"
value={newDefName}
onChange={(e) => setNewDefName(e.target.value)}
className="border border-gray-300 rounded px-2 py-1 mr-2"
placeholder="Definition name"
/>
<button onClick={handleAddDef} className="text-green-600 mr-1">
<Check size={16} />
</button>
<button onClick={() => {
setIsAddingDef(false);
setNewDefName('');
}} className="text-red-600">
<X size={16} />
</button>
</div>
)}
</div>
{defs && Object.entries(defs).map(([defName, defSchema]) => (
<div key={defName} className={styles.card}>
<div className="flex items-center justify-between mb-2">
<h3 className="font-medium">{defName}</h3>
<button onClick={() => handleDeleteDef(defName)} className="text-red-500 hover:text-red-700">
<Trash2 size={16} />
</button>
</div>
<PropertyEditor
schema={defSchema}
path={`$defs.${defName}`}
onChange={(newDefSchema) => {
const newDefs = { ...defs };
newDefs[defName] = newDefSchema;
onChange(newDefs);
}}
rootSchema={rootSchema}
visibility={visibility}
/>
</div>
))}
{(!defs || Object.keys(defs).length === 0) && (
<p className="text-gray-500 italic">No definitions yet. Add one to reference it elsewhere in your schema.</p>
)}
</div>
);
};
export default DefsEditor;

View File

@@ -0,0 +1,169 @@
import { useState } from 'react';
import {Copy, Eye, EyeOff} from 'lucide-react';
import type {JSONSchemaType} from "../defs/types.tsx";
import DefsEditor from "./DefsEditor.tsx";
import styles from '../defs/styles.tsx';
import PropertyEditor from "./PropertyEditor.tsx";
import type {FieldVisibility} from "../defs/interfaces.tsx";
type VisibilityField = keyof FieldVisibility;
export default function JSONSchemaEditor() {
const [schema, setSchema] = useState<JSONSchemaType>({
$schema: "https://json-schema.org/draft/2020-12/schema",
$id: "https://example.com/schema.json",
type: "object",
properties: {
firstName: {
type: "string",
description: "The person's first name"
},
lastName: {
type: "string",
description: "The person's last name"
},
age: {
type: "integer",
description: "Age in years",
minimum: 0
}
},
required: ["firstName", "lastName"]
});
const [activeTab, setActiveTab] = useState('editor');
const [visibility, setVisibility] = useState<FieldVisibility>({
showTitle: true,
showDescription: true,
showPropertyLimit: true,
showRequired: true,
showAdditionalProperties: true,
showUniqueItems: true,
showArrayLimit: true,
showNumericLimit: true,
showMultipleOf: true,
showStringLength: true,
showStringFormat: true,
showStringPattern: true
});
const handleSchemaChange = (newSchema: JSONSchemaType) => {
setSchema(newSchema);
};
const toggleVisibility = (field: VisibilityField) => {
setVisibility(prev => ({...prev, [field]: !prev[field]}));
};
const renderToggleButton = (field: VisibilityField, label: string) => {
const isVisible = visibility[field];
return (
<button
key={field}
onClick={() => toggleVisibility(field)}
className={`${styles.visibilityToggle} ${
isVisible
? styles.visibilityToggleShow
: styles.visibilityToggleHide
}`}
title={isVisible ? `Hide ${label}` : `Show ${label}`}
>
{isVisible ? <Eye size={14} className="mr-1" /> : <EyeOff size={14} className="mr-1" />}
{label}
</button>
);
};
const handleCopyJson = () => {
navigator.clipboard.writeText(JSON.stringify(schema, null, 2));
};
return (
<div className={styles.container}>
<header className={styles.header}>
<h1 className="text-2xl font-bold">JSON Schema Editor</h1>
<p>JSON Schema Editor</p>
</header>
<main className="flex gap-4 md:gap-6 mt-4">
<div className={styles.editor}>
<div className={styles.toolbar}>
<div className={styles.tabs}>
<button
className={activeTab === 'editor' ? styles.activeTab : styles.tab}
onClick={() => setActiveTab('editor')}
>
Schema
</button>
<button
className={activeTab === 'defs' ? styles.activeTab : styles.tab}
onClick={() => setActiveTab('defs')}
>
$defs
</button>
</div>
<div>
<button
onClick={handleCopyJson}
className="flex items-center text-blue-600 hover:text-blue-800"
>
<Copy size={16} className="mr-1" /> Copy JSON
</button>
</div>
</div>
<div className={styles.visibilityToggles}>
{renderToggleButton('showTitle', 'Title')}
{renderToggleButton('showDescription', 'Description')}
{renderToggleButton('showPropertyLimit', 'Prop Limits (Obj)')}
{renderToggleButton('showRequired', 'Required (Obj)')}
{renderToggleButton('showAdditionalProperties', 'Add. Props (Obj)')}
{renderToggleButton('showUniqueItems', 'Unique Items (Arr)')}
{renderToggleButton('showArrayLimit', 'Array Limits (Arr)')}
{renderToggleButton('showNumericLimit', 'Num Limits (Num/Int)')}
{renderToggleButton('showMultipleOf', 'MultipleOf (Num/Int)')}
{renderToggleButton('showStringLength', 'Length (Str)')}
{renderToggleButton('showStringFormat', 'Format (Str)')}
{renderToggleButton('showStringPattern', 'Pattern (Str)')}
</div>
<div className={styles.propertyEditor}>
{activeTab === 'editor' ? (
<PropertyEditor
schema={schema}
path="root"
onChange={handleSchemaChange}
rootSchema={schema}
visibility={visibility}
/>
) : (
<DefsEditor
defs={schema.$defs || {}}
onChange={(newDefs) => {
setSchema({
...schema,
$defs: newDefs
});
}}
rootSchema={schema}
visibility={visibility}
/>
)}
</div>
</div>
<div className={styles.jsonPreviewContainer}>
<div className={styles.toolbar}>
<h3 className="font-medium">JSON Preview</h3>
</div>
<pre className="p-4 h-full overflow-auto">
{JSON.stringify(schema, null, 2)}
</pre>
</div>
</main>
</div>
);
}

View File

@@ -0,0 +1,598 @@
import { useState } from "react";
import type { JSONSchemaType } from "../defs/types";
import styles from "../defs/styles";
import { ChevronDown, ChevronRight, Trash2, Plus, Edit, Check, X } from 'lucide-react';
import type {FieldVisibility} from "../defs/interfaces.tsx";
const getDefaultSchemaForType = (type: string): JSONSchemaType => {
switch (type) {
case 'string':
return { type: 'string' };
case 'number':
return { type: 'number' };
case 'integer':
return { type: 'integer' };
case 'boolean':
return { type: 'boolean' };
case 'array':
return { type: 'array', items: { type: 'string' } };
case 'object':
return { type: 'object', properties: {} };
case 'null':
return { type: 'null' };
default:
return {};
}
};
const getTypeColor = (type: string) => {
switch (type) {
case 'string': return styles.stringType;
case 'number':
case 'integer': return styles.numberType;
case 'boolean': return styles.booleanType;
case 'object': return styles.objectType;
case 'array': return styles.arrayType;
case 'null': return styles.nullType;
default: return '';
}
};
export const PropertyEditor = ({
schema,
path,
onChange,
onDelete,
rootSchema,
onRename,
visibility,
}: {
schema: JSONSchemaType,
path: string,
onChange: (schema: JSONSchemaType) => void,
onDelete?: () => void,
rootSchema: JSONSchemaType,
onRename?: (oldName: string, newName: string) => void,
visibility: FieldVisibility,
}) => {
const propertyName = path.split('.').pop() || (path === 'root' ? 'Schema Root' : 'items');
const [expanded, setExpanded] = useState(true);
const [isEditingName, setIsEditingName] = useState(false);
const [tempName, setTempName] = useState(propertyName);
const handleTypeChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
const newType = e.target.value;
if (newType === '$ref') {
onChange({...schema, $ref: '#'});
} else {
const newSchema = {...schema};
if (newSchema.$ref) {
delete newSchema.$ref;
}
onChange({
...newSchema,
...getDefaultSchemaForType(newType)
});
}
};
const handleFieldChange = (field: string, value: JSONSchemaType[keyof JSONSchemaType]) => {
onChange({...schema, [field]: value});
};
const handleRefChange = (value: string) => {
onChange({...schema, $ref: value});
};
const handleAddProperty = () => {
const properties = schema.properties
? {...schema.properties}
: {};
const newPropName = `newProperty${Object.keys(properties).length + 1}`;
properties[newPropName] = {type: 'string'};
onChange({
...schema,
properties
});
};
const handleAddArrayItem = () => {
if (!schema.items || Array.isArray(schema.items)) {
onChange({
...schema,
items: {type: 'string'}
});
}
};
const handleEditName = () => {
if(path === 'root' || path.endsWith('.items')) return;
setTempName(propertyName);
setIsEditingName(true);
};
const handleNameSave = () => {
const newName = tempName.trim();
if(newName && newName !== propertyName){
onRename?.(propertyName, newName);
}
setIsEditingName(false);
};
const handleNameCancel = () => {
setIsEditingName(false);
};
const handlePropertyRename = (oldPropName: string, newName: string) => {
if(!newName || !schema.properties || !schema.properties[oldPropName] || schema.properties[newName])
{
setIsEditingName(false);
return;
}
const currentProperties = {...schema.properties};
const propertySchema = currentProperties[oldPropName];
delete currentProperties[oldPropName];
currentProperties[newName] = propertySchema;
let currentRequired = schema.required ? [...schema.required] : undefined;
if(currentRequired?.includes(oldPropName))
currentRequired = currentRequired.map(name => (name === oldPropName ? newName : name));
onChange({
...schema,
properties: currentProperties,
required: currentRequired
});
};
const getRefOptions = () => {
const options: string[] = [];
options.push('#');
if (rootSchema.$defs) {
Object.keys(rootSchema.$defs).forEach(defName => {
options.push(`#/$defs/${defName}`);
});
}
return options;
};
if (schema.$ref !== undefined) {
return (
<div className={styles.schemaItem}>
<div className="flex items-center mb-2">
{isEditingName ? (
<div className="flex items-center">
<input
type="text"
value={tempName}
onChange={(e) => setTempName(e.target.value)}
className="border border-gray-300 rounded px-2 py-1 mr-2"
/>
<button onClick={handleNameSave} className="text-green-600 mr-1">
<Check size={16}/>
</button>
<button onClick={handleNameCancel} className="text-red-600">
<X size={16}/>
</button>
</div>
) : (
<>
<div className="flex-1 flex items-center">
<span className={styles.schemaName}>{propertyName}</span>
<button onClick={handleEditName} className="ml-2 text-gray-500 hover:text-gray-700">
<Edit size={14}/>
</button>
</div>
<div className="flex items-center">
<select
value="$ref"
onChange={handleTypeChange}
className="border border-gray-300 rounded px-2 py-1 mr-2 text-sm"
>
<option value="$ref">$ref</option>
<option value="string">string</option>
<option value="number">number</option>
<option value="integer">integer</option>
<option value="boolean">boolean</option>
<option value="array">array</option>
<option value="object">object</option>
<option value="null">null</option>
</select>
{onDelete && (
<button onClick={onDelete} className="text-red-500 hover:text-red-700">
<Trash2 size={16}/>
</button>
)}
</div>
</>
)}
</div>
<div className="ml-4">
<div className="mb-2">
<label className={styles.label}>Reference Path:</label>
<select
value={schema.$ref}
onChange={(e) => handleRefChange(e.target.value)}
className={styles.select}
>
{getRefOptions().map(option => (
<option key={option} value={option}>{option}</option>
))}
</select>
</div>
</div>
</div>
);
}
return (
<div className={styles.schemaItem}>
<div className="flex items-center mb-2">
<button
onClick={() => setExpanded(!expanded)}
className="mr-1 text-gray-500"
>
{expanded ? <ChevronDown size={16}/> : <ChevronRight size={16}/>}
</button>
{isEditingName ? (
<div className="flex items-center">
<input
type="text"
value={tempName}
onChange={(e) => setTempName(e.target.value)}
className="border border-gray-300 rounded px-2 py-1 mr-2"
/>
<button onClick={handleNameSave} className="text-green-600 mr-1">
<Check size={16}/>
</button>
<button onClick={handleNameCancel} className="text-red-600">
<X size={16}/>
</button>
</div>
) : (
<>
<div className="flex-1 flex items-center">
<span className={styles.schemaName}>{propertyName}</span>
{path !== 'root' && (
<button onClick={handleEditName} className="ml-2 text-gray-500 hover:text-gray-700">
<Edit size={14}/>
</button>
)}
</div>
<div className="flex items-center">
<span className={`${styles.typeTag} ${getTypeColor(schema.type as string)} mr-2`}>
{schema.type || 'any'}
</span>
<select
value={schema.type as string || ''}
onChange={handleTypeChange}
className="border border-gray-300 rounded px-2 py-1 mr-2 text-sm"
>
<option value="string">string</option>
<option value="number">number</option>
<option value="integer">integer</option>
<option value="boolean">boolean</option>
<option value="array">array</option>
<option value="object">object</option>
<option value="null">null</option>
<option value="$ref">$ref</option>
</select>
{onDelete && (
<button onClick={onDelete} className="text-red-500 hover:text-red-700">
<Trash2 size={16}/>
</button>
)}
</div>
</>
)}
</div>
{expanded && (
<div className="ml-4">
{visibility.showTitle && (
<div className="mb-2">
<label className={styles.label}>Title:</label>
<input
type="text"
value={schema.title || ''}
onChange={(e) => handleFieldChange('title', e.target.value)}
className={styles.input}
placeholder="Schema title"
/>
</div>
)}
{visibility.showDescription && (
<div className="mb-2">
<label className={styles.label}>Description:</label>
<textarea
value={schema.description || ''}
onChange={(e) => handleFieldChange('description', e.target.value)}
className={styles.input}
placeholder="Schema description"
rows={2}
/>
</div>
)}
{schema.type === 'string' && (
<>
{visibility.showStringFormat && (
<div className="mb-2">
<label className={styles.label}>Format:</label>
<select
value={schema.format || ''}
onChange={(e) => handleFieldChange('format', e.target.value || undefined)}
className={styles.select}
>
<option value="">None</option>
<option value="date-time">date-time</option>
<option value="date">date</option>
<option value="time">time</option>
<option value="email">email</option>
<option value="uri">uri</option>
<option value="uuid">uuid</option>
<option value="regex">regex</option>
</select>
</div>
)}
{visibility.showStringPattern && (
<div className="mb-2">
<label className={styles.label}>Pattern:</label>
<input
type="text"
value={schema.pattern || ''}
onChange={(e) => handleFieldChange('pattern', e.target.value || undefined)}
className={styles.input}
placeholder="Regular expression pattern"
/>
</div>
)}
{visibility.showStringLength && (
<div className="grid grid-cols-2 gap-2 mb-2">
<div>
<label className={styles.label}>Min Length:</label>
<input
type="number"
value={schema.minLength || ''}
onChange={(e) => handleFieldChange('minLength', e.target.value ? parseInt(e.target.value) : undefined)}
className={styles.input}
placeholder="Min length"
/>
</div>
<div>
<label className={styles.label}>Max Length:</label>
<input
type="number"
value={schema.maxLength || ''}
onChange={(e) => handleFieldChange('maxLength', e.target.value ? parseInt(e.target.value) : undefined)}
className={styles.input}
placeholder="Max length"
/>
</div>
</div>
)}
</>
)}
{(schema.type === 'number' || schema.type === 'integer') && (
<>
{visibility.showNumericLimit && (
<div className="grid grid-cols-2 gap-2 mb-2">
<div>
<label className={styles.label}>Minimum:</label>
<input
type="number"
value={schema.minimum || ''}
onChange={(e) => handleFieldChange('minimum', e.target.value ? parseFloat(e.target.value) : undefined)}
className={styles.input}
placeholder="Minimum value"
/>
</div>
<div>
<label className={styles.label}>Maximum:</label>
<input
type="number"
value={schema.maximum || ''}
onChange={(e) => handleFieldChange('maximum', e.target.value ? parseFloat(e.target.value) : undefined)}
className={styles.input}
placeholder="Maximum value"
/>
</div>
</div>
)}
{visibility.showMultipleOf && (
<div className="mb-2">
<label className={styles.label}>Multiple Of:</label>
<input
type="number"
value={schema.multipleOf || ''}
onChange={(e) => handleFieldChange('multipleOf', e.target.value ? parseFloat(e.target.value) : undefined)}
className={styles.input}
placeholder="Multiple of"
/>
</div>
)}
</>
)}
{schema.type === 'array' && (
<>
{visibility.showArrayLimit && (
<div className="grid grid-cols-2 gap-2 mb-2">
<div>
<label className={styles.label}>Min Items:</label>
<input
type="number"
value={schema.minItems || ''}
onChange={(e) => handleFieldChange('minItems', e.target.value ? parseInt(e.target.value) : undefined)}
className={styles.input}
placeholder="Min items"
/>
</div>
<div>
<label className={styles.label}>Max Items:</label>
<input
type="number"
value={schema.maxItems || ''}
onChange={(e) => handleFieldChange('maxItems', e.target.value ? parseInt(e.target.value) : undefined)}
className={styles.input}
placeholder="Max items"
/>
</div>
</div>
)}
{visibility.showUniqueItems && (
<div className="mb-2">
<label className={styles.label}>Unique Items:</label>
<select
value={schema.uniqueItems ? 'true' : 'false'}
onChange={(e) => handleFieldChange('uniqueItems', e.target.value === 'true')}
className={styles.select}
>
<option value="false">False</option>
<option value="true">True</option>
</select>
</div>
)}
<div className="mb-2">
<div className="flex items-center justify-between mb-1">
<label className={styles.label}>Array Items:</label>
<button
onClick={handleAddArrayItem}
className="text-blue-500 hover:text-blue-700 text-sm flex items-center"
>
<Plus size={14} className="mr-1"/> Define Items
</button>
</div>
{schema.items && !Array.isArray(schema.items) && (
<PropertyEditor
schema={schema.items}
path={`${path}.items`}
onChange={(newItemsSchema) => handleFieldChange('items', newItemsSchema)}
rootSchema={rootSchema}
visibility={visibility}
/>
)}
</div>
</>
)}
{schema.type === 'object' && (
<>
{visibility.showPropertyLimit && (
<div className="grid grid-cols-2 gap-2 mb-2">
<div>
<label className={styles.label}>Min Properties:</label>
<input
type="number"
value={schema.minProperties || ''}
onChange={(e) => handleFieldChange('minProperties', e.target.value ? parseInt(e.target.value) : undefined)}
className={styles.input}
placeholder="Min properties"
/>
</div>
<div>
<label className={styles.label}>Max Properties:</label>
<input
type="number"
value={schema.maxProperties || ''}
onChange={(e) => handleFieldChange('maxProperties', e.target.value ? parseInt(e.target.value) : undefined)}
className={styles.input}
placeholder="Max properties"
/>
</div>
</div>
)}
{visibility.showRequired && (
<div className="mb-2">
<label className={styles.label}>Required Properties:</label>
<select
multiple
value={schema.required || []}
onChange={(e) => {
const options = Array.from(e.target.selectedOptions).map(option => option.value);
handleFieldChange('required', options.length ? options : undefined);
}}
className={styles.select}
style={{height: '100px'}}
>
{schema.properties && Object.keys(schema.properties).map(propName => (
<option key={propName} value={propName}>{propName}</option>
))}
</select>
</div>
)}
{visibility.showAdditionalProperties && (
<div className="mb-2">
<label className={styles.label}>Additional Properties:</label>
<select
value={schema.additionalProperties === false ? 'false' : 'true'}
onChange={(e) => handleFieldChange('additionalProperties', e.target.value === 'false' ? false : true)}
className={styles.select}
>
<option value="true">Allow</option>
<option value="false">Disallow</option>
</select>
</div>
)}
<div className="mb-2">
<div className="flex items-center justify-between mb-1">
<label className={styles.label}>Properties:</label>
<button
onClick={handleAddProperty}
className="text-blue-500 hover:text-blue-700 text-sm flex items-center"
>
<Plus size={14} className="mr-1"/> Add Property
</button>
</div>
{schema.properties && Object.entries(schema.properties).map(([propName, propSchema]) => (
<PropertyEditor
key={propName}
schema={propSchema}
path={`${path}.properties.${propName}`}
onChange={(newPropSchema) => {
const newProperties = {...schema.properties};
newProperties[propName] = newPropSchema;
handleFieldChange('properties', newProperties);
}}
onDelete={() => {
const newProperties = {...schema.properties};
delete newProperties[propName];
handleFieldChange('properties', Object.keys(newProperties).length ? newProperties : undefined);
if (schema.required && schema.required.includes(propName)) {
const newRequired = schema.required.filter(name => name !== propName);
handleFieldChange('required', newRequired.length ? newRequired : undefined);
}
}}
onRename={handlePropertyRename}
rootSchema={rootSchema}
visibility={visibility}
/>
))}
</div>
</>
)}
</div>
)}
</div>
);
};
export default PropertyEditor;

15
src/defs/interfaces.tsx Normal file
View File

@@ -0,0 +1,15 @@
export interface FieldVisibility {
showTitle: boolean;
showDescription: boolean;
showPropertyLimit: boolean;
showRequired: boolean;
showAdditionalProperties: boolean;
showArrayLimit: boolean;
showUniqueItems: boolean;
showNumericLimit: boolean;
showMultipleOf: boolean;
showStringLength: boolean;
showStringFormat: boolean;
showStringPattern: boolean;
}

43
src/defs/styles.tsx Normal file
View File

@@ -0,0 +1,43 @@
export const styles = {
container: "bg-gray-100 min-h-screen p-6",
header: "bg-blue-600 text-white p-4 rounded-t-lg shadow mb-4",
main: "flex gap-6",
editor: "flex-1 flex flex-col bg-white shadow-lg rounded-lg overflow-hidden border border-gray-200",
propertyEditor: "flex-1 p-4 overflow-y-auto",
jsonPreviewContainer: "flex-1 flex flex-col bg-gray-800 text-sm rounded-lg overflow-hidden border border-gray-700",
jsonPreviewContent: "p-4 text-green-400 font-mono overflow-auto",
toolbar: "bg-gray-100 p-2 flex items-center justify-between border-b border-gray-300",
schemaItem: "border-l-2 border-gray-200 my-1 relative hover:border-blue-400",
activeSchemaItem: "border-l-2 border-blue-500 my-1 bg-blue-50 relative",
schemaName: "font-medium text-gray-800",
input: "border border-gray-300 rounded px-3 py-1.5 w-full text-sm focus:border-blue-500 focus:ring-1 focus:ring-blue-500 outline-none", // 统一输入框样式
select: "border border-gray-300 rounded px-3 py-1.5 w-full text-sm focus:border-blue-500 focus:ring-1 focus:ring-blue-500 outline-none bg-white", // 统一选择框样式
label: "block text-xs font-medium text-gray-600 mb-1",
propertyHeader: "flex justify-between items-center",
tab: "px-4 py-2 cursor-pointer text-gray-600 hover:text-blue-600",
activeTab: "px-4 py-2 cursor-pointer border-b-2 border-blue-500 font-medium text-blue-600",
tabs: "flex border-b border-gray-300",
typeTag: "text-xs px-2 py-0.5 rounded-full font-medium whitespace-nowrap",
stringType: "bg-green-100 text-green-700",
numberType: "bg-purple-100 text-purple-700",
booleanType: "bg-yellow-100 text-yellow-700",
objectType: "bg-blue-100 text-blue-700",
arrayType: "bg-pink-100 text-pink-700",
nullType: "bg-gray-200 text-gray-700",
refType: "bg-indigo-100 text-indigo-700",
card: "bg-white rounded-lg shadow p-4 mb-4 border border-gray-200",
form: "space-y-3",
formRow: "flex flex-col",
errorText: "text-red-500 text-sm mt-1",
iconButton: "p-1 rounded text-gray-500 hover:bg-gray-100 hover:text-gray-700",
button: "inline-flex items-center justify-center px-3 py-1.5 text-sm font-medium border border-transparent rounded shadow-sm text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50",
buttonSecondary: "inline-flex items-center justify-center px-3 py-1.5 text-sm font-medium border border-gray-300 rounded shadow-sm text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 disabled:opacity-50",
visibilityToggle: "flex items-center px-2 py-1 border rounded text-xs mr-2 mb-1",
visibilityToggleShow: "bg-blue-100 border-blue-300 text-blue-700",
visibilityToggleHide: "bg-gray-100 border-gray-300 text-gray-600 hover:bg-gray-200",
visibilityToggles: "p-2 border-b border-gray-300 flex flex-wrap bg-gray-50",
};
export default styles;

45
src/defs/types.tsx Normal file
View File

@@ -0,0 +1,45 @@
type JSONValue = string | number | boolean | null | JSONObject | JSONArray;
type JSONObject = { [key: string]: JSONValue };
type JSONArray = JSONValue[];
type JSONSchemaType = {
type?: string | string[];
properties?: Record<string, JSONSchemaType>;
items?: JSONSchemaType | JSONSchemaType[];
required?: string[];
enum?: JSONValue[];
const?: JSONValue;
title?: string;
description?: string;
default?: JSONValue;
examples?: JSONValue[];
$ref?: string;
$defs?: Record<string, JSONSchemaType>;
$id?: string;
anyOf?: JSONSchemaType[];
allOf?: JSONSchemaType[];
oneOf?: JSONSchemaType[];
not?: JSONSchemaType;
if?: JSONSchemaType;
then?: JSONSchemaType;
else?: JSONSchemaType;
format?: string;
pattern?: string;
minLength?: number;
maxLength?: number;
minimum?: number;
maximum?: number;
exclusiveMinimum?: number | boolean;
exclusiveMaximum?: number | boolean;
multipleOf?: number;
minItems?: number;
maxItems?: number;
uniqueItems?: boolean;
minProperties?: number;
maxProperties?: number;
additionalProperties?: boolean | JSONSchemaType;
propertyNames?: JSONSchemaType;
[key: string]: JSONValue | JSONSchemaType | JSONSchemaType[] | string[] | boolean | Record<string, JSONSchemaType> | undefined;
};
export type { JSONValue, JSONObject, JSONArray, JSONSchemaType };

4
src/index.css Normal file
View File

@@ -0,0 +1,4 @@
@import "tailwindcss";
@tailwind base;
@tailwind components;
@tailwind utilities;

10
src/main.tsx Normal file
View File

@@ -0,0 +1,10 @@
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.tsx'
createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
</StrictMode>,
)

1
src/vite-env.d.ts vendored Normal file
View File

@@ -0,0 +1 @@
/// <reference types="vite/client" />

10
tailwind.config.js Normal file
View File

@@ -0,0 +1,10 @@
export default {
content: [
"./index.html",
"./src/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}

27
tsconfig.app.json Normal file
View File

@@ -0,0 +1,27 @@
{
"compilerOptions": {
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"moduleDetection": "force",
"noEmit": true,
"jsx": "react-jsx",
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"erasableSyntaxOnly": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true
},
"include": ["src"]
}

7
tsconfig.json Normal file
View File

@@ -0,0 +1,7 @@
{
"files": [],
"references": [
{ "path": "./tsconfig.app.json" },
{ "path": "./tsconfig.node.json" }
]
}

25
tsconfig.node.json Normal file
View File

@@ -0,0 +1,25 @@
{
"compilerOptions": {
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
"target": "ES2022",
"lib": ["ES2023"],
"module": "ESNext",
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"moduleDetection": "force",
"noEmit": true,
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"erasableSyntaxOnly": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true
},
"include": ["vite.config.ts"]
}

11
vite.config.ts Normal file
View File

@@ -0,0 +1,11 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// https://vite.dev/config/
import tailwindcss from "@tailwindcss/vite";
export default defineConfig({
plugins: [react(), tailwindcss()]
})