✨ 封装滚动条组件并统一图标加载
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<script setup lang="ts">
|
||||
|
||||
import { OverlayScrollbarsComponent } from 'overlayscrollbars-vue';
|
||||
import type { OverflowBehavior, ScrollbarsAutoHideBehavior, ScrollbarsVisibilityBehavior } from 'overlayscrollbars';
|
||||
|
||||
const props = withDefaults(defineProps<{
|
||||
x?: string;
|
||||
y?: string;
|
||||
}>(), {
|
||||
x: 'hidden',
|
||||
y: 'scroll',
|
||||
});
|
||||
const scrollbarOptions = {
|
||||
scrollbars: {
|
||||
theme: 'os-theme-thin-dark',
|
||||
visibility: 'auto' as ScrollbarsVisibilityBehavior,
|
||||
autoHide: 'move' as ScrollbarsAutoHideBehavior,
|
||||
autoHideDelay: 1300,
|
||||
dragScroll: true,
|
||||
clickScroll: true,
|
||||
},
|
||||
overflow: {
|
||||
x: props.x as OverflowBehavior,
|
||||
y: props.y as OverflowBehavior,
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<overlay-scrollbars-component class="easy-scrollbar" :options="scrollbarOptions">
|
||||
<slot/>
|
||||
</overlay-scrollbars-component>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.easy-scrollbar :deep(.os-scrollbar) {
|
||||
--os-size: 4px;
|
||||
--os-padding-perpendicular: 0px;
|
||||
--os-padding-axis: 0px;
|
||||
--os-track-border-radius: 2px;
|
||||
--os-handle-border-radius: 2px;
|
||||
--os-handle-bg: rgba(156, 163, 175, 0.5);
|
||||
--os-handle-bg-hover: rgba(156, 163, 175, 0.8);
|
||||
--os-handle-bg-active: rgba(107, 114, 128, 1);
|
||||
}
|
||||
|
||||
/* 暗色模式 */
|
||||
.easy-scrollbar:is(.dark *) :deep(.os-scrollbar) {
|
||||
--os-handle-bg: rgba(75, 85, 99, 0.5);
|
||||
--os-handle-bg-hover: rgba(75, 85, 99, 0.8);
|
||||
--os-handle-bg-active: rgba(107, 114, 128, 1);
|
||||
}
|
||||
</style>
|
||||
@@ -5,8 +5,6 @@ import { DataAnomaly } from '~/types/PostMetaData';
|
||||
import 'overlayscrollbars/overlayscrollbars.css';
|
||||
import useColorModeStore from '~/stores/colorModeStore';
|
||||
import useIconStore from '~/stores/iconStore';
|
||||
import { OverlayScrollbarsComponent } from 'overlayscrollbars-vue';
|
||||
import type { OverflowBehavior, ScrollbarsAutoHideBehavior, ScrollbarsVisibilityBehavior } from 'overlayscrollbars';
|
||||
|
||||
const props = withDefaults(defineProps<{
|
||||
asyncKey: string;
|
||||
@@ -26,7 +24,7 @@ const techStackLightIconSVG = ref<string[]>([]);
|
||||
const techStackDarkIconSVG = ref<string[]>([]);
|
||||
const { colorMode } = storeToRefs(useColorModeStore());
|
||||
|
||||
// 没有这个asyncKey会加载不出正确的图标
|
||||
// 没有这个 asyncKey 会加载不出正确的图标
|
||||
const { pending } = useAsyncData(props.asyncKey, async () => {
|
||||
if (props.techStackIconNames === DataAnomaly.DataNotFound || props.techStackIconNames === DataAnomaly.Invalid)
|
||||
return [];
|
||||
@@ -64,7 +62,7 @@ const renderChart = () => {
|
||||
chart: {
|
||||
type: 'bar',
|
||||
backgroundColor: 'transparent',
|
||||
reflow: false,
|
||||
reflow: true,
|
||||
},
|
||||
credits: {
|
||||
enabled: false,
|
||||
@@ -165,21 +163,6 @@ onUnmounted(() => {
|
||||
useColorModeStore().unregisterCallBack(colorModeCallBackKey);
|
||||
});
|
||||
|
||||
const scrollbarOptions = {
|
||||
scrollbars: {
|
||||
theme: 'os-theme-thin-dark',
|
||||
visibility: 'auto' as ScrollbarsVisibilityBehavior,
|
||||
autoHide: 'move' as ScrollbarsAutoHideBehavior,
|
||||
autoHideDelay: 1300,
|
||||
dragScroll: true,
|
||||
clickScroll: true,
|
||||
},
|
||||
overflow: {
|
||||
x: 'hidden' as OverflowBehavior,
|
||||
y: 'scroll' as OverflowBehavior,
|
||||
},
|
||||
};
|
||||
|
||||
const mounted = ref(false);
|
||||
onMounted(() => {
|
||||
mounted.value = true;
|
||||
@@ -221,29 +204,8 @@ onMounted(() => {
|
||||
</g>
|
||||
</svg>
|
||||
</div>
|
||||
<overlay-scrollbars-component v-else class="max-h-full" :options="scrollbarOptions">
|
||||
<easy-scroll-bar v-else class="max-h-full max-w-full">
|
||||
<div ref="chartRef" class="w-full"/>
|
||||
</overlay-scrollbars-component>
|
||||
</easy-scroll-bar>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
|
||||
:deep(.os-scrollbar) {
|
||||
--os-size: 4px;
|
||||
--os-padding-perpendicular: 0px;
|
||||
--os-padding-axis: 0px;
|
||||
--os-track-border-radius: 2px;
|
||||
--os-handle-border-radius: 2px;
|
||||
--os-handle-bg: rgba(156, 163, 175, 0.5);
|
||||
--os-handle-bg-hover: rgba(156, 163, 175, 0.8);
|
||||
--os-handle-bg-active: rgba(107, 114, 128, 1);
|
||||
}
|
||||
|
||||
/* 暗色模式 */
|
||||
.dark :deep(.os-scrollbar) {
|
||||
--os-handle-bg: rgba(75, 85, 99, 0.5);
|
||||
--os-handle-bg-hover: rgba(75, 85, 99, 0.8);
|
||||
--os-handle-bg-active: rgba(107, 114, 128, 1);
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -33,6 +33,17 @@ export default defineNuxtConfig({
|
||||
script: [{ src: '/darkVerify.js' }],
|
||||
},
|
||||
},
|
||||
icon: {
|
||||
provider: 'server',
|
||||
serverBundle: 'local',
|
||||
|
||||
fallbackToApi: true,
|
||||
|
||||
clientBundle: {
|
||||
scan: true,
|
||||
icons: [],
|
||||
},
|
||||
},
|
||||
// sourcemap: {
|
||||
// server: true,
|
||||
// client: true,
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
import { DataAnomaly, defaultMetaData } from '~/types/PostMetaData';
|
||||
import type { PostMetaData } from '~/types/PostMetaData';
|
||||
import { OverlayScrollbarsComponent } from 'overlayscrollbars-vue';
|
||||
|
||||
withDefaults(defineProps<{
|
||||
metaData?: PostMetaData;
|
||||
@@ -95,9 +94,9 @@ onMounted(() => {
|
||||
|
||||
</div>
|
||||
<div class="flex mt-2 justify-between h-28">
|
||||
<overlay-scrollbars-component v-if="mounted">
|
||||
<easy-scroll-bar v-if="mounted">
|
||||
{{ metaData?.description }}
|
||||
</overlay-scrollbars-component>
|
||||
</easy-scroll-bar>
|
||||
<div v-else class="w-4/5">
|
||||
<USkeleton class="h-4 mb-3 w-full bg-old-neutral-600"/>
|
||||
<USkeleton class="h-4 mb-3 w-full bg-old-neutral-600"/>
|
||||
|
||||
+22
-30
@@ -1,3 +1,7 @@
|
||||
import { loadIcon } from '@iconify/vue';
|
||||
import { iconToHTML, iconToSVG, replaceIDs } from '@iconify/utils';
|
||||
|
||||
// call await set first, then call getIcon or getColoredIcon
|
||||
const useIconStore = defineStore('icon', {
|
||||
state: () => ({
|
||||
iconCache: new Map<string, string>(),
|
||||
@@ -30,40 +34,28 @@ const useIconStore = defineStore('icon', {
|
||||
});
|
||||
}
|
||||
this.addingSet.add(iconName);
|
||||
if (iconData === undefined || iconData === null || iconData === '') {
|
||||
this.iconCache.set(iconName, await fetchSvg(iconName));
|
||||
} else
|
||||
this.iconCache.set(iconName, iconData as string);
|
||||
if (!iconData) {
|
||||
try {
|
||||
const data = await loadIcon(iconName);
|
||||
const { body, attributes } = iconToSVG(data, {
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
});
|
||||
|
||||
iconData = iconToHTML(replaceIDs(body), attributes);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
iconData = '';
|
||||
}
|
||||
}
|
||||
if (iconData) {
|
||||
this.iconCache.set(iconName, iconData);
|
||||
}
|
||||
this.addingSet.delete(iconName);
|
||||
this.waitingCallbackFunctions.get(iconName)?.forEach((callback) => callback());
|
||||
this.waitingCallbackFunctions.delete(iconName);
|
||||
},
|
||||
|
||||
},
|
||||
});
|
||||
|
||||
export default useIconStore;
|
||||
|
||||
export async function fetchSvg(svgName: string, maxRetries = 3) {
|
||||
const iconifyUrl = 'https://api.iconify.design/';
|
||||
|
||||
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
try {
|
||||
return await (await $fetch<Blob>(iconifyUrl + svgName + '.svg', {
|
||||
method: 'GET',
|
||||
params: {
|
||||
width: '100%',
|
||||
},
|
||||
})).text();
|
||||
} catch (error) {
|
||||
console.warn(`Attempt ${attempt} failed for ${svgName}:`, error);
|
||||
|
||||
if (attempt === maxRetries) {
|
||||
console.error(`All ${maxRetries} attempts failed for ${svgName}`);
|
||||
}
|
||||
|
||||
// 等待一段时间后重试(可选)
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000 * attempt));
|
||||
}
|
||||
}
|
||||
return '<svg></svg>';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user