From 4a7645fe015e1982480046db1f7b6a5c2ddb0348 Mon Sep 17 00:00:00 2001 From: mertmit Date: Tue, 28 Mar 2023 21:39:42 +0300 Subject: [PATCH] fix: tree view reorder Signed-off-by: mertmit --- .../nc-gui/components/dashboard/TreeView.vue | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/nc-gui/components/dashboard/TreeView.vue b/packages/nc-gui/components/dashboard/TreeView.vue index dd1a6f129c..b7ed8178ed 100644 --- a/packages/nc-gui/components/dashboard/TreeView.vue +++ b/packages/nc-gui/components/dashboard/TreeView.vue @@ -90,8 +90,6 @@ const initSortable = (el: Element) => { if (sortables[base_id]) sortables[base_id].destroy() Sortable.create(el as HTMLLIElement, { onEnd: async (evt) => { - const offset = tables.value.findIndex((table) => table.base_id === base_id) - const { newIndex = 0, oldIndex = 0 } = evt const itemEl = evt.item as HTMLLIElement @@ -120,8 +118,19 @@ const initSortable = (el: Element) => { item.order = ((itemBefore.order as number) + (itemAfter.order as number)) / 2 } - // update the order of the moved item - tables.value?.splice(newIndex + offset, 0, ...tables.value?.splice(oldIndex + offset, 1)) + // find the index of the moved item + const itemIndex = tables.value?.findIndex((table) => table.id === item.id) + + // move the item to the new position + if (itemBefore) { + // find the index of the item before the moved item + const itemBeforeIndex = tables.value?.findIndex((table) => table.id === itemBefore.id) + tables.value?.splice(itemBeforeIndex + (newIndex > oldIndex ? 0 : 1), 0, ...tables.value?.splice(itemIndex, 1)) + } else { + // if the item before is undefined (moving item to first slot), then find the index of the item after the moved item + const itemAfterIndex = tables.value?.findIndex((table) => table.id === itemAfter.id) + tables.value?.splice(itemAfterIndex, 0, ...tables.value?.splice(itemIndex, 1)) + } // force re-render the list if (keys[base_id]) {