Skip to content

Commit d217235

Browse files
committed
feat: add page search and configure switch
1 parent ffab29c commit d217235

26 files changed

+628
-417
lines changed

settings.yaml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,16 @@ spec:
6464
label: Banner背景图片
6565
value: /themes/theme-ocean/assets/images/default-background.png
6666
- $formkit: radio
67-
name: banner_search
68-
label: 是否显示搜索框
69-
value: true
67+
name: search_method
68+
value: page
69+
label: 搜索形式
7070
options:
71-
- label: 显示
72-
value: true
71+
- label: 页面
72+
value: page
73+
- label: 弹窗
74+
value: popup
7375
- label: 隐藏
74-
value: false
76+
value: none
7577
- $formkit: tagCheckbox
7678
name: tags
7779
id: tags

src/alpine-data/pagination.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ export default (page: number, totalPages: number, totalVisible: number) => ({
4949

5050
return range;
5151
},
52-
});
52+
});

src/alpine-data/post-util.ts

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,75 @@
11
import axios from "axios";
22

3-
export default (postId: any,likeNum: string) => ({
3+
export default (postId: any, likeNum: string) => ({
44
postId: postId,
55
likeNum: likeNum,
66
loading: false,
77
liked: false,
88
get upvote() {
9-
this.init()
9+
this.init();
1010
if (this.loading || this.liked) return;
1111

1212
this.loading = true;
13-
axios.post(
14-
'/apis/api.halo.run/v1alpha1/trackers/upvote',
15-
{
13+
axios
14+
.post(
15+
"/apis/api.halo.run/v1alpha1/trackers/upvote",
16+
{
1617
group: "content.halo.run",
1718
plural: "posts",
1819
name: this.postId,
19-
},
20-
{
21-
headers: {
22-
'Content-Type': 'application/json; charset=utf-8'
20+
},
21+
{
22+
headers: {
23+
"Content-Type": "application/json; charset=utf-8",
24+
},
2325
}
24-
}
25-
).then(() => {
26-
this.liked = true;
27-
this.loading = false;
28-
localStorage.setItem("likeNum:" + this.postId, String(Number(this.likeNum) + 1))
29-
this.likeNum = String(Number(this.likeNum) + 1)
30-
}).catch(() => {
26+
)
27+
.then(() => {
28+
this.liked = true;
29+
this.loading = false;
30+
localStorage.setItem("likeNum:" + this.postId, String(Number(this.likeNum) + 1));
31+
this.likeNum = String(Number(this.likeNum) + 1);
32+
})
33+
.catch(() => {
3134
this.loading = false;
3235
});
3336
},
3437
get downvote() {
3538
if (this.loading || !this.liked) return;
3639

3740
this.loading = true;
38-
axios.post(
39-
'/apis/api.halo.run/v1alpha1/trackers/downvote',
40-
{
41-
group: "content.halo.run",
42-
plural: "posts",
43-
name: this.postId,
44-
},
45-
{
46-
headers: {
47-
'Content-Type': 'application/json; charset=utf-8'
41+
axios
42+
.post(
43+
"/apis/api.halo.run/v1alpha1/trackers/downvote",
44+
{
45+
group: "content.halo.run",
46+
plural: "posts",
47+
name: this.postId,
48+
},
49+
{
50+
headers: {
51+
"Content-Type": "application/json; charset=utf-8",
52+
},
4853
}
49-
}
50-
).then(() => {
51-
let num = Number(this.likeNum) - 1
52-
this.likeNum = String(num < 0 ? 0 : num)
53-
localStorage.removeItem("likeNum:" + this.postId)
54-
this.loading = false;
55-
this.liked = !this.liked
56-
}).catch(() => {
57-
this.loading = false;
58-
});
54+
)
55+
.then(() => {
56+
let num = Number(this.likeNum) - 1;
57+
this.likeNum = String(num < 0 ? 0 : num);
58+
localStorage.removeItem("likeNum:" + this.postId);
59+
this.loading = false;
60+
this.liked = !this.liked;
61+
})
62+
.catch(() => {
63+
this.loading = false;
64+
});
5965
},
6066
init() {
61-
if(this.likeNumExists(this.postId) !== null && Number(this.likeNumExists(this.postId)) > 0){
62-
this.liked = true
67+
if (this.likeNumExists(this.postId) !== null && Number(this.likeNumExists(this.postId)) > 0) {
68+
this.liked = true;
6369
}
64-
return this.liked
70+
return this.liked;
6571
},
6672
likeNumExists(id: any) {
6773
return localStorage.getItem("likeNum:" + id);
6874
},
69-
7075
});
71-

src/alpine-data/search.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import axios from "axios";
2+
3+
export default () => ({
4+
loading: false,
5+
isSearch: false,
6+
keys: "",
7+
searchData: {},
8+
init() {
9+
this.searchData = {};
10+
this.isSearch = false;
11+
},
12+
get keywords() {
13+
if (this.loading || this.keys == "") {
14+
this.keyClear
15+
return;
16+
}
17+
this.loading = true;
18+
axios
19+
.get("/apis/api.halo.run/v1alpha1/indices/post", {
20+
params: {
21+
limit: 20,
22+
keyword: this.keys,
23+
highlightPreTag: "<b style='background-color:#D8EAFD;color:#1077D1'>",
24+
highlightPostTag: "</b>",
25+
},
26+
headers: {
27+
"Content-Type": "application/json; charset=utf-8",
28+
},
29+
})
30+
.then((res) => {
31+
this.loading = false;
32+
this.searchData = res.data;
33+
this.isSearch = true;
34+
})
35+
.catch(() => {
36+
this.isSearch = false;
37+
this.loading = false;
38+
});
39+
},
40+
get keyClear() {
41+
this.keys = "";
42+
this.searchData = {};
43+
this.isSearch = false;
44+
return;
45+
},
46+
});

src/main.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import dropdown from "./alpine-data/dropdown";
66
import colorSchemeSwitcher from "./alpine-data/color-scheme-switcher";
77
import pagination from "./alpine-data/pagination";
88
import postUtil from "./alpine-data/post-util";
9+
import search from "./alpine-data/search";
910

1011
window.Alpine = Alpine;
1112

@@ -15,6 +16,8 @@ Alpine.data("colorSchemeSwitcher", colorSchemeSwitcher);
1516
Alpine.data("pagination", pagination);
1617
// @ts-ignore
1718
Alpine.data("postUtil", postUtil);
19+
// @ts-ignore
20+
Alpine.data("search", search);
1821

1922
Alpine.start();
2023

@@ -83,22 +86,28 @@ window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", fun
8386
});
8487

8588
/*移除HTML标签代码*/
86-
export function removeHTMLTag(str: String){
87-
str = str.replace(/<.*?>/g, ''); //去除HTML tag
88-
str = str.replace(/<\/?[^>]*>/g, ''); //去除HTML tag
89-
str = str.replace(/[ | ]*\n/g, '\n'); //去除行尾空
90-
str = str.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
91-
str = str.replace(/ /ig, '');//去掉
89+
export function removeHTMLTag(str: String) {
90+
str = str.replace(/<.*?>/g, ""); //去除HTML tag
91+
str = str.replace(/<\/?[^>]*>/g, ""); //去除HTML tag
92+
str = str.replace(/[ | ]*\n/g, "\n"); //去除行尾空
93+
str = str.replace(/\n[\s| | ]*\r/g, "\n"); //去除多余空行
94+
str = str.replace(/ /gi, ""); //去掉
9295
// str = str.replace(/[a-zA-Z]+/g, ''); //去除字母
9396
return str;
9497
}
9598

9699
/*阅读时间*/
97-
export function readTime(){
100+
export function readTime() {
98101
const contentHtml: HTMLElement | null = document.getElementById("content");
99102
// @ts-ignore
100-
let str = contentHtml.innerHTML
101-
return '文章共计 ' + removeHTMLTag(str).length +' 个字,阅读完成需要 '+ Math.ceil(removeHTMLTag(str).length/400) +' 分钟';
103+
let str = contentHtml.innerHTML;
104+
return (
105+
"文章共计 " +
106+
removeHTMLTag(str).length +
107+
" 个字,阅读完成需要 " +
108+
Math.ceil(removeHTMLTag(str).length / 400) +
109+
" 分钟"
110+
);
102111
}
103112

104113
// 快速返回顶部或底部
@@ -108,7 +117,7 @@ const onScrollToTop = () => {
108117
if (window.scrollY < 100) {
109118
backToTop?.classList.add("hidden");
110119
backToDown?.classList.add("hidden");
111-
}else if (window.scrollY > 300) {
120+
} else if (window.scrollY > 300) {
112121
backToTop?.classList.remove("hidden");
113122
backToDown?.classList.add("hidden");
114123
} else {
@@ -118,5 +127,3 @@ const onScrollToTop = () => {
118127
};
119128

120129
window.addEventListener("scroll", onScrollToTop);
121-
122-

src/styles/main.css

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
body {
2-
font-family: Roboto,Helvetica,PingFang SC,Arial,sans-serif !important;
2+
font-family: Roboto, Helvetica, PingFang SC, Arial, sans-serif !important;
33
color: #333;
44
-webkit-font-smoothing: antialiased;
55
-moz-osx-font-smoothing: grayscale;
@@ -40,7 +40,7 @@ body {
4040
}
4141

4242
.text-overflow {
43-
overflow: hidden;
43+
overflow: hidden;
4444
text-overflow: ellipsis;
4545
display: -webkit-box;
4646
-webkit-line-clamp: 1;
@@ -56,32 +56,32 @@ body {
5656
}
5757

5858
/*a标签增加指上下划线*/
59-
.hover-within a{
60-
position: relative
59+
.hover-within a {
60+
position: relative;
6161
}
6262

63-
.hover-within a::after{
63+
.hover-within a::after {
6464
--tw-bg-opacity: 1;
65-
background-color: rgb(255 255 255/var(--tw-bg-opacity));
65+
background-color: rgb(255 255 255 / var(--tw-bg-opacity));
6666
content: "";
6767
display: block;
6868
height: 1px;
6969
margin-top: 2px;
7070
position: absolute;
7171
right: 0;
72-
transition: width .2s ease;
73-
-webkit-transition: width .2s ease;
74-
width: 0
72+
transition: width 0.2s ease;
73+
-webkit-transition: width 0.2s ease;
74+
width: 0;
7575
}
7676

7777
.hover-within a:hover::after {
78-
background: #3296EF;
78+
background: #3296ef;
7979
left: 0;
80-
width: 100%
80+
width: 100%;
8181
}
8282

8383
/*a标签增加指上下划线*/
8484

8585
.prose h1 {
8686
font-size: 2em !important;
87-
}
87+
}

0 commit comments

Comments
 (0)