fix(json-schema-editor): getNodeJsonPath 路径修复 - 包装对象父节点增加 .attrs. 前缀
根节点直子之外的 { type, attrs } 包装对象,其子字段路径中缺少 .attrs. 段。
通过给包装节点添加 _isWrapped 标记,让路径生成逻辑正确插入 .attrs. 前缀。
例如 messages.attrs[0].content 改为 messages.attrs[0].attrs.content
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -101,6 +101,7 @@ interface JsonNodeData {
|
||||
primitiveValue: string | number | boolean | null;
|
||||
children: JsonNodeData[];
|
||||
config?: FieldConfig;
|
||||
_isWrapped?: boolean;
|
||||
}
|
||||
|
||||
interface FieldFormData {
|
||||
@@ -160,11 +161,11 @@ function valueToNode(value: any, key: string, keyEditable: boolean): JsonNodeDat
|
||||
const hasCfg = Object.keys(cfg).length > 0;
|
||||
if (jtype === 'object') {
|
||||
const children = rawVal ? Object.entries(rawVal).map(([k, v]) => valueToNode(v, k, true)) : [];
|
||||
return { _id: id, key, keyEditable, type: 'object', primitiveValue: null, children, config: hasCfg ? cfg : undefined };
|
||||
return { _id: id, key, keyEditable, type: 'object', primitiveValue: null, children, config: hasCfg ? cfg : undefined, _isWrapped: true };
|
||||
}
|
||||
if (jtype === 'array') {
|
||||
const children = Array.isArray(rawVal) ? rawVal.map((item, idx) => valueToNode(item, String(idx), false)) : [];
|
||||
return { _id: id, key, keyEditable, type: 'array', primitiveValue: null, children, config: hasCfg ? cfg : undefined };
|
||||
return { _id: id, key, keyEditable, type: 'array', primitiveValue: null, children, config: hasCfg ? cfg : undefined, _isWrapped: true };
|
||||
}
|
||||
return {
|
||||
_id: id, key, keyEditable,
|
||||
@@ -172,6 +173,7 @@ function valueToNode(value: any, key: string, keyEditable: boolean): JsonNodeDat
|
||||
primitiveValue: rawVal ?? getDefaultPrimitive(jtype),
|
||||
children: [],
|
||||
config: hasCfg ? cfg : undefined,
|
||||
_isWrapped: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -265,7 +267,7 @@ function getAvailableFields(nodeId: string): AvailableField[] {
|
||||
return result;
|
||||
}
|
||||
|
||||
/** 计算某个节点在 JSON 中的完整路径(如 messages.attrs[1].content.attrs[1].type) */
|
||||
/** 计算某个节点在 JSON 中的完整路径(如 messages.attrs[0].attrs.content.attrs[0].attrs.type) */
|
||||
function getNodeJsonPath(nodeId: string): string {
|
||||
// 构建祖先链 [root, ..., target]
|
||||
const chain: JsonNodeData[] = [];
|
||||
@@ -277,7 +279,6 @@ function getNodeJsonPath(nodeId: string): string {
|
||||
if (chain.length < 1) return '';
|
||||
|
||||
let path = '';
|
||||
let inAttrs = true;
|
||||
|
||||
for (let i = 1; i < chain.length; i++) {
|
||||
const node = chain[i];
|
||||
@@ -285,16 +286,15 @@ function getNodeJsonPath(nodeId: string): string {
|
||||
|
||||
if (i === 1) {
|
||||
path = node.key;
|
||||
inAttrs = true;
|
||||
} else if (parent.type === 'array') {
|
||||
// 数组父节点:{ type: "array", attrs: [...] } 固定走 .attrs[index]
|
||||
path += `.attrs[${node.key}]`;
|
||||
inAttrs = true;
|
||||
} else if (inAttrs) {
|
||||
path += `.${node.key}`;
|
||||
inAttrs = false;
|
||||
} else {
|
||||
} else if (parent._isWrapped) {
|
||||
// 包装对象父节点(来自 { type, attrs }):字段通过 .attrs. 访问
|
||||
path += `.attrs.${node.key}`;
|
||||
inAttrs = true;
|
||||
} else {
|
||||
// 普通对象父节点:直接 . 访问
|
||||
path += `.${node.key}`;
|
||||
}
|
||||
}
|
||||
return path;
|
||||
|
||||
Reference in New Issue
Block a user