Skip to content

Commit 5f50ffd

Browse files
committed
refactor: Optimize the method of adding components so that they can be arranged in sequence
1 parent 10a0051 commit 5f50ffd

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

frontend/src/views/dashboard/canvas/CanvasCore.vue

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,8 @@ const forceComputed = () => {
10631063
}
10641064
10651065
function addItemBox(item: CanvasItem) {
1066+
// @ts-expect-error eslint-disable-next-line @typescript-eslint/ban-ts-comment
1067+
item.x = findPositionX(item)
10661068
canvasComponentData.value.push(item)
10671069
forceComputed()
10681070
nextTick(() => {
@@ -1166,6 +1168,49 @@ function tabMoveInCheckSQ() {
11661168
}
11671169
}
11681170
1171+
/**
1172+
* Find position box
1173+
*/
1174+
function findPositionX(item: CanvasItem) {
1175+
const width = item.sizeX
1176+
let resultX = 1
1177+
let checkPointYIndex = -1 // -1 means not occupying any Y-direction canvas
1178+
// Component width
1179+
let pb = positionBox.value
1180+
if (width <= 0) return
1181+
// Find the highest position index of the component. Component rule: the latest y is 1.
1182+
canvasComponentData.value.forEach((component) => {
1183+
const componentYIndex = component.y + component.sizeY - 2
1184+
if (checkPointYIndex < componentYIndex) {
1185+
checkPointYIndex = componentYIndex
1186+
}
1187+
})
1188+
// Start checking from index i in the X direction;
1189+
const pbX = pb[checkPointYIndex]
1190+
// Get the last column array in the X direction
1191+
if (checkPointYIndex < 0 || !pbX) {
1192+
return 1
1193+
} else {
1194+
// The width to check is the component width. The end index of the check is checkEndIndex = i + width - 1;
1195+
// The exit condition for the check is when the end index checkEndIndex is out of bounds (exceeds the end index of pbX).
1196+
for (let i = 0, checkEndIndex = width - 1; checkEndIndex < pbX.length; i++, checkEndIndex++) {
1197+
let adaptorCount = 0
1198+
// Locate the occupied position in the last column
1199+
for (let k = 0; k < width; k++) {
1200+
// pbX[i + k].el === false indicates that the current matrix point is not occupied. When the width of consecutive unoccupied matrix points equals the component width, the starting point i is available.
1201+
if (!pbX[i + k].el) {
1202+
adaptorCount++
1203+
}
1204+
}
1205+
if (adaptorCount === width) {
1206+
resultX = i + 1
1207+
break
1208+
}
1209+
}
1210+
return resultX
1211+
}
1212+
}
1213+
11691214
useEmitt({
11701215
name: `editor-delete-${props.canvasId}`,
11711216
callback: removeItemById,

frontend/src/views/dashboard/editor/index.vue

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,23 @@ const addComponent = (componentType: string, viewInfo?: any) => {
4040
component.propValue[0].title = t('dashboard.new_tab')
4141
component.activeTabName = subTabName
4242
}
43-
component.y = 100
43+
component.y = maxYComponentCount() + 10
4444
// @ts-expect-error eslint-disable-next-line @typescript-eslint/ban-ts-comment
4545
dashboardEditorInnerRef.value.addItemToBox(component)
4646
}
4747
}
4848
49+
const maxYComponentCount = () => {
50+
if (componentData.value.length === 0) {
51+
return 1
52+
} else {
53+
return componentData.value
54+
.filter((item) => item['y'])
55+
.map((item) => item['y'] + item['sizeY']) // Calculate the y+sizeY of each element
56+
.reduce((max, current) => Math.max(max, current), 0)
57+
}
58+
}
59+
4960
onMounted(() => {
5061
// @ts-expect-error eslint-disable-next-line @typescript-eslint/ban-ts-comment
5162
state.opt = router.currentRoute.value.query.opt

0 commit comments

Comments
 (0)