Merge branch 'virtual-scrolling-2.0' into shigusegubu-themes3
This commit is contained in:
commit
b53abd8132
6 changed files with 161 additions and 142 deletions
|
|
@ -1,4 +1,3 @@
|
||||||
import { get } from 'lodash-es'
|
|
||||||
import { storeToRefs } from 'pinia'
|
import { storeToRefs } from 'pinia'
|
||||||
import {
|
import {
|
||||||
computed,
|
computed,
|
||||||
|
|
@ -20,8 +19,6 @@ import ThreadTree from 'src/components/thread_tree/thread_tree.vue'
|
||||||
|
|
||||||
import { useInterfaceStore } from 'src/stores/interface.js'
|
import { useInterfaceStore } from 'src/stores/interface.js'
|
||||||
import { useMergedConfigStore } from 'src/stores/merged_config.js'
|
import { useMergedConfigStore } from 'src/stores/merged_config.js'
|
||||||
import { useStatusesStore } from 'src/stores/statuses.js'
|
|
||||||
import { useStreamingStore } from 'src/stores/streaming.js'
|
|
||||||
|
|
||||||
import { useConversation } from 'src/composables/useConversation.js'
|
import { useConversation } from 'src/composables/useConversation.js'
|
||||||
import { useInterfaceSizes } from 'src/composables/useInterfaceSizes.js'
|
import { useInterfaceSizes } from 'src/composables/useInterfaceSizes.js'
|
||||||
|
|
@ -29,8 +26,6 @@ import { useScrollPosition } from 'src/composables/useScrollPosition.js'
|
||||||
import { useTreeConversationTopology } from 'src/composables/useTreeConversationTopology.js'
|
import { useTreeConversationTopology } from 'src/composables/useTreeConversationTopology.js'
|
||||||
import { useVirtualScrolling } from 'src/composables/useVirtualScrolling.js'
|
import { useVirtualScrolling } from 'src/composables/useVirtualScrolling.js'
|
||||||
|
|
||||||
import { WSConnectionStatus } from 'src/api/websocket.js'
|
|
||||||
|
|
||||||
import { library } from '@fortawesome/fontawesome-svg-core'
|
import { library } from '@fortawesome/fontawesome-svg-core'
|
||||||
import {
|
import {
|
||||||
faAngleDoubleDown,
|
faAngleDoubleDown,
|
||||||
|
|
@ -72,17 +67,6 @@ export default {
|
||||||
},
|
},
|
||||||
emits: ['heightChange', 'suspendableStateChange'],
|
emits: ['heightChange', 'suspendableStateChange'],
|
||||||
setup(props, { emit }) {
|
setup(props, { emit }) {
|
||||||
// # Helpers
|
|
||||||
const getStatusObject = (id) => useStatusesStore().allStatuses.get(id)
|
|
||||||
const getConversationId = (statusId) => {
|
|
||||||
const status = getStatusObject(statusId)
|
|
||||||
return get(
|
|
||||||
status,
|
|
||||||
'retweeted_status.statusnet_conversation_id',
|
|
||||||
get(status, 'statusnet_conversation_id'),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
const scroller = useScrollPosition()
|
const scroller = useScrollPosition()
|
||||||
const tryScrollTo = async (id) => {
|
const tryScrollTo = async (id) => {
|
||||||
if (!id) {
|
if (!id) {
|
||||||
|
|
@ -100,15 +84,30 @@ export default {
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
|
const onReset = () => {
|
||||||
|
resetThreadDisplay()
|
||||||
|
}
|
||||||
|
|
||||||
|
// # Main things
|
||||||
|
const {
|
||||||
|
currentStatus,
|
||||||
|
mainStatus,
|
||||||
|
fullConversation,
|
||||||
|
replies,
|
||||||
|
getReplies,
|
||||||
|
fetchConversation,
|
||||||
|
loadError,
|
||||||
|
focusedId,
|
||||||
|
setFocused,
|
||||||
|
reset,
|
||||||
|
} = useConversation({
|
||||||
|
statusId,
|
||||||
|
onReset,
|
||||||
|
})
|
||||||
|
|
||||||
// # Main Configuration / global state
|
// # Main Configuration / global state
|
||||||
const { mergedConfig } = storeToRefs(useMergedConfigStore())
|
const { mergedConfig } = storeToRefs(useMergedConfigStore())
|
||||||
const { mastoUserSocketStatus } = storeToRefs(useStreamingStore())
|
|
||||||
const displayStyle = computed(() => mergedConfig.value.conversationDisplay)
|
const displayStyle = computed(() => mergedConfig.value.conversationDisplay)
|
||||||
const streamingEnabled = computed(
|
|
||||||
() =>
|
|
||||||
mergedConfig.value.useStreamingApi &&
|
|
||||||
mastoUserSocketStatus === WSConnectionStatus.JOINED,
|
|
||||||
)
|
|
||||||
const { layoutType } = storeToRefs(useInterfaceStore())
|
const { layoutType } = storeToRefs(useInterfaceStore())
|
||||||
const mobileLayout = computed(() => layoutType.value === 'mobile')
|
const mobileLayout = computed(() => layoutType.value === 'mobile')
|
||||||
|
|
||||||
|
|
@ -123,32 +122,11 @@ export default {
|
||||||
provide('isPage', isPage)
|
provide('isPage', isPage)
|
||||||
provide('expandable', true)
|
provide('expandable', true)
|
||||||
|
|
||||||
// # Focus
|
|
||||||
const focusedId = ref(statusId.value)
|
|
||||||
const focused = computed(() => (isExpanded.value ? focusedId.value : null))
|
const focused = computed(() => (isExpanded.value ? focusedId.value : null))
|
||||||
const setFocused = (id) => {
|
|
||||||
if (!id) return
|
|
||||||
focusedId.value = id
|
|
||||||
|
|
||||||
if (!streamingEnabled.value) {
|
|
||||||
useStatusesStore().fetchStatus(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
useStatusesStore().fetchFavsAndRepeats(id)
|
|
||||||
useStatusesStore().fetchEmojiReactions(id)
|
|
||||||
}
|
|
||||||
provide('focused', focused)
|
provide('focused', focused)
|
||||||
|
const conversation = computed(() =>
|
||||||
// # Main things
|
!expanded.value ? [currentStatus.value] : fullConversation.value,
|
||||||
const {
|
)
|
||||||
currentStatus,
|
|
||||||
mainStatus,
|
|
||||||
conversation,
|
|
||||||
replies,
|
|
||||||
getReplies,
|
|
||||||
fetchConversation,
|
|
||||||
loadError,
|
|
||||||
} = useConversation(focusedId, isExpanded)
|
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
expanded,
|
expanded,
|
||||||
|
|
@ -156,32 +134,13 @@ export default {
|
||||||
if (value) {
|
if (value) {
|
||||||
await fetchConversation()
|
await fetchConversation()
|
||||||
} else {
|
} else {
|
||||||
resetDisplayState()
|
reset()
|
||||||
}
|
}
|
||||||
if (isPage.value) return
|
if (isPage.value) return
|
||||||
},
|
},
|
||||||
{ flush: 'post' },
|
{ flush: 'post' },
|
||||||
)
|
)
|
||||||
|
|
||||||
const resetDisplayState = () => {
|
|
||||||
setFocused(statusId.value)
|
|
||||||
resetThreadDisplay()
|
|
||||||
}
|
|
||||||
watch(statusId, (newVal, oldVal) => {
|
|
||||||
const newConversationId = getConversationId(newVal)
|
|
||||||
const oldConversationId = getConversationId(oldVal)
|
|
||||||
if (
|
|
||||||
newConversationId &&
|
|
||||||
oldConversationId &&
|
|
||||||
newConversationId === oldConversationId
|
|
||||||
) {
|
|
||||||
setFocused(newVal)
|
|
||||||
} else {
|
|
||||||
resetDisplayState()
|
|
||||||
fetchConversation()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// Component created
|
// Component created
|
||||||
if (isPage.value) {
|
if (isPage.value) {
|
||||||
fetchConversation()
|
fetchConversation()
|
||||||
|
|
@ -192,10 +151,17 @@ export default {
|
||||||
const lastStatus = computed(
|
const lastStatus = computed(
|
||||||
() => conversation.value[conversation.value.legnth - 1],
|
() => conversation.value[conversation.value.legnth - 1],
|
||||||
)
|
)
|
||||||
const getStatusClasses = (status, active) => ({
|
const getStatusClasses = (status, ancestor) => {
|
||||||
'-first': status.id === firstStatus.value?.id,
|
const result = {
|
||||||
'-last': status.id === lastStatus.value?.id,
|
'-first': status.id === firstStatus.value?.id,
|
||||||
})
|
'-last': status.id === lastStatus.value?.id,
|
||||||
|
}
|
||||||
|
if (ancestor) {
|
||||||
|
result['-ancestor'] = true
|
||||||
|
result['-fade'] = mergedConfig.value.conversationTreeFadeAncestors
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
const { fontSize } = useInterfaceSizes()
|
const { fontSize } = useInterfaceSizes()
|
||||||
|
|
||||||
|
|
@ -228,6 +194,9 @@ export default {
|
||||||
? mutedStatusHeight
|
? mutedStatusHeight
|
||||||
: normalStatusHeight
|
: normalStatusHeight
|
||||||
|
|
||||||
|
const anchorIds = computed(
|
||||||
|
() => new Set([mainStatus.value?.id, currentStatus.value?.id]),
|
||||||
|
)
|
||||||
// # Linear style stuff
|
// # Linear style stuff
|
||||||
const isLinearView = computed(() => displayStyle.value !== 'tree')
|
const isLinearView = computed(() => displayStyle.value !== 'tree')
|
||||||
const linearElement = useTemplateRef('linear')
|
const linearElement = useTemplateRef('linear')
|
||||||
|
|
@ -241,8 +210,7 @@ export default {
|
||||||
body: linearElement,
|
body: linearElement,
|
||||||
scrollPositionInstance: scroller,
|
scrollPositionInstance: scroller,
|
||||||
scrollCompensation: linearScrollCompensation,
|
scrollCompensation: linearScrollCompensation,
|
||||||
anchorId: mainStatus.value?.id,
|
anchorIds,
|
||||||
anchorRepeatId: currentStatus.value?.id,
|
|
||||||
getPlaceholderHeight,
|
getPlaceholderHeight,
|
||||||
})
|
})
|
||||||
const changeSuspendStateLinearLocal = (e) => {
|
const changeSuspendStateLinearLocal = (e) => {
|
||||||
|
|
@ -277,6 +245,7 @@ export default {
|
||||||
body: ancestorsElement,
|
body: ancestorsElement,
|
||||||
scrollPositionInstance: scroller,
|
scrollPositionInstance: scroller,
|
||||||
scrollCompensation: treeScrollCompensation,
|
scrollCompensation: treeScrollCompensation,
|
||||||
|
collapseMode: 'height',
|
||||||
getPlaceholderHeight,
|
getPlaceholderHeight,
|
||||||
})
|
})
|
||||||
const changeSuspendStateAncestorsLocal = (e) => {
|
const changeSuspendStateAncestorsLocal = (e) => {
|
||||||
|
|
@ -307,9 +276,6 @@ export default {
|
||||||
const shouldShowAncestors = computed(
|
const shouldShowAncestors = computed(
|
||||||
() => isExpanded.value && heightChartAncestors.value.length > 0,
|
() => isExpanded.value && heightChartAncestors.value.length > 0,
|
||||||
)
|
)
|
||||||
const shouldFadeAncestors = computed(
|
|
||||||
() => mergedConfig.value.conversationTreeFadeAncestors,
|
|
||||||
)
|
|
||||||
|
|
||||||
// # Scrolling
|
// # Scrolling
|
||||||
const diveIntoStatus = (id) => tryScrollTo(id)
|
const diveIntoStatus = (id) => tryScrollTo(id)
|
||||||
|
|
@ -367,7 +333,6 @@ export default {
|
||||||
treeViewIsSimple,
|
treeViewIsSimple,
|
||||||
shouldShowAllConversationButton,
|
shouldShowAllConversationButton,
|
||||||
shouldShowAncestors,
|
shouldShowAncestors,
|
||||||
shouldFadeAncestors,
|
|
||||||
|
|
||||||
// # Scrolling
|
// # Scrolling
|
||||||
diveToTopLevel,
|
diveToTopLevel,
|
||||||
|
|
|
||||||
|
|
@ -91,15 +91,14 @@
|
||||||
ref="ancestors"
|
ref="ancestors"
|
||||||
class="thread-ancestors"
|
class="thread-ancestors"
|
||||||
>
|
>
|
||||||
<article
|
<template
|
||||||
v-for="element in heightChartAncestors"
|
v-for="element in heightChartAncestors"
|
||||||
class="thread-ancestor"
|
:key="element.id"
|
||||||
:class="{'thread-ancestor-has-other-replies': getReplies(element.id).size > 1, '-faded': shouldFadeAncestors}"
|
|
||||||
>
|
>
|
||||||
<Status
|
<Status
|
||||||
v-if="element.type === 'item'"
|
v-if="element.type === 'item'"
|
||||||
class="conversation-status panel-body"
|
class="conversation-status panel-body"
|
||||||
:class="getStatusClasses(element.item)"
|
:class="getStatusClasses(element.item, true)"
|
||||||
|
|
||||||
:status-id="element.item.id"
|
:status-id="element.item.id"
|
||||||
:replies="getReplies(element.item.id)"
|
:replies="getReplies(element.item.id)"
|
||||||
|
|
@ -119,8 +118,9 @@
|
||||||
v-if="element.type === 'spacer'"
|
v-if="element.type === 'spacer'"
|
||||||
class="virtual-spacer"
|
class="virtual-spacer"
|
||||||
:style="{ height: element.height + 'px' }"
|
:style="{ height: element.height + 'px' }"
|
||||||
|
aria-hidden="true"
|
||||||
/>
|
/>
|
||||||
</article>
|
</template>
|
||||||
</div>
|
</div>
|
||||||
<ThreadTree
|
<ThreadTree
|
||||||
:status-id="currentStatus.id"
|
:status-id="currentStatus.id"
|
||||||
|
|
@ -138,14 +138,13 @@
|
||||||
ref="linear"
|
ref="linear"
|
||||||
class="thread-body"
|
class="thread-body"
|
||||||
>
|
>
|
||||||
<article
|
<template
|
||||||
v-for="element in heightChartLinear"
|
v-for="element in heightChartLinear"
|
||||||
class="panel-body"
|
:key="element.id"
|
||||||
:key="element.id ?? element.ids"
|
|
||||||
>
|
>
|
||||||
<Status
|
<Status
|
||||||
v-if="element.type === 'item'"
|
v-if="element.type === 'item'"
|
||||||
class="conversation-status"
|
class="panel-body conversation-status"
|
||||||
:class="getStatusClasses(element.item)"
|
:class="getStatusClasses(element.item)"
|
||||||
:status-id="element.item.id"
|
:status-id="element.item.id"
|
||||||
:replies="getReplies(element.item.id)"
|
:replies="getReplies(element.item.id)"
|
||||||
|
|
@ -163,10 +162,11 @@
|
||||||
/>
|
/>
|
||||||
<div
|
<div
|
||||||
v-if="element.type === 'spacer'"
|
v-if="element.type === 'spacer'"
|
||||||
class="virtual-spacer"
|
aria-hidden="true"
|
||||||
|
class="panel-body virtual-spacer"
|
||||||
:style="{ height: element.height + 'px' }"
|
:style="{ height: element.height + 'px' }"
|
||||||
/>
|
/>
|
||||||
</article>
|
</template>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -592,7 +592,7 @@ const Status = {
|
||||||
const [entry] = e
|
const [entry] = e
|
||||||
this.$emit('heightChange', {
|
this.$emit('heightChange', {
|
||||||
id: this.status.id,
|
id: this.status.id,
|
||||||
height: entry.contentRect.height,
|
height: entry.contentRect.height + 1,
|
||||||
element: this.$el,
|
element: this.$el,
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
@ -600,13 +600,18 @@ const Status = {
|
||||||
mounted() {
|
mounted() {
|
||||||
if (this.$refs.root) {
|
if (this.$refs.root) {
|
||||||
this.resizeObserver.observe(this.$refs.root)
|
this.resizeObserver.observe(this.$refs.root)
|
||||||
|
this.updateVirtualHeight([
|
||||||
|
{
|
||||||
|
contentRect: this.$refs.root.getBoundingClientRect(),
|
||||||
|
},
|
||||||
|
])
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
unmounted() {
|
unmounted() {
|
||||||
this.resizeObserver.disconnect()
|
this.resizeObserver.disconnect()
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
hideStatus: function (element) {
|
hideStatus: function () {
|
||||||
if (this.$refs.root) {
|
if (this.$refs.root) {
|
||||||
this.resizeObserver.observe(this.$refs.root)
|
this.resizeObserver.observe(this.$refs.root)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
<template>
|
<template>
|
||||||
<div
|
<article
|
||||||
v-if="!hideStatus"
|
v-if="!hideStatus"
|
||||||
ref="root"
|
ref="root"
|
||||||
class="Status"
|
class="Status"
|
||||||
|
|
@ -568,7 +568,7 @@
|
||||||
</template>
|
</template>
|
||||||
</i18n-t>
|
</i18n-t>
|
||||||
</template>
|
</template>
|
||||||
</div>
|
</article>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script src="./status.js"></script>
|
<script src="./status.js"></script>
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,26 @@
|
||||||
import { get } from 'lodash-es'
|
import { get } from 'lodash-es'
|
||||||
import { computed, provide, ref } from 'vue'
|
import { storeToRefs } from 'pinia'
|
||||||
|
import { computed, provide, ref, watch } from 'vue'
|
||||||
|
|
||||||
|
import { useMergedConfigStore } from 'src/stores/merged_config.js'
|
||||||
import { useOAuthStore } from 'src/stores/oauth.js'
|
import { useOAuthStore } from 'src/stores/oauth.js'
|
||||||
import { useStatusesStore } from 'src/stores/statuses.js'
|
import { useStatusesStore } from 'src/stores/statuses.js'
|
||||||
|
import { useStreamingStore } from 'src/stores/streaming.js'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
fetchConversation as apiFetchConversation,
|
fetchConversation as apiFetchConversation,
|
||||||
fetchStatus as apiFetchStatus,
|
fetchStatus as apiFetchStatus,
|
||||||
} from 'src/api/public.js'
|
} from 'src/api/public.js'
|
||||||
|
|
||||||
export function useConversation(statusId, expanded) {
|
export function useConversation({ statusId, onReset }) {
|
||||||
|
const { mastoUserSocketStatus } = storeToRefs(useStreamingStore())
|
||||||
|
const { mergedConfig } = storeToRefs(useMergedConfigStore())
|
||||||
|
const streamingEnabled = computed(
|
||||||
|
() =>
|
||||||
|
mergedConfig.value.useStreamingApi &&
|
||||||
|
mastoUserSocketStatus === WSConnectionStatus.JOINED,
|
||||||
|
)
|
||||||
|
|
||||||
const getStatusObject = (id) => useStatusesStore().allStatuses.get(id)
|
const getStatusObject = (id) => useStatusesStore().allStatuses.get(id)
|
||||||
const getConversationId = (statusId) => {
|
const getConversationId = (statusId) => {
|
||||||
const status = getStatusObject(statusId)
|
const status = getStatusObject(statusId)
|
||||||
|
|
@ -19,6 +30,37 @@ export function useConversation(statusId, expanded) {
|
||||||
get(status, 'statusnet_conversation_id'),
|
get(status, 'statusnet_conversation_id'),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
// # Focus
|
||||||
|
const focusedId = ref(statusId.value)
|
||||||
|
const setFocused = (id) => {
|
||||||
|
if (!id) return
|
||||||
|
focusedId.value = id
|
||||||
|
|
||||||
|
if (!streamingEnabled.value) {
|
||||||
|
useStatusesStore().fetchStatus(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
useStatusesStore().fetchFavsAndRepeats(id)
|
||||||
|
useStatusesStore().fetchEmojiReactions(id)
|
||||||
|
}
|
||||||
|
const reset = () => {
|
||||||
|
onReset?.()
|
||||||
|
setFocused(statusId.value)
|
||||||
|
}
|
||||||
|
watch(statusId, (newVal, oldVal) => {
|
||||||
|
const newConversationId = getConversationId(newVal)
|
||||||
|
const oldConversationId = getConversationId(oldVal)
|
||||||
|
if (
|
||||||
|
newConversationId &&
|
||||||
|
oldConversationId &&
|
||||||
|
newConversationId === oldConversationId
|
||||||
|
) {
|
||||||
|
setFocused(newVal)
|
||||||
|
} else {
|
||||||
|
resetDisplayState()
|
||||||
|
fetchConversation()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const loadError = ref(null)
|
const loadError = ref(null)
|
||||||
const currentStatus = computed(() => getStatusObject(statusId.value))
|
const currentStatus = computed(() => getStatusObject(statusId.value))
|
||||||
|
|
@ -49,10 +91,6 @@ export function useConversation(statusId, expanded) {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!expanded.value) {
|
|
||||||
return [currentStatus.value]
|
|
||||||
}
|
|
||||||
|
|
||||||
const conversation = useStatusesStore().conversations.get(
|
const conversation = useStatusesStore().conversations.get(
|
||||||
conversationId.value,
|
conversationId.value,
|
||||||
)
|
)
|
||||||
|
|
@ -119,10 +157,13 @@ export function useConversation(statusId, expanded) {
|
||||||
return {
|
return {
|
||||||
currentStatus,
|
currentStatus,
|
||||||
mainStatus,
|
mainStatus,
|
||||||
conversation,
|
fullConversation: conversation,
|
||||||
|
focusedId,
|
||||||
|
setFocused,
|
||||||
replies,
|
replies,
|
||||||
getReplies,
|
getReplies,
|
||||||
fetchConversation,
|
fetchConversation,
|
||||||
|
reset,
|
||||||
loadError,
|
loadError,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { last } from 'lodash-es'
|
||||||
import { computed, nextTick, ref, toValue, watch } from 'vue'
|
import { computed, nextTick, ref, toValue, watch } from 'vue'
|
||||||
|
|
||||||
import { useWindowSize } from 'src/composables/useWindowSize.js'
|
import { useWindowSize } from 'src/composables/useWindowSize.js'
|
||||||
|
|
@ -9,12 +10,18 @@ export function useVirtualScrolling({
|
||||||
body,
|
body,
|
||||||
// useScrollPosition composable, used to prevent dupicating instances
|
// useScrollPosition composable, used to prevent dupicating instances
|
||||||
scrollPositionInstance,
|
scrollPositionInstance,
|
||||||
// ID of anchor element, used for scroll compensation.
|
// buffer zone, the amount of placeholder heights to include
|
||||||
// Omitting it makes last element the anchor
|
buffer,
|
||||||
anchorId,
|
|
||||||
anchorRepeatId,
|
|
||||||
// whether to use scroll compensation when elements above anchor change
|
// whether to use scroll compensation when elements above anchor change
|
||||||
|
// set to 'positive' to only compensate for positive increase (useful when
|
||||||
|
// combined with infinite scroll)
|
||||||
scrollCompensation,
|
scrollCompensation,
|
||||||
|
// How to handle collapse/expansion (going from 0 elements to full and back)
|
||||||
|
// - false - don't do scroll compensation at all
|
||||||
|
// - 'height' - compensate scroll according to list's height
|
||||||
|
collapseMode,
|
||||||
|
// Anchor. Set of IDs of element relative to which do scroll compensation
|
||||||
|
anchorIds,
|
||||||
// Placeholder height specification. Must be a function.
|
// Placeholder height specification. Must be a function.
|
||||||
// function will be called either:
|
// function will be called either:
|
||||||
// - without arguments (for generic placeholder, i.e. buffer zone size)
|
// - without arguments (for generic placeholder, i.e. buffer zone size)
|
||||||
|
|
@ -38,14 +45,13 @@ export function useVirtualScrolling({
|
||||||
// Map every height and suspendable state
|
// Map every height and suspendable state
|
||||||
const chart = list.value.map((item) => {
|
const chart = list.value.map((item) => {
|
||||||
const { id } = item
|
const { id } = item
|
||||||
const height =
|
const height = (() => {
|
||||||
(() => {
|
if (heights.value.has(id)) {
|
||||||
if (heights.value.has(id)) {
|
return heights.value.get(id)
|
||||||
return heights.value.get(id)
|
} else {
|
||||||
} else {
|
return getPlaceholderHeight(id).value
|
||||||
return getPlaceholderHeight(id).value
|
}
|
||||||
}
|
})()
|
||||||
})() + 1 //including border
|
|
||||||
const suspendable = !unsuspendibleIds.value.has(id)
|
const suspendable = !unsuspendibleIds.value.has(id)
|
||||||
return { id, height, suspendable, item }
|
return { id, height, suspendable, item }
|
||||||
})
|
})
|
||||||
|
|
@ -69,41 +75,41 @@ export function useVirtualScrolling({
|
||||||
if (newVal.length === 0 && oldVal.length === 0) return
|
if (newVal.length === 0 && oldVal.length === 0) return
|
||||||
pauseWatchers()
|
pauseWatchers()
|
||||||
|
|
||||||
// If we're not given an achor, treat last element as one
|
const expansion = oldVal.length === 0 && newVal.length !== 0
|
||||||
const getAnchoredEl = (list) =>
|
const collapse = oldVal.length !== 0 && newVal.length === 0
|
||||||
toValue(anchorId)
|
|
||||||
? list.find(
|
|
||||||
({ id }) =>
|
|
||||||
id === toValue(anchorId) || id === toValue(anchorRepeatId),
|
|
||||||
)
|
|
||||||
: list[list.length - 1]
|
|
||||||
|
|
||||||
const oldElement = getAnchoredEl(oldVal)
|
|
||||||
const newElement = getAnchoredEl(newVal)
|
|
||||||
|
|
||||||
const diff = (() => {
|
const diff = (() => {
|
||||||
if (oldElement && newElement) {
|
if (expansion || collapse) {
|
||||||
// Generic shifting
|
if (toValue(collapseMode) === 'height') {
|
||||||
const oldOffset = toValue(anchorId)
|
const oldBottomElement = last(oldVal)
|
||||||
? oldElement.top
|
const newBottomElement = last(newVal)
|
||||||
: oldElement.top + oldElement.height
|
|
||||||
const newOffset = toValue(anchorId)
|
if (expansion) {
|
||||||
? newElement.top
|
return newBottomElement.top + newBottomElement.height
|
||||||
: newElement.top + newElement.height
|
} else if (collapse) {
|
||||||
return newOffset - oldOffset
|
return 0 - oldBottomElement.top - oldBottomElement.height
|
||||||
} else if (!oldElement && newElement) {
|
}
|
||||||
// Expansion
|
}
|
||||||
return newElement.top + newElement.height
|
return 0
|
||||||
} else if (oldElement && !newElement) {
|
} else if (toValue(anchorIds) != null) {
|
||||||
// Collapsing
|
const anchorOld = oldVal.find(({ id }) => toValue(anchorIds).has(id))
|
||||||
return 0 - oldElement.top - oldElement.height
|
const anchorNew = newVal.find(({ id }) => toValue(anchorIds).has(id))
|
||||||
|
if (anchorOld == null) {
|
||||||
|
throw new Error('Anchor not found!')
|
||||||
|
}
|
||||||
|
|
||||||
|
const disappeared = anchorOld != null && anchorNew == null
|
||||||
|
if (disappeared) {
|
||||||
|
throw new Error('Anchor disappeared!')
|
||||||
|
}
|
||||||
|
|
||||||
|
return anchorNew.top - anchorOld.top
|
||||||
} else {
|
} else {
|
||||||
throw new Error(
|
return 0
|
||||||
"Somehow both new and old elements are missing, this shouldn't happen",
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
|
|
||||||
|
console.log(diff)
|
||||||
if (diff !== 0) {
|
if (diff !== 0) {
|
||||||
// Scroll by amount offset changed to keep it in view
|
// Scroll by amount offset changed to keep it in view
|
||||||
topScrollBoundary.value += diff
|
topScrollBoundary.value += diff
|
||||||
|
|
@ -160,7 +166,9 @@ export function useVirtualScrolling({
|
||||||
|
|
||||||
// # Visiblity
|
// # Visiblity
|
||||||
// Add buffer zone to boundary, equal to approx 3 items heights
|
// Add buffer zone to boundary, equal to approx 3 items heights
|
||||||
const buffer = computed(() => getPlaceholderHeight().value * 3)
|
const bufferZone = computed(
|
||||||
|
() => getPlaceholderHeight().value * (toValue(buffer) ?? 3),
|
||||||
|
)
|
||||||
|
|
||||||
const heightChartGrouped = computed(() => {
|
const heightChartGrouped = computed(() => {
|
||||||
// Determine visibility state
|
// Determine visibility state
|
||||||
|
|
@ -169,9 +177,9 @@ export function useVirtualScrolling({
|
||||||
const itemBottomBoundary = heightChartItem.top + heightChartItem.height
|
const itemBottomBoundary = heightChartItem.top + heightChartItem.height
|
||||||
|
|
||||||
// Include buffer zone
|
// Include buffer zone
|
||||||
const finalTopScrollBoundary = topScrollBoundary.value - buffer.value
|
const finalTopScrollBoundary = topScrollBoundary.value - bufferZone.value
|
||||||
const finalBottomScrollBoundary =
|
const finalBottomScrollBoundary =
|
||||||
bottomScrollBoundary.value + buffer.value
|
bottomScrollBoundary.value + bufferZone.value
|
||||||
|
|
||||||
// To be visible, item's bottom boundary shoud be below top scroll boundary)
|
// To be visible, item's bottom boundary shoud be below top scroll boundary)
|
||||||
const isBelowTopBoundary = itemBottomBoundary > finalTopScrollBoundary
|
const isBelowTopBoundary = itemBottomBoundary > finalTopScrollBoundary
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue