|
@@ -1,9 +1,29 @@
|
|
|
'use client';
|
|
'use client';
|
|
|
|
|
|
|
|
-import { useState, useEffect } from 'react';
|
|
|
|
|
|
|
+import { useState, useEffect, useMemo } from 'react';
|
|
|
import api from '@/lib/api';
|
|
import api from '@/lib/api';
|
|
|
-import { Loader2, Plus, Save, X, FileJson, List, Edit3, Trash2 } from 'lucide-react';
|
|
|
|
|
-import JsonEditor from '@/components/common/JsonEditor';
|
|
|
|
|
|
|
+import {
|
|
|
|
|
+ Loader2,
|
|
|
|
|
+ Plus,
|
|
|
|
|
+ Save,
|
|
|
|
|
+ X,
|
|
|
|
|
+ FileJson,
|
|
|
|
|
+ Search,
|
|
|
|
|
+ Trash2,
|
|
|
|
|
+ Copy,
|
|
|
|
|
+ Check,
|
|
|
|
|
+ Code2,
|
|
|
|
|
+ Eye,
|
|
|
|
|
+ Columns,
|
|
|
|
|
+ Sparkles,
|
|
|
|
|
+ Layers,
|
|
|
|
|
+ HelpCircle,
|
|
|
|
|
+ Braces,
|
|
|
|
|
+ Minimize2,
|
|
|
|
|
+ AlertCircle
|
|
|
|
|
+} from 'lucide-react';
|
|
|
|
|
+import Editor from '@monaco-editor/react';
|
|
|
|
|
+import { toast } from 'react-hot-toast';
|
|
|
import { SchemaItem } from '@/types';
|
|
import { SchemaItem } from '@/types';
|
|
|
|
|
|
|
|
interface SchemaManagerProps {
|
|
interface SchemaManagerProps {
|
|
@@ -11,25 +31,149 @@ interface SchemaManagerProps {
|
|
|
onClose: () => void;
|
|
onClose: () => void;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// 常用 Schema 预设模板
|
|
|
|
|
+const SCHEMA_TEMPLATES = [
|
|
|
|
|
+ {
|
|
|
|
|
+ name: '标准签证申请人材料模板',
|
|
|
|
|
+ description: '包含姓名、护照号、出生日期、签证中心等常用字段',
|
|
|
|
|
+ schema_json: JSON.stringify(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'object',
|
|
|
|
|
+ title: '签证申请人基础资料',
|
|
|
|
|
+ description: '请准确填写申请人护照信息与预约需求',
|
|
|
|
|
+ required: ['applicant_name', 'passport_number', 'nationality', 'visa_center'],
|
|
|
|
|
+ properties: {
|
|
|
|
|
+ applicant_name: {
|
|
|
|
|
+ type: 'string',
|
|
|
|
|
+ title: '申请人全名 (拼音)',
|
|
|
|
|
+ placeholder: '如: ZHANG SAN',
|
|
|
|
|
+ },
|
|
|
|
|
+ passport_number: {
|
|
|
|
|
+ type: 'string',
|
|
|
|
|
+ title: '护照号码',
|
|
|
|
|
+ placeholder: '如: E12345678',
|
|
|
|
|
+ },
|
|
|
|
|
+ nationality: {
|
|
|
|
|
+ type: 'string',
|
|
|
|
|
+ title: '国籍',
|
|
|
|
|
+ default: 'China',
|
|
|
|
|
+ enum: ['China', 'Hong Kong', 'Taiwan', 'Other'],
|
|
|
|
|
+ },
|
|
|
|
|
+ birthday: {
|
|
|
|
|
+ type: 'string',
|
|
|
|
|
+ format: 'date',
|
|
|
|
|
+ title: '出生日期',
|
|
|
|
|
+ placeholder: 'YYYY-MM-DD',
|
|
|
|
|
+ },
|
|
|
|
|
+ visa_center: {
|
|
|
|
|
+ type: 'string',
|
|
|
|
|
+ title: '递签签证中心',
|
|
|
|
|
+ enum: ['Beijing', 'Shanghai', 'Guangzhou', 'Chengdu', 'Wuhan', 'Shenyang'],
|
|
|
|
|
+ },
|
|
|
|
|
+ slot_routing_key: {
|
|
|
|
|
+ type: 'string',
|
|
|
|
|
+ title: '意向自动化槽位标识 (选填)',
|
|
|
|
|
+ placeholder: '例如: fr_tls_shanghai_bot',
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ null,
|
|
|
|
|
+ 2
|
|
|
|
|
+ ),
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ name: '联系人与寄送信息模板',
|
|
|
|
|
+ description: '包含联系电话、电子邮箱、寄送地址',
|
|
|
|
|
+ schema_json: JSON.stringify(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'object',
|
|
|
|
|
+ title: '联系与交付信息',
|
|
|
|
|
+ required: ['contact_name', 'phone_number', 'email'],
|
|
|
|
|
+ properties: {
|
|
|
|
|
+ contact_name: {
|
|
|
|
|
+ type: 'string',
|
|
|
|
|
+ title: '联系人姓名',
|
|
|
|
|
+ placeholder: '中文姓名',
|
|
|
|
|
+ },
|
|
|
|
|
+ phone_number: {
|
|
|
|
|
+ type: 'string',
|
|
|
|
|
+ title: '手机号码',
|
|
|
|
|
+ placeholder: '用于接收进度短信通知',
|
|
|
|
|
+ },
|
|
|
|
|
+ email: {
|
|
|
|
|
+ type: 'string',
|
|
|
|
|
+ title: '电子邮箱',
|
|
|
|
|
+ placeholder: '用于接收确认函与文件',
|
|
|
|
|
+ },
|
|
|
|
|
+ delivery_address: {
|
|
|
|
|
+ type: 'string',
|
|
|
|
|
+ title: '回邮收件地址 (选填)',
|
|
|
|
|
+ placeholder: '省/市/区/详细地址',
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ null,
|
|
|
|
|
+ 2
|
|
|
|
|
+ ),
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ name: '极简预约说明模板',
|
|
|
|
|
+ description: '仅包含预约日期期望与备注文档',
|
|
|
|
|
+ schema_json: JSON.stringify(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'object',
|
|
|
|
|
+ title: '预约偏好设置',
|
|
|
|
|
+ required: ['preferred_start_date'],
|
|
|
|
|
+ properties: {
|
|
|
|
|
+ preferred_start_date: {
|
|
|
|
|
+ type: 'string',
|
|
|
|
|
+ format: 'date',
|
|
|
|
|
+ title: '最早期望预约日期',
|
|
|
|
|
+ },
|
|
|
|
|
+ preferred_end_date: {
|
|
|
|
|
+ type: 'string',
|
|
|
|
|
+ format: 'date',
|
|
|
|
|
+ title: '最晚期望预约日期',
|
|
|
|
|
+ },
|
|
|
|
|
+ notes: {
|
|
|
|
|
+ type: 'string',
|
|
|
|
|
+ title: '特殊说明与备注',
|
|
|
|
|
+ placeholder: '如有同行人或特定时间段要求请注明',
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ null,
|
|
|
|
|
+ 2
|
|
|
|
|
+ ),
|
|
|
|
|
+ },
|
|
|
|
|
+];
|
|
|
|
|
+
|
|
|
export default function SchemaManager({ isOpen, onClose }: SchemaManagerProps) {
|
|
export default function SchemaManager({ isOpen, onClose }: SchemaManagerProps) {
|
|
|
const [schemas, setSchemas] = useState<SchemaItem[]>([]);
|
|
const [schemas, setSchemas] = useState<SchemaItem[]>([]);
|
|
|
const [loading, setLoading] = useState(false);
|
|
const [loading, setLoading] = useState(false);
|
|
|
-
|
|
|
|
|
- // 移动端 Tab 状态: 'list' | 'editor'
|
|
|
|
|
- const [activeTab, setActiveTab] = useState<'list' | 'editor'>('list');
|
|
|
|
|
|
|
+ const [saving, setSaving] = useState(false);
|
|
|
|
|
+ const [searchKey, setSearchKey] = useState('');
|
|
|
|
|
+
|
|
|
|
|
+ // 移动端 Tab: 'list' | 'editor'
|
|
|
|
|
+ const [mobileTab, setMobileTab] = useState<'list' | 'editor'>('list');
|
|
|
|
|
+
|
|
|
|
|
+ // 桌面端视图模式: 'split' | 'code' | 'preview'
|
|
|
|
|
+ const [viewMode, setViewMode] = useState<'split' | 'code' | 'preview'>('split');
|
|
|
|
|
|
|
|
// 编辑模式状态
|
|
// 编辑模式状态
|
|
|
const [editingId, setEditingId] = useState<number | null>(null);
|
|
const [editingId, setEditingId] = useState<number | null>(null);
|
|
|
const [formData, setFormData] = useState({
|
|
const [formData, setFormData] = useState({
|
|
|
name: '',
|
|
name: '',
|
|
|
description: '',
|
|
description: '',
|
|
|
- schema_json: '{\n "type": "object",\n "properties": {\n "field_name": { "type": "string", "title": "示例字段" }\n }\n}'
|
|
|
|
|
|
|
+ schema_json: SCHEMA_TEMPLATES[0].schema_json,
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ const [copied, setCopied] = useState(false);
|
|
|
|
|
+
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
if (isOpen) {
|
|
if (isOpen) {
|
|
|
fetchSchemas();
|
|
fetchSchemas();
|
|
|
- setActiveTab('list'); // 打开时默认显示列表
|
|
|
|
|
|
|
+ setMobileTab('list');
|
|
|
}
|
|
}
|
|
|
}, [isOpen]);
|
|
}, [isOpen]);
|
|
|
|
|
|
|
@@ -37,216 +181,614 @@ export default function SchemaManager({ isOpen, onClose }: SchemaManagerProps) {
|
|
|
setLoading(true);
|
|
setLoading(true);
|
|
|
try {
|
|
try {
|
|
|
const res = await api.get('/api/vas/schema/list');
|
|
const res = await api.get('/api/vas/schema/list');
|
|
|
- setSchemas(Array.isArray(res.data.data) ? res.data.data : []);
|
|
|
|
|
|
|
+ const list = Array.isArray(res.data?.data) ? res.data.data : [];
|
|
|
|
|
+ setSchemas(list);
|
|
|
|
|
+ // 如果没有正在编辑的对象,且列表有数据,默认不强行选中,保持清晰新建态
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
- console.warn("API Error, using mock schema data");
|
|
|
|
|
|
|
+ toast.error('加载 Schema 列表失败');
|
|
|
setSchemas([]);
|
|
setSchemas([]);
|
|
|
} finally {
|
|
} finally {
|
|
|
setLoading(false);
|
|
setLoading(false);
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+ const handleSelectSchema = async (item: SchemaItem) => {
|
|
|
|
|
+ setEditingId(item.id);
|
|
|
|
|
+ setMobileTab('editor');
|
|
|
|
|
+
|
|
|
|
|
+ // 尝试拉取完整详情获取完整的 schema_json
|
|
|
|
|
+ try {
|
|
|
|
|
+ const res = await api.get('/api/vas/schema/detail', {
|
|
|
|
|
+ params: { schema_id: item.id, id: item.id },
|
|
|
|
|
+ });
|
|
|
|
|
+ const detail = res.data?.data || res.data || item;
|
|
|
|
|
+ const rawJson = detail.schema_json || detail.content;
|
|
|
|
|
+ setFormData({
|
|
|
|
|
+ name: detail.name || item.name,
|
|
|
|
|
+ description: detail.description || item.description || '',
|
|
|
|
|
+ schema_json:
|
|
|
|
|
+ typeof rawJson === 'string'
|
|
|
|
|
+ ? rawJson
|
|
|
|
|
+ : JSON.stringify(rawJson || {}, null, 2),
|
|
|
|
|
+ });
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ const rawJson = item.schema_json || item.content;
|
|
|
|
|
+ setFormData({
|
|
|
|
|
+ name: item.name,
|
|
|
|
|
+ description: item.description || '',
|
|
|
|
|
+ schema_json:
|
|
|
|
|
+ typeof rawJson === 'string'
|
|
|
|
|
+ ? rawJson
|
|
|
|
|
+ : JSON.stringify(rawJson || {}, null, 2),
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const handleCreateNew = () => {
|
|
|
|
|
+ setEditingId(null);
|
|
|
|
|
+ setFormData({
|
|
|
|
|
+ name: '',
|
|
|
|
|
+ description: '',
|
|
|
|
|
+ schema_json: SCHEMA_TEMPLATES[0].schema_json,
|
|
|
|
|
+ });
|
|
|
|
|
+ setMobileTab('editor');
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const handleApplyTemplate = (template: typeof SCHEMA_TEMPLATES[0]) => {
|
|
|
|
|
+ if (!formData.name) {
|
|
|
|
|
+ setFormData({
|
|
|
|
|
+ name: template.name,
|
|
|
|
|
+ description: template.description,
|
|
|
|
|
+ schema_json: template.schema_json,
|
|
|
|
|
+ });
|
|
|
|
|
+ } else {
|
|
|
|
|
+ setFormData((prev) => ({
|
|
|
|
|
+ ...prev,
|
|
|
|
|
+ schema_json: template.schema_json,
|
|
|
|
|
+ }));
|
|
|
|
|
+ }
|
|
|
|
|
+ toast.success(`已插入模板: ${template.name}`);
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const handleFormatJson = () => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const parsed = JSON.parse(formData.schema_json);
|
|
|
|
|
+ setFormData((prev) => ({ ...prev, schema_json: JSON.stringify(parsed, null, 2) }));
|
|
|
|
|
+ toast.success('JSON 已格式化');
|
|
|
|
|
+ } catch (e: any) {
|
|
|
|
|
+ toast.error('JSON 语法错误,无法格式化: ' + e.message);
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const handleCompactJson = () => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const parsed = JSON.parse(formData.schema_json);
|
|
|
|
|
+ setFormData((prev) => ({ ...prev, schema_json: JSON.stringify(parsed) }));
|
|
|
|
|
+ toast.success('JSON 已压缩');
|
|
|
|
|
+ } catch (e: any) {
|
|
|
|
|
+ toast.error('JSON 语法错误,无法压缩');
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const handleCopyJson = () => {
|
|
|
|
|
+ navigator.clipboard.writeText(formData.schema_json);
|
|
|
|
|
+ setCopied(true);
|
|
|
|
|
+ toast.success('已复制 Schema JSON');
|
|
|
|
|
+ setTimeout(() => setCopied(false), 1500);
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
const handleSave = async () => {
|
|
const handleSave = async () => {
|
|
|
- if (!formData.name) return alert("名称必填");
|
|
|
|
|
|
|
+ if (!formData.name.trim()) {
|
|
|
|
|
+ toast.error('请填写 Schema 模板名称');
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ let parsed: any;
|
|
|
try {
|
|
try {
|
|
|
- JSON.parse(formData.schema_json); // 校验 JSON
|
|
|
|
|
|
|
+ parsed = JSON.parse(formData.schema_json);
|
|
|
|
|
+ } catch (e: any) {
|
|
|
|
|
+ toast.error('JSON 语法格式有误,请先修复语法错误');
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ setSaving(true);
|
|
|
|
|
+ try {
|
|
|
if (editingId) {
|
|
if (editingId) {
|
|
|
- await api.post(`/api/vas/schema/update`, formData, {params: {"id": editingId}});
|
|
|
|
|
|
|
+ await api.post('/api/vas/schema/update', formData, { params: { id: editingId } });
|
|
|
|
|
+ toast.success(`Schema #${editingId} 更新成功`);
|
|
|
} else {
|
|
} else {
|
|
|
- await api.post('/api/vas/schema/create', formData);
|
|
|
|
|
|
|
+ const res = await api.post('/api/vas/schema/create', formData);
|
|
|
|
|
+ toast.success('新 Schema 创建成功');
|
|
|
|
|
+ if (res.data?.data?.id) {
|
|
|
|
|
+ setEditingId(res.data.data.id);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- alert("保存成功");
|
|
|
|
|
fetchSchemas();
|
|
fetchSchemas();
|
|
|
-
|
|
|
|
|
- // 保存后逻辑:
|
|
|
|
|
- // 如果是新建,保存后清空表单并留再编辑页,或者切回列表
|
|
|
|
|
- // 这里选择保留在当前页,但如果是移动端可以考虑切回列表
|
|
|
|
|
- if (!editingId) {
|
|
|
|
|
- setFormData({ name: '', description: '', schema_json: '{}' });
|
|
|
|
|
- }
|
|
|
|
|
} catch (e: any) {
|
|
} catch (e: any) {
|
|
|
- alert("保存失败: " + (e.message || "JSON 格式错误"));
|
|
|
|
|
|
|
+ toast.error('保存失败: ' + (e.response?.data?.detail || e.message));
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ setSaving(false);
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- const handleCreateNew = () => {
|
|
|
|
|
- setEditingId(null);
|
|
|
|
|
- setFormData({ name: '', description: '', schema_json: '{}' });
|
|
|
|
|
- setActiveTab('editor'); // 移动端自动切换到编辑器 tab
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- const handleEdit = (item: SchemaItem) => {
|
|
|
|
|
- setEditingId(item.id);
|
|
|
|
|
- setFormData({
|
|
|
|
|
- name: item.name,
|
|
|
|
|
- description: item.description || '',
|
|
|
|
|
- schema_json: typeof item.schema_json === 'string' ? item.schema_json : JSON.stringify(item.schema_json, null, 2)
|
|
|
|
|
- });
|
|
|
|
|
- setActiveTab('editor'); // 移动端自动切换到编辑器 tab
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
const handleDelete = async (id: number) => {
|
|
const handleDelete = async (id: number) => {
|
|
|
- if(!confirm("删除 Schema 可能导致关联商品失效,确定删除吗?")) return;
|
|
|
|
|
|
|
+ if (!confirm('删除 Schema 可能导致关联商品下单异常,确定删除吗?')) return;
|
|
|
try {
|
|
try {
|
|
|
- await api.delete('/api/vas/schema/delete', {params: {"id": id}});
|
|
|
|
|
|
|
+ await api.delete('/api/vas/schema/delete', { params: { id: id } });
|
|
|
|
|
+ toast.success('Schema 已删除');
|
|
|
fetchSchemas();
|
|
fetchSchemas();
|
|
|
if (editingId === id) {
|
|
if (editingId === id) {
|
|
|
- setEditingId(null);
|
|
|
|
|
- setFormData({ name: '', description: '', schema_json: '{}' });
|
|
|
|
|
- setActiveTab('list'); // 删除后切回列表
|
|
|
|
|
|
|
+ handleCreateNew();
|
|
|
}
|
|
}
|
|
|
- } catch (e) { alert("删除失败"); }
|
|
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ toast.error('删除失败');
|
|
|
|
|
+ }
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+ // 解析当前编辑中的 Schema 用于实时渲染
|
|
|
|
|
+ const parsedPreview = useMemo(() => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const obj = JSON.parse(formData.schema_json);
|
|
|
|
|
+ return {
|
|
|
|
|
+ valid: true,
|
|
|
|
|
+ data: obj,
|
|
|
|
|
+ properties: obj.properties || {},
|
|
|
|
|
+ required: Array.isArray(obj.required) ? obj.required : [],
|
|
|
|
|
+ title: obj.title || formData.name || '动态表单实时预览',
|
|
|
|
|
+ description: obj.description || formData.description,
|
|
|
|
|
+ };
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ return {
|
|
|
|
|
+ valid: false,
|
|
|
|
|
+ error: (e as Error).message,
|
|
|
|
|
+ properties: {},
|
|
|
|
|
+ required: [],
|
|
|
|
|
+ title: '',
|
|
|
|
|
+ description: '',
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+ }, [formData.schema_json, formData.name, formData.description]);
|
|
|
|
|
+
|
|
|
|
|
+ // 过滤 Schema 列表
|
|
|
|
|
+ const filteredSchemas = useMemo(() => {
|
|
|
|
|
+ if (!searchKey.trim()) return schemas;
|
|
|
|
|
+ const lower = searchKey.trim().toLowerCase();
|
|
|
|
|
+ return schemas.filter(
|
|
|
|
|
+ (s) =>
|
|
|
|
|
+ s.name.toLowerCase().includes(lower) ||
|
|
|
|
|
+ (s.description || '').toLowerCase().includes(lower) ||
|
|
|
|
|
+ String(s.id).includes(lower)
|
|
|
|
|
+ );
|
|
|
|
|
+ }, [schemas, searchKey]);
|
|
|
|
|
+
|
|
|
if (!isOpen) return null;
|
|
if (!isOpen) return null;
|
|
|
|
|
|
|
|
return (
|
|
return (
|
|
|
- <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm p-0 md:p-4">
|
|
|
|
|
- {/*
|
|
|
|
|
- 容器:
|
|
|
|
|
- - 移动端:全屏 (w-full h-full),无圆角
|
|
|
|
|
- - 桌面端:固定大小 (max-w-4xl h-[85vh]),有圆角
|
|
|
|
|
- */}
|
|
|
|
|
- <div className="bg-white w-full h-full md:w-full md:max-w-4xl md:h-[85vh] md:rounded-xl shadow-2xl flex flex-col overflow-hidden animate-in zoom-in duration-200">
|
|
|
|
|
|
|
+ <div className="fixed inset-0 z-50 flex items-center justify-center bg-slate-900/60 backdrop-blur-sm p-0 md:p-4 animate-in fade-in duration-200">
|
|
|
|
|
+ <div className="bg-white w-full h-full md:w-full md:max-w-6xl md:h-[90vh] md:rounded-2xl shadow-2xl flex flex-col overflow-hidden border border-slate-200">
|
|
|
|
|
|
|
|
{/* Header */}
|
|
{/* Header */}
|
|
|
- <div className="px-4 py-3 md:px-6 md:py-4 border-b flex justify-between items-center bg-slate-50 flex-shrink-0">
|
|
|
|
|
- <h3 className="font-bold text-gray-900 text-lg flex items-center gap-2">
|
|
|
|
|
- <FileJson className="text-purple-600" /> 表单 Schema 管理
|
|
|
|
|
- </h3>
|
|
|
|
|
- <button onClick={onClose} className="text-gray-400 hover:text-gray-600 p-1">
|
|
|
|
|
- <X size={24} />
|
|
|
|
|
- </button>
|
|
|
|
|
|
|
+ <div className="px-5 py-3.5 border-b border-slate-200 flex justify-between items-center bg-slate-50 flex-shrink-0">
|
|
|
|
|
+ <div className="flex items-center gap-3">
|
|
|
|
|
+ <div className="p-2 bg-purple-50 text-purple-600 rounded-xl border border-purple-100 hidden sm:block">
|
|
|
|
|
+ <FileJson size={22} />
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <div>
|
|
|
|
|
+ <div className="flex items-center gap-2">
|
|
|
|
|
+ <h3 className="font-extrabold text-slate-900 text-base md:text-lg">
|
|
|
|
|
+ 表单 Schema 定义中心
|
|
|
|
|
+ </h3>
|
|
|
|
|
+ <span className="px-2 py-0.5 rounded-full text-xs font-semibold bg-purple-100 text-purple-800">
|
|
|
|
|
+ JSON Schema IDE
|
|
|
|
|
+ </span>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <p className="text-xs text-slate-500">
|
|
|
|
|
+ 定义前台用户下单时的动态字段采集规范与表单交互逻辑
|
|
|
|
|
+ </p>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ <div className="flex items-center gap-2">
|
|
|
|
|
+ <button
|
|
|
|
|
+ onClick={onClose}
|
|
|
|
|
+ className="p-1.5 text-slate-400 hover:text-slate-700 rounded-lg hover:bg-slate-200 transition"
|
|
|
|
|
+ >
|
|
|
|
|
+ <X size={20} />
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </div>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
- {/* Mobile Tabs (仅移动端显示) */}
|
|
|
|
|
- <div className="flex md:hidden border-b border-gray-200 flex-shrink-0">
|
|
|
|
|
- <button
|
|
|
|
|
- onClick={() => setActiveTab('list')}
|
|
|
|
|
- className={`flex-1 py-3 text-sm font-medium flex items-center justify-center gap-2 ${
|
|
|
|
|
- activeTab === 'list' ? 'text-blue-600 border-b-2 border-blue-600 bg-blue-50/50' : 'text-gray-500'
|
|
|
|
|
|
|
+ {/* Mobile Tabs */}
|
|
|
|
|
+ <div className="flex md:hidden border-b border-slate-200 flex-shrink-0 bg-slate-50 text-xs font-bold">
|
|
|
|
|
+ <button
|
|
|
|
|
+ onClick={() => setMobileTab('list')}
|
|
|
|
|
+ className={`flex-1 py-2.5 flex items-center justify-center gap-1.5 ${
|
|
|
|
|
+ mobileTab === 'list'
|
|
|
|
|
+ ? 'text-purple-700 border-b-2 border-purple-600 bg-white'
|
|
|
|
|
+ : 'text-slate-500'
|
|
|
}`}
|
|
}`}
|
|
|
>
|
|
>
|
|
|
- <List size={16} /> 列表
|
|
|
|
|
|
|
+ <Layers size={14} /> 模板列表 ({schemas.length})
|
|
|
</button>
|
|
</button>
|
|
|
- <button
|
|
|
|
|
- onClick={() => setActiveTab('editor')}
|
|
|
|
|
- className={`flex-1 py-3 text-sm font-medium flex items-center justify-center gap-2 ${
|
|
|
|
|
- activeTab === 'editor' ? 'text-blue-600 border-b-2 border-blue-600 bg-blue-50/50' : 'text-gray-500'
|
|
|
|
|
|
|
+ <button
|
|
|
|
|
+ onClick={() => setMobileTab('editor')}
|
|
|
|
|
+ className={`flex-1 py-2.5 flex items-center justify-center gap-1.5 ${
|
|
|
|
|
+ mobileTab === 'editor'
|
|
|
|
|
+ ? 'text-purple-700 border-b-2 border-purple-600 bg-white'
|
|
|
|
|
+ : 'text-slate-500'
|
|
|
}`}
|
|
}`}
|
|
|
>
|
|
>
|
|
|
- <Edit3 size={16} /> {editingId ? '编辑' : '新建'}
|
|
|
|
|
|
|
+ <Code2 size={14} /> {editingId ? `编辑 #${editingId}` : '新建 Schema'}
|
|
|
</button>
|
|
</button>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
{/* Content Body */}
|
|
{/* Content Body */}
|
|
|
- <div className="flex flex-1 overflow-hidden relative">
|
|
|
|
|
|
|
+ <div className="flex flex-1 overflow-hidden">
|
|
|
|
|
|
|
|
- {/*
|
|
|
|
|
- 左侧:列表
|
|
|
|
|
- - 移动端:根据 activeTab === 'list' 显示/隐藏
|
|
|
|
|
- - 桌面端:md:flex md:w-1/3
|
|
|
|
|
- */}
|
|
|
|
|
- <div className={`
|
|
|
|
|
- flex-col bg-slate-50 border-r border-slate-200 overflow-y-auto p-4
|
|
|
|
|
- ${activeTab === 'list' ? 'flex w-full' : 'hidden'}
|
|
|
|
|
- md:flex md:w-1/3
|
|
|
|
|
- `}>
|
|
|
|
|
- <button
|
|
|
|
|
|
|
+ {/* ======================= */}
|
|
|
|
|
+ {/* 左侧:Schema 列表检索栏 */}
|
|
|
|
|
+ {/* ======================= */}
|
|
|
|
|
+ <div
|
|
|
|
|
+ className={`flex-col bg-slate-50/80 border-r border-slate-200 overflow-y-auto p-4 ${
|
|
|
|
|
+ mobileTab === 'list' ? 'flex w-full' : 'hidden'
|
|
|
|
|
+ } md:flex md:w-80 lg:w-96 flex-shrink-0`}
|
|
|
|
|
+ >
|
|
|
|
|
+ {/* 新建按钮 */}
|
|
|
|
|
+ <button
|
|
|
onClick={handleCreateNew}
|
|
onClick={handleCreateNew}
|
|
|
- className="w-full mb-4 py-2.5 border-2 border-dashed border-blue-300 text-blue-600 rounded-lg hover:bg-blue-50 flex items-center justify-center gap-2 font-bold text-sm transition"
|
|
|
|
|
|
|
+ className="w-full mb-3.5 py-2.5 px-4 bg-purple-600 hover:bg-purple-700 text-white rounded-xl flex items-center justify-center gap-2 font-bold text-xs shadow-sm transition active:scale-98"
|
|
|
>
|
|
>
|
|
|
- <Plus size={16} /> 新建 Schema
|
|
|
|
|
|
|
+ <Plus size={16} /> 新建 Schema 模板
|
|
|
</button>
|
|
</button>
|
|
|
- <div className="space-y-2">
|
|
|
|
|
- {loading && <div className="text-center py-4 text-gray-400"><Loader2 className="animate-spin inline mr-2"/> 加载中...</div>}
|
|
|
|
|
- {!loading && schemas.length === 0 && <div className="text-center py-10 text-gray-400 text-xs">暂无数据</div>}
|
|
|
|
|
-
|
|
|
|
|
- {schemas.map(s => (
|
|
|
|
|
- <div
|
|
|
|
|
- key={s.id}
|
|
|
|
|
- onClick={() => handleEdit(s)}
|
|
|
|
|
- className={`p-3 rounded-lg cursor-pointer border transition ${
|
|
|
|
|
- editingId === s.id
|
|
|
|
|
- ? 'bg-white border-blue-500 shadow-md ring-1 ring-blue-500'
|
|
|
|
|
- : 'bg-white border-slate-200 hover:border-blue-300'
|
|
|
|
|
- }`}
|
|
|
|
|
- >
|
|
|
|
|
- <div className="font-bold text-slate-800 text-sm">{s.name}</div>
|
|
|
|
|
- <div className="text-xs text-slate-500 truncate mt-1">ID: {s.id}</div>
|
|
|
|
|
|
|
+
|
|
|
|
|
+ {/* 搜索框 */}
|
|
|
|
|
+ <div className="relative mb-3">
|
|
|
|
|
+ <input
|
|
|
|
|
+ type="text"
|
|
|
|
|
+ placeholder="搜索 Schema 名称 / ID..."
|
|
|
|
|
+ className="w-full pl-8 pr-3 py-1.5 border border-slate-300 rounded-lg text-xs outline-none focus:ring-2 focus:ring-purple-500 bg-white"
|
|
|
|
|
+ value={searchKey}
|
|
|
|
|
+ onChange={(e) => setSearchKey(e.target.value)}
|
|
|
|
|
+ />
|
|
|
|
|
+ <Search size={14} className="absolute left-2.5 top-2 text-slate-400" />
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ {/* 列表流 */}
|
|
|
|
|
+ <div className="space-y-2 flex-1 overflow-y-auto pr-0.5">
|
|
|
|
|
+ {loading ? (
|
|
|
|
|
+ <div className="py-12 text-center text-xs text-slate-400">
|
|
|
|
|
+ <div className="inline-block animate-spin rounded-full h-5 w-5 border-2 border-slate-300 border-t-purple-600 mb-2" />
|
|
|
|
|
+ <div>加载 Schema 列表中...</div>
|
|
|
</div>
|
|
</div>
|
|
|
- ))}
|
|
|
|
|
|
|
+ ) : filteredSchemas.length > 0 ? (
|
|
|
|
|
+ filteredSchemas.map((s) => {
|
|
|
|
|
+ const isSelected = editingId === s.id;
|
|
|
|
|
+ return (
|
|
|
|
|
+ <div
|
|
|
|
|
+ key={s.id}
|
|
|
|
|
+ onClick={() => handleSelectSchema(s)}
|
|
|
|
|
+ className={`p-3 rounded-xl border transition cursor-pointer text-xs relative ${
|
|
|
|
|
+ isSelected
|
|
|
|
|
+ ? 'bg-purple-50 border-purple-300 ring-1 ring-purple-300 shadow-sm'
|
|
|
|
|
+ : 'bg-white border-slate-200 hover:border-slate-300 hover:bg-slate-50'
|
|
|
|
|
+ }`}
|
|
|
|
|
+ >
|
|
|
|
|
+ <div className="flex items-start justify-between gap-2">
|
|
|
|
|
+ <span className="font-bold text-slate-900 line-clamp-1">{s.name}</span>
|
|
|
|
|
+ <span className="font-mono text-[10px] text-purple-700 bg-purple-100/80 px-1.5 py-0.2 rounded shrink-0">
|
|
|
|
|
+ #{s.id}
|
|
|
|
|
+ </span>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ {s.description && (
|
|
|
|
|
+ <p className="text-slate-500 text-[11px] line-clamp-1 mt-1">
|
|
|
|
|
+ {s.description}
|
|
|
|
|
+ </p>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ );
|
|
|
|
|
+ })
|
|
|
|
|
+ ) : (
|
|
|
|
|
+ <div className="py-8 text-center text-xs text-slate-400 bg-white rounded-lg border border-dashed border-slate-200">
|
|
|
|
|
+ 暂无匹配的 Schema
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
</div>
|
|
</div>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
- {/*
|
|
|
|
|
- 右侧:编辑区
|
|
|
|
|
- - 移动端:根据 activeTab === 'editor' 显示/隐藏
|
|
|
|
|
- - 桌面端:md:flex md:w-2/3
|
|
|
|
|
- */}
|
|
|
|
|
- <div className={`
|
|
|
|
|
- flex-col p-4 md:p-6 overflow-y-auto w-full
|
|
|
|
|
- ${activeTab === 'editor' ? 'flex' : 'hidden'}
|
|
|
|
|
- md:flex md:w-2/3
|
|
|
|
|
- `}>
|
|
|
|
|
- <h4 className="text-lg font-bold mb-4 border-b pb-2 flex justify-between items-center">
|
|
|
|
|
- <span>{editingId ? `编辑 Schema #${editingId}` : '创建新 Schema'}</span>
|
|
|
|
|
- {editingId && (
|
|
|
|
|
- <button onClick={() => handleDelete(editingId)} className="text-red-500 hover:text-red-700 text-xs flex items-center gap-1 md:hidden">
|
|
|
|
|
- <Trash2 size={14} /> 删除
|
|
|
|
|
- </button>
|
|
|
|
|
- )}
|
|
|
|
|
- </h4>
|
|
|
|
|
-
|
|
|
|
|
- <div className="space-y-4">
|
|
|
|
|
- <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
|
|
|
|
|
|
+ {/* ======================= */}
|
|
|
|
|
+ {/* 右侧:工作区 (表单 + 编辑器 + 实时预览) */}
|
|
|
|
|
+ {/* ======================= */}
|
|
|
|
|
+ <div
|
|
|
|
|
+ className={`flex-1 flex-col overflow-y-auto bg-white ${
|
|
|
|
|
+ mobileTab === 'editor' ? 'flex' : 'hidden'
|
|
|
|
|
+ } md:flex`}
|
|
|
|
|
+ >
|
|
|
|
|
+ {/* 工作区顶部属性区 */}
|
|
|
|
|
+ <div className="p-4 bg-slate-50/60 border-b border-slate-200 space-y-3 flex-shrink-0">
|
|
|
|
|
+ <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3">
|
|
|
|
|
+ <div className="flex items-center gap-2">
|
|
|
|
|
+ <span className="font-mono text-xs font-bold text-purple-700 bg-purple-100 px-2 py-0.5 rounded">
|
|
|
|
|
+ {editingId ? `编辑中 #${editingId}` : '创建新 Schema'}
|
|
|
|
|
+ </span>
|
|
|
|
|
+ <span className="text-xs text-slate-400 font-medium">|</span>
|
|
|
|
|
+ <span className="text-xs text-slate-500">
|
|
|
|
|
+ 字段数: <strong className="text-purple-700">{Object.keys(parsedPreview.properties).length}</strong>
|
|
|
|
|
+ </span>
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ {/* 视图切换按钮 & 操作 */}
|
|
|
|
|
+ <div className="flex items-center gap-2">
|
|
|
|
|
+ {/* 桌面端视图切换 */}
|
|
|
|
|
+ <div className="hidden lg:flex bg-slate-200/80 p-0.5 rounded-lg text-xs font-semibold text-slate-600">
|
|
|
|
|
+ <button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ onClick={() => setViewMode('split')}
|
|
|
|
|
+ className={`px-2.5 py-1 rounded-md flex items-center gap-1 transition ${
|
|
|
|
|
+ viewMode === 'split' ? 'bg-white text-purple-700 shadow-sm' : 'hover:text-slate-900'
|
|
|
|
|
+ }`}
|
|
|
|
|
+ >
|
|
|
|
|
+ <Columns size={13} /> 双栏对照
|
|
|
|
|
+ </button>
|
|
|
|
|
+ <button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ onClick={() => setViewMode('code')}
|
|
|
|
|
+ className={`px-2.5 py-1 rounded-md flex items-center gap-1 transition ${
|
|
|
|
|
+ viewMode === 'code' ? 'bg-white text-purple-700 shadow-sm' : 'hover:text-slate-900'
|
|
|
|
|
+ }`}
|
|
|
|
|
+ >
|
|
|
|
|
+ <Code2 size={13} /> 仅代码
|
|
|
|
|
+ </button>
|
|
|
|
|
+ <button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ onClick={() => setViewMode('preview')}
|
|
|
|
|
+ className={`px-2.5 py-1 rounded-md flex items-center gap-1 transition ${
|
|
|
|
|
+ viewMode === 'preview' ? 'bg-white text-purple-700 shadow-sm' : 'hover:text-slate-900'
|
|
|
|
|
+ }`}
|
|
|
|
|
+ >
|
|
|
|
|
+ <Eye size={13} /> 仅表单预览
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ {editingId && (
|
|
|
|
|
+ <button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ onClick={() => handleDelete(editingId)}
|
|
|
|
|
+ className="px-2.5 py-1.5 text-rose-600 hover:bg-rose-50 border border-rose-200 rounded-lg text-xs font-semibold transition flex items-center gap-1"
|
|
|
|
|
+ >
|
|
|
|
|
+ <Trash2 size={13} /> 删除
|
|
|
|
|
+ </button>
|
|
|
|
|
+ )}
|
|
|
|
|
+
|
|
|
|
|
+ <button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ onClick={handleSave}
|
|
|
|
|
+ disabled={saving}
|
|
|
|
|
+ className="px-4 py-1.5 bg-purple-600 hover:bg-purple-700 text-white rounded-lg text-xs font-bold shadow-sm transition flex items-center gap-1.5 disabled:opacity-50"
|
|
|
|
|
+ >
|
|
|
|
|
+ {saving ? <Loader2 size={14} className="animate-spin" /> : <Save size={14} />}
|
|
|
|
|
+ 保存 Schema
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ {/* 基础信息输入 */}
|
|
|
|
|
+ <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
|
|
<div>
|
|
<div>
|
|
|
- <label className="block text-sm font-medium mb-1 text-slate-700">名称 (Name) <span className="text-red-500">*</span></label>
|
|
|
|
|
- <input
|
|
|
|
|
- type="text"
|
|
|
|
|
- className="w-full border border-slate-300 rounded-lg p-2.5 text-sm focus:ring-2 focus:ring-blue-500 outline-none"
|
|
|
|
|
- value={formData.name}
|
|
|
|
|
- onChange={e => setFormData({...formData, name: e.target.value})}
|
|
|
|
|
- placeholder="e.g. Japan Visa Form"
|
|
|
|
|
|
|
+ <label className="block text-[11px] font-bold text-slate-500 uppercase mb-1">
|
|
|
|
|
+ Schema 名称 <span className="text-red-500">*</span>
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <input
|
|
|
|
|
+ type="text"
|
|
|
|
|
+ required
|
|
|
|
|
+ placeholder="如:法国申根签证采集表单"
|
|
|
|
|
+ className="w-full border border-slate-300 rounded-lg px-3 py-1.5 text-xs text-slate-800 focus:ring-2 focus:ring-purple-500 outline-none bg-white"
|
|
|
|
|
+ value={formData.name}
|
|
|
|
|
+ onChange={(e) => setFormData({ ...formData, name: e.target.value })}
|
|
|
/>
|
|
/>
|
|
|
</div>
|
|
</div>
|
|
|
<div>
|
|
<div>
|
|
|
- <label className="block text-sm font-medium mb-1 text-slate-700">描述 (Desc)</label>
|
|
|
|
|
- <input
|
|
|
|
|
- type="text"
|
|
|
|
|
- className="w-full border border-slate-300 rounded-lg p-2.5 text-sm focus:ring-2 focus:ring-blue-500 outline-none"
|
|
|
|
|
- value={formData.description}
|
|
|
|
|
- onChange={e => setFormData({...formData, description: e.target.value})}
|
|
|
|
|
- placeholder="简短描述用途"
|
|
|
|
|
|
|
+ <label className="block text-[11px] font-bold text-slate-500 uppercase mb-1">
|
|
|
|
|
+ 表单功能描述
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <input
|
|
|
|
|
+ type="text"
|
|
|
|
|
+ placeholder="简述该 Schema 的适用场景或所需材料..."
|
|
|
|
|
+ className="w-full border border-slate-300 rounded-lg px-3 py-1.5 text-xs text-slate-800 focus:ring-2 focus:ring-purple-500 outline-none bg-white"
|
|
|
|
|
+ value={formData.description}
|
|
|
|
|
+ onChange={(e) => setFormData({ ...formData, description: e.target.value })}
|
|
|
/>
|
|
/>
|
|
|
</div>
|
|
</div>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
- {/* JSON Editor */}
|
|
|
|
|
- <div className="flex-1 min-h-[300px]">
|
|
|
|
|
- <JsonEditor
|
|
|
|
|
- label="Schema JSON (定义动态表单结构)"
|
|
|
|
|
- value={formData.schema_json}
|
|
|
|
|
- onChange={(val) => setFormData({...formData, schema_json: val})}
|
|
|
|
|
- height="h-[400px] md:h-80" // 移动端稍微高一点
|
|
|
|
|
- placeholder='{"type": "object", ...}'
|
|
|
|
|
- />
|
|
|
|
|
|
|
+ {/* 快速预设模板注入器 */}
|
|
|
|
|
+ <div className="pt-2 border-t border-slate-200/80 flex flex-wrap items-center gap-2">
|
|
|
|
|
+ <span className="text-[11px] text-slate-400 font-bold flex items-center gap-1">
|
|
|
|
|
+ <Sparkles size={12} className="text-amber-500" /> 快速注入模板:
|
|
|
|
|
+ </span>
|
|
|
|
|
+ {SCHEMA_TEMPLATES.map((tmpl, idx) => (
|
|
|
|
|
+ <button
|
|
|
|
|
+ key={idx}
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ onClick={() => handleApplyTemplate(tmpl)}
|
|
|
|
|
+ className="px-2.5 py-1 bg-white hover:bg-purple-50 text-slate-700 hover:text-purple-700 border border-slate-200 hover:border-purple-200 rounded-md text-[11px] font-medium transition"
|
|
|
|
|
+ >
|
|
|
|
|
+ {tmpl.name}
|
|
|
|
|
+ </button>
|
|
|
|
|
+ ))}
|
|
|
</div>
|
|
</div>
|
|
|
|
|
+ </div>
|
|
|
|
|
|
|
|
- <div className="flex justify-end gap-3 pt-4 border-t mt-auto">
|
|
|
|
|
- {editingId && (
|
|
|
|
|
- <button onClick={() => handleDelete(editingId)} className="text-red-600 px-4 text-sm hover:underline hidden md:block">删除</button>
|
|
|
|
|
- )}
|
|
|
|
|
- <button onClick={handleSave} className="bg-blue-600 text-white px-6 py-2.5 rounded-lg font-bold shadow hover:bg-blue-700 flex items-center gap-2 transition w-full md:w-auto justify-center">
|
|
|
|
|
- <Save size={18} /> 保存配置
|
|
|
|
|
- </button>
|
|
|
|
|
- </div>
|
|
|
|
|
|
|
+ {/* 编辑与预览主体区 */}
|
|
|
|
|
+ <div className="flex-1 flex overflow-hidden min-h-[350px]">
|
|
|
|
|
+
|
|
|
|
|
+ {/* CODE 编辑器 (Monaco) */}
|
|
|
|
|
+ {(viewMode === 'split' || viewMode === 'code') && (
|
|
|
|
|
+ <div
|
|
|
|
|
+ className={`flex flex-col border-r border-slate-200 ${
|
|
|
|
|
+ viewMode === 'split' ? 'w-full lg:w-1/2' : 'w-full'
|
|
|
|
|
+ }`}
|
|
|
|
|
+ >
|
|
|
|
|
+ {/* Monaco 工具栏 */}
|
|
|
|
|
+ <div className="px-3 py-1.5 bg-slate-900 text-slate-400 text-xs flex items-center justify-between border-b border-slate-800">
|
|
|
|
|
+ <span className="font-mono text-[11px] text-purple-300 flex items-center gap-1.5">
|
|
|
|
|
+ <Code2 size={13} /> JSON Schema Source
|
|
|
|
|
+ </span>
|
|
|
|
|
+ <div className="flex items-center gap-1.5">
|
|
|
|
|
+ <button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ onClick={handleFormatJson}
|
|
|
|
|
+ className="px-2 py-0.5 rounded bg-slate-800 hover:bg-slate-700 text-slate-300 text-[11px] flex items-center gap-1 font-medium transition"
|
|
|
|
|
+ >
|
|
|
|
|
+ <Braces size={12} /> 格式化
|
|
|
|
|
+ </button>
|
|
|
|
|
+ <button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ onClick={handleCompactJson}
|
|
|
|
|
+ className="px-2 py-0.5 rounded bg-slate-800 hover:bg-slate-700 text-slate-300 text-[11px] flex items-center gap-1 font-medium transition"
|
|
|
|
|
+ >
|
|
|
|
|
+ <Minimize2 size={12} /> 压缩
|
|
|
|
|
+ </button>
|
|
|
|
|
+ <button
|
|
|
|
|
+ type="button"
|
|
|
|
|
+ onClick={handleCopyJson}
|
|
|
|
|
+ className="px-2 py-0.5 rounded bg-slate-800 hover:bg-slate-700 text-slate-300 text-[11px] flex items-center gap-1 font-medium transition"
|
|
|
|
|
+ >
|
|
|
|
|
+ {copied ? <Check size={12} className="text-emerald-400" /> : <Copy size={12} />} 复制
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ <div className="flex-1 min-h-[300px]">
|
|
|
|
|
+ <Editor
|
|
|
|
|
+ height="100%"
|
|
|
|
|
+ language="json"
|
|
|
|
|
+ theme="vs-dark"
|
|
|
|
|
+ value={formData.schema_json}
|
|
|
|
|
+ onChange={(val) => setFormData((prev) => ({ ...prev, schema_json: val || '' }))}
|
|
|
|
|
+ options={{
|
|
|
|
|
+ minimap: { enabled: false },
|
|
|
|
|
+ fontSize: 12,
|
|
|
|
|
+ wordWrap: 'on',
|
|
|
|
|
+ scrollBeyondLastLine: false,
|
|
|
|
|
+ automaticLayout: true,
|
|
|
|
|
+ tabSize: 2,
|
|
|
|
|
+ }}
|
|
|
|
|
+ />
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+
|
|
|
|
|
+ {/* LIVE FORM 实时表单渲染预览 */}
|
|
|
|
|
+ {(viewMode === 'split' || viewMode === 'preview') && (
|
|
|
|
|
+ <div
|
|
|
|
|
+ className={`flex flex-col bg-slate-50/50 overflow-y-auto ${
|
|
|
|
|
+ viewMode === 'split' ? 'hidden lg:flex lg:w-1/2' : 'w-full'
|
|
|
|
|
+ }`}
|
|
|
|
|
+ >
|
|
|
|
|
+ <div className="px-4 py-2 bg-slate-100/80 border-b border-slate-200 flex items-center justify-between text-xs text-slate-600 font-bold">
|
|
|
|
|
+ <span className="flex items-center gap-1.5 text-indigo-700">
|
|
|
|
|
+ <Eye size={14} /> 交互式表单渲染预览 (所见即所得)
|
|
|
|
|
+ </span>
|
|
|
|
|
+ <span className="text-[11px] font-normal text-slate-400">
|
|
|
|
|
+ 模拟前台下单填写效果
|
|
|
|
|
+ </span>
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ <div className="p-5 overflow-y-auto space-y-4">
|
|
|
|
|
+ {parsedPreview.valid ? (
|
|
|
|
|
+ <div className="bg-white p-5 rounded-xl border border-slate-200 shadow-sm space-y-4">
|
|
|
|
|
+ <div className="border-b border-slate-100 pb-3">
|
|
|
|
|
+ <h4 className="font-bold text-slate-900 text-sm">
|
|
|
|
|
+ {parsedPreview.title}
|
|
|
|
|
+ </h4>
|
|
|
|
|
+ {parsedPreview.description && (
|
|
|
|
|
+ <p className="text-xs text-slate-500 mt-0.5">
|
|
|
|
|
+ {parsedPreview.description}
|
|
|
|
|
+ </p>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ {Object.keys(parsedPreview.properties).length > 0 ? (
|
|
|
|
|
+ <div className="space-y-4">
|
|
|
|
|
+ {Object.entries(parsedPreview.properties).map(([fieldKey, prop]: [string, any]) => {
|
|
|
|
|
+ const isRequired = parsedPreview.required.includes(fieldKey);
|
|
|
|
|
+ const fieldTitle = prop.title || fieldKey;
|
|
|
|
|
+ const placeholder = prop.placeholder || `请输入${fieldTitle}`;
|
|
|
|
|
+
|
|
|
|
|
+ return (
|
|
|
|
|
+ <div key={fieldKey} className="space-y-1">
|
|
|
|
|
+ <label className="block text-xs font-bold text-slate-700">
|
|
|
|
|
+ {fieldTitle}{' '}
|
|
|
|
|
+ {isRequired && <span className="text-rose-500">*</span>}
|
|
|
|
|
+ <span className="ml-1.5 font-mono text-[10px] font-normal text-slate-400">
|
|
|
|
|
+ ({fieldKey})
|
|
|
|
|
+ </span>
|
|
|
|
|
+ </label>
|
|
|
|
|
+
|
|
|
|
|
+ {/* 下拉选择 */}
|
|
|
|
|
+ {prop.enum && Array.isArray(prop.enum) ? (
|
|
|
|
|
+ <select className="w-full border border-slate-300 rounded-lg px-3 py-2 text-xs bg-white text-slate-700 outline-none focus:ring-2 focus:ring-indigo-500 shadow-sm">
|
|
|
|
|
+ <option value="">-- 请选择 {fieldTitle} --</option>
|
|
|
|
|
+ {prop.enum.map((opt: string) => (
|
|
|
|
|
+ <option key={opt} value={opt}>
|
|
|
|
|
+ {opt}
|
|
|
|
|
+ </option>
|
|
|
|
|
+ ))}
|
|
|
|
|
+ </select>
|
|
|
|
|
+ ) : prop.format === 'date' ? (
|
|
|
|
|
+ /* 日期选择器 */
|
|
|
|
|
+ <input
|
|
|
|
|
+ type="date"
|
|
|
|
|
+ className="w-full border border-slate-300 rounded-lg px-3 py-2 text-xs bg-white text-slate-700 outline-none focus:ring-2 focus:ring-indigo-500 shadow-sm"
|
|
|
|
|
+ />
|
|
|
|
|
+ ) : prop.type === 'number' || prop.type === 'integer' ? (
|
|
|
|
|
+ /* 数字输入框 */
|
|
|
|
|
+ <input
|
|
|
|
|
+ type="number"
|
|
|
|
|
+ placeholder={placeholder}
|
|
|
|
|
+ className="w-full border border-slate-300 rounded-lg px-3 py-2 text-xs bg-white text-slate-700 outline-none focus:ring-2 focus:ring-indigo-500 shadow-sm"
|
|
|
|
|
+ />
|
|
|
|
|
+ ) : (
|
|
|
|
|
+ /* 标准文本输入框 */
|
|
|
|
|
+ <input
|
|
|
|
|
+ type="text"
|
|
|
|
|
+ placeholder={placeholder}
|
|
|
|
|
+ className="w-full border border-slate-300 rounded-lg px-3 py-2 text-xs bg-white text-slate-700 outline-none focus:ring-2 focus:ring-indigo-500 shadow-sm"
|
|
|
|
|
+ />
|
|
|
|
|
+ )}
|
|
|
|
|
+
|
|
|
|
|
+ {prop.description && (
|
|
|
|
|
+ <p className="text-[11px] text-slate-400 mt-0.5">
|
|
|
|
|
+ {prop.description}
|
|
|
|
|
+ </p>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ );
|
|
|
|
|
+ })}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ ) : (
|
|
|
|
|
+ <div className="py-8 text-center text-xs text-slate-400 bg-slate-50 rounded-lg border border-dashed border-slate-200">
|
|
|
|
|
+ Schema 中未定义任何 properties 字段
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ ) : (
|
|
|
|
|
+ <div className="p-4 bg-rose-50 border border-rose-200 rounded-xl text-xs text-rose-700 space-y-1">
|
|
|
|
|
+ <div className="font-bold flex items-center gap-1">
|
|
|
|
|
+ <AlertCircle size={14} /> JSON 解析错误
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <p className="font-mono text-[11px]">{parsedPreview.error}</p>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
</div>
|
|
</div>
|
|
|
|
|
+
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
</div>
|
|
</div>
|
|
|
|
|
+
|
|
|
</div>
|
|
</div>
|
|
|
</div>
|
|
</div>
|
|
|
);
|
|
);
|