Skip to content

Commit

Permalink
fix(files) selection store typing
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv authored and backportbot[bot] committed Feb 7, 2024
1 parent c48edcb commit 468bb92
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 41 deletions.
19 changes: 9 additions & 10 deletions apps/files/src/components/FileEntry.vue
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ import type { PropType } from 'vue'
import { extname, join } from 'path'
import { FileType, formatFileSize, Permission, Folder, File as NcFile, NodeStatus, Node, View } from '@nextcloud/files'
import { getUploader } from '@nextcloud/upload'
import { Upload, getUploader } from '@nextcloud/upload'
import { showError, showSuccess } from '@nextcloud/dialogs'
import { translate as t } from '@nextcloud/l10n'
import { vOnClickOutside } from '@vueuse/components'
Expand Down Expand Up @@ -229,7 +229,7 @@ export default defineComponent({
return this.$route.params?.fileid || this.$route.query?.fileid || null
},
fileid() {
return this.source?.fileid?.toString?.()
return this.source?.fileid
},
uniqueId() {
return hashCode(this.source.source)
Expand Down Expand Up @@ -304,7 +304,7 @@ export default defineComponent({
return this.selectionStore.selected
},
isSelected() {
return this.selectedFiles.includes(this.fileid)
return this.fileid && this.selectedFiles.includes(this.fileid)
},
isRenaming() {
Expand All @@ -315,7 +315,7 @@ export default defineComponent({
},
isActive() {
return this.fileid === this.currentFileId?.toString?.()
return this.fileid?.toString?.() === this.currentFileId?.toString?.()
},
canDrag() {
Expand All @@ -341,7 +341,7 @@ export default defineComponent({
}
// If the current folder is also being dragged, we can't drop it on itself
if (this.draggingFiles.includes(this.fileid)) {
if (this.fileid && this.draggingFiles.includes(this.fileid)) {
return false
}
Expand All @@ -350,7 +350,7 @@ export default defineComponent({
openedMenu: {
get() {
return this.actionsMenuStore.opened === this.uniqueId
return this.actionsMenuStore.opened === this.uniqueId.toString()
},
set(opened) {
// Only reset when opening a new menu
Expand All @@ -362,7 +362,7 @@ export default defineComponent({
root.style.removeProperty('--mouse-pos-y')
}
this.actionsMenuStore.opened = opened ? this.uniqueId : null
this.actionsMenuStore.opened = opened ? this.uniqueId.toString() : null
},
},
},
Expand Down Expand Up @@ -408,7 +408,7 @@ export default defineComponent({
// If the clicked row is in the selection, open global menu
const isMoreThanOneSelected = this.selectedFiles.length > 1
this.actionsMenuStore.opened = this.isSelected && isMoreThanOneSelected ? 'global' : this.uniqueId
this.actionsMenuStore.opened = this.isSelected && isMoreThanOneSelected ? 'global' : this.uniqueId.toString()
// Prevent any browser defaults
event.preventDefault()
Expand Down Expand Up @@ -460,7 +460,7 @@ export default defineComponent({
async onDragStart(event: DragEvent) {
event.stopPropagation()
if (!this.canDrag) {
if (!this.canDrag || !this.fileid) {
event.preventDefault()
event.stopPropagation()
return
Expand Down Expand Up @@ -545,7 +545,6 @@ export default defineComponent({
logger.debug('Files uploaded successfully')
showSuccess(t('files', 'Files uploaded successfully'))
return
}
Expand Down
11 changes: 6 additions & 5 deletions apps/files/src/components/FileEntry/FileEntryCheckbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<script lang="ts">
import { Node, FileType } from '@nextcloud/files'
import { translate as t } from '@nextcloud/l10n'
import Vue, { PropType } from 'vue'
import { type PropType, defineComponent } from 'vue'
import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadioSwitch.js'
import NcLoadingIcon from '@nextcloud/vue/dist/Components/NcLoadingIcon.js'
Expand All @@ -42,7 +42,7 @@ import { useKeyboardStore } from '../../store/keyboard.ts'
import { useSelectionStore } from '../../store/selection.ts'
import logger from '../../logger.js'
export default Vue.extend({
export default defineComponent({
name: 'FileEntryCheckbox',
components: {
Expand All @@ -52,7 +52,7 @@ export default Vue.extend({
props: {
fileid: {
type: String,
type: Number,
required: true,
},
isLoading: {
Expand Down Expand Up @@ -86,7 +86,7 @@ export default Vue.extend({
return this.selectedFiles.includes(this.fileid)
},
index() {
return this.nodes.findIndex((node: Node) => node.fileid === parseInt(this.fileid))
return this.nodes.findIndex((node: Node) => node.fileid === this.fileid)
},
isFile() {
return this.source.type === FileType.File
Expand All @@ -112,8 +112,9 @@ export default Vue.extend({
const lastSelection = this.selectionStore.lastSelection
const filesToSelect = this.nodes
.map(file => file.fileid?.toString?.())
.map(file => file.fileid)
.slice(start, end + 1)
.filter(Boolean) as number[]
// If already selected, update the new selection _without_ the current file
const selection = [...lastSelection, ...filesToSelect]
Expand Down
35 changes: 23 additions & 12 deletions apps/files/src/components/FileEntryGrid.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ import type { PropType } from 'vue'
import { extname, join } from 'path'
import { FileType, Permission, Folder, File as NcFile, NodeStatus, Node, View } from '@nextcloud/files'
import { getUploader } from '@nextcloud/upload'
import { Upload, getUploader } from '@nextcloud/upload'
import { showError, showSuccess } from '@nextcloud/dialogs'
import { translate as t } from '@nextcloud/l10n'
import { generateUrl } from '@nextcloud/router'
import { vOnClickOutside } from '@vueuse/components'
import Vue from 'vue'
import Vue, { defineComponent } from 'vue'
import { action as sidebarAction } from '../actions/sidebarAction.ts'
import { getDragAndDropPreview } from '../utils/dragUtils.ts'
Expand All @@ -102,7 +102,7 @@ import logger from '../logger.js'
Vue.directive('onClickOutside', vOnClickOutside)
export default Vue.extend({
export default defineComponent({
name: 'FileEntryGrid',
components: {
Expand Down Expand Up @@ -163,7 +163,7 @@ export default Vue.extend({
return this.$route.params?.fileid || this.$route.query?.fileid || null
},
fileid() {
return this.source?.fileid?.toString?.()
return this.source?.fileid
},
uniqueId() {
return hashCode(this.source.source)
Expand Down Expand Up @@ -194,15 +194,15 @@ export default Vue.extend({
return this.selectionStore.selected
},
isSelected() {
return this.selectedFiles.includes(this.fileid)
return this.fileid && this.selectedFiles.includes(this.fileid)
},
isRenaming() {
return this.renamingStore.renamingNode === this.source
},
isActive() {
return this.fileid === this.currentFileId?.toString?.()
return this.fileid?.toString?.() === this.currentFileId?.toString?.()
},
canDrag() {
Expand All @@ -224,7 +224,7 @@ export default Vue.extend({
}
// If the current folder is also being dragged, we can't drop it on itself
if (this.draggingFiles.includes(this.fileid)) {
if (this.fileid && this.draggingFiles.includes(this.fileid)) {
return false
}
Expand All @@ -233,10 +233,19 @@ export default Vue.extend({
openedMenu: {
get() {
return this.actionsMenuStore.opened === this.uniqueId
return this.actionsMenuStore.opened === this.uniqueId.toString()
},
set(opened) {
this.actionsMenuStore.opened = opened ? this.uniqueId : null
// Only reset when opening a new menu
if (opened) {
// Reset any right click position override on close
// Wait for css animation to be done
const root = this.$root.$el as HTMLElement
root.style.removeProperty('--mouse-pos-x')
root.style.removeProperty('--mouse-pos-y')
}
this.actionsMenuStore.opened = opened ? this.uniqueId.toString() : null
},
},
},
Expand Down Expand Up @@ -327,13 +336,16 @@ export default Vue.extend({
async onDragStart(event: DragEvent) {
event.stopPropagation()
if (!this.canDrag) {
if (!this.canDrag || !this.fileid) {
event.preventDefault()
event.stopPropagation()
return
}
logger.debug('Drag started')
logger.debug('Drag started', { event })
// Make sure that we're not dragging a file like the preview
event.dataTransfer?.clearData?.()
// Reset any renaming
this.renamingStore.$reset()
Expand Down Expand Up @@ -409,7 +421,6 @@ export default Vue.extend({
logger.debug('Files uploaded successfully')
showSuccess(t('files', 'Files uploaded successfully'))
return
}
Expand Down
13 changes: 6 additions & 7 deletions apps/files/src/components/FilesListTableHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,21 @@
<script lang="ts">
import { translate as t } from '@nextcloud/l10n'
import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadioSwitch.js'
import Vue from 'vue'
import { defineComponent, type PropType } from 'vue'
import { useFilesStore } from '../store/files.ts'
import { useSelectionStore } from '../store/selection.ts'
import FilesListTableHeaderActions from './FilesListTableHeaderActions.vue'
import FilesListTableHeaderButton from './FilesListTableHeaderButton.vue'
import filesSortingMixin from '../mixins/filesSorting.ts'
import logger from '../logger.js'
import type { Node } from '@nextcloud/files'
export default Vue.extend({
export default defineComponent({
name: 'FilesListTableHeader',
components: {
FilesListTableHeaderButton,
NcCheckboxRadioSwitch,
FilesListTableHeaderActions,
},
mixins: [
Expand All @@ -105,7 +104,7 @@ export default Vue.extend({
default: false,
},
nodes: {
type: Array,
type: Array as PropType<Node[]>,
required: true,
},
filesListWidth: {
Expand Down Expand Up @@ -181,13 +180,13 @@ export default Vue.extend({
'files-list__column': true,
'files-list__column--sortable': !!column.sort,
'files-list__row-column-custom': true,
[`files-list__row-${this.currentView.id}-${column.id}`]: true,
[`files-list__row-${this.currentView?.id}-${column.id}`]: true,
}
},
onToggleAll(selected) {
if (selected) {
const selection = this.nodes.map(node => node.fileid.toString())
const selection = this.nodes.map(node => node.fileid).filter(Boolean) as number[]
logger.debug('Added all nodes to selection', { selection })
this.selectionStore.setLastIndex(null)
this.selectionStore.set(selection)
Expand Down
13 changes: 7 additions & 6 deletions apps/files/src/components/FilesListTableHeaderActions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,26 @@
</template>

<script lang="ts">
import { NodeStatus, getFileActions } from '@nextcloud/files'
import { Node, NodeStatus, View, getFileActions } from '@nextcloud/files'
import { showError, showSuccess } from '@nextcloud/dialogs'
import { translate } from '@nextcloud/l10n'
import NcActionButton from '@nextcloud/vue/dist/Components/NcActionButton.js'
import NcActions from '@nextcloud/vue/dist/Components/NcActions.js'
import NcIconSvgWrapper from '@nextcloud/vue/dist/Components/NcIconSvgWrapper.js'
import NcLoadingIcon from '@nextcloud/vue/dist/Components/NcLoadingIcon.js'
import Vue from 'vue'
import Vue, { defineComponent, type PropType } from 'vue'
import { useActionsMenuStore } from '../store/actionsmenu.ts'
import { useFilesStore } from '../store/files.ts'
import { useSelectionStore } from '../store/selection.ts'
import filesListWidthMixin from '../mixins/filesListWidth.ts'
import logger from '../logger.js'
import type { FileId } from '../types'
// The registered actions list
const actions = getFileActions()
export default Vue.extend({
export default defineComponent({
name: 'FilesListTableHeaderActions',
components: {
Expand All @@ -76,11 +77,11 @@ export default Vue.extend({
props: {
currentView: {
type: Object,
type: Object as PropType<View>,
required: true,
},
selectedNodes: {
type: Array,
type: Array as PropType<FileId[]>,
default: () => ([]),
},
},
Expand Down Expand Up @@ -117,7 +118,7 @@ export default Vue.extend({
nodes() {
return this.selectedNodes
.map(fileid => this.getNode(fileid))
.filter(node => node)
.filter(Boolean) as Node[]
},
areSomeNodesLoading() {
Expand Down
2 changes: 1 addition & 1 deletion apps/files/src/services/DropService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { translate as t } from '@nextcloud/l10n'

import logger from '../logger.js'

export const handleDrop = async (data: DataTransfer) => {
export const handleDrop = async (data: DataTransfer): Promise<Upload[]> => {
// TODO: Maybe handle `getAsFileSystemHandle()` in the future

const uploads = [] as Upload[]
Expand Down

0 comments on commit 468bb92

Please sign in to comment.