fix(editor): getLayout增加判断fixed

This commit is contained in:
roymondchen 2022-04-21 20:41:23 +08:00 committed by jia000
parent 2647ace396
commit 8e004f9766
2 changed files with 16 additions and 9 deletions

View File

@ -108,7 +108,7 @@ class Editor extends BaseService {
* @returns {EditorNodeInfo} * @returns {EditorNodeInfo}
*/ */
public getNodeInfo(id: Id): EditorNodeInfo { public getNodeInfo(id: Id): EditorNodeInfo {
const root = this.get<MApp | null>('root'); const root = toRaw(this.get<MApp | null>('root'));
if (!root) return {}; if (!root) return {};
if (id === root.id) { if (id === root.id) {
@ -159,13 +159,15 @@ class Editor extends BaseService {
/** /**
* *
*/ */
public async getLayout(node: MNode): Promise<Layout> { public async getLayout(parent: MNode, node?: MNode): Promise<Layout> {
if (node.layout) { if (node && typeof node !== 'function' && isFixed(node)) return Layout.FIXED;
return node.layout;
if (parent.layout) {
return parent.layout;
} }
// 如果该节点没有设置position则认为是流式布局例如获取root的布局时 // 如果该节点没有设置position则认为是流式布局例如获取root的布局时
if (!node.style?.position) { if (!parent.style?.position) {
return Layout.RELATIVE; return Layout.RELATIVE;
} }
@ -209,7 +211,8 @@ class Editor extends BaseService {
* @param parent * @param parent
* @returns * @returns
*/ */
public async add({ type, ...config }: AddMNode, parent?: MContainer | null): Promise<MNode> { public async add(addNode: AddMNode, parent?: MContainer | null): Promise<MNode> {
const { type, ...config } = addNode;
const curNode = this.get<MContainer>('node'); const curNode = this.get<MContainer>('node');
let parentNode: MNode | undefined; let parentNode: MNode | undefined;
@ -227,7 +230,7 @@ class Editor extends BaseService {
if (!parentNode) throw new Error('未找到父元素'); if (!parentNode) throw new Error('未找到父元素');
const layout = await this.getLayout(parentNode); const layout = await this.getLayout(toRaw(parentNode), addNode as MNode);
const newNode = initPosition( const newNode = initPosition(
{ ...toRaw(await propsService.getPropsValue(type, config)) }, { ...toRaw(await propsService.getPropsValue(type, config)) },
layout, layout,
@ -432,7 +435,7 @@ class Editor extends BaseService {
public async alignCenter(config: MNode): Promise<MNode | void> { public async alignCenter(config: MNode): Promise<MNode | void> {
const parent = this.get<MContainer>('parent'); const parent = this.get<MContainer>('parent');
const node = this.get<MNode>('node'); const node = this.get<MNode>('node');
const layout = await this.getLayout(parent); const layout = await this.getLayout(toRaw(parent), toRaw(node));
if (layout === Layout.RELATIVE) { if (layout === Layout.RELATIVE) {
return; return;
} }

View File

@ -213,7 +213,11 @@ export const change2Fixed = (node: MNode, root: MApp) => {
return node; return node;
}; };
export const Fixed2Other = async (node: MNode, root: MApp, getLayout: (node: MNode) => Promise<Layout>) => { export const Fixed2Other = async (
node: MNode,
root: MApp,
getLayout: (parent: MNode, node?: MNode) => Promise<Layout>,
) => {
const path = getNodePath(node.id, root.items); const path = getNodePath(node.id, root.items);
const cur = path.pop(); const cur = path.pop();
const offset = { const offset = {