Skip to content

Commit

Permalink
fix(editor): 在组件树将组件拖入不同布局的容器内,需要改变其布局
Browse files Browse the repository at this point in the history
fix #552
  • Loading branch information
roymondchen committed Nov 13, 2023
1 parent 7f6ba9d commit ce0c941
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
13 changes: 11 additions & 2 deletions packages/editor/src/services/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
getInitPositionStyle,
getNodeIndex,
isFixed,
setChilrenLayout,
setLayout,
} from '@editor/utils/editor';
import { beforePaste, getAddParent } from '@editor/utils/operator';
Expand Down Expand Up @@ -510,8 +511,8 @@ class Editor extends BaseService {

const newLayout = await this.getLayout(newConfig);
const layout = await this.getLayout(node);
if (newLayout !== layout) {
newConfig = setLayout(newConfig, newLayout);
if (Array.isArray(newConfig.items) && newLayout !== layout) {
newConfig = setChilrenLayout(newConfig as MContainer, newLayout);
}

parentNodeItems[index] = newConfig;
Expand Down Expand Up @@ -788,7 +789,15 @@ class Editor extends BaseService {
}
}

const layout = await this.getLayout(parent);
const newLayout = await this.getLayout(targetParent);

if (newLayout !== layout) {
setLayout(config, newLayout);
}

parent.items?.splice(index, 1);

targetParent.items?.splice(targetIndex, 0, config);

const page = this.get('page');
Expand Down
31 changes: 18 additions & 13 deletions packages/editor/src/utils/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,28 @@ export const getInitPositionStyle = (style: Record<string, any> = {}, layout: La
return style;
};

export const setLayout = (node: MNode, layout: Layout) => {
export const setChilrenLayout = (node: MContainer, layout: Layout) => {
node.items?.forEach((child: MNode) => {
if (isPop(child)) return;
setLayout(child, layout);
});
return node;
};

const style = child.style || {};
export const setLayout = (node: MNode, layout: Layout) => {
if (isPop(node)) return;

// 是 fixed 不做处理
if (style.position === 'fixed') return;
const style = node.style || {};

if (layout !== Layout.RELATIVE) {
style.position = 'absolute';
} else {
child.style = getRelativeStyle(style);
child.style.right = 'auto';
child.style.bottom = 'auto';
}
});
// 是 fixed 不做处理
if (style.position === 'fixed') return;

if (layout !== Layout.RELATIVE) {
style.position = 'absolute';
} else {
node.style = getRelativeStyle(style);
node.style.right = 'auto';
node.style.bottom = 'auto';
}
return node;
};

Expand Down

0 comments on commit ce0c941

Please sign in to comment.