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

[next] feat(reference): Add opt-in for interactive mode and render widgets lazy once in view #5375

Merged
merged 7 commits into from
Mar 20, 2024
Merged
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
3 changes: 3 additions & 0 deletions l10n/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ msgstr ""
msgid "Emoji picker"
msgstr ""

msgid "Enable interactive view"
msgstr ""

msgid "Enter link"
msgstr ""

Expand Down
17 changes: 10 additions & 7 deletions src/components/NcRichText/NcReferenceList.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div v-if="isVisible" class="widgets--list" :class="{'icon-loading': loading }">
<div v-for="reference in displayedReferences" :key="reference?.openGraphObject?.id">
<NcReferenceWidget :reference="reference" />
<NcReferenceWidget :reference="reference" :interactive="interactive" :interactive-opt-in="interactiveOptIn" />
</div>
</div>
</template>
Expand Down Expand Up @@ -35,6 +35,10 @@ export default {
default: false,
},
interactive: {
type: Boolean,
default: true,
},
interactiveOptIn: {
type: Boolean,
default: false,
},
Expand Down Expand Up @@ -65,12 +69,7 @@ export default {
return this.values[0] ?? null
},
displayedReferences() {
return this.values.slice(0, this.limit).map(reference => {
return {
...reference,
interactive: this.interactive,
}
})
return this.values.slice(0, this.limit)
},
fallbackReference() {
return {
Expand Down Expand Up @@ -129,6 +128,10 @@ export default {
}
</script>
<style lang="scss" scoped>
.widget--list {
width: var(--widget-full-width, 100%);
}

.widgets--list.icon-loading {
min-height: 44px;
}
Expand Down
172 changes: 147 additions & 25 deletions src/components/NcRichText/NcReferenceWidget.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<template>
<div>
<div v-if="reference && hasCustomWidget" class="widget-custom">
<div ref="customWidget" />
</div>
<div ref="widgetRoot" :class="{'toggle-interactive': hasInteractiveView && !isInteractive }">
<div v-if="reference && hasCustomWidget"
ref="customWidget"
class="widget-custom"
:class="{ 'full-width': hasFullWidth }" />

<component :is="referenceWidgetLinkComponent"
v-else-if="!noAccess && reference && reference.openGraphObject && !hasCustomWidget"
Expand All @@ -11,37 +12,105 @@
class="widget-default">
<img v-if="reference.openGraphObject.thumb" class="widget-default--image" :src="reference.openGraphObject.thumb">
<div class="widget-default--details">
<p class="widget-default--name">{{ reference.openGraphObject.name }}</p>
<p class="widget-default--description" :style="descriptionStyle">{{ reference.openGraphObject.description }}</p>
<p class="widget-default--link">{{ compactLink }}</p>
<p class="widget-default--name">
{{ reference.openGraphObject.name }}
</p>
<p class="widget-default--description" :style="descriptionStyle">
{{ reference.openGraphObject.description }}
</p>
<p class="widget-default--link">
{{ compactLink }}
</p>
</div>
</component>
<NcButton v-if="interactiveOptIn && hasInteractiveView && !isInteractive" class="toggle-interactive--button" @click="enableInteractive">
{{ t('Enable interactive view') }}
</NcButton>
</div>
</template>
<script>
import { useResizeObserver } from '@vueuse/core'
import { useIntersectionObserver, useResizeObserver } from '@vueuse/core'
import { ref } from 'vue'
import { RouterLink } from 'vue-router'

import { t } from '../../l10n.js'
import { getRoute } from './autolink.ts'
import { renderWidget, isWidgetRegistered, destroyWidget } from './../../functions/reference/widgets.js'
import { renderWidget, isWidgetRegistered, destroyWidget, hasInteractiveView, hasFullWidth } from './../../functions/reference/widgets.ts'

import NcButton from '../../components/NcButton/NcButton.vue'

const IDLE_TIMEOUT = 3 * 60 * 1000 // 3 minutes outside of viewport before widget is removed from the DOM

export default {
name: 'NcReferenceWidget',
components: {
NcButton,
},
props: {
reference: {
type: Object,
required: true,
},
interactive: {
type: Boolean,
default: true,
},
interactiveOptIn: {
type: Boolean,
default: false,
},
},

setup() {
const compact = ref(3)
const isVisible = ref(false)
// This is the widget root node
const widgetRoot = ref()

useIntersectionObserver(widgetRoot, (entries) => {
isVisible.value = entries[0]?.isIntersecting ?? false
})

useResizeObserver(widgetRoot, (entries) => {
if (entries[0].contentRect.width < 450) {
compact.value = 0
} else if (entries[0].contentRect.width < 550) {
compact.value = 1
} else if (entries[0].contentRect.width < 650) {
compact.value = 2
} else {
compact.value = 3
}
})

return {
compact,
isVisible,
widgetRoot,
}
},

data() {
return {
compact: 3,
showInteractive: false,
rendered: false,
idleTimeout: null,
}
},

computed: {
isInteractive() {
return (!this.interactiveOptIn && this.interactive) || this.showInteractive
},
hasFullWidth() {
return hasFullWidth(this.reference.richObjectType)
},
hasCustomWidget() {
return isWidgetRegistered(this.reference.richObjectType)
},
hasInteractiveView() {
return isWidgetRegistered(this.reference.richObjectType) && hasInteractiveView(this.reference.richObjectType)
},
noAccess() {
return this.reference && !this.reference.accessible
},
Expand Down Expand Up @@ -84,37 +153,66 @@ export default {
: { href: this.reference.openGraphObject.link, target: '_blank' }
},
},
mounted() {
this.renderWidget()
useResizeObserver(this.$el, entries => {
if (entries[0].contentRect.width < 450) {
this.compact = 0
} else if (entries[0].contentRect.width < 550) {
this.compact = 1
} else if (entries[0].contentRect.width < 650) {
this.compact = 2
} else {
this.compact = 3
}
watch: {
isVisible: {
handler(val) {
if (!val) {
this.idleTimeout = setTimeout(() => {
// If the widget is still outside of viewport after timeout, destroy it
if (!this.isVisible) {
this.destroyWidget()
}
}, IDLE_TIMEOUT)
return
}

})
if (this.idleTimeout) {
clearTimeout(this.idleTimeout)
this.idleTimeout = null
}

if (!this.rendered) {
this.renderWidget()
}
},
immediate: true,
},
},
beforeUnmount() {
destroyWidget(this.reference.richObjectType, this.$el)
this.destroyWidget()
},
methods: {
t,

enableInteractive() {
this.showInteractive = true
this.renderWidget()
},
renderWidget() {
if (this.$refs.customWidget) {
this.$refs.customWidget.innerHTML = ''
}
if (this?.reference?.richObjectType === 'open-graph') {
return
}
// create a separate element so we can rerender on the ref again
const widget = document.createElement('div')
this.$refs.customWidget.appendChild(widget)
this.$nextTick(() => {
// Waiting for the ref to become available
renderWidget(this.$refs.customWidget, this.reference)
renderWidget(widget, {
...this.reference,
interactive: this.isInteractive,
})
this.rendered = true
})
},
destroyWidget() {
if (this.rendered) {
destroyWidget(this.reference.richObjectType, this.$el)
this.rendered = false
}
},
},
}
</script>
Expand All @@ -134,6 +232,12 @@ export default {

.widget-custom {
@include widget;

&.full-width {
width: var(--widget-full-width, 100%) !important;
left: calc( (var(--widget-full-width, 100%) - 100%) / 2 * -1);
position: relative;
}
}

.widget-access {
Expand Down Expand Up @@ -204,4 +308,22 @@ export default {
white-space: nowrap;
}
}

.toggle-interactive {
position: relative;
.toggle-interactive--button {
position: absolute;
top: 50%;
z-index: 10000;
left: 50%;
transform: translateX(-50%) translateY(-50%);
opacity: 0;
}

&:focus-within, &:hover {
.toggle-interactive--button {
opacity: 1;
}
}
}
</style>
2 changes: 1 addition & 1 deletion src/components/NcRichText/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import NcRichText from './NcRichText.vue'

import { registerWidget, renderWidget, isWidgetRegistered } from './../../functions/reference/widgets.js'
import { registerWidget, renderWidget, isWidgetRegistered } from './../../functions/reference/widgets.ts'
import NcReferenceList from './NcReferenceList.vue'
import NcReferenceWidget from './NcReferenceWidget.vue'
import NcReferencePicker from './NcReferencePicker/NcReferencePicker.vue'
Expand Down
3 changes: 2 additions & 1 deletion src/functions/reference/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { registerWidget, renderWidget, isWidgetRegistered } from './widgets.js'
import { registerWidget, renderWidget, isWidgetRegistered, hasInteractiveView } from './widgets.ts'
import { getLinkWithPicker } from './referencePickerModal.js'
import {
getProvider,
Expand All @@ -18,6 +18,7 @@ export {
registerWidget,
renderWidget,
isWidgetRegistered,
hasInteractiveView,

NcCustomPickerRenderResult,
registerCustomPickerElement,
Expand Down
54 changes: 0 additions & 54 deletions src/functions/reference/widgets.js

This file was deleted.

Loading
Loading