Skip to content

Commit

Permalink
refactor: Use composable for currentView to make it reactive when s…
Browse files Browse the repository at this point in the history
…hared with other Vue apps

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Jun 21, 2024
1 parent 96e629e commit eed437f
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 55 deletions.
16 changes: 10 additions & 6 deletions apps/files/src/components/BreadCrumbs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

<script lang="ts">
import type { Node } from '@nextcloud/files'
import type { FileSource } from '../types.ts'
import { basename } from 'path'
import { defineComponent } from 'vue'
Expand All @@ -45,6 +46,7 @@ import NcBreadcrumb from '@nextcloud/vue/dist/Components/NcBreadcrumb.js'
import NcBreadcrumbs from '@nextcloud/vue/dist/Components/NcBreadcrumbs.js'
import NcIconSvgWrapper from '@nextcloud/vue/dist/Components/NcIconSvgWrapper.js'
import { useCurrentView } from '../composables/useCurrentView'
import { onDropInternalFiles, dataTransferToFileTree, onDropExternalFiles } from '../services/DropService'
import { showError } from '@nextcloud/dialogs'
import { useDragAndDropStore } from '../store/dragging.ts'
Expand All @@ -54,7 +56,6 @@ import { useSelectionStore } from '../store/selection.ts'
import { useUploaderStore } from '../store/uploader.ts'
import filesListWidthMixin from '../mixins/filesListWidth.ts'
import logger from '../logger'
import type { FileSource } from '../types.ts'
export default defineComponent({
name: 'BreadCrumbs',
Expand Down Expand Up @@ -82,21 +83,20 @@ export default defineComponent({
const pathsStore = usePathsStore()
const selectionStore = useSelectionStore()
const uploaderStore = useUploaderStore()
const { currentView } = useCurrentView()
return {
draggingStore,
filesStore,
pathsStore,
selectionStore,
uploaderStore,
currentView,
}
},
computed: {
currentView() {
return this.$navigation.active
},
dirs(): string[] {
const cumulativePath = (acc: string) => (value: string) => (acc += `${value}/`)
// Generate a cumulative path for each path segment: ['/', '/foo', '/foo/bar', ...] etc
Expand Down Expand Up @@ -151,7 +151,7 @@ export default defineComponent({
return this.filesStore.getNode(source)
},
getFileSourceFromPath(path: string): FileSource | undefined {
return this.pathsStore.getPath(this.currentView?.id, path)
return this.pathsStore.getPath(this.currentView!.id, path)
},
getDirDisplayName(path: string): string {
if (path === '/') {
Expand All @@ -170,6 +170,10 @@ export default defineComponent({
},
onDragOver(event: DragEvent, path: string) {
if (!event.dataTransfer) {
return
}
// Cannot drop on the current directory
if (path === this.dirs[this.dirs.length - 1]) {
event.dataTransfer.dropEffect = 'none'
Expand Down
22 changes: 14 additions & 8 deletions apps/files/src/components/DragAndDropNotice.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { Folder, Permission } from '@nextcloud/files'
import type { Folder } from '@nextcloud/files'
import { Permission } from '@nextcloud/files'
import { showError } from '@nextcloud/dialogs'
import { translate as t } from '@nextcloud/l10n'
import { UploadStatus } from '@nextcloud/upload'
import { defineComponent, type PropType } from 'vue'
import TrayArrowDownIcon from 'vue-material-design-icons/TrayArrowDown.vue'
import logger from '../logger.js'
import { useCurrentView } from '../composables/useCurrentView'
import { dataTransferToFileTree, onDropExternalFiles } from '../services/DropService'
import logger from '../logger.js'
export default defineComponent({
name: 'DragAndDropNotice',
Expand All @@ -46,22 +48,26 @@ export default defineComponent({
props: {
currentFolder: {
type: Folder,
type: Object as PropType<Folder>,
required: true,
},
},
setup() {
const { currentView } = useCurrentView()
return {
currentView,
}
},
data() {
return {
dragover: false,
}
},
computed: {
currentView() {
return this.$navigation.active
},
/**
* Check if the current folder has create permissions
*/
Expand Down
16 changes: 11 additions & 5 deletions apps/files/src/components/FileEntry.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@
<script lang="ts">
import { defineComponent } from 'vue'
import { Permission, formatFileSize } from '@nextcloud/files'
import { formatFileSize } from '@nextcloud/files'
import moment from '@nextcloud/moment'
import { useCurrentView } from '../composables/useCurrentView'
import { useActionsMenuStore } from '../store/actionsmenu.ts'
import { useDragAndDropStore } from '../store/dragging.ts'
import { useFilesStore } from '../store/files.ts'
Expand Down Expand Up @@ -140,12 +141,16 @@ export default defineComponent({
const filesStore = useFilesStore()
const renamingStore = useRenamingStore()
const selectionStore = useSelectionStore()
const { currentView } = useCurrentView()
return {
actionsMenuStore,
draggingStore,
filesStore,
renamingStore,
selectionStore,
currentView,
}
},
Expand Down Expand Up @@ -179,21 +184,22 @@ export default defineComponent({
},
size() {
const size = parseInt(this.source.size, 10)
if (typeof size !== 'number' || isNaN(size) || size < 0) {
const size = this.source.size
if (!size || size < 0) {
return this.t('files', 'Pending')
}
return formatFileSize(size, true)
},
sizeOpacity() {
const maxOpacitySize = 10 * 1024 * 1024
const size = parseInt(this.source.size, 10)
const size = this.source.size
if (!size || isNaN(size) || size < 0) {
return {}
}
const ratio = Math.round(Math.min(100, 100 * Math.pow((this.source.size / maxOpacitySize), 2)))
const ratio = Math.round(Math.min(100, 100 * Math.pow((size / maxOpacitySize), 2)))
return {
color: `color-mix(in srgb, var(--color-main-text) ${ratio}%, var(--color-text-maxcontrast))`,
}
Expand Down
23 changes: 15 additions & 8 deletions apps/files/src/components/FileEntry/FileEntryActions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,22 @@
</template>
<script lang="ts">
import type { PropType } from 'vue'
import type { PropType, ShallowRef } from 'vue'
import type { FileAction, Node, View } from '@nextcloud/files'
import { DefaultType, FileAction, Node, NodeStatus, View, getFileActions } from '@nextcloud/files'
import { DefaultType, NodeStatus, getFileActions } from '@nextcloud/files'
import { showError, showSuccess } from '@nextcloud/dialogs'
import { translate as t } from '@nextcloud/l10n'
import { defineComponent } from 'vue'
import NcActionButton from '@nextcloud/vue/dist/Components/NcActionButton.js'
import NcActions from '@nextcloud/vue/dist/Components/NcActions.js'
import NcActionSeparator from '@nextcloud/vue/dist/Components/NcActionSeparator.js'
import NcIconSvgWrapper from '@nextcloud/vue/dist/Components/NcIconSvgWrapper.js'
import NcLoadingIcon from '@nextcloud/vue/dist/Components/NcLoadingIcon.js'
import ArrowLeftIcon from 'vue-material-design-icons/ArrowLeft.vue'
import Vue, { defineComponent } from 'vue'
import { useCurrentView } from '../../composables/useCurrentView'
import CustomElementRender from '../CustomElementRender.vue'
import logger from '../../logger.js'
Expand Down Expand Up @@ -132,6 +134,14 @@ export default defineComponent({
},
},
setup() {
const { currentView } = useCurrentView()
return {
currentView: currentView as ShallowRef<View>,
}
},
data() {
return {
openedSubmenu: null as FileAction | null,
Expand All @@ -143,9 +153,6 @@ export default defineComponent({
// Remove any trailing slash but leave root slash
return (this.$route?.query?.dir?.toString() || '/').replace(/^(.+)\/$/, '$1')
},
currentView(): View {
return this.$navigation.active as View
},
isLoading() {
return this.source.status === NodeStatus.LOADING
},
Expand Down Expand Up @@ -269,7 +276,7 @@ export default defineComponent({
try {
// Set the loading marker
this.$emit('update:loading', action.id)
Vue.set(this.source, 'status', NodeStatus.LOADING)
this.$set(this.source, 'status', NodeStatus.LOADING)
const success = await action.exec(this.source, this.currentView, this.currentDir)
Expand All @@ -289,7 +296,7 @@ export default defineComponent({
} finally {
// Reset the loading marker
this.$emit('update:loading', '')
Vue.set(this.source, 'status', undefined)
this.$set(this.source, 'status', undefined)
// If that was a submenu, we just go back after the action
if (isSubmenu) {
Expand Down
17 changes: 9 additions & 8 deletions apps/files/src/components/FileEntry/FileEntryName.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</template>

<script lang="ts">
import type { Node, View } from '@nextcloud/files'
import type { Node } from '@nextcloud/files'
import type { PropType } from 'vue'
import { showError, showSuccess } from '@nextcloud/dialogs'
Expand All @@ -46,10 +46,11 @@ import { FileType, NodeStatus, Permission } from '@nextcloud/files'
import { loadState } from '@nextcloud/initial-state'
import { translate as t } from '@nextcloud/l10n'
import axios, { isAxiosError } from '@nextcloud/axios'
import Vue, { defineComponent } from 'vue'
import { defineComponent } from 'vue'
import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js'
import { useCurrentView } from '../../composables/useCurrentView'
import { useRenamingStore } from '../../store/renaming.ts'
import logger from '../../logger.js'
Expand Down Expand Up @@ -90,17 +91,17 @@ export default defineComponent({
},
setup() {
const { currentView } = useCurrentView()
const renamingStore = useRenamingStore()
return {
currentView,
renamingStore,
}
},
computed: {
currentView(): View {
return this.$navigation.active as View
},
isRenaming() {
return this.renamingStore.renamingNode === this.source
},
Expand Down Expand Up @@ -282,7 +283,7 @@ export default defineComponent({
}
// Set loading state
Vue.set(this.source, 'status', NodeStatus.LOADING)
this.$set(this.source, 'status', NodeStatus.LOADING)
// Update node
this.source.rename(newName)
Expand Down Expand Up @@ -327,7 +328,7 @@ export default defineComponent({
// Unknown error
showError(t('files', 'Could not rename "{oldName}"', { oldName }))
} finally {
Vue.set(this.source, 'status', undefined)
this.$set(this.source, 'status', undefined)
}
},
Expand Down
5 changes: 5 additions & 0 deletions apps/files/src/components/FileEntryGrid.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<script lang="ts">
import { defineComponent } from 'vue'
import { useCurrentView } from '../composables/useCurrentView'
import { useActionsMenuStore } from '../store/actionsmenu.ts'
import { useDragAndDropStore } from '../store/dragging.ts'
import { useFilesStore } from '../store/files.ts'
Expand Down Expand Up @@ -93,12 +94,16 @@ export default defineComponent({
const filesStore = useFilesStore()
const renamingStore = useRenamingStore()
const selectionStore = useSelectionStore()
const { currentView } = useCurrentView()
return {
actionsMenuStore,
draggingStore,
filesStore,
renamingStore,
selectionStore,
currentView,
}
},
Expand Down
4 changes: 0 additions & 4 deletions apps/files/src/components/FileEntryMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ export default defineComponent({
},

computed: {
currentView(): View {
return this.$navigation.active as View
},

currentDir() {
// Remove any trailing slash but leave root slash
return (this.$route?.query?.dir?.toString() || '/').replace(/^(.+)\/$/, '$1')
Expand Down
20 changes: 12 additions & 8 deletions apps/files/src/components/FilesListTableHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,21 @@
</template>

<script lang="ts">
import type { Node } from '@nextcloud/files'
import type { PropType } from 'vue'
import type { FileSource } from '../types.ts'
import { translate as t } from '@nextcloud/l10n'
import { defineComponent } from 'vue'
import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadioSwitch.js'
import { defineComponent, type PropType } from 'vue'
import FilesListTableHeaderButton from './FilesListTableHeaderButton.vue'
import { useCurrentView } from '../composables/useCurrentView'
import { useFilesStore } from '../store/files.ts'
import { useSelectionStore } from '../store/selection.ts'
import FilesListTableHeaderButton from './FilesListTableHeaderButton.vue'
import filesSortingMixin from '../mixins/filesSorting.ts'
import logger from '../logger.js'
import type { Node } from '@nextcloud/files'
import type { FileSource } from '../types.ts'
export default defineComponent({
name: 'FilesListTableHeader',
Expand Down Expand Up @@ -100,17 +104,17 @@ export default defineComponent({
setup() {
const filesStore = useFilesStore()
const selectionStore = useSelectionStore()
const { currentView } = useCurrentView()
return {
filesStore,
selectionStore,
currentView,
}
},
computed: {
currentView() {
return this.$navigation.active
},
columns() {
// Hide columns if the list is too small
if (this.filesListWidth < 512) {
Expand Down
Loading

0 comments on commit eed437f

Please sign in to comment.