Merge pull request #3798 from thelounge/richrd/image-viewer-navigation

Implement navigation in image viewer
This commit is contained in:
Pavel Djundik 2020-04-13 13:05:29 +03:00 committed by GitHub
commit 58553d7691
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 10 deletions

View File

@ -9,6 +9,20 @@
> >
<template v-if="link !== null"> <template v-if="link !== null">
<button class="close-btn" aria-label="Close"></button> <button class="close-btn" aria-label="Close"></button>
<button
v-if="previousImage"
class="previous-image-btn"
aria-label="Previous image"
@click.stop="previous"
></button>
<button
v-if="nextImage"
class="next-image-btn"
aria-label="Next image"
@click.stop="next"
></button>
<a class="open-btn" :href="link.link" target="_blank" rel="noopener"></a> <a class="open-btn" :href="link.link" target="_blank" rel="noopener"></a>
<img <img
@ -25,11 +39,17 @@
</template> </template>
<script> <script>
import Mousetrap from "mousetrap";
export default { export default {
name: "ImageViewer", name: "ImageViewer",
data() { data() {
return { return {
link: null, link: null,
previousImage: null,
nextImage: null,
channel: null,
position: { position: {
x: 0, x: 0,
y: 0, y: 0,
@ -56,20 +76,25 @@ export default {
}, },
}, },
watch: { watch: {
link() { link(newLink, oldLink) {
// TODO: history.pushState // TODO: history.pushState
if (this.link === null) { if (newLink === null) {
this.$root.$off("escapekey", this.closeViewer);
this.$root.$off("resize", this.correctPosition);
Mousetrap.unbind("left", this.previous);
Mousetrap.unbind("right", this.next);
return; return;
} }
this.$root.$on("resize", this.correctPosition); this.setPrevNextImages();
},
}, if (!oldLink) {
mounted() {
this.$root.$on("escapekey", this.closeViewer); this.$root.$on("escapekey", this.closeViewer);
this.$root.$on("resize", this.correctPosition);
Mousetrap.bind("left", this.previous);
Mousetrap.bind("right", this.next);
}
}, },
destroyed() {
this.$root.$off("escapekey", this.closeViewer);
}, },
methods: { methods: {
closeViewer() { closeViewer() {
@ -77,9 +102,36 @@ export default {
return; return;
} }
this.$root.$off("resize", this.correctPosition); this.channel = null;
this.previousImage = null;
this.nextImage = null;
this.link = null; this.link = null;
}, },
setPrevNextImages() {
if (!this.channel) {
return null;
}
const links = this.channel.messages
.map((msg) => msg.previews)
.flat()
.filter((preview) => preview.thumb);
const currentIndex = links.indexOf(this.link);
this.previousImage = links[currentIndex - 1] || null;
this.nextImage = links[currentIndex + 1] || null;
},
previous() {
if (this.previousImage) {
this.link = this.previousImage;
}
},
next() {
if (this.nextImage) {
this.link = this.nextImage;
}
},
onImageLoad() { onImageLoad() {
this.prepareImage(); this.prepareImage();
}, },

View File

@ -137,6 +137,7 @@ export default {
props: { props: {
link: Object, link: Object,
keepScrollPosition: Function, keepScrollPosition: Function,
channel: Object,
}, },
data() { data() {
return { return {
@ -214,6 +215,7 @@ export default {
e.preventDefault(); e.preventDefault();
const imageViewer = this.$root.$refs.app.$refs.imageViewer; const imageViewer = this.$root.$refs.app.$refs.imageViewer;
imageViewer.channel = this.channel;
imageViewer.link = this.link; imageViewer.link = this.link;
}, },
onMoreClick() { onMoreClick() {

View File

@ -32,6 +32,7 @@
:key="preview.link" :key="preview.link"
:keep-scroll-position="keepScrollPosition" :keep-scroll-position="keepScrollPosition"
:link="preview" :link="preview"
:channel="channel"
/> />
</span> </span>
</template> </template>
@ -70,6 +71,7 @@
:key="preview.link" :key="preview.link"
:keep-scroll-position="keepScrollPosition" :keep-scroll-position="keepScrollPosition"
:link="preview" :link="preview"
:channel="channel"
/> />
</span> </span>
</template> </template>