Skip to content

Commit

Permalink
🐛
Browse files Browse the repository at this point in the history
  • Loading branch information
nanarino committed Jan 10, 2025
1 parent bc00573 commit 8ba92b3
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/scripts/client/theme/cover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@ const style = { light, dark } as const

interface Doodle extends HTMLElement {
update: (styles: string) => void
compiled?: Record<string, any>
}

function _cover_init() {
const doodle = document.querySelector("css-doodle") as Doodle
if (doodle) doodle.innerHTML = style[window.theme]
function redraw(css = style[window.theme]) {
const doodle = document.querySelector("css-doodle") as Doodle | null
if (!doodle) return
if (doodle.compiled) {
doodle.update(css)
} else {
doodle.innerHTML = css
}
}

// 初始化
_cover_init()
document.addEventListener("astro:after-swap", _cover_init)

redraw()
// 路由切換
document.addEventListener("astro:after-swap", () => redraw())
// 响应主题
window.addEventListener(`${prefix}-theme`, (event: CustomEvent<Theme>) => {
const doodle = document.querySelector("css-doodle") as Doodle
if (doodle) doodle.update(style[event.detail])
})
window.addEventListener(`${prefix}-theme`, (event: CustomEvent<Theme>) =>
redraw(style[event.detail])
)

0 comments on commit 8ba92b3

Please sign in to comment.