237 lines
7.1 KiB
Vue
237 lines
7.1 KiB
Vue
/**
|
|
* shared/components/AnnouncementCardCarousel.vue
|
|
* 公告横向滑动走马灯:每次仅展示一条,无层叠侧卡。
|
|
* 依赖:WorkspaceAnnouncement、lucide-vue-next、truncateAnnouncementPreview。
|
|
* 逻辑:LoginPage / HomePage 传入公告列表;点击卡片 emit select 打开详情弹窗。
|
|
*/
|
|
<script setup lang="ts">
|
|
import { computed, onBeforeUnmount, ref, watch } from 'vue';
|
|
import { useI18n } from 'vue-i18n';
|
|
import { ChevronLeft, ChevronRight } from 'lucide-vue-next';
|
|
import { truncateAnnouncementPreview } from '@/features/admin/types/announcement.types';
|
|
import type { WorkspaceAnnouncement } from '@/features/workspace/types/workspace.types';
|
|
|
|
const props = withDefaults(
|
|
defineProps<{
|
|
announcements: WorkspaceAnnouncement[];
|
|
/** 视觉主题:login 融入登录页毛玻璃区,workspace 工作台卡片风格 */
|
|
variant?: 'login' | 'workspace';
|
|
autoPlay?: boolean;
|
|
autoPlayInterval?: number;
|
|
}>(),
|
|
{
|
|
variant: 'login',
|
|
autoPlay: false,
|
|
autoPlayInterval: 6000,
|
|
},
|
|
);
|
|
|
|
const emit = defineEmits<{
|
|
select: [WorkspaceAnnouncement];
|
|
}>();
|
|
|
|
const { t } = useI18n();
|
|
|
|
const currentIndex = ref(0);
|
|
let autoPlayTimer: ReturnType<typeof setInterval> | null = null;
|
|
|
|
const total = computed(() => props.announcements.length);
|
|
const hasMultiple = computed(() => total.value > 1);
|
|
const activeAnnouncement = computed(() => props.announcements[currentIndex.value] ?? null);
|
|
const trackStyle = computed(() => ({
|
|
transform: `translateX(-${currentIndex.value * 100}%)`,
|
|
}));
|
|
|
|
/**
|
|
* 将索引归一化到 [0, length) 区间,支持首尾循环。
|
|
* @param index - 原始索引
|
|
* @returns 合法索引
|
|
*/
|
|
const normalizeIndex = (index: number): number => {
|
|
if (total.value <= 0) return 0;
|
|
return ((index % total.value) + total.value) % total.value;
|
|
};
|
|
|
|
/** 切换到上一条公告。 */
|
|
const goPrev = (): void => {
|
|
if (!hasMultiple.value) return;
|
|
currentIndex.value = normalizeIndex(currentIndex.value - 1);
|
|
};
|
|
|
|
/** 切换到下一条公告。 */
|
|
const goNext = (): void => {
|
|
if (!hasMultiple.value) return;
|
|
currentIndex.value = normalizeIndex(currentIndex.value + 1);
|
|
};
|
|
|
|
/**
|
|
* 跳转到指定索引。
|
|
* @param index - 目标索引
|
|
*/
|
|
const goTo = (index: number): void => {
|
|
if (index < 0 || index >= total.value) return;
|
|
currentIndex.value = index;
|
|
};
|
|
|
|
/**
|
|
* 点击当前公告卡片,打开详情弹窗。
|
|
* @param ann - 被点击公告
|
|
*/
|
|
const openDetail = (ann: WorkspaceAnnouncement): void => {
|
|
emit('select', ann);
|
|
};
|
|
|
|
/**
|
|
* 格式化公告日期为本地化短日期。
|
|
* @param iso - created_at
|
|
*/
|
|
const formatDate = (iso: string): string => new Date(iso).toLocaleDateString();
|
|
|
|
/** 启动自动轮播定时器。 */
|
|
const startAutoPlay = (): void => {
|
|
stopAutoPlay();
|
|
if (!props.autoPlay || !hasMultiple.value) return;
|
|
autoPlayTimer = setInterval(goNext, props.autoPlayInterval);
|
|
};
|
|
|
|
/** 停止自动轮播定时器。 */
|
|
const stopAutoPlay = (): void => {
|
|
if (autoPlayTimer) {
|
|
clearInterval(autoPlayTimer);
|
|
autoPlayTimer = null;
|
|
}
|
|
};
|
|
|
|
watch(
|
|
() => [props.announcements, props.autoPlay, props.autoPlayInterval] as const,
|
|
() => {
|
|
currentIndex.value = normalizeIndex(currentIndex.value);
|
|
if (currentIndex.value >= total.value) {
|
|
currentIndex.value = 0;
|
|
}
|
|
startAutoPlay();
|
|
},
|
|
{ immediate: true, deep: true },
|
|
);
|
|
|
|
onBeforeUnmount(() => {
|
|
stopAutoPlay();
|
|
});
|
|
|
|
const panelClass = computed(() =>
|
|
props.variant === 'workspace'
|
|
? 'rounded-2xl border border-emerald-100 bg-gradient-to-br from-white to-emerald-50/40'
|
|
: 'rounded-xl',
|
|
);
|
|
|
|
const accentTextClass = computed(() =>
|
|
props.variant === 'workspace' ? 'text-emerald-600' : 'text-brand-600',
|
|
);
|
|
|
|
const dateBadgeClass = computed(() =>
|
|
props.variant === 'workspace'
|
|
? 'text-emerald-700 bg-emerald-50'
|
|
: 'text-muted bg-slate-100/80',
|
|
);
|
|
|
|
const dotActiveClass = computed(() =>
|
|
props.variant === 'workspace' ? 'bg-emerald-500' : 'bg-brand-600',
|
|
);
|
|
|
|
const navBtnClass = computed(() =>
|
|
props.variant === 'workspace'
|
|
? 'hover:bg-emerald-50 hover:text-emerald-700 disabled:hover:bg-transparent'
|
|
: 'hover:bg-white/80 hover:text-brand-700 disabled:hover:bg-transparent',
|
|
);
|
|
</script>
|
|
|
|
<template>
|
|
<div
|
|
class="w-full"
|
|
@mouseenter="stopAutoPlay"
|
|
@mouseleave="startAutoPlay"
|
|
>
|
|
<div class="overflow-hidden" :class="panelClass">
|
|
<div
|
|
class="flex transition-transform duration-500 ease-out"
|
|
:style="trackStyle"
|
|
>
|
|
<article
|
|
v-for="ann in announcements"
|
|
:key="ann.id"
|
|
class="w-full shrink-0 cursor-pointer group px-1 py-1 sm:px-2 sm:py-2"
|
|
@click="openDetail(ann)"
|
|
>
|
|
<div class="flex items-start justify-between gap-3 mb-2">
|
|
<h4
|
|
class="text-sm font-semibold text-primary line-clamp-1 transition-colors"
|
|
:class="variant === 'workspace' ? 'group-hover:text-emerald-700' : 'group-hover:text-brand-700'"
|
|
>
|
|
{{ ann.title }}
|
|
</h4>
|
|
<time
|
|
class="text-[10px] font-medium px-2 py-0.5 rounded-full whitespace-nowrap shrink-0"
|
|
:class="dateBadgeClass"
|
|
:datetime="ann.created_at"
|
|
>
|
|
{{ formatDate(ann.created_at) }}
|
|
</time>
|
|
</div>
|
|
<p class="text-xs sm:text-sm text-secondary line-clamp-3 leading-relaxed min-h-[3.75rem]">
|
|
{{ truncateAnnouncementPreview(ann.content, 100) }}
|
|
</p>
|
|
<p class="mt-2.5 text-xs font-medium transition-colors" :class="accentTextClass">
|
|
{{ t('workspace.announcements.viewDetail') }} →
|
|
</p>
|
|
</article>
|
|
</div>
|
|
</div>
|
|
|
|
<div
|
|
v-if="hasMultiple"
|
|
class="mt-3 flex items-center justify-between gap-3"
|
|
>
|
|
<button
|
|
type="button"
|
|
class="p-1.5 rounded-lg text-muted transition-colors disabled:opacity-30"
|
|
:class="navBtnClass"
|
|
:aria-label="t('workspace.announcements.prev')"
|
|
@click="goPrev"
|
|
>
|
|
<ChevronLeft class="w-4 h-4" />
|
|
</button>
|
|
|
|
<div class="flex items-center gap-3">
|
|
<div class="flex items-center gap-1.5">
|
|
<button
|
|
v-for="(_, index) in announcements"
|
|
:key="index"
|
|
type="button"
|
|
class="h-1.5 rounded-full transition-all duration-300"
|
|
:class="index === currentIndex ? `${dotActiveClass} w-5` : 'w-1.5 bg-slate-300 hover:bg-slate-400'"
|
|
:aria-label="t('workspace.announcements.goTo', { index: index + 1 })"
|
|
@click="goTo(index)"
|
|
/>
|
|
</div>
|
|
<span class="text-[11px] text-muted tabular-nums whitespace-nowrap">
|
|
{{ t('workspace.announcements.currentOf', { current: currentIndex + 1, total }) }}
|
|
</span>
|
|
</div>
|
|
|
|
<button
|
|
type="button"
|
|
class="p-1.5 rounded-lg text-muted transition-colors"
|
|
:class="navBtnClass"
|
|
:aria-label="t('workspace.announcements.next')"
|
|
@click="goNext"
|
|
>
|
|
<ChevronRight class="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
|
|
<p v-if="activeAnnouncement && hasMultiple" class="sr-only">
|
|
{{ activeAnnouncement.title }}
|
|
</p>
|
|
</div>
|
|
</template>
|