File size: 6,302 Bytes
96dd062
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
---
import type { MarkdownHeading } from "astro";
import Advertisement from "@/components/widget/Advertisement.astro";
import Announcement from "@/components/widget/Announcement.astro";
import Calendar from "@/components/widget/Calendar.astro";
import Categories from "@/components/widget/Categories.astro";
import Music from "@/components/widget/Music.astro";
import Profile from "@/components/widget/Profile.astro";
import SidebarTOC from "@/components/widget/SidebarTOC.astro";
import SiteStats from "@/components/widget/SiteStats.astro";
import Tags from "@/components/widget/Tags.astro";
import { sidebarLayoutConfig } from "@/config";
import type {
	MobileBottomComponentConfig,
	WidgetComponentConfig,
	WidgetComponentType,
} from "@/types/config";

interface Props {
	class?: string;
	headings?: MarkdownHeading[];
	side?: "left" | "right" | "bottom";
}

// 侧边栏位置常量
const SIDEBAR_SIDE = {
	LEFT: "left",
	RIGHT: "right",
	BOTTOM: "bottom",
} as const;

// 组件位置常量
const COMPONENT_POSITION = {
	TOP: "top",
	STICKY: "sticky",
} as const;

// 动画延迟配置
const ANIMATION_DELAY_UNIT = 50; // ms

// 组件映射表
const componentMap = {
	profile: Profile,
	announcement: Announcement,
	categories: Categories,
	tags: Tags,
	sidebarToc: SidebarTOC,
	advertisement: Advertisement,
	stats: SiteStats,
	calendar: Calendar,
	music: Music,
} satisfies Record<WidgetComponentType, typeof Profile>;

// 获取侧边栏位置
const side = (Astro.props.side ||
	SIDEBAR_SIDE.LEFT) as (typeof SIDEBAR_SIDE)[keyof typeof SIDEBAR_SIDE];
const className = Astro.props.class;

// 根据 side 属性获取对应的组件列表
const getComponents = (): (
	| WidgetComponentConfig
	| MobileBottomComponentConfig
)[] => {
	if (side === SIDEBAR_SIDE.LEFT) {
		return sidebarLayoutConfig.leftComponents;
	}
	if (side === SIDEBAR_SIDE.RIGHT) {
		return sidebarLayoutConfig.rightComponents;
	}
	if (side === SIDEBAR_SIDE.BOTTOM) {
		return sidebarLayoutConfig.mobileBottomComponents;
	}
	return [];
};

// 过滤并排序组件(不再进行 showOnPostPage 过滤,交由客户端处理)
const filterAndSortComponents = (

	components: (WidgetComponentConfig | MobileBottomComponentConfig)[],

) => {
	return components.filter((comp) => comp.enable);
};

// 分离 top 和 sticky 位置的组件(保持原始数组顺序)
const getComponentsByPosition = (

	components: (WidgetComponentConfig | MobileBottomComponentConfig)[],

) => {
	const topComponents = components.filter(
		(c) => "position" in c && c.position === COMPONENT_POSITION.TOP,
	) as WidgetComponentConfig[];
	const stickyComponents = components.filter(
		(c) => "position" in c && c.position === COMPONENT_POSITION.STICKY,
	) as WidgetComponentConfig[];
	return { topComponents, stickyComponents };
};

// 获取动画延迟(硬编码)
const getAnimationDelay = (index: number): string => {
	return `${index * ANIMATION_DELAY_UNIT}ms`;
};

// 动态构建组件 props
const getComponentProps = (
	config: WidgetComponentConfig | MobileBottomComponentConfig,
	index: number,
): Record<string, unknown> => {
	const baseProps: Record<string, unknown> = {
		class: "onload-animation",
		style: `animation-delay: ${getAnimationDelay(index)}`,
	};

	// 添加 showOnPostPage 和 showOnNonPostPage 标记class(仅WidgetComponentConfig有这些属性)
	if ("showOnPostPage" in config && config.showOnPostPage === false) {
		baseProps.class = `${baseProps.class} widget-hide-on-post`;
	}
	if ("showOnNonPostPage" in config && config.showOnNonPostPage === false) {
		baseProps.class = `${baseProps.class} widget-hide-on-non-post`;
	}

	// 特殊处理 SidebarTOC 组件
	if (config.type === "sidebarToc") {
		return { ...baseProps, headings: Astro.props.headings || [] };
	}

	// 特殊处理 Advertisement 组件
	if (
		config.type === "advertisement" &&
		"configId" in config &&
		config.configId
	) {
		return { ...baseProps, configId: config.configId };
	}

	return baseProps;
};

// 获取所有需要渲染的组件
const allComponents = getComponents();
const filteredComponents = filterAndSortComponents(allComponents);

// 对于移动端底部组件,直接使用所有组件,不进行position分组
const isMobileBottom = side === SIDEBAR_SIDE.BOTTOM;
const { topComponents, stickyComponents } = !isMobileBottom
	? getComponentsByPosition(filteredComponents)
	: { topComponents: [], stickyComponents: [] };
const bottomComponents = isMobileBottom ? filteredComponents : [];
---

{
	(topComponents.length > 0 || stickyComponents.length > 0 || bottomComponents.length > 0) && (
		<div id={`${side}-sidebar`} class:list={[className, "flex flex-col w-full pt-0"]}>

			{/* Mobile bottom components - 直接渲染所有组件,不分组 */}

			{isMobileBottom ? (

				<div class="flex flex-col w-full gap-4">

					{bottomComponents.map((comp, index) => {

						const Component = componentMap[comp.type];

						if (!Component) return null;

						const props = getComponentProps(comp, index) as any;

						return <Component {...props} />;

					})}

				</div>

			) : (

				<>

					{/* Top components */}

					{topComponents.length > 0 && (

						<div class="flex flex-col w-full gap-4 mb-4">

							{topComponents.map((comp, index) => {

								const Component = componentMap[comp.type];

								if (!Component) return null;

								const props = getComponentProps(comp, index) as any;

								return <Component {...props} />;

							})}

						</div>

					)}



					{/* Sticky components */}

					{stickyComponents.length > 0 && (

						<div

							id={`${side}-sidebar-sticky`}

							class:list={[

								"transition-all duration-700 flex flex-col w-full mt-0",

								"sticky",

								topComponents.length > 0 ? "top-4 gap-4" : "top-0",

							]}

						>

							{stickyComponents.map((comp, index) => {

								const Component = componentMap[comp.type];

								if (!Component) return null;

								const props = getComponentProps(

									comp,

									topComponents.length + index

								) as any;

								return <Component {...props} />;

							})}

						</div>

					)}

				</>
			)}
		</div>
	)
}