feat: flex模式点选添加组件逻辑优化 (#10315)

* feat: flex模式点选添加组件逻辑优化

* bugfix

* bugfix

---------

Co-authored-by: qinhaoyan <30946345+qinhaoyan@users.noreply.github.com>
This commit is contained in:
qkiroc 2024-05-28 10:33:48 +08:00 committed by GitHub
parent 71f634668c
commit 27ea2e92de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 62 additions and 24 deletions

View File

@ -12,32 +12,67 @@ export default class FlexLayout implements LayoutInterface {
const region = context.region; const region = context.region;
const body = [...(context.schema?.[region] || [])]; const body = [...(context.schema?.[region] || [])];
let row = 0; let row = 0;
let beforeId = context.beforeId;
let position = context.dragInfo?.position || 'bottom';
if (body?.length) { if (body?.length) {
const beforeId = context.beforeId;
const beforeNodeIndex = body.findIndex( const beforeNodeIndex = body.findIndex(
(item: any) => item.$$id === beforeId (item: any) => item.$$id === beforeId
); );
const beforeNode = body[beforeNodeIndex] || body[body.length - 1]; let beforeNode = body[beforeNodeIndex];
const beforeRow = beforeNode?.row; let beforeRow = beforeNode?.row;
const position = context.dragInfo?.position || 'bottom'; const preNode =
row = beforeRow; // left、bottom、top使用beforeRowbottom、top后续行需要加1 beforeNodeIndex > -1
? body[beforeNodeIndex - 1]
: body[body.length - 1];
const preRow = preNode?.row;
if (position === 'right') { // 处理直接点击组件添加的情况
const preNode = body[beforeNodeIndex - 1]; if (!context.dragInfo) {
// 如果前一个节点的row和beforeRow不一样需要减1 // 检查下插入的位置是否还有空间
if (preNode && preNode.row !== beforeRow) { const rowNodes = body.filter((item: any) => item.row === preRow);
row = beforeRow - 1; if (rowNodes.find((item: any) => item.colSize === 'auto')) {
position = 'bottom';
} else {
const leftSize = rowNodes.reduce((size: number, item: any) => {
const split = item.colSize?.split('/');
const colSize =
split?.[0] && split?.[1] ? split[0] / split[1] : item.colSize;
return size - colSize;
}, 1);
if (leftSize >= eval(context.data.$$defaultColSize || 1)) {
position = 'right';
} else {
position = 'bottom';
}
}
// 如果需要插入到下边但是前后的row是一样的则需要找下一行的第一个元素作为beforeNode
if (position === 'bottom' && beforeRow === preRow) {
const lastIndex = findLastIndex(
body,
(item: any) => item.row === preRow
);
beforeNode = body[lastIndex + 1];
beforeId = beforeNode?.$$id;
beforeRow = beforeNode?.row;
} }
} }
if (position === 'bottom') { if (position === 'left') {
if (beforeNodeIndex < 0) { row = beforeRow;
row = beforeRow + 1; }
} if (position === 'right') {
row = preRow;
}
if (position === 'bottom' || position === 'top') {
row = preRow + 1;
} }
} }
return { return {
...context, ...context,
position,
beforeId,
data: { data: {
...context.data, ...context.data,
row row
@ -53,7 +88,7 @@ export default class FlexLayout implements LayoutInterface {
const {isMobile} = store; const {isMobile} = store;
const region = context.region; const region = context.region;
const body = [...(context.schema?.[region] || [])]; const body = [...(context.schema?.[region] || [])];
const position = context.dragInfo?.position || 'bottom'; const position = context.dragInfo?.position || context.position || 'bottom';
const currentIndex = context.regionList.findIndex( const currentIndex = context.regionList.findIndex(
(item: any) => item.$$id === context.data.$$id (item: any) => item.$$id === context.data.$$id
); );
@ -98,11 +133,14 @@ export default class FlexLayout implements LayoutInterface {
context.data.$$defaultColSize && context.data.$$defaultColSize &&
(regionList[currentIndex].colSize = context.data.$$defaultColSize); (regionList[currentIndex].colSize = context.data.$$defaultColSize);
} else { } else {
const rows = regionList.filter(
(item: any) => item.row === context.data.row
);
regionList = regionList.map((item: any) => { regionList = regionList.map((item: any) => {
if (item.row === context.data.row) { if (item.row === context.data.row) {
item = { item = {
...item, ...item,
colSize: 'auto' colSize: `1/${rows.length}`
}; };
} }
return item; return item;
@ -235,7 +273,7 @@ export default class FlexLayout implements LayoutInterface {
preRow = regionList[i].row; preRow = regionList[i].row;
} }
} }
regionList = setDefaultColSize(regionList, -1, preRow); regionList = setDefaultColSize(regionList, context.schema.row);
return { return {
...context, ...context,
regionList regionList

View File

@ -1024,11 +1024,7 @@ export class EditorManager {
value, value,
nextId, nextId,
subRenderer || node.info, subRenderer || node.info,
{ undefined, // 不是拖拽,不需要传递拖拽信息
id: store.dragId,
type: store.dragType,
data: store.dragSchema
},
reGenerateId reGenerateId
); );
if (child && activeChild) { if (child && activeChild) {

View File

@ -1583,15 +1583,19 @@ export function mergeDefinitions(
export function setDefaultColSize( export function setDefaultColSize(
regionList: any[], regionList: any[],
row: number, row: number,
preRow: number preRow?: number
) { ) {
const tempList = [...regionList]; const tempList = [...regionList];
const preRowNodeLength = filter(tempList, n => n.row === preRow).length; const preRowNodeLength = filter(tempList, n => n.row === preRow).length;
const currentRowNodeLength = filter(tempList, n => n.row === row).length; const currentRowNodeLength = filter(tempList, n => n.row === row).length;
for (let i = 0; i < tempList.length; i++) { for (let i = 0; i < tempList.length; i++) {
const item = tempList[i]; const item = tempList[i];
if (item.row === preRow) {
item.colSize = preRowNodeLength > 1 ? `1/${preRowNodeLength}` : '1';
}
if (item.row === row) { if (item.row === row) {
item.colSize = 'auto'; item.colSize =
currentRowNodeLength > 1 ? `1/${currentRowNodeLength}` : '1';
} }
// 原来的行只有一个节点,且有默认宽度,则设置默认宽度 // 原来的行只有一个节点,且有默认宽度,则设置默认宽度
if ( if (