Skip to content

Commit 4ea4fbd

Browse files
committed
aside 图标功能增强
1 parent a507b0b commit 4ea4fbd

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

packages/pure/components/user/Aside.astro

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
import { AstroError } from 'astro/errors'
33
44
import Icon from './Icon.astro'
5+
import { Icons } from '../../libs/icons'
56
67
const asideVariants = ['important', 'info', 'note', 'tip', 'warning', 'caution'] as const
7-
const icons = {
8+
9+
type IconName = keyof typeof Icons;
10+
11+
const defaultIcons: Record<typeof asideVariants[number], IconName> = {
812
important: 'important',
913
info: 'callout_info',
1014
note: 'note',
@@ -16,9 +20,10 @@ const icons = {
1620
interface Props {
1721
type?: (typeof asideVariants)[number]
1822
title?: string
23+
icon?: string
1924
}
2025
21-
let { type = 'note', title } = Astro.props
26+
let { type = 'note', title, icon } = Astro.props
2227
2328
if (!asideVariants.includes(type)) {
2429
throw new AstroError(
@@ -31,14 +36,51 @@ if (!asideVariants.includes(type)) {
3136
if (!title) {
3237
title = type.toUpperCase()
3338
}
39+
40+
// Determine the icon to render
41+
let iconToRender: IconName | undefined
42+
let isEmoji = false
43+
44+
if (icon) {
45+
// Check if the provided icon is an emoji
46+
const emojiRegex = /\p{Emoji}/u; // Unicode property escape for emoji
47+
if (emojiRegex.test(icon)) {
48+
iconToRender = undefined // Set to undefined if it's an emoji, handled by isEmoji
49+
isEmoji = true
50+
} else if (icon in Icons) {
51+
iconToRender = icon as IconName
52+
} else {
53+
// Fallback to default icon if provided icon name is not found in Icons
54+
console.warn(`Icon "${icon}" not found in icons.ts. Falling back to default icon for type "${type}".`);
55+
iconToRender = defaultIcons[type] as IconName;
56+
}
57+
} else {
58+
iconToRender = defaultIcons[type] as IconName;
59+
}
3460
---
3561

3662
<aside aria-label={title} class={`aside aside-${type}`}>
3763
<p class='aside-title'>
38-
<Icon name={icons[type]} />
64+
{isEmoji ? (
65+
<span class="aside-icon-emoji">{icon}</span>
66+
) : (
67+
<Icon name={iconToRender!} />
68+
)}
3969
<span>{title}</span>
4070
</p>
4171
<div class='aside-content'>
4272
<slot />
4373
</div>
4474
</aside>
75+
76+
<style>
77+
.aside-icon-emoji {
78+
font-size: 1.5em; /* Adjust size to match SVG icons */
79+
line-height: 1;
80+
display: flex;
81+
align-items: center;
82+
justify-content: center;
83+
}
84+
</style>
85+
86+

src/pages/guestbook/index.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ function getReactionEmoji(reaction: string) {
619619
</div>
620620
<div class="comments-list">
621621
{
622-
comments.map((comment: Comment, index: number) => {
622+
comments.map((comment: Comment) => {
623623
const repliedToComment = comment.replyToId ? commentsById.get(comment.replyToId) : null;
624624
return (
625625
<div class="comment-item" id={comment.id}>
@@ -747,7 +747,7 @@ function getReactionEmoji(reaction: string) {
747747
});
748748

749749
// Highlight code blocks
750-
document.addEventListener('DOMContentLoaded', (event) => {
750+
document.addEventListener('DOMContentLoaded', () => {
751751
document.querySelectorAll('.comment-body pre code').forEach((el) => {
752752
hljs.highlightElement(el);
753753
});

0 commit comments

Comments
 (0)