Fix folder moving UX

fixes #1747

Signed-off-by: Marcel Klehr <mklehr@gmx.net>
This commit is contained in:
Marcel Klehr 2022-02-20 15:12:59 +01:00
parent 97db9c95b4
commit 6703897f8c
4 changed files with 60 additions and 41 deletions

View File

@ -759,32 +759,28 @@ export default {
}
const oldParent = folder.parent_folder
folder.parent_folder = folderId
commit(mutations.MOVE_FOLDER, { folder: folder.id, target: folderId })
await dispatch(actions.SAVE_FOLDER, folder.id) // reloads children order for new parent
await dispatch(
dispatch(
actions.LOAD_FOLDER_CHILDREN_ORDER,
oldParent
)
},
10
)
await Promise.all([
state.selection.folders.length
? dispatch(actions.LOAD_FOLDERS)
: Promise.resolve(),
Parallel.each(
state.selection.bookmarks,
bookmark => {
commit(mutations.REMOVE_BOOKMARK, bookmark.id)
return dispatch(actions.MOVE_BOOKMARK, {
oldFolder:
bookmark.folders[bookmark.folders.length - 1], // FIXME This is veeeery ugly and will cause issues. Inevitably.
newFolder: folderId,
bookmark: bookmark.id,
})
},
10
),
])
await Parallel.each(
state.selection.bookmarks,
bookmark => {
commit(mutations.REMOVE_BOOKMARK, bookmark.id)
return dispatch(actions.MOVE_BOOKMARK, {
oldFolder:
bookmark.folders[bookmark.folders.length - 1], // FIXME This is veeeery ugly and will cause issues. Inevitably.
newFolder: folderId,
bookmark: bookmark.id,
})
},
10
)
// Because we're possibly moving across share boundaries we need to recount
dispatch(actions.COUNT_BOOKMARKS, -1)

26
src/store/findFolder.js Normal file
View File

@ -0,0 +1,26 @@
/**
* @param id
* @param children
*/
export function findFolder(id, children) {
if (!children) {
return []
} else if (Number(id) === -1) {
return [{ id: -1, children }]
} else if (!children.length) {
return []
}
const folders = children.filter(folder => Number(folder.id) === Number(id))
if (folders.length) {
return folders
} else {
for (const child of children) {
const folders = findFolder(id, child.children)
if (folders.length) {
folders.push(child)
return folders
}
}
return []
}
}

View File

@ -11,6 +11,7 @@ import Actions from './actions'
import { privateRoutes, publicRoutes } from '../router'
import { generateUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import { findFolder } from './findFolder'
Vue.use(Vuex)
@ -53,7 +54,6 @@ export default {
sharedFoldersById: {},
tags: [],
folders: [],
foldersById: {},
childrenByFolder: {},
tokensByFolder: {},
countsByFolder: {},
@ -136,24 +136,3 @@ export default {
},
},
}
/**
* @param id
* @param children
*/
function findFolder(id, children) {
if (!children || !children.length) return []
const folders = children.filter(folder => Number(folder.id) === Number(id))
if (folders.length) {
return folders
} else {
for (const child of children) {
const folders = findFolder(id, child.children)
if (folders.length) {
folders.push(child)
return folders
}
}
return []
}
}

View File

@ -6,6 +6,7 @@
import Vue from 'vue'
import axios from '@nextcloud/axios'
import { findFolder } from './findFolder'
export const mutations = {
SET_AUTH_TOKEN: 'SET_AUTH_TOKEN',
@ -39,6 +40,7 @@ export const mutations = {
SET_ERROR: 'SET_ERROR',
SET_NOTIFICATION: 'SET_NOTIFICATION',
SET_FOLDERS: 'SET_FOLDERS',
MOVE_FOLDER: 'MOVE_FOLDER',
SET_SIDEBAR: 'SET_SIDEBAR',
SET_SETTING: 'SET_SETTING',
SET_VIEW_MODE: 'SET_VIEW_MODE',
@ -75,6 +77,22 @@ export default {
[mutations.SET_FOLDERS](state, folders) {
state.folders = sortFolders(folders)
},
[mutations.MOVE_FOLDER](state, { folder, target }) {
const currentFolder = findFolder(folder, state.folders)[0]
const oldParent = findFolder(currentFolder.parent_folder, state.folders)[0]
const index = oldParent.children.indexOf(currentFolder)
oldParent.children.splice(index, 1)
const newParent = findFolder(target, state.folders)[0]
newParent.children.push(currentFolder)
if (state.childrenByFolder[oldParent.id]) {
const index = state.childrenByFolder[oldParent.id].findIndex(item => item.id === currentFolder.id)
state.childrenByFolder[oldParent.id].splice(index, 1)
}
if (state.childrenByFolder[newParent.id]) {
state.childrenByFolder[newParent.id].push({ type: 'folder', id: currentFolder.id })
}
},
[mutations.ADD_TAG](state, tag) {
state.tags.push({ name: tag, count: 0 })
},