pick: 同步 master 中关于 id 重新计算的逻辑 (#8721)

This commit is contained in:
liaoxuezhi 2023-11-14 10:10:26 +08:00 committed by GitHub
parent 3157498c88
commit 118aaf5da8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -73,13 +73,13 @@ let themeUselessPropKeys: Array<string> = [];
* $$id 便
* @param obj
*/
export function JSONPipeIn(obj: any, generateId = false): any {
export function JSONPipeIn(obj: any, generateId = false, idMap: any = {}): any {
if (!isObject(obj) || obj.$$typeof) {
return obj;
}
if (Array.isArray(obj)) {
return obj.map((item, index) => JSONPipeIn(item, generateId));
return obj.map((item, index) => JSONPipeIn(item, generateId, idMap));
}
let toUpdate: any = {};
@ -122,8 +122,13 @@ export function JSONPipeIn(obj: any, generateId = false): any {
flag = true;
/** 脚手架构建的Schema提前构建好了组件 ID此时无需生成 ID避免破坏事件动作 */
if (!obj.__origin || obj.__origin !== 'scaffold') {
toUpdate.id = generateNodeId();
if (
(!obj.__origin || obj.__origin !== 'scaffold') &&
(typeof obj.id !== 'string' || !obj.id || obj.id.startsWith('u:'))
) {
const newId = generateNodeId();
obj.id && (idMap[obj.id] = newId);
toUpdate.id = newId;
}
}
}
@ -137,7 +142,7 @@ export function JSONPipeIn(obj: any, generateId = false): any {
let flag2 = false;
let patched = prop.map((item: any) => {
let patched = JSONPipeIn(item, generateId);
let patched = JSONPipeIn(item, generateId, idMap);
if (patched !== item) {
flag2 = true;
@ -151,7 +156,14 @@ export function JSONPipeIn(obj: any, generateId = false): any {
toUpdate[key] = patched;
}
} else {
let patched = JSONPipeIn(prop, generateId);
let patched = JSONPipeIn(prop, generateId, idMap);
if (generateId && typeof patched === 'string') {
Object.keys(idMap).forEach(oldId => {
const newId = idMap[oldId];
patched = (patched as string).replaceAll(oldId, newId);
});
}
if (patched !== prop) {
flag = true;