-
Notifications
You must be signed in to change notification settings - Fork 9
/
FilePreview.vue
54 lines (45 loc) · 1.52 KB
/
FilePreview.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!--
- SPDX-FileCopyrightText: 2023-2024 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<div :style="canLoadPreview ? { backgroundImage: `url(${previewURL})`} : undefined"
:class="fileListIconStyles['file-picker__file-icon']">
<template v-if="!canLoadPreview">
<IconFile v-if="isFile" :size="20" />
<IconFolder v-else :size="20" />
</template>
</div>
</template>
<script setup lang="ts">
import { FileType, type Node } from '@nextcloud/files'
import { computed, ref, watchEffect } from 'vue'
import { getPreviewURL } from '../../composables/preview'
import IconFile from 'vue-material-design-icons/File.vue'
import IconFolder from 'vue-material-design-icons/Folder.vue'
// CSS modules
import fileListIconStylesModule from './FileListIcon.module.scss'
// workaround for vue2.7 bug, can be removed with vue3
const fileListIconStyles = ref(fileListIconStylesModule)
const props = defineProps<{
node: Node
cropImagePreviews: boolean
}>()
const previewURL = computed(() => getPreviewURL(props.node, { cropPreview: props.cropImagePreviews }))
const isFile = computed(() => props.node.type === FileType.File)
const canLoadPreview = ref(false)
watchEffect(() => {
canLoadPreview.value = false
if (previewURL.value) {
const loader = new Image()
loader.src = previewURL.value.href
loader.onerror = () => loader.remove()
loader.onload = () => { canLoadPreview.value = true; loader.remove() }
}
})
</script>
<script lang="ts">
export default {
name: 'FilePreview',
}
</script>