diff --git a/course/.vitepress/theme/index.ts b/course/.vitepress/theme/index.ts index 4e480201..91f4f26a 100644 --- a/course/.vitepress/theme/index.ts +++ b/course/.vitepress/theme/index.ts @@ -7,24 +7,25 @@ import "./style/print.css"; import { h } from "vue"; -// 保证在打印时所有的 details 都是展开的 -window.addEventListener("beforeprint", function () { - document.querySelectorAll("details").forEach(function (details) { - details.setAttribute("open", ""); - }); -}); - -// 打印后重置 details 的展开状态 -window.addEventListener("afterprint", function () { - document.querySelectorAll("details").forEach(function (details) { - details.removeAttribute("open"); - }); -}); - export default { ...DefaultTheme, enhanceApp(ctx: any) { DefaultTheme.enhanceApp(ctx); + if (typeof window != "undefined") { + // 保证在打印时所有的 details 都是展开的 + window.addEventListener("beforeprint", function () { + document.querySelectorAll("details").forEach(function (details) { + details.setAttribute("open", ""); + }); + }); + + // 打印后重置 details 的展开状态 + window.addEventListener("afterprint", function () { + document.querySelectorAll("details").forEach(function (details) { + details.removeAttribute("open"); + }); + }); + } }, Layout() { return h(DefaultTheme.Layout, null, {