Skip to content

Commit 49685da

Browse files
committed
refactor(json-editor): 优化键值对添加逻辑及布局调整
- 移除自动生成唯一键名逻辑,新增键值对时默认空键名 - 调整键输入框和值输入框的栅格宽度,提升编辑界面布局 - 优化整体视觉呈现,使键和值输入区域更合理分布 feat(channels): 添加模型映射解析及反向映射显示功能 - 新增解析模型映射配置的函数,安全处理JSON解析错误 - 实现反向映射创建,将映射值对应回键名,用于显示 - 在模型选项中优先显示映射后的名称,原名作为提示 - 依赖列表新增对模型映射配置的监听,确保更新及时
1 parent a3c2b28 commit 49685da

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

web/src/components/common/ui/JSONEditor.jsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,9 @@ const JSONEditor = ({
247247
// 添加键值对
248248
const addKeyValue = useCallback(() => {
249249
const newPairs = [...keyValuePairs];
250-
const existingKeys = newPairs.map(p => p.key);
251-
let counter = 1;
252-
let newKey = `field_${counter}`;
253-
while (existingKeys.includes(newKey)) {
254-
counter += 1;
255-
newKey = `field_${counter}`;
256-
}
257250
newPairs.push({
258251
id: generateUniqueId(),
259-
key: newKey,
252+
key: '',
260253
value: ''
261254
});
262255
handleVisualChange(newPairs);
@@ -408,7 +401,7 @@ const JSONEditor = ({
408401

409402
return (
410403
<Row key={pair.id} gutter={8} align="middle">
411-
<Col span={6}>
404+
<Col span={12}>
412405
<div className="relative">
413406
<Input
414407
placeholder={t('键名')}
@@ -435,7 +428,7 @@ const JSONEditor = ({
435428
)}
436429
</div>
437430
</Col>
438-
<Col span={16}>
431+
<Col span={10}>
439432
{renderValueInput(pair.id, pair.value)}
440433
</Col>
441434
<Col span={2}>

web/src/components/table/channels/modals/EditChannelModal.jsx

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,28 @@ const EditChannelModal = (props) => {
500500
}
501501
};
502502

503+
// 解析模型映射配置并创建反向映射
504+
const parseModelMapping = (mappingStr) => {
505+
if (!mappingStr || mappingStr.trim() === '') return {};
506+
try {
507+
return JSON.parse(mappingStr);
508+
} catch (error) {
509+
console.warn('Failed to parse model mapping:', error);
510+
return {};
511+
}
512+
};
513+
514+
// 根据模型映射创建反向映射 (值 -> 键)
515+
const createReverseMapping = (mapping) => {
516+
const reverseMap = {};
517+
for (const [key, value] of Object.entries(mapping)) {
518+
if (typeof value === 'string' && value.trim() !== '') {
519+
reverseMap[value] = key;
520+
}
521+
}
522+
return reverseMap;
523+
};
524+
503525
useEffect(() => {
504526
const modelMap = new Map();
505527

@@ -521,29 +543,39 @@ const EditChannelModal = (props) => {
521543
}
522544
});
523545

546+
// 解析模型映射配置
547+
const modelMapping = parseModelMapping(inputs.model_mapping);
548+
const reverseMapping = createReverseMapping(modelMapping);
549+
524550
const categories = getModelCategories(t);
525551
const optionsWithIcon = Array.from(modelMap.values()).map((opt) => {
526-
const modelName = opt.value;
552+
const originalModelName = opt.value;
553+
// 如果存在反向映射,使用映射后的名称作为显示名称
554+
const displayModelName = reverseMapping[originalModelName] || originalModelName;
555+
527556
let icon = null;
528557
for (const [key, category] of Object.entries(categories)) {
529-
if (key !== 'all' && category.filter({ model_name: modelName })) {
558+
if (key !== 'all' && category.filter({ model_name: originalModelName })) {
530559
icon = category.icon;
531560
break;
532561
}
533562
}
563+
534564
return {
535565
...opt,
536566
label: (
537567
<span className="flex items-center gap-1">
538568
{icon}
539-
{modelName}
569+
<span title={originalModelName !== displayModelName ? `原始名称: ${originalModelName}` : undefined}>
570+
{displayModelName}
571+
</span>
540572
</span>
541573
),
542574
};
543575
});
544576

545577
setModelOptions(optionsWithIcon);
546-
}, [originModelOptions, inputs.models, t]);
578+
}, [originModelOptions, inputs.models, inputs.model_mapping, t]);
547579

548580
useEffect(() => {
549581
fetchModels().then();

0 commit comments

Comments
 (0)