Skip to content

Commit 76aa2b1

Browse files
committed
fix: update position handling in drag-and-drop functionality
1 parent f0f7985 commit 76aa2b1

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

ui/src/views/paragraph/index.vue

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
{
140140
paragraph_id: item.id,
141141
new_position: setPosition(val, index),
142+
target_index: setTargetIndex(val, index),
142143
},
143144
index,
144145
)
@@ -266,15 +267,22 @@ watch(
266267
267268
function setPosition(val: string, index: number) {
268269
if (val === 'top') {
269-
return 0
270+
return 1
270271
} else if (val === 'bottom') {
271-
return paginationConfig.total - 1
272+
return paragraphDetail.value[paragraphDetail.value.length - 1]?.position ?? paginationConfig.total
272273
} else if (val === 'up') {
273-
return index - 1
274+
return paragraphDetail.value[index - 1]?.position ?? paragraphDetail.value[index].position
274275
} else if (val === 'down') {
275-
return index + 1
276+
return paragraphDetail.value[index + 1]?.position ?? paragraphDetail.value[index].position
276277
}
277278
}
279+
function setTargetIndex(val: string, index: number) {
280+
if (val === 'top') return 0
281+
if (val === 'bottom') return paragraphDetail.value.length - 1
282+
if (val === 'up') return index - 1
283+
if (val === 'down') return index + 1
284+
return index
285+
}
278286
function dialogVisibleChange(val: boolean) {
279287
dialogVisible.value = val
280288
}
@@ -450,12 +458,13 @@ function onEnd(event?: any, params?: any, index?: number) {
450458
return
451459
}
452460
const p = cloneDeep(params)
453-
if (p) {
454-
p.new_position = p.new_position + 1 // 由于拖拽时会将当前段落位置作为新位置,所以需要加1
455-
}
456461
const obj = p ?? {
457462
paragraph_id: paragraphDetail.value[event.newIndex].id, // 当前拖动的段落ID
458-
new_position: event.newIndex + 1,
463+
// 向下拖动时取前一个元素的position,向上拖动时取后一个元素的position
464+
new_position:
465+
event.newIndex > event.oldIndex
466+
? paragraphDetail.value[event.newIndex - 1]?.position ?? paragraphDetail.value.length
467+
: paragraphDetail.value[event.newIndex + 1]?.position ?? paragraphDetail.value.length,
459468
}
460469
// console.log(paragraphDetail.value[event.newIndex], obj)
461470
loadSharedApi({ type: 'paragraph', systemType: apiType.value }).putAdjustPosition(
@@ -466,7 +475,7 @@ function onEnd(event?: any, params?: any, index?: number) {
466475
)
467476
if (params) {
468477
const movedItem = paragraphDetail.value.splice(index as number, 1)[0]
469-
paragraphDetail.value.splice(params.new_position, 0, movedItem)
478+
paragraphDetail.value.splice(params.target_index, 0, movedItem)
470479
}
471480
}
472481

0 commit comments

Comments
 (0)