feat: 歌单内元素懒加载

This commit is contained in:
539943419 committed 2024-09-27 18:23:40 +08:00
1 parent cdc0bce108
commit 171af1219e
3 files changed
+43 -10

No files matched your search

+21 -2
View File
@@ -1,15 +1,20 @@
<!-- 只适合用于渲染过程较为复杂的组件,不然可能得不偿失 <!-- 只适合用于渲染过程较为复杂的组件,不然可能得不偿失
这么泛用的组件,性能差点也能理解 --> 这么泛用的组件,性能差点也能理解 -->
<template> <template>
<div class="miao-lazy" ref="miaoLazyRoot" :style="!show ? { minHeight, minWidth, margin } : {}"> <div class="miao-lazy" ref="miaoLazyRoot"
:style="!show ? { minHeight: _autoHeight !== undefined ? _autoHeight : props.minHeight, minWidth, margin } : {}">
<slot v-if="show" /> <slot v-if="show" />
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from 'vue'; import { nextTick, onBeforeUnmount, onMounted, ref } from 'vue';
const props = defineProps({ const props = defineProps({
// disable: {
// type: Boolean,
// default: false,
// },
disableLazy: { disableLazy: {
type: Boolean, type: Boolean,
default: false, default: false,
@@ -26,11 +31,17 @@ const props = defineProps({
margin: { margin: {
type: String, type: String,
default: 'none', default: 'none',
},
autoHeight: {
type: Boolean,
default: false
} }
}) })
const miaoLazyRoot = ref<HTMLDivElement>() const miaoLazyRoot = ref<HTMLDivElement>()
const _autoHeight = ref<string>()
const show = ref<boolean>(props.disableLazy) const show = ref<boolean>(props.disableLazy)
const isVisible = (dom: HTMLDivElement) => { const isVisible = (dom: HTMLDivElement) => {
@@ -49,6 +60,9 @@ const { showSlot, hideSlot, clearTimer } = (() => {
} }
const hideSlot = () => { const hideSlot = () => {
clearTimer() clearTimer()
if (props.autoHeight) {
_autoHeight.value = `${miaoLazyRoot.value?.clientHeight}px`
}
show.value = false show.value = false
} }
const showSlot = () => { const showSlot = () => {
@@ -69,7 +83,12 @@ const { showSlot, hideSlot, clearTimer } = (() => {
hideSlot() hideSlot()
} }
}, 1000) }, 1000)
// if (props.autoHeight) {
// nextTick(() => {
// console.log(miaoLazyRoot.value?.clientHeight)
// _autoHeight.value = `${miaoLazyRoot.value?.clientHeight}px`
// }) // })
// }
} }
return { showSlot, hideSlot, clearTimer } return { showSlot, hideSlot, clearTimer }
})() })()
@@ -1,5 +1,6 @@
<template> <template>
<miao-drop-handler @on-virtual-files="handleDropVFiles"> <miao-drop-handler @on-virtual-files="handleDropVFiles">
<miao-alertTip-provider ref="miaoMessageRef">
<div class="miaoMusic-container"> <div class="miaoMusic-container">
<NScrollbar> <NScrollbar>
<div class="miaoMusic-main"> <div class="miaoMusic-main">
@@ -7,11 +8,13 @@
</NScrollbar> </NScrollbar>
<div ref="aplayer" /> <div ref="aplayer" />
</div> </div>
</miao-alertTip-provider>
</miao-drop-handler> </miao-drop-handler>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import miaoDropHandler from '@/components/miaoDropHandler.vue' import miaoDropHandler from '@/components/miaoDropHandler.vue'
import miaoAlertTipProvider from '@/components/miaoAlertTipProvider.vue'
import useAplayerMiao from './hooks/aplayerMiao'; import useAplayerMiao from './hooks/aplayerMiao';
import { VirtualFile } from '@/class/VirtualDirectory'; import { VirtualFile } from '@/class/VirtualDirectory';
import { onMounted, onUnmounted, reactive, ref, watch } from 'vue'; import { onMounted, onUnmounted, reactive, ref, watch } from 'vue';
@@ -23,13 +26,22 @@ import config from '@/config';
import { difference, uniq } from 'lodash'; import { difference, uniq } from 'lodash';
import apType from './types/ap'; import apType from './types/ap';
const miaoMessageRef = ref<InstanceType<typeof miaoAlertTipProvider>>()
const currentFiles = defineModel<VirtualFile[]>('currentFiles', { const currentFiles = defineModel<VirtualFile[]>('currentFiles', {
required: true required: true
}); });
watch(()=>currentFiles.value, (value, oldValue) => { watch(() => currentFiles.value, (value, oldValue) => {
ap.list.add(getMusicList(difference(value, oldValue))) const diff = difference(value, oldValue)
if (diff.length > 0) {
const _s = `等${diff.length}个资源`
miaoMessageRef.value?.alertTip(`已添加 ${diff[0].name}${diff.length !== 1 ? _s : ''}`, {
type: 'success',
timeout: 3000
})
ap.list.add(getMusicList(diff))
}
}) })
const getMusicList = (mp3VFiles: VirtualFile[]) => mp3VFiles.map((v, index) => ({ const getMusicList = (mp3VFiles: VirtualFile[]) => mp3VFiles.map((v, index) => ({
@@ -70,11 +82,11 @@ onMounted(() => {
}, },
}) })
ap.on('listremove', (...args: any) => { ap.on('listremove', (...args: any) => {
const {index} = args[0] as {index: number} const { index } = args[0] as { index: number }
const _name = ap.list.audios[index].name const _name = ap.list.audios[index].name
currentFiles.value = currentFiles.value.filter(v=>v.name !== _name) currentFiles.value = currentFiles.value.filter(v => v.name !== _name)
}) })
console.log({ ap, vueApp }) // console.log({ ap, vueApp })
}); });
onUnmounted(() => { onUnmounted(() => {
@@ -2,8 +2,8 @@
<ol :style="{ maxHeight: ap.options.listMaxHeight }" :key="refreshKey" ref="ol"> <ol :style="{ maxHeight: ap.options.listMaxHeight }" :key="refreshKey" ref="ol">
<VueDraggable v-model="ap.list.audios" @start="onDragStart" @end="onDragEnd" item-key="id" <VueDraggable v-model="ap.list.audios" @start="onDragStart" @end="onDragEnd" item-key="id"
handle=".aplayer-list-control-btn-reorder" ghostClass="iSDragging" :animation="150"> handle=".aplayer-list-control-btn-reorder" ghostClass="iSDragging" :animation="150">
<li v-for="(element, index) in ap.list.audios" <miaoLazyDiv v-for="(element, index) in ap.list.audios" :min-height="'32px'" auto-height>
:class="activeAudioIndex == index ? 'aplayer-list-light' : ''" class="aplayer-list-new" <li :class="activeAudioIndex == index ? 'aplayer-list-light' : ''" class="aplayer-list-new"
:key="element.id"> :key="element.id">
<span class="aplayer-list-cur" :style="{ backgroundColor: apTheme }"></span> <span class="aplayer-list-cur" :style="{ backgroundColor: apTheme }"></span>
<span class="aplayer-list-index">{{ index + 1 }}</span> <span class="aplayer-list-index">{{ index + 1 }}</span>
@@ -34,6 +34,7 @@
<span class="aplayer-list-author" v-show="element.artist !== 'Audio artist1'">{{ element.artist <span class="aplayer-list-author" v-show="element.artist !== 'Audio artist1'">{{ element.artist
}}</span> }}</span>
</li> </li>
</miaoLazyDiv>
</VueDraggable> </VueDraggable>
</ol> </ol>
</template> </template>
@@ -42,6 +43,7 @@
import { computed, nextTick, onMounted, ref } from 'vue'; import { computed, nextTick, onMounted, ref } from 'vue';
import { ChevronUpOutline, ChevronDownOutline, CloseOutline, ReorderFourOutline, PlayOutline, HeartOutline } from '@vicons/ionicons5'; import { ChevronUpOutline, ChevronDownOutline, CloseOutline, ReorderFourOutline, PlayOutline, HeartOutline } from '@vicons/ionicons5';
import { VueDraggable } from 'vue-draggable-plus' import { VueDraggable } from 'vue-draggable-plus'
import miaoLazyDiv from '@/components/miaoLazyDiv.vue';
import apType, { audioType } from '../types/ap'; import apType, { audioType } from '../types/ap';
const props = defineProps<{ const props = defineProps<{
@@ -148,7 +150,7 @@ const { onDragStart, onDragEnd } = (() => {
// } // }
onMounted(() => { onMounted(() => {
console.log(props.ap) // console.log(props.ap)
}) })
</script> </script>