fix(files): fix favorites legacy to vue handling and sorting

Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
This commit is contained in:
John Molakvoæ 2023-04-27 10:40:19 +02:00
parent 0984970cd8
commit f029c8bd82
No known key found for this signature in database
GPG key ID: 60C25B8C072916CF
2 changed files with 106 additions and 59 deletions

View file

@ -194,13 +194,25 @@
tags = tags.split('|');
tags = _.without(tags, '');
var isFavorite = tags.indexOf(OC.TAG_FAVORITE) >= 0;
// Fake Node object for vue compatibility
const node = {
type: 'folder',
path: (dir + '/' + fileName).replace(/\/\/+/g, '/'),
root: '/files/' + OC.getCurrentUser().uid
}
if (isFavorite) {
// remove tag from list
tags = _.without(tags, OC.TAG_FAVORITE);
removeFavoriteFromList(dir + '/' + fileName);
// vue compatibility
window._nc_event_bus.emit('files:favorites:removed', node)
} else {
tags.push(OC.TAG_FAVORITE);
addFavoriteToList(dir + '/' + fileName);
// vue compatibility
window._nc_event_bus.emit('files:favorites:added', node)
}
// pre-toggle the star

View file

@ -21,7 +21,7 @@
*/
import type NavigationService from '../services/Navigation.ts'
import type { Navigation } from '../services/Navigation.ts'
import { translate as t } from '@nextcloud/l10n'
import { getLanguage, translate as t } from '@nextcloud/l10n'
import StarSvg from '@mdi/svg/svg/star.svg?raw'
import FolderSvg from '@mdi/svg/svg/folder.svg?raw'
@ -33,69 +33,13 @@ import { subscribe } from '@nextcloud/event-bus'
import { Node, FileType } from '@nextcloud/files'
import logger from '../logger'
const favoriteFolders = loadState('files', 'favoriteFolders', [])
export default () => {
const Navigation = window.OCP.Files.Navigation as NavigationService
Navigation.register({
id: 'favorites',
name: t('files', 'Favorites'),
caption: t('files', 'List of favorites files and folders.'),
icon: StarSvg,
order: 5,
columns: [],
getContents,
} as Navigation)
favoriteFolders.forEach((folder) => {
Navigation.register(generateFolderView(folder))
})
/**
* Update favourites navigation when a new folder is added
*/
subscribe('files:favorites:added', (node: Node) => {
if (node.type !== FileType.Folder) {
return
}
// Sanity check
if (node.path === null || !node.root?.startsWith('/files')) {
logger.error('Favorite folder is not within user files root', { node })
return
}
Navigation.register(generateFolderView(node.path))
})
/**
* Remove favourites navigation when a folder is removed
*/
subscribe('files:favorites:removed', (node: Node) => {
if (node.type !== FileType.Folder) {
return
}
// Sanity check
if (node.path === null || !node.root?.startsWith('/files')) {
logger.error('Favorite folder is not within user files root', { node })
return
}
Navigation.remove(generateIdFromPath(node.path))
})
}
const generateFolderView = function(folder: string): Navigation {
const generateFolderView = function(folder: string, index = 0): Navigation {
return {
id: generateIdFromPath(folder),
name: basename(folder),
icon: FolderSvg,
order: -100, // always first
order: index,
params: {
dir: folder,
view: 'favorites',
@ -112,3 +56,94 @@ const generateFolderView = function(folder: string): Navigation {
const generateIdFromPath = function(path: string): string {
return `favorite-${hashCode(path)}`
}
const favoriteFolders = loadState('files', 'favoriteFolders', []) as string[]
const favoriteFoldersViews = favoriteFolders.map((folder, index) => generateFolderView(folder, index))
export default () => {
const Navigation = window.OCP.Files.Navigation as NavigationService
Navigation.register({
id: 'favorites',
name: t('files', 'Favorites'),
caption: t('files', 'List of favorites files and folders.'),
icon: StarSvg,
order: 5,
columns: [],
getContents,
} as Navigation)
favoriteFoldersViews.forEach(view => Navigation.register(view))
/**
* Update favourites navigation when a new folder is added
*/
subscribe('files:favorites:added', (node: Node) => {
if (node.type !== FileType.Folder) {
return
}
// Sanity check
if (node.path === null || !node.root?.startsWith('/files')) {
logger.error('Favorite folder is not within user files root', { node })
return
}
addPathToFavorites(node.path)
})
/**
* Remove favourites navigation when a folder is removed
*/
subscribe('files:favorites:removed', (node: Node) => {
if (node.type !== FileType.Folder) {
return
}
// Sanity check
if (node.path === null || !node.root?.startsWith('/files')) {
logger.error('Favorite folder is not within user files root', { node })
return
}
removePathFromFavorites(node.path)
})
/**
* Sort the favorites paths array and
* update the order property of the existing views
*/
const updateAndSortViews = function() {
favoriteFolders.sort((a, b) => a.localeCompare(b, getLanguage(), { ignorePunctuation: true }))
favoriteFolders.forEach((folder, index) => {
const view = favoriteFoldersViews.find(view => view.id === generateIdFromPath(folder))
if (view) {
view.order = index
}
})
}
// Add a folder to the favorites paths array and update the views
const addPathToFavorites = function(path: string) {
const view = generateFolderView(path)
// Update arrays
favoriteFolders.push(path)
favoriteFoldersViews.push(view)
// Update and sort views
updateAndSortViews()
Navigation.register(view)
}
// Remove a folder from the favorites paths array and update the views
const removePathFromFavorites = function(path: string) {
const id = generateIdFromPath(path)
const index = favoriteFolders.findIndex(f => f === path)
// Update arrays
favoriteFolders.splice(index, 1)
favoriteFoldersViews.splice(index, 1)
Navigation.remove(id)
updateAndSortViews()
}
}