From 45cf1ff60d36ca126b1fa269cf60726343dfc74f Mon Sep 17 00:00:00 2001 From: wibetter <365533093@qq.com> Date: Thu, 5 Jan 2023 10:42:27 +0800 Subject: [PATCH] =?UTF-8?q?fix(amis-saas-2815):=20=E4=BC=98=E5=8C=96addChi?= =?UTF-8?q?ld=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: If016b68cdf7f8d3b7e42170ac5af89b411d0266c --- packages/amis-editor-core/package.json | 2 +- packages/amis-editor-core/src/manager.ts | 10 ++++------ packages/amis-editor-core/src/util.ts | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/packages/amis-editor-core/package.json b/packages/amis-editor-core/package.json index f457f0cc2..1763cfbc8 100644 --- a/packages/amis-editor-core/package.json +++ b/packages/amis-editor-core/package.json @@ -1,6 +1,6 @@ { "name": "amis-editor-core", - "version": "5.2.3-beta.13", + "version": "5.2.3-beta.15", "description": "amis 可视化编辑器", "main": "lib/index.js", "module": "esm/index.js", diff --git a/packages/amis-editor-core/src/manager.ts b/packages/amis-editor-core/src/manager.ts index bc89f9aaf..57a73ef1e 100644 --- a/packages/amis-editor-core/src/manager.ts +++ b/packages/amis-editor-core/src/manager.ts @@ -43,6 +43,7 @@ import { guid, reactionWithOldValue, reGenerateID, + JsonGenerateID, isString, isObject, isLayoutPlugin, @@ -63,7 +64,7 @@ import {EditorDNDManager} from './dnd'; import {IScopedContext} from 'amis'; import {SchemaObject, SchemaCollection} from 'amis/lib/Schema'; import type {RendererConfig} from 'amis-core/lib/factory'; -import {isPlainObject} from 'lodash'; +import isPlainObject from 'lodash/isPlainObject'; export interface EditorManagerConfig extends Omit {} @@ -1370,11 +1371,8 @@ export class EditorManager { let index: number = -1; const commonContext = this.buildEventContext(id); - // 填充id,有些脚手架生成了复杂的布局等,这里都填充一下id - if (isPlainObject(json) && json.type && !json.id) { - json = {...json, id: 'u:' + guid()}; - json = reGenerateID(json); - } + // 填充id,有些脚手架生成了复杂的布局等,自动填充一下id + JsonGenerateID(json); if (beforeId) { const arr = commonContext.schema[region]; diff --git a/packages/amis-editor-core/src/util.ts b/packages/amis-editor-core/src/util.ts index 9141d152f..41e6b3fb0 100644 --- a/packages/amis-editor-core/src/util.ts +++ b/packages/amis-editor-core/src/util.ts @@ -556,6 +556,27 @@ export function reGenerateID( return json; } +// 遍历json,每层元素自动生成uid。 +export function JsonGenerateID(json: any) { + if (Array.isArray(json)) { + json.map((item: any) => JsonGenerateID(item)); + } + if (!isObject(json) || isObservable(json)) { + return; + } + + if (json.type && !json.id) { + json.id = generateNodeId(); + } + + Object.keys(json).forEach(key => { + let curElem = json[key]; + if (isObject(curElem) || Array.isArray(curElem)) { + JsonGenerateID(curElem); + } + }); +} + export function createElementFromHTML(htmlString: string): HTMLElement { var div = document.createElement('div'); // bca-disable-next-line