blog/themes/farallon/assets/ts/app.ts

180 lines
6.8 KiB
TypeScript
Raw Normal View History

2024-06-14 14:03:34 +08:00
import { farallonHelper } from "./utils";
import farallonDate from "./date.ts";
import farallonActions from "./action.ts";
import { farallonComment } from "./comment.ts";
import Douban from "./db.ts";
declare global {
interface Window {
actionDomain: string;
timeFormat: string;
dbAPIBase: string;
}
}
class farallonBase extends farallonHelper {
is_single: boolean = false;
post_id: number = 0;
is_archive: boolean = false;
VERSION: string = "0.4.3";
like_btn: any;
selctor: string = ".like-btn";
actionDomain: string = window.actionDomain;
constructor() {
super();
this.initCopyright();
this.initThemeSwitch();
this.initBack2Top();
this.initSearch();
}
initSearch() {
document
.querySelector('[data-action="show-search"]')!
.addEventListener("click", () => {
document
.querySelector(".site--header__center .inner")!
.classList.toggle("search--active");
});
}
initBack2Top() {
if (document.querySelector(".backToTop")) {
const backToTop = document.querySelector(
".backToTop"
) as HTMLElement;
window.addEventListener("scroll", () => {
const t = window.scrollY || window.pageYOffset;
// console.log(t);
// const documentHeight = document.body.clientHeight;
//const windowHeight = window.innerHeight;
// const percent = Math.ceil((t / (documentHeight - windowHeight)) * 100);
t > 200
? backToTop!.classList.add("is-active")
: backToTop!.classList.remove("is-active");
});
backToTop.addEventListener("click", () => {
window.scrollTo({ top: 0, behavior: "smooth" });
});
2024-05-06 12:00:46 +08:00
}
2024-06-14 14:03:34 +08:00
}
2024-05-06 12:00:46 +08:00
2024-06-14 14:03:34 +08:00
initCopyright() {
2024-05-06 12:00:46 +08:00
const copyright = `<div class="site--footer__info">
Theme <a href="https://fatesinger.com/101971" target="_blank">farallon</a> by bigfa / version ${this.VERSION}
</div>`;
document
.querySelector(".site--footer__content")!
.insertAdjacentHTML("afterend", copyright);
document
.querySelector(".icon--copryrights")!
.addEventListener("click", () => {
document
.querySelector(".site--footer__info")!
.classList.toggle("active");
});
}
2024-06-14 14:03:34 +08:00
initThemeSwitch() {
2024-05-06 12:00:46 +08:00
const theme = localStorage.getItem("theme")
? localStorage.getItem("theme")
: "auto";
const html = `<div class="fixed--theme">
<span class="${
theme == "dark" ? "is-active" : ""
}" data-action-value="dark">
<svg fill="none" height="24" shape-rendering="geometricPrecision" stroke="currentColor" stroke-linecap="round"
stroke-linejoin="round" stroke-width="1.5" viewBox="0 0 24 24" width="24"
style="color: currentcolor; width: 16px; height: 16px;">
<path d="M21 12.79A9 9 0 1111.21 3 7 7 0 0021 12.79z"></path>
</svg>
</span>
<span class="${
theme == "light" ? "is-active" : ""
}" data-action-value="light">
<svg fill="none" height="24" shape-rendering="geometricPrecision" stroke="currentColor" stroke-linecap="round"
stroke-linejoin="round" stroke-width="1.5" viewBox="0 0 24 24" width="24"
style="color: currentcolor; width: 16px; height: 16px;">
<circle cx="12" cy="12" r="5"></circle>
<path d="M12 1v2"></path>
<path d="M12 21v2"></path>
<path d="M4.22 4.22l1.42 1.42"></path>
<path d="M18.36 18.36l1.42 1.42"></path>
<path d="M1 12h2"></path>
<path d="M21 12h2"></path>
<path d="M4.22 19.78l1.42-1.42"></path>
<path d="M18.36 5.64l1.42-1.42"></path>
</svg>
</span>
<span class="${
theme == "auto" ? "is-active" : ""
}" data-action-value="auto">
<svg fill="none" height="24" shape-rendering="geometricPrecision" stroke="currentColor" stroke-linecap="round"
stroke-linejoin="round" stroke-width="1.5" viewBox="0 0 24 24" width="24"
style="color: currentcolor; width: 16px; height: 16px;">
<rect x="2" y="3" width="20" height="14" rx="2" ry="2"></rect>
<path d="M8 21h8"></path>
<path d="M12 17v4"></path>
</svg>
</span>
</div>`;
document.querySelector("body")!.insertAdjacentHTML("beforeend", html);
document.querySelectorAll(".fixed--theme span").forEach((item) => {
item.addEventListener("click", () => {
if (item.classList.contains("is-active")) return;
document
.querySelectorAll(".fixed--theme span")
2024-06-14 14:03:34 +08:00
.forEach((item: Element) => {
2024-05-06 12:00:46 +08:00
item.classList.remove("is-active");
});
2024-06-14 14:03:34 +08:00
if ((item as HTMLElement).dataset.actionValue == "dark") {
2024-05-06 12:00:46 +08:00
localStorage.setItem("theme", "dark");
document.querySelector("body")!.classList.remove("auto");
document.querySelector("body")!.classList.add("dark");
item.classList.add("is-active");
//this.showNotice('夜间模式已开启');
2024-06-14 14:03:34 +08:00
} else if (
(item as HTMLElement).dataset.actionValue == "light"
) {
2024-05-06 12:00:46 +08:00
localStorage.setItem("theme", "light");
document.querySelector("body")!.classList.remove("auto");
document.querySelector("body")!.classList.remove("dark");
item.classList.add("is-active");
//this.showNotice('夜间模式已关闭');
2024-06-14 14:03:34 +08:00
} else if (
(item as HTMLElement).dataset.actionValue == "auto"
) {
2024-05-06 12:00:46 +08:00
localStorage.setItem("theme", "auto");
document.querySelector("body")!.classList.remove("dark");
document.querySelector("body")!.classList.add("auto");
item.classList.add("is-active");
//this.showNotice('夜间模式已关闭');
}
});
});
}
}
2024-06-14 14:03:34 +08:00
new farallonActions({
singleSelector: ".post--single",
articleSelector: ".post--item",
likeButtonSelctor: ".like-btn",
actionDomain: window.actionDomain,
});
2024-05-06 12:00:46 +08:00
new farallonBase();
2024-06-14 14:03:34 +08:00
new farallonDate({
selector: ".humane--time",
timeFormat: window.timeFormat,
});
new farallonComment();
new Douban({
baseAPI: window.dbAPIBase,
container: ".db--container",
});