Skip to content

Commit

Permalink
feat(files): add select/unselect all keyboard shortcut
Browse files Browse the repository at this point in the history
Signed-off-by: skjnldsv <[email protected]>
  • Loading branch information
skjnldsv committed Nov 21, 2024
1 parent e204098 commit 93885ec
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
31 changes: 31 additions & 0 deletions apps/files/src/components/FilesListTableHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import { useFilesStore } from '../store/files.ts'
import { useSelectionStore } from '../store/selection.ts'
import filesSortingMixin from '../mixins/filesSorting.ts'
import logger from '../logger.ts'
import { isDialogOpen } from '../utils/dialogUtils.ts'
export default defineComponent({
name: 'FilesListTableHeader',
Expand Down Expand Up @@ -155,6 +156,14 @@ export default defineComponent({
},
},
beforeMount() {
document.addEventListener('keydown', this.onKeyDown)
},
beforeDestroy() {
document.removeEventListener('keydown', this.onKeyDown)
},
methods: {
ariaSortForMode(mode: string): ARIAMixin['ariaSort'] {
if (this.sortingMode === mode) {
Expand All @@ -172,6 +181,28 @@ export default defineComponent({
}
},
onKeyDown(event: KeyboardEvent) {
// Don't react to the event if a dialog is open
if (isDialogOpen()) {
return
}
// Escape key cancels selection
if (event.key === 'Escape' && (this.isSomeSelected || this.isAllSelected)) {
this.resetSelection()
event.preventDefault()
event.stopPropagation()
return
}
// ctrl+a selects all
if (event.key === 'a' && event.ctrlKey) {
this.onToggleAll(true)
event.preventDefault()
event.stopPropagation()
}
},
onToggleAll(selected) {
if (selected) {
const selection = this.nodes.map(node => node.source).filter(Boolean) as FileSource[]
Expand Down
14 changes: 14 additions & 0 deletions apps/files/src/utils/dialogUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
export function isDialogOpen() {
// Select all elements with role="dialog"
const dialogs = document.querySelectorAll('[role="dialog"]')

// Check if any dialog is visible
return Array.from(dialogs).some(dialog => {
const style = window.getComputedStyle(dialog)
return style.display !== 'none' && style.visibility !== 'hidden'
})
}

0 comments on commit 93885ec

Please sign in to comment.