thelounge/client/components/LinkPreview.vue

264 lines
5.8 KiB
Vue
Raw Normal View History

2018-07-10 13:57:11 +02:00
<template>
2019-11-25 20:37:51 +01:00
<div
v-if="link.shown"
v-show="link.sourceLoaded || link.type === 'link'"
ref="container"
class="preview"
dir="ltr"
>
2018-07-12 10:26:12 +02:00
<div
ref="content"
2019-07-17 11:33:59 +02:00
:class="['toggle-content', 'toggle-type-' + link.type, {opened: isContentShown}]"
>
2018-07-10 13:57:11 +02:00
<template v-if="link.type === 'link'">
2018-07-10 14:04:25 +02:00
<a
v-if="link.thumb"
2019-11-25 20:37:51 +01:00
v-show="link.sourceLoaded"
2018-07-10 14:04:25 +02:00
:href="link.link"
class="toggle-thumbnail"
target="_blank"
rel="noopener"
2019-09-20 20:10:48 +02:00
@click="onThumbnailClick"
>
2018-07-10 14:04:25 +02:00
<img
:src="link.thumb"
decoding="async"
alt=""
2018-07-10 16:44:50 +02:00
class="thumb"
@error="onThumbnailError"
@abort="onThumbnailError"
@load="onPreviewReady"
2019-07-17 11:33:59 +02:00
/>
2018-07-10 13:57:11 +02:00
</a>
<div class="toggle-text" dir="auto">
2018-07-10 13:57:11 +02:00
<div class="head">
<div class="overflowable">
2018-07-10 14:04:25 +02:00
<a
:href="link.link"
:title="link.head"
target="_blank"
rel="noopener"
2019-07-17 11:33:59 +02:00
>{{ link.head }}</a
>
2018-07-10 13:57:11 +02:00
</div>
2018-07-10 14:04:25 +02:00
<button
2018-07-12 10:26:12 +02:00
v-if="showMoreButton"
:aria-expanded="isContentShown"
:aria-label="moreButtonLabel"
dir="auto"
2018-07-10 14:04:25 +02:00
class="more"
@click="onMoreClick"
2019-07-17 11:33:59 +02:00
>
<span class="more-caret" />
</button>
2018-07-10 13:57:11 +02:00
</div>
<div class="body overflowable">
2019-07-17 11:33:59 +02:00
<a :href="link.link" :title="link.body" target="_blank" rel="noopener">{{
link.body
}}</a>
2018-07-10 13:57:11 +02:00
</div>
</div>
</template>
<template v-else-if="link.type === 'image'">
2019-09-20 20:10:48 +02:00
<a
:href="link.link"
class="toggle-thumbnail"
target="_blank"
rel="noopener"
@click="onThumbnailClick"
>
2019-11-25 20:37:51 +01:00
<img
v-show="link.sourceLoaded"
:src="link.thumb"
decoding="async"
alt=""
@load="onPreviewReady"
/>
2018-07-10 13:57:11 +02:00
</a>
</template>
<template v-else-if="link.type === 'video'">
2019-11-25 20:37:51 +01:00
<video
v-show="link.sourceLoaded"
preload="metadata"
controls
@canplay="onPreviewReady"
>
2019-07-17 11:33:59 +02:00
<source :src="link.media" :type="link.mediaType" />
2018-07-10 13:57:11 +02:00
</video>
</template>
<template v-else-if="link.type === 'audio'">
2019-11-25 20:37:51 +01:00
<audio
v-show="link.sourceLoaded"
controls
preload="metadata"
@canplay="onPreviewReady"
>
2019-07-17 11:33:59 +02:00
<source :src="link.media" :type="link.mediaType" />
2018-07-10 13:57:11 +02:00
</audio>
</template>
<template v-else-if="link.type === 'error'">
<em v-if="link.error === 'image-too-big'">
This image is larger than {{ imageMaxSize }} and cannot be previewed.
2019-07-17 11:33:59 +02:00
<a :href="link.link" target="_blank" rel="noopener">Click here</a>
2018-07-10 13:57:11 +02:00
to open it in a new window.
</em>
<template v-else-if="link.error === 'message'">
<div>
<em>
A preview could not be loaded.
2019-07-17 11:33:59 +02:00
<a :href="link.link" target="_blank" rel="noopener">Click here</a>
2018-07-10 13:57:11 +02:00
to open it in a new window.
</em>
2019-07-17 11:33:59 +02:00
<br />
2018-07-10 14:04:25 +02:00
<pre class="prefetch-error">{{ link.message }}</pre>
2018-07-10 13:57:11 +02:00
</div>
2018-07-10 14:04:25 +02:00
<button
2018-07-12 10:26:12 +02:00
:aria-expanded="isContentShown"
:aria-label="moreButtonLabel"
2018-07-10 14:04:25 +02:00
class="more"
@click="onMoreClick"
2019-07-17 11:33:59 +02:00
>
<span class="more-caret" />
</button>
2018-07-10 13:57:11 +02:00
</template>
</template>
</div>
</div>
</template>
<script>
2020-03-16 18:58:40 +01:00
import eventbus from "../js/eventbus";
2019-11-16 18:24:03 +01:00
import friendlysize from "../js/helpers/friendlysize";
2018-07-10 13:57:11 +02:00
export default {
name: "LinkPreview",
props: {
link: Object,
keepScrollPosition: Function,
2020-03-09 17:39:05 +01:00
channel: Object,
2018-07-10 13:57:11 +02:00
},
2018-07-12 10:26:12 +02:00
data() {
return {
showMoreButton: false,
isContentShown: false,
};
},
computed: {
moreButtonLabel() {
return this.isContentShown ? "Less" : "More";
},
imageMaxSize() {
2019-11-25 20:37:51 +01:00
if (!this.link.maxSize) {
return;
}
return friendlysize(this.link.maxSize);
},
2018-07-12 10:26:12 +02:00
},
watch: {
"link.type"() {
this.updateShownState();
this.onPreviewUpdate();
},
},
created() {
this.updateShownState();
},
2018-07-10 16:44:50 +02:00
mounted() {
2020-03-16 18:58:40 +01:00
eventbus.on("resize", this.handleResize);
this.onPreviewUpdate();
2018-07-10 16:44:50 +02:00
},
2021-03-29 04:55:35 +02:00
beforeUnmount() {
2020-03-16 18:58:40 +01:00
eventbus.off("resize", this.handleResize);
},
2021-03-29 04:55:35 +02:00
unmounted() {
// Let this preview go through load/canplay events again,
// Otherwise the browser can cause a resize on video elements
2019-11-25 20:37:51 +01:00
this.link.sourceLoaded = false;
},
2018-07-10 16:44:50 +02:00
methods: {
onPreviewUpdate() {
// Don't display previews while they are loading on the server
if (this.link.type === "loading") {
return;
}
2019-11-25 20:37:51 +01:00
// Error does not have any media to render
if (this.link.type === "error") {
this.onPreviewReady();
}
// If link doesn't have a thumbnail, render it
2019-11-25 20:37:51 +01:00
if (this.link.type === "link") {
this.handleResize();
this.keepScrollPosition();
}
},
2018-07-10 16:44:50 +02:00
onPreviewReady() {
this.keepScrollPosition();
2019-11-25 20:37:51 +01:00
if (this.link.type === "link") {
this.handleResize();
}
},
onThumbnailError() {
// If thumbnail fails to load, hide it and show the preview without it
this.link.thumb = "";
this.onPreviewReady();
2018-07-10 16:44:50 +02:00
},
2019-09-20 20:10:48 +02:00
onThumbnailClick(e) {
e.preventDefault();
const imageViewer = this.$root.$refs.app.$refs.imageViewer;
2020-03-09 17:39:05 +01:00
imageViewer.channel = this.channel;
2019-09-20 20:10:48 +02:00
imageViewer.link = this.link;
},
2018-07-12 10:26:12 +02:00
onMoreClick() {
this.isContentShown = !this.isContentShown;
2018-07-25 13:04:10 +02:00
this.keepScrollPosition();
2018-07-12 10:26:12 +02:00
},
handleResize() {
this.$nextTick(() => {
if (!this.$refs.content) {
return;
}
2019-07-17 11:33:59 +02:00
this.showMoreButton =
this.$refs.content.offsetWidth >= this.$refs.container.offsetWidth;
});
},
updateShownState() {
// User has manually toggled the preview, do not apply default
if (this.link.shown !== null) {
return;
}
let defaultState = false;
switch (this.link.type) {
2019-07-17 11:33:59 +02:00
case "error":
2019-12-23 11:15:23 +01:00
// Collapse all errors by default unless its a message about image being too big
if (this.link.error === "image-too-big") {
defaultState = this.$store.state.settings.media;
}
2019-07-17 11:33:59 +02:00
break;
case "link":
defaultState = this.$store.state.settings.links;
2019-07-17 11:33:59 +02:00
break;
default:
defaultState = this.$store.state.settings.media;
}
this.link.shown = defaultState;
},
2018-07-10 16:44:50 +02:00
},
2018-07-10 13:57:11 +02:00
};
</script>