perf: 优化页面切换时缩略图的表现

This commit is contained in:
pipipi-pikachu 2023-11-05 16:36:09 +08:00
parent 99c53c385d
commit a71c9ffbd3
2 changed files with 39 additions and 3 deletions

View File

@ -226,3 +226,18 @@ export const getLineElementPath = (element: PPTLineElement) => {
}
return `M${start} L${end}`
}
/**
*
* @param element
* @param parent
*/
export const isElementInViewport = (element: HTMLElement, parent: HTMLElement): boolean => {
const elementRect = element.getBoundingClientRect()
const parentRect = parent.getBoundingClientRect()
return (
elementRect.top >= parentRect.top &&
elementRect.bottom <= parentRect.bottom
)
}

View File

@ -17,6 +17,7 @@
<Draggable
class="thumbnail-list"
ref="thumbnailsRef"
:modelValue="slides"
:animation="200"
:scroll="true"
@ -46,10 +47,11 @@
</template>
<script lang="ts" setup>
import { computed, ref } from 'vue'
import { computed, nextTick, ref, watch } from 'vue'
import { storeToRefs } from 'pinia'
import { useMainStore, useSlidesStore, useKeyboardStore } from '@/store'
import { fillDigit } from '@/utils/common'
import { isElementInViewport } from '@/utils/element'
import type { ContextmenuItem } from '@/components/Contextmenu/types'
import useSlideHandler from '@/hooks/useSlideHandler'
import useScreening from '@/hooks/useScreening'
@ -85,6 +87,26 @@ const {
sortSlides,
} = useSlideHandler()
//
const thumbnailsRef = ref<InstanceType<typeof Draggable>>()
watch(() => slideIndex.value, () => {
//
if (selectedSlidesIndex.value.length) {
mainStore.updateSelectedSlidesIndex([])
}
//
nextTick(() => {
const activeThumbnailRef: HTMLElement = thumbnailsRef.value?.$el?.querySelector('.thumbnail-item.active')
if (thumbnailsRef.value && activeThumbnailRef && !isElementInViewport(activeThumbnailRef, thumbnailsRef.value.$el)) {
setTimeout(() => {
activeThumbnailRef.scrollIntoView({ behavior: 'smooth' })
}, 100)
}
})
})
//
const changeSlideIndex = (index: number) => {
mainStore.setActiveElementIdList([])
@ -100,6 +122,7 @@ const handleClickSlideThumbnail = (e: MouseEvent, index: number) => {
if (isMultiSelected && selectedSlidesIndex.value.includes(index) && e.button !== 0) return
// Ctrl
//
if (ctrlKeyState.value) {
if (slideIndex.value === index) {
if (!isMultiSelected) return
@ -116,7 +139,6 @@ const handleClickSlideThumbnail = (e: MouseEvent, index: number) => {
else {
const newSelectedSlidesIndex = [...selectedSlidesIndex.value, index]
mainStore.updateSelectedSlidesIndex(newSelectedSlidesIndex)
changeSlideIndex(index)
}
}
}
@ -135,7 +157,6 @@ const handleClickSlideThumbnail = (e: MouseEvent, index: number) => {
const newSelectedSlidesIndex = []
for (let i = minIndex; i <= maxIndex; i++) newSelectedSlidesIndex.push(i)
mainStore.updateSelectedSlidesIndex(newSelectedSlidesIndex)
changeSlideIndex(index)
}
//
else {