From 377db11b7c14a736ff9d11406f363954f80ff8fe Mon Sep 17 00:00:00 2001 From: wibetter <365533093@qq.com> Date: Tue, 26 Dec 2023 18:53:17 +0800 Subject: [PATCH] =?UTF-8?q?fix(amis-editor):=20=E4=BC=98=E5=8C=96reGenerat?= =?UTF-8?q?eID=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../amis-editor-core/src/component/Editor.tsx | 4 +-- packages/amis-editor-core/src/manager.ts | 26 ++++++++++++++----- packages/amis-editor-core/src/plugin.ts | 4 +-- packages/amis-editor-core/src/util.ts | 16 ++++++------ 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/packages/amis-editor-core/src/component/Editor.tsx b/packages/amis-editor-core/src/component/Editor.tsx index 37dd3a6b4..222d6d1bb 100644 --- a/packages/amis-editor-core/src/component/Editor.tsx +++ b/packages/amis-editor-core/src/component/Editor.tsx @@ -535,9 +535,9 @@ export default class Editor extends Component { ); if (this.store.activeId === this.curCopySchemaData.$$id) { // 复制和粘贴是同一个元素,则直接追加到当前元素后面 - this.manager.appendSiblingSchema(reGenerateID(curSimpleSchema)); + this.manager.appendSiblingSchema(reGenerateID(curSimpleSchema), false); } else { - this.manager.addElem(reGenerateID(curSimpleSchema)); + this.manager.addElem(reGenerateID(curSimpleSchema), false); } } } diff --git a/packages/amis-editor-core/src/manager.ts b/packages/amis-editor-core/src/manager.ts index 6fe36cbc4..1dcb26386 100644 --- a/packages/amis-editor-core/src/manager.ts +++ b/packages/amis-editor-core/src/manager.ts @@ -923,7 +923,13 @@ export class EditorManager { !isSpecialLayout ) { // 布局能力提升: 点击插入新元素,当wrapper为空插入布局容器时,自动改为置换,避免过多层级 - this.replaceChild(curActiveId, curElemSchema); + this.replaceChild( + curActiveId, + curElemSchema, + subRenderer, + store.insertRegion, + reGenerateId + ); setTimeout(() => { this.updateConfigPanel(); }, 0); @@ -1009,7 +1015,7 @@ export class EditorManager { regionNodeRegion, value, nextId, - subRenderer, + subRenderer || node.info, { id: store.dragId, type: store.dragType, @@ -1055,7 +1061,8 @@ export class EditorManager { async appendSiblingSchema( rendererSchema: Object, beforeInsert?: boolean, - disabledAutoSelectInsertElem?: boolean + disabledAutoSelectInsertElem?: boolean, + reGenerateId?: boolean ) { if (!rendererSchema) { return; @@ -1113,7 +1120,14 @@ export class EditorManager { regionNodeId, regionNodeRegion, rendererSchema, - nextId + nextId, + node.info, + { + id: store.dragId, + type: store.dragType, + data: store.dragSchema + }, + reGenerateId ); if (child && !disabledAutoSelectInsertElem) { // mobx 修改数据是异步的 @@ -1542,7 +1556,7 @@ export class EditorManager { region: string, json: any, beforeId?: string, - subRenderer?: SubRendererInfo, + subRenderer?: SubRendererInfo | RendererInfo, dragInfo?: { id: string; type: string; @@ -1625,7 +1639,7 @@ export class EditorManager { replaceChild( id: string, json: any, - subRenderer?: SubRendererInfo, + subRenderer?: SubRendererInfo | RendererInfo, region?: string, reGenerateId?: boolean ): boolean { diff --git a/packages/amis-editor-core/src/plugin.ts b/packages/amis-editor-core/src/plugin.ts index 2228331f3..d2db67b33 100644 --- a/packages/amis-editor-core/src/plugin.ts +++ b/packages/amis-editor-core/src/plugin.ts @@ -529,7 +529,7 @@ export interface InsertEventContext extends BaseEventContext { beforeId?: string; index: number; data: any; - subRenderer?: SubRendererInfo; + subRenderer?: SubRendererInfo | RendererInfo; dragInfo?: { id: string; type: string; @@ -539,7 +539,7 @@ export interface InsertEventContext extends BaseEventContext { export interface ReplaceEventContext extends BaseEventContext { data: any; - subRenderer?: SubRendererInfo; + subRenderer?: SubRendererInfo | RendererInfo; region?: string; } diff --git a/packages/amis-editor-core/src/util.ts b/packages/amis-editor-core/src/util.ts index 6615f2993..58ae71446 100644 --- a/packages/amis-editor-core/src/util.ts +++ b/packages/amis-editor-core/src/util.ts @@ -586,14 +586,14 @@ export function reGenerateID( JSONTraverse(json, (value: any, key: string, host: any) => { const isNodeIdFormat = typeof value === 'string' && value.indexOf('u:') === 0; - if (key === 'id' && isNodeIdFormat && host) { - const newID = generateNodeId(); - reIds[host.id] = newID; - host.id = newID; - } - // 组件ID,给新的id内容 - else if (key === 'componentId' && isNodeIdFormat) { - host.componentId = reIds[value] ?? value; + if ((key === 'id' || key === 'componentId') && isNodeIdFormat && host) { + if (reIds[value]) { + host[key] = reIds[value]; + } else { + const newID = generateNodeId(); + reIds[value] = newID; + host[key] = newID; + } } return value;