diff --git a/src/store/keyboard.js b/src/store/keyboard.js new file mode 100644 index 000000000..20693a71d --- /dev/null +++ b/src/store/keyboard.js @@ -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 +} diff --git a/src/store/selection.js b/src/store/selection.js new file mode 100644 index 000000000..a6e6ed3b1 --- /dev/null +++ b/src/store/selection.js @@ -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 +} diff --git a/src/views/FilesList/FileEntry/FileEntry.vue b/src/views/FilesList/FileEntry/FileEntry.vue index fd832d4fe..69e4c57e2 100644 --- a/src/views/FilesList/FileEntry/FileEntry.vue +++ b/src/views/FilesList/FileEntry/FileEntry.vue @@ -5,6 +5,10 @@