Browse Source

feat: undo/redo for view reorder

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/5332/head
mertmit 2 years ago
parent
commit
1e87d877b5
  1. 36
      packages/nc-gui/components/smartsheet/sidebar/MenuTop.vue

36
packages/nc-gui/components/smartsheet/sidebar/MenuTop.vue

@ -95,10 +95,12 @@ function onSortStart(evt: SortableEvent) {
dragging = true dragging = true
} }
async function onSortEnd(evt: SortableEvent) { async function onSortEnd(evt: SortableEvent, undo = false) {
evt.stopImmediatePropagation() if (!undo) {
evt.preventDefault() evt.stopImmediatePropagation()
dragging = false evt.preventDefault()
dragging = false
}
if (views.length < 2) return if (views.length < 2) return
@ -106,6 +108,32 @@ async function onSortEnd(evt: SortableEvent) {
if (newIndex === oldIndex) return if (newIndex === oldIndex) return
if (!undo) {
addUndo({
redo: {
fn: async () => {
const ord = sortable.toArray()
const temp = ord.splice(oldIndex, 1)
ord.splice(newIndex, 0, temp[0])
sortable.sort(ord)
await onSortEnd(evt, true)
},
args: [],
},
undo: {
fn: async () => {
const ord = sortable.toArray()
const temp = ord.splice(newIndex, 1)
ord.splice(oldIndex, 0, temp[0])
sortable.sort(ord)
await onSortEnd({ ...evt, oldIndex: newIndex, newIndex: oldIndex }, true)
},
args: [],
},
scope: defineModelScope({ view: activeView.value }),
})
}
const children = evt.to.children as unknown as HTMLLIElement[] const children = evt.to.children as unknown as HTMLLIElement[]
const previousEl = children[newIndex - 1] const previousEl = children[newIndex - 1]

Loading…
Cancel
Save