fix(amis-editor): 优化reGenerateID,支持复制&粘贴场景中事件动作失效问题

This commit is contained in:
wibetter 2023-12-28 16:44:00 +08:00
parent 377db11b7c
commit d7822dd29a

View File

@ -575,7 +575,17 @@ export function JSONDuplicate(
} }
/** /**
* * id
* 使
* 1. componentId需替换成最新的
* 2. &componentId关联的是页面其他组件componentId
*
* 1. idid对应关系(reIds)
* 2. componentId替换成reIds中的新idreIds中不存在对应的idcomponentIdreComptIds中
* 3. reComptIds中是否存在reIds中没有的组件id2reIds种的id记录到resetComptIds
* 4. resetComptIds中的componentId
*
* 1. 使使
* @param json * @param json
*/ */
export function reGenerateID( export function reGenerateID(
@ -583,21 +593,46 @@ export function reGenerateID(
// 有时候复制时因为局部会有事件动作等内容需要改为复制部分的新id这里把老id与新id的关系存下来 // 有时候复制时因为局部会有事件动作等内容需要改为复制部分的新id这里把老id与新id的关系存下来
reIds: {[propKey: string]: string} = {} reIds: {[propKey: string]: string} = {}
) { ) {
const reComptIds: {[propKey: string]: string} = {}; // 记录事件动作中的id
JSONTraverse(json, (value: any, key: string, host: any) => { JSONTraverse(json, (value: any, key: string, host: any) => {
const isNodeIdFormat = const isNodeIdFormat =
typeof value === 'string' && value.indexOf('u:') === 0; typeof value === 'string' && value.indexOf('u:') === 0;
if ((key === 'id' || key === 'componentId') && isNodeIdFormat && host) { if ((key === 'id' || key === 'componentId') && isNodeIdFormat && host) {
if (reIds[value]) { if (reIds[value]) {
host[key] = reIds[value]; host[key] = reIds[value];
} else if (reComptIds[value]) {
host[key] = reComptIds[value];
reIds[value] = reComptIds[value];
} else { } else {
const newID = generateNodeId(); const newID = generateNodeId();
reIds[value] = newID;
host[key] = newID; host[key] = newID;
if (key === 'id') {
reIds[value] = newID;
} else if (key === 'componentId') {
reComptIds[value] = newID;
}
} }
} }
return value; return value;
}); });
const resetComptIds: {[propKey: string]: string} = {};
Object.keys(reComptIds).forEach((uidKey: string) => {
if (!reIds[uidKey]) {
resetComptIds[reComptIds[uidKey]] = uidKey; // 以新id为key
}
});
// 恢复resetComptIds中的componentId避免事件动作失效
JSONTraverse(json, (value: any, key: string, host: any) => {
const isNodeIdFormat =
typeof value === 'string' && value.indexOf('u:') === 0;
if (key === 'componentId' && isNodeIdFormat && resetComptIds[value]) {
host.componentId = resetComptIds[value];
}
return value;
});
return json; return json;
} }