-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make possible select multiple files
Signed-off-by: Vitor Mattos <vitor@php.rio>
- Loading branch information
1 parent
f358a51
commit 03c5b24
Showing
6 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 LibreCode coop and contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
import { defineStore } from 'pinia' | ||
import { set } from 'vue' | ||
|
||
/** | ||
* Observe various events and save the current | ||
* special keys states. Useful for checking the | ||
* current status of a key when executing a method. | ||
* @param {...any} args properties | ||
*/ | ||
export const useKeyboardStore = function(...args) { | ||
const store = defineStore('keyboard', { | ||
state: () => ({ | ||
altKey: false, | ||
ctrlKey: false, | ||
metaKey: false, | ||
shiftKey: false, | ||
}), | ||
|
||
actions: { | ||
onEvent(event) { | ||
if (!event) { | ||
event = window.event | ||
} | ||
set(this, 'altKey', !!event.altKey) | ||
set(this, 'ctrlKey', !!event.ctrlKey) | ||
set(this, 'metaKey', !!event.metaKey) | ||
set(this, 'shiftKey', !!event.shiftKey) | ||
}, | ||
}, | ||
}) | ||
|
||
const keyboardStore = store(...args) | ||
// Make sure we only register the listeners once | ||
if (!keyboardStore._initialized) { | ||
window.addEventListener('keydown', keyboardStore.onEvent) | ||
window.addEventListener('keyup', keyboardStore.onEvent) | ||
window.addEventListener('mousemove', keyboardStore.onEvent) | ||
|
||
keyboardStore._initialized = true | ||
} | ||
|
||
return keyboardStore | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 LibreCode coop and contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
import { defineStore } from 'pinia' | ||
import { set } from 'vue' | ||
|
||
import { subscribe } from '@nextcloud/event-bus' | ||
|
||
export const useSelectionStore = function(...args) { | ||
const store = defineStore('selection', { | ||
state: () => ({ | ||
selected: [], | ||
lastSelection: [], | ||
lastSelectedIndex: null, | ||
}), | ||
|
||
actions: { | ||
/** | ||
* Set the selection of fileIds | ||
* @param {Array} selection Selected files | ||
*/ | ||
set(selection = []) { | ||
set(this, 'selected', [...new Set(selection)]) | ||
}, | ||
|
||
/** | ||
* Set the last selected index | ||
* @param {number | null} lastSelectedIndex Position of last selected file | ||
*/ | ||
setLastIndex(lastSelectedIndex = null) { | ||
// Update the last selection if we provided a new selection starting point | ||
set(this, 'lastSelection', lastSelectedIndex ? this.selected : []) | ||
set(this, 'lastSelectedIndex', lastSelectedIndex) | ||
}, | ||
|
||
/** | ||
* Reset the selection | ||
*/ | ||
reset() { | ||
set(this, 'selected', []) | ||
set(this, 'lastSelection', []) | ||
set(this, 'lastSelectedIndex', null) | ||
}, | ||
}, | ||
}) | ||
|
||
const selectionStore = store(...args) | ||
|
||
// Make sure we only register the listeners once | ||
if (!selectionStore._initialized) { | ||
subscribe('libresign:filters:update', selectionStore.reset) | ||
selectionStore._initialized = true | ||
} | ||
|
||
return selectionStore | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<!-- | ||
- SPDX-FileCopyrightText: 2024 LibreCode coop and contributors | ||
- SPDX-License-Identifier: AGPL-3.0-or-later | ||
--> | ||
<template> | ||
<td class="files-list__row-checkbox" | ||
@keyup.esc.exact="resetSelection"> | ||
<NcLoadingIcon v-if="isLoading" :name="loadingLabel" /> | ||
<NcCheckboxRadioSwitch v-else | ||
:aria-label="ariaLabel" | ||
:checked="isSelected" | ||
@update:checked="onSelectionChange" /> | ||
</td> | ||
</template> | ||
|
||
<script> | ||
import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadioSwitch.js' | ||
import NcLoadingIcon from '@nextcloud/vue/dist/Components/NcLoadingIcon.js' | ||
import logger from '../../../logger.js' | ||
import { useFilesStore } from '../../../store/files.js' | ||
import { useKeyboardStore } from '../../../store/keyboard.js' | ||
import { useSelectionStore } from '../../../store/selection.js' | ||
export default { | ||
name: 'FileEntryCheckbox', | ||
components: { | ||
NcCheckboxRadioSwitch, | ||
NcLoadingIcon, | ||
}, | ||
props: { | ||
isLoading: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
source: { | ||
type: Object, | ||
required: true, | ||
}, | ||
}, | ||
setup() { | ||
const filesStore = useFilesStore() | ||
const keyboardStore = useKeyboardStore() | ||
const selectionStore = useSelectionStore() | ||
return { | ||
filesStore, | ||
keyboardStore, | ||
selectionStore, | ||
} | ||
}, | ||
computed: { | ||
selectedFiles() { | ||
return this.selectionStore.selected | ||
}, | ||
isSelected() { | ||
return this.selectedFiles.includes(this.source.nodeId) | ||
}, | ||
index() { | ||
return this.filesStore.ordered.findIndex(nodeId => Number(nodeId) === this.source.nodeId) | ||
}, | ||
ariaLabel() { | ||
return t('libresign', 'Toggle selection for file "{displayName}"', { displayName: this.source.basename }) | ||
}, | ||
loadingLabel() { | ||
return t('libresign', 'File is loading') | ||
}, | ||
}, | ||
methods: { | ||
onSelectionChange(selected) { | ||
const newSelectedIndex = this.index | ||
const lastSelectedIndex = this.selectionStore.lastSelectedIndex | ||
// Get the last selected and select all files in between | ||
if (this.keyboardStore?.shiftKey && lastSelectedIndex !== null) { | ||
const isAlreadySelected = this.selectedFiles.includes(this.source.nodeId) | ||
const start = Math.min(newSelectedIndex, lastSelectedIndex) | ||
const end = Math.max(lastSelectedIndex, newSelectedIndex) | ||
const lastSelection = this.selectionStore.lastSelection | ||
const filesToSelect = this.filesStore.ordered | ||
.slice(start, end + 1) | ||
// If already selected, update the new selection _without_ the current file | ||
const selection = [...new Set([...lastSelection, ...filesToSelect])] | ||
.filter(nodeId => !isAlreadySelected || nodeId !== this.source.nodeId) | ||
logger.debug('Shift key pressed, selecting all files in between', { start, end, filesToSelect, isAlreadySelected }) | ||
// Keep previous lastSelectedIndex to be use for further shift selections | ||
this.selectionStore.set(selection) | ||
return | ||
} | ||
const selection = selected | ||
? [...this.selectedFiles, this.source.nodeId] | ||
: this.selectedFiles.filter(nodeId => nodeId !== this.source.nodeId) | ||
logger.debug('Updating selection', { selection }) | ||
this.selectionStore.set(selection) | ||
this.selectionStore.setLastIndex(newSelectedIndex) | ||
}, | ||
resetSelection() { | ||
this.selectionStore.reset() | ||
}, | ||
}, | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters