From 54caf2d787abe6db8b718b68ab9381debe34e86c Mon Sep 17 00:00:00 2001 From: skjnldsv Date: Wed, 11 Dec 2024 11:30:46 +0100 Subject: [PATCH] fixup! fix(files): simplify active store handling Signed-off-by: skjnldsv --- apps/files/src/actions/deleteAction.ts | 11 ++++ .../components/FileEntry/FileEntryActions.vue | 5 -- apps/files/src/store/active.ts | 1 + apps/files/src/utils/actionUtils.ts | 61 +++++++++++++++++++ apps/files/src/views/FilesList.vue | 13 ++-- 5 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 apps/files/src/utils/actionUtils.ts diff --git a/apps/files/src/actions/deleteAction.ts b/apps/files/src/actions/deleteAction.ts index 3ac1d2e797fe2..c774fb5c84c11 100644 --- a/apps/files/src/actions/deleteAction.ts +++ b/apps/files/src/actions/deleteAction.ts @@ -5,6 +5,7 @@ import { Permission, Node, View, FileAction } from '@nextcloud/files' import { showInfo } from '@nextcloud/dialogs' import { translate as t } from '@nextcloud/l10n' +import { useHotKey } from '@nextcloud/vue/dist/Composables/useHotKey.js' import PQueue from 'p-queue' import CloseSvg from '@mdi/svg/svg/close.svg?raw' @@ -13,6 +14,7 @@ import TrashCanSvg from '@mdi/svg/svg/trash-can.svg?raw' import logger from '../logger.ts' import { askConfirmation, canDisconnectOnly, canUnshareOnly, deleteNode, displayName, isTrashbinEnabled } from './deleteUtils' +import { executeAction } from '../utils/actionUtils.ts' const queue = new PQueue({ concurrency: 5 }) @@ -107,3 +109,12 @@ export const action = new FileAction({ order: 100, }) + +const executeDeleteAction = function() { + executeAction(action) +} + +useHotKey('Delete', executeDeleteAction, { + stop: true, + prevent: true, +}) diff --git a/apps/files/src/components/FileEntry/FileEntryActions.vue b/apps/files/src/components/FileEntry/FileEntryActions.vue index 2b494e0486bb9..f4504803d4c2f 100644 --- a/apps/files/src/components/FileEntry/FileEntryActions.vue +++ b/apps/files/src/components/FileEntry/FileEntryActions.vue @@ -273,11 +273,6 @@ export default defineComponent({ prevent: true, }) - useHotKey('Delete', this.onKeyDown, { - stop: true, - prevent: true, - }) - useHotKey('s', this.onKeyDown, { stop: true, prevent: true, diff --git a/apps/files/src/store/active.ts b/apps/files/src/store/active.ts index f36b9d1ca954d..fb79bdbbfdb55 100644 --- a/apps/files/src/store/active.ts +++ b/apps/files/src/store/active.ts @@ -58,6 +58,7 @@ export const useActiveStore = function(...args) { subscribe('files:node:deleted', activeStore.onDeletedNode) activeStore._initialized = true + activeStore.onChangedView(navigation.active) // Or you can react to changes of the current active view navigation.addEventListener('updateActive', (event) => { diff --git a/apps/files/src/utils/actionUtils.ts b/apps/files/src/utils/actionUtils.ts new file mode 100644 index 0000000000000..af2a3f4c61b78 --- /dev/null +++ b/apps/files/src/utils/actionUtils.ts @@ -0,0 +1,61 @@ +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +import type { FileAction } from '@nextcloud/files' + +import { NodeStatus } from '@nextcloud/files' +import { showError, showSuccess } from '@nextcloud/dialogs' +import { t } from '@nextcloud/l10n' +import Vue from 'vue' + +import { useActiveStore } from '../store/active' +import logger from '../logger' + +/** + * Execute an action on the current active node + * + * @param action The action to execute + */ +export async function executeAction(action: FileAction) { + const activeStore = useActiveStore() + const currentDir = (window.OCP.Files.Router?.query?.dir || '/') as string + const currentNode = activeStore.activeNode + const currentView = activeStore.activeView + + if (!action.enabled!([currentNode], currentView)) { + logger.debug('Action is not not available for the current context', { action, node: currentNode, view: currentView }) + return + } + + let displayName = action.id + try { + displayName = action.displayName([currentNode], currentView) + } catch (error) { + logger.error('Error while getting action display name', { action, error }) + } + + try { + // Set the loading marker + Vue.set(currentNode, 'status', NodeStatus.LOADING) + + const success = await action.exec(currentNode, currentView, currentDir) + + // If the action returns null, we stay silent + if (success === null || success === undefined) { + return + } + + if (success) { + showSuccess(t('files', '"{displayName}" action executed successfully', { displayName })) + return + } + showError(t('files', '"{displayName}" action failed', { displayName })) + } catch (error) { + logger.error('Error while executing action', { action, error }) + showError(t('files', '"{displayName}" action failed', { displayName })) + } finally { + // Reset the loading marker + Vue.set(currentNode, 'status', undefined) + } +} diff --git a/apps/files/src/views/FilesList.vue b/apps/files/src/views/FilesList.vue index 0d218da13ca85..6ce385776f264 100644 --- a/apps/files/src/views/FilesList.vue +++ b/apps/files/src/views/FilesList.vue @@ -135,16 +135,17 @@ import type { Route } from 'vue-router' import type { Upload } from '@nextcloud/upload' import type { UserConfig } from '../types.ts' -import { getCapabilities } from '@nextcloud/capabilities' +import { defineComponent } from 'vue' import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus' -import { Node, Permission, sortNodes, getFileListActions } from '@nextcloud/files' -import { translate as t } from '@nextcloud/l10n' +import { getCapabilities } from '@nextcloud/capabilities' import { join, dirname, normalize } from 'path' -import { showError, showWarning } from '@nextcloud/dialogs' +import { loadState } from '@nextcloud/initial-state' +import { Node, Permission, sortNodes, getFileListActions } from '@nextcloud/files' import { ShareType } from '@nextcloud/sharing' +import { showError, showWarning } from '@nextcloud/dialogs' +import { translate as t } from '@nextcloud/l10n' import { UploadPicker, UploadStatus } from '@nextcloud/upload' -import { loadState } from '@nextcloud/initial-state' -import { defineComponent } from 'vue' +import { useHotKey } from '@nextcloud/vue/dist/Composables/useHotKey.js' import AccountPlusIcon from 'vue-material-design-icons/AccountPlus.vue' import IconAlertCircleOutline from 'vue-material-design-icons/AlertCircleOutline.vue'