From 8e004f9766761d0ef7bd3aeca947fc6197e321bc Mon Sep 17 00:00:00 2001 From: roymondchen Date: Thu, 21 Apr 2022 20:41:23 +0800 Subject: [PATCH] =?UTF-8?q?fix(editor):=20getLayout=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=88=A4=E6=96=ADfixed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/editor/src/services/editor.ts | 19 +++++++++++-------- packages/editor/src/utils/editor.ts | 6 +++++- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/packages/editor/src/services/editor.ts b/packages/editor/src/services/editor.ts index 6599b748..d4328294 100644 --- a/packages/editor/src/services/editor.ts +++ b/packages/editor/src/services/editor.ts @@ -108,7 +108,7 @@ class Editor extends BaseService { * @returns {EditorNodeInfo} */ public getNodeInfo(id: Id): EditorNodeInfo { - const root = this.get('root'); + const root = toRaw(this.get('root')); if (!root) return {}; if (id === root.id) { @@ -159,13 +159,15 @@ class Editor extends BaseService { /** * 只有容器拥有布局 */ - public async getLayout(node: MNode): Promise { - if (node.layout) { - return node.layout; + public async getLayout(parent: MNode, node?: MNode): Promise { + if (node && typeof node !== 'function' && isFixed(node)) return Layout.FIXED; + + if (parent.layout) { + return parent.layout; } // 如果该节点没有设置position,则认为是流式布局,例如获取root的布局时 - if (!node.style?.position) { + if (!parent.style?.position) { return Layout.RELATIVE; } @@ -209,7 +211,8 @@ class Editor extends BaseService { * @param parent 要添加到的容器组件节点配置,如果不设置,默认为当前选中的组件的父节点 * @returns 添加后的节点 */ - public async add({ type, ...config }: AddMNode, parent?: MContainer | null): Promise { + public async add(addNode: AddMNode, parent?: MContainer | null): Promise { + const { type, ...config } = addNode; const curNode = this.get('node'); let parentNode: MNode | undefined; @@ -227,7 +230,7 @@ class Editor extends BaseService { if (!parentNode) throw new Error('未找到父元素'); - const layout = await this.getLayout(parentNode); + const layout = await this.getLayout(toRaw(parentNode), addNode as MNode); const newNode = initPosition( { ...toRaw(await propsService.getPropsValue(type, config)) }, layout, @@ -432,7 +435,7 @@ class Editor extends BaseService { public async alignCenter(config: MNode): Promise { const parent = this.get('parent'); const node = this.get('node'); - const layout = await this.getLayout(parent); + const layout = await this.getLayout(toRaw(parent), toRaw(node)); if (layout === Layout.RELATIVE) { return; } diff --git a/packages/editor/src/utils/editor.ts b/packages/editor/src/utils/editor.ts index 3306de17..cb44f33b 100644 --- a/packages/editor/src/utils/editor.ts +++ b/packages/editor/src/utils/editor.ts @@ -213,7 +213,11 @@ export const change2Fixed = (node: MNode, root: MApp) => { return node; }; -export const Fixed2Other = async (node: MNode, root: MApp, getLayout: (node: MNode) => Promise) => { +export const Fixed2Other = async ( + node: MNode, + root: MApp, + getLayout: (parent: MNode, node?: MNode) => Promise, +) => { const path = getNodePath(node.id, root.items); const cur = path.pop(); const offset = {