aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/composables/useHotKeys.ts
blob: ff56627b2f938da80433761f6627788fcd70486f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
 * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
import { useHotKey } from '@nextcloud/vue/composables/useHotKey'
import { dirname } from 'path'
import { useRoute, useRouter } from 'vue-router/composables'

import { action as deleteAction } from '../actions/deleteAction.ts'
import { action as favoriteAction } from '../actions/favoriteAction.ts'
import { action as renameAction } from '../actions/renameAction.ts'
import { action as sidebarAction } from '../actions/sidebarAction.ts'
import { useUserConfigStore } from '../store/userconfig.ts'
import { useRouteParameters } from './useRouteParameters.ts'
import { executeAction } from '../utils/actionUtils.ts'
import logger from '../logger.ts'

/**
 * This register the hotkeys for the Files app.
 * As much as possible, we try to have all the hotkeys in one place.
 * Please make sure to add tests for the hotkeys after adding a new one.
 */
export function useHotKeys(): void {
	const userConfigStore = useUserConfigStore()
	const { directory } = useRouteParameters()
	const router = useRouter()
	const route = useRoute()

	// d opens the sidebar
	useHotKey('d', () => executeAction(sidebarAction), {
		stop: true,
		prevent: true,
	})

	// F2 renames the file
	useHotKey('F2', () => executeAction(renameAction), {
		stop: true,
		prevent: true,
	})

	// s toggle favorite
	useHotKey('s', () => executeAction(favoriteAction), {
		stop: true,
		prevent: true,
	})

	// Delete deletes the file
	useHotKey('Delete', () => executeAction(deleteAction), {
		stop: true,
		prevent: true,
	})

	// alt+up go to parent directory
	useHotKey('ArrowUp', goToParentDir, {
		stop: true,
		prevent: true,
		alt: true,
	})

	// v toggle grid view
	useHotKey('v', toggleGridView, {
		stop: true,
		prevent: true,
	})

	logger.debug('Hotkeys registered')

	/**
	 * Use the router to go to the parent directory
	 */
	function goToParentDir() {
		const dir = dirname(directory.value)

		logger.debug('Navigating to parent directory', { dir })
		router.push({ params: { ...route.params }, query: { ...route.query, dir } })
	}

	/**
	 * Toggle the grid view
	 */
	function toggleGridView() {
		const value = userConfigStore.userConfig.grid_view
		logger.debug('Toggling grid view', { old: value, new: !value })
		userConfigStore.update('grid_view', !value)
	}
}