Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(files): Add proper visual loading feedback for image preview #45431

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 26 additions & 24 deletions apps/files/src/components/FileEntry/FileEntryPreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,13 @@
</template>
</template>

<NcLoadingIcon v-else-if="!error && !loaded" />

<!-- Decorative image, should not be aria documented -->
<img v-else-if="previewUrl && backgroundFailed !== true"
ref="previewImg"
<img v-else-if="previewUrl && loaded"
alt=""
class="files-list__row-icon-preview"
:class="{'files-list__row-icon-preview--loaded': backgroundFailed === false}"
loading="lazy"
:src="previewUrl"
@error="onBackgroundError"
@load="backgroundFailed = false">
:src="previewUrl">

<FileIcon v-else v-once />

Expand All @@ -65,6 +62,7 @@ import { translate as t } from '@nextcloud/l10n'
import { Type as ShareType } from '@nextcloud/sharing'

import Vue from 'vue'
import NcLoadingIcon from '@nextcloud/vue/dist/Components/NcLoadingIcon.js'
import AccountGroupIcon from 'vue-material-design-icons/AccountGroup.vue'
import AccountPlusIcon from 'vue-material-design-icons/AccountPlus.vue'
import FileIcon from 'vue-material-design-icons/File.vue'
Expand Down Expand Up @@ -95,6 +93,7 @@ export default Vue.extend({
KeyIcon,
LinkIcon,
NetworkIcon,
NcLoadingIcon,
TagIcon,
},

Expand Down Expand Up @@ -122,7 +121,8 @@ export default Vue.extend({

data() {
return {
backgroundFailed: undefined as boolean | undefined,
loaded: false,
error: false,
}
},

Expand All @@ -146,10 +146,6 @@ export default Vue.extend({
return null
}

if (this.backgroundFailed === true) {
return null
}

try {
const previewUrl = this.source.attributes.previewUrl
|| generateUrl('/core/preview?fileId={fileid}', {
Expand Down Expand Up @@ -218,22 +214,28 @@ export default Vue.extend({
},
},

watch: {
previewUrl: {
immediate: true,
handler() {
this.error = false
this.loaded = false
if (this.previewUrl) {
const img = new Image()
img.onload = () => { this.loaded = true }
img.onerror = () => { this.error = true }
img.src = this.previewUrl
}
},
},
},

methods: {
// Called from FileEntry
reset() {
// Reset background state to cancel any ongoing requests
this.backgroundFailed = undefined
if (this.$refs.previewImg) {
this.$refs.previewImg.src = ''
}
},

onBackgroundError(event) {
// Do not fail if we just reset the background
if (event.target?.src === '') {
return
}
this.backgroundFailed = true
this.loaded = false
this.error = false
},

t,
Expand Down
6 changes: 0 additions & 6 deletions apps/files/src/components/FilesListVirtual.vue
Original file line number Diff line number Diff line change
Expand Up @@ -550,12 +550,6 @@ export default defineComponent({
// Center and contain the preview
object-fit: contain;
object-position: center;

/* Preview not loaded animation effect */
&:not(.files-list__row-icon-preview--loaded) {
background: var(--color-loading-dark);
// animation: preview-gradient-fade 1.2s ease-in-out infinite;
}
}

&-favorite {
Expand Down
Loading