add plugin vue-observe-visibility

This commit is contained in:
Moriz Wahl 2021-02-24 14:46:21 +01:00
parent 88fd83ec2b
commit ae61baadfb
5 changed files with 95 additions and 80 deletions

View File

@ -6,10 +6,11 @@
<base-card <base-card
:lang="post.language" :lang="post.language"
:class="{ :class="{
'disabled-content': post.disabled, 'disabled-content': post.disabled,
'--blur-image': post.image && post.image.sensitive, '--blur-image': post.image && post.image.sensitive,
}" }"
:highlight="isPinned" :highlight="isPinned"
v-observe-visibility="(isVisible, entry) => visibilityChanged(isVisible, entry, post.id)"
> >
<template v-if="post.image" #heroImage> <template v-if="post.image" #heroImage>
<img :src="post.image | proxyApiUrl" class="image" /> <img :src="post.image | proxyApiUrl" class="image" />
@ -59,84 +60,87 @@
</template> </template>
<script> <script>
import UserTeaser from '~/components/UserTeaser/UserTeaser' import UserTeaser from '~/components/UserTeaser/UserTeaser'
import ContentMenu from '~/components/ContentMenu/ContentMenu' import ContentMenu from '~/components/ContentMenu/ContentMenu'
import HcRibbon from '~/components/Ribbon' import HcRibbon from '~/components/Ribbon'
import CounterIcon from '~/components/_new/generic/CounterIcon/CounterIcon' import CounterIcon from '~/components/_new/generic/CounterIcon/CounterIcon'
import { mapGetters } from 'vuex' import { mapGetters } from 'vuex'
import { postMenuModalsData, deletePostMutation } from '~/components/utils/PostHelpers' import { postMenuModalsData, deletePostMutation } from '~/components/utils/PostHelpers'
export default { export default {
name: 'PostTeaser', name: 'PostTeaser',
components: { components: {
UserTeaser, UserTeaser,
HcRibbon, HcRibbon,
ContentMenu, ContentMenu,
CounterIcon, CounterIcon,
}, },
props: { props: {
post: { post: {
type: Object, type: Object,
required: true, required: true,
}, },
width: { width: {
type: Object, type: Object,
default: () => {}, default: () => {},
}, },
}, },
mounted() { mounted() {
const { image } = this.post const { image } = this.post
if (!image) return if (!image) return
const width = this.$el.offsetWidth const width = this.$el.offsetWidth
const height = Math.min(width / image.aspectRatio, 2000) const height = Math.min(width / image.aspectRatio, 2000)
const imageElement = this.$el.querySelector('.hero-image') const imageElement = this.$el.querySelector('.hero-image')
if (imageElement) { if (imageElement) {
imageElement.style.height = `${height}px` imageElement.style.height = `${height}px`
} }
}, },
computed: { computed: {
...mapGetters({ ...mapGetters({
user: 'auth/user', user: 'auth/user',
}), }),
excerpt() { excerpt() {
return this.$filters.removeLinks(this.post.contentExcerpt) return this.$filters.removeLinks(this.post.contentExcerpt)
}, },
isAuthor() { isAuthor() {
const { author } = this.post const { author } = this.post
if (!author) return false if (!author) return false
return this.user.id === this.post.author.id return this.user.id === this.post.author.id
}, },
menuModalsData() { menuModalsData() {
return postMenuModalsData( return postMenuModalsData(
// "this.post" may not always be defined at the beginning // "this.post" may not always be defined at the beginning
this.post ? this.$filters.truncate(this.post.title, 30) : '', this.post ? this.$filters.truncate(this.post.title, 30) : '',
this.deletePostCallback, this.deletePostCallback,
) )
}, },
isPinned() { isPinned() {
return this.post && this.post.pinned return this.post && this.post.pinned
}, },
}, },
methods: { methods: {
async deletePostCallback() { async deletePostCallback() {
try { try {
const { const {
data: { DeletePost }, data: { DeletePost },
} = await this.$apollo.mutate(deletePostMutation(this.post.id)) } = await this.$apollo.mutate(deletePostMutation(this.post.id))
this.$toast.success(this.$t('delete.contribution.success')) this.$toast.success(this.$t('delete.contribution.success'))
this.$emit('removePostFromList', DeletePost) this.$emit('removePostFromList', DeletePost)
} catch (err) { } catch (err) {
this.$toast.error(err.message) this.$toast.error(err.message)
} }
}, },
pinPost(post) { pinPost(post) {
this.$emit('pinPost', post) this.$emit('pinPost', post)
}, },
unpinPost(post) { unpinPost(post) {
this.$emit('unpinPost', post) this.$emit('unpinPost', post)
}, },
}, visibilityChanged(isVisible, entry, id) {
} console.log('--', isVisible, id)
},
},
}
</script> </script>
<style lang="scss"> <style lang="scss">
.post-teaser, .post-teaser,

View File

@ -115,6 +115,7 @@ export default {
{ src: '~/plugins/izi-toast.js', ssr: false }, { src: '~/plugins/izi-toast.js', ssr: false },
{ src: '~/plugins/vue-filters.js' }, { src: '~/plugins/vue-filters.js' },
{ src: '~/plugins/vue-infinite-loading.js', ssr: false }, { src: '~/plugins/vue-infinite-loading.js', ssr: false },
{ src: '~/plugins/vue-observe-visibility.js', ssr: false },
], ],
router: { router: {

View File

@ -88,6 +88,7 @@
"vue-count-to": "~1.0.13", "vue-count-to": "~1.0.13",
"vue-infinite-loading": "^2.4.5", "vue-infinite-loading": "^2.4.5",
"vue-izitoast": "^1.2.1", "vue-izitoast": "^1.2.1",
"vue-observe-visibility": "^1.0.0",
"vue-scrollto": "^2.17.1", "vue-scrollto": "^2.17.1",
"vue-sweetalert-icons": "~4.2.0", "vue-sweetalert-icons": "~4.2.0",
"vuex-i18n": "~1.13.1", "vuex-i18n": "~1.13.1",

View File

@ -0,0 +1,4 @@
import Vue from 'vue'
import VueObserveVisibility from 'vue-observe-visibility'
Vue.use(VueObserveVisibility)

View File

@ -17770,6 +17770,11 @@ vue-no-ssr@^1.1.1:
resolved "https://registry.yarnpkg.com/vue-no-ssr/-/vue-no-ssr-1.1.1.tgz#875f3be6fb0ae41568a837f3ac1a80eaa137b998" resolved "https://registry.yarnpkg.com/vue-no-ssr/-/vue-no-ssr-1.1.1.tgz#875f3be6fb0ae41568a837f3ac1a80eaa137b998"
integrity sha512-ZMjqRpWabMPqPc7gIrG0Nw6vRf1+itwf0Itft7LbMXs2g3Zs/NFmevjZGN1x7K3Q95GmIjWbQZTVerxiBxI+0g== integrity sha512-ZMjqRpWabMPqPc7gIrG0Nw6vRf1+itwf0Itft7LbMXs2g3Zs/NFmevjZGN1x7K3Q95GmIjWbQZTVerxiBxI+0g==
vue-observe-visibility@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/vue-observe-visibility/-/vue-observe-visibility-1.0.0.tgz#17cf1b2caf74022f0f3c95371468ddf2b9573152"
integrity sha512-s5TFh3s3h3Mhd3jaz3zGzkVHKHnc/0C/gNr30olO99+yw2hl3WBhK3ng3/f9OF+qkW4+l7GkmwfAzDAcY3lCFg==
vue-resize@^0.4.5: vue-resize@^0.4.5:
version "0.4.5" version "0.4.5"
resolved "https://registry.yarnpkg.com/vue-resize/-/vue-resize-0.4.5.tgz#4777a23042e3c05620d9cbda01c0b3cc5e32dcea" resolved "https://registry.yarnpkg.com/vue-resize/-/vue-resize-0.4.5.tgz#4777a23042e3c05620d9cbda01c0b3cc5e32dcea"