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] fix(NcReferenceWidget): use requestAnimationFrame in observers #5566

Merged
merged 3 commits into from
May 9, 2024
Merged
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
46 changes: 28 additions & 18 deletions src/components/NcRichText/NcReferenceWidget.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</template>
<script>
import { useIntersectionObserver, useResizeObserver } from '@vueuse/core'
import { ref } from 'vue'
import { nextTick, ref } from 'vue'
import { RouterLink } from 'vue-router'

import { t } from '../../l10n.js'
Expand Down Expand Up @@ -62,29 +62,35 @@ export default {
},

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

useIntersectionObserver(widgetRoot, (entries) => {
isVisible.value = entries[0]?.isIntersecting ?? false
useIntersectionObserver(widgetRoot, () => {
nextTick(() => {
isVisible.value = widgetRoot.value?.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
}
/**
* Measure the width of the widgetRoot after a resize
*/
useResizeObserver(widgetRoot, () => {
/**
* Wait till the next tick to allow the resize to finish first
* and avoid triggering content updates during the resize.
*
* Without the nextTick we were seeing crashing browsers
* in cypress tests.
*/
nextTick(() => {
width.value = widgetRoot.value?.contentRect.width ?? 0
})
})

return {
compact,
width,
isVisible,
widgetRoot,
}
Expand Down Expand Up @@ -115,18 +121,22 @@ export default {
return this.reference && !this.reference.accessible
},
descriptionStyle() {
if (this.compact === 0) {
if (this.numberOfLines === 0) {
return {
display: 'none',
}
}

const lineClamp = this.compact < 4 ? this.compact : 3
const lineClamp = this.numberOfLines
return {
lineClamp,
webkitLineClamp: lineClamp,
}
},
numberOfLines() {
// no description for width < 450, one line until 550 and so on
const lineCountOffsets = [450, 550, 650, Infinity]
return lineCountOffsets.findIndex(max => this.width.value < max)
},
compactLink() {
const link = this.reference.openGraphObject.link
if (!link) {
Expand Down
Loading