Skip to content

Commit 3d45516

Browse files
committed
添加欢迎语
1 parent c803da6 commit 3d45516

File tree

4 files changed

+66
-36
lines changed

4 files changed

+66
-36
lines changed

src/components/Message.astro

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
---
2-
3-
const { index, btnName, content } = Astro.props as Message;
2+
const { index } = Astro.props;
43
if (!index) {
5-
throw Error(btnName + " " + content + " 未定义index!");
4+
throw Error(" 未定义index!");
65
}
6+
// 有空时候请完善此组件,应当考虑多通知框情况
77
---
88

9-
<div class:list={["toast", index]}>
10-
{content}
11-
</div>
12-
<button class:list={["toastBtn", index]}>{btnName}</button>
13-
9+
<div class:list={["toast", index]} data-index={index}></div>
1410
<script>
11+
import type { ToastMessageEvent } from "../utils/types";
12+
1513
const msgBoxs: boolean[] = [true, true, true, true, true];
1614
/**
1715
* 将toastBtn绑定到对应的toast上
1816
* @param event
1917
*/
20-
function showToast(event: Event) {
21-
// 获取到toast元素
22-
const target = event.target as HTMLElement;
23-
const classList = target.classList
24-
.toString()
25-
.replaceAll("toastBtn", "toast");
26-
const toast = document
27-
.getElementsByClassName(classList)
28-
.item(0) as HTMLElement;
18+
function showToast(event: ToastMessageEvent) {
19+
const toasts = document.getElementsByClassName("toast");
20+
21+
let toast;
22+
23+
for (const t of toasts) {
24+
const t1 = t as HTMLElement;
25+
if (t1.dataset.index && t1.dataset.index === event.index) {
26+
toast = t1 as HTMLDivElement;
27+
}
28+
}
29+
if (!toast) {
30+
console.log("无法找到指定toast");
31+
return;
32+
}
33+
34+
toast.textContent = event.text;
2935

3036
// 判断toast情况
3137
if (toast.style.top != "-50px" && toast.style.top != "") {
@@ -59,11 +65,9 @@ if (!index) {
5965
}, 3000);
6066
}
6167

68+
document.addEventListener("welcomeEvent", showToast as any);
6269
document.addEventListener("astro:page-load", () => {
63-
const buttons = document.querySelectorAll(".toastBtn");
64-
buttons.forEach((button) => {
65-
button.addEventListener("click", showToast);
66-
});
70+
document.addEventListener("welcomeEvent", showToast as any);
6771
});
6872
</script>
6973

src/layouts/BaseLayout.astro

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ClientRouter } from "astro:transitions";
55
import gopherImage from "../assets/pilot-bust.svg";
66
import { Image } from "astro:assets";
77
import BaseHead from "../heads/BaseHead.astro";
8+
import Message from "../components/Message.astro";
89
const { description, keywords, title, canonical, ogType } = Astro.props;
910
---
1011

@@ -27,6 +28,7 @@ const { description, keywords, title, canonical, ogType } = Astro.props;
2728
<title>Ling的小窝{title ? ` - ${title}` : ""}</title>
2829
</head>
2930
<body>
31+
<Message index="welcome" />
3032
<div id="container">
3133
<header id="menu">
3234
<HeadMenu />
@@ -125,6 +127,31 @@ const { description, keywords, title, canonical, ogType } = Astro.props;
125127
});
126128
</script>
127129

130+
<script>
131+
import { ToastMessageEvent } from "../utils/types";
132+
133+
const welcome = () => {
134+
console.log("欢迎");
135+
// 获取完整的来源 URL
136+
let referrer = document.referrer;
137+
if (!referrer) {
138+
return;
139+
}
140+
let url = new URL(referrer);
141+
console.log("来源域名:", url.hostname);
142+
document.dispatchEvent(
143+
new ToastMessageEvent(
144+
"welcomeEvent",
145+
"welcome",
146+
`欢迎来自${url.hostname}的访客`
147+
)
148+
);
149+
};
150+
window.onload = welcome;
151+
const btn = document.getElementById("test") as HTMLButtonElement;
152+
btn.addEventListener("click", welcome);
153+
</script>
154+
128155
<style>
129156
* {
130157
box-sizing: border-box; /* 确保所有元素的宽高包含 padding 和 border */

src/pages/index.astro

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import BaseLayout from "../layouts/BaseLayout.astro";
66

77
<BaseLayout
88
description="欢迎来到我的小窝!我会在这里放一些编程学习记录和生活随笔,欢迎留下你的评论和友链!"
9-
keywords="主页,main,Ling,LingLambda"
9+
keywords="主页,main,LingLambda,博客,个人博客,blog"
1010
title="技术博客与生活分享"
1111
canonical={import.meta.env.SITE}
1212
>
@@ -18,18 +18,6 @@ import BaseLayout from "../layouts/BaseLayout.astro";
1818
</div>
1919
</BaseLayout>
2020

21-
<!-- <script>
22-
window.onload = () => {
23-
// 获取完整的来源 URL
24-
let referrer = document.referrer;
25-
if (!referrer) {
26-
return;
27-
}
28-
let url = new URL(referrer);
29-
console.log("来源域名:", url.hostname);
30-
};
31-
</script> -->
32-
3321
<style>
3422
.content-wrapper {
3523
padding: 0 1.5rem;

src/utils/types.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,21 @@ type PlayedGame = {
1414
playtime_mac_forever: number;
1515
playtime_linux_forever: number;
1616
playtime_deck_forever: number;
17-
}
17+
};
1818

1919
type Message = {
2020
index: string;
2121
btnName: string;
2222
content: string;
23-
}
23+
};
24+
25+
export class ToastMessageEvent extends Event {
26+
constructor(
27+
type: string,
28+
public index: string,
29+
public text: string,
30+
eventInitDict?: EventInit
31+
) {
32+
super(type, eventInitDict);
33+
}
34+
}

0 commit comments

Comments
 (0)