1
0
mirror of https://github.com/sendevia/website.git synced 2026-03-05 23:32:45 +08:00

refactor: replace window checks with centralized env utility

This commit is contained in:
2025-12-10 15:43:46 +08:00
parent ecd2385e79
commit 2b4e351c08
7 changed files with 43 additions and 13 deletions

View File

@@ -1,12 +1,13 @@
<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useGlobalData } from "../composables/useGlobalData";
import { isClient } from "../utils/env";
const { page, frontmatter, theme } = useGlobalData();
const seed = ref(1);
const defaultImpression = theme.value.defaultImpression;
if (typeof window !== "undefined") {
if (isClient()) {
onMounted(() => {
seed.value = Date.now();
});

View File

@@ -2,6 +2,7 @@
import { ref, computed, onMounted, onBeforeUnmount, nextTick, watch } from "vue";
import { useGlobalData } from "../composables/useGlobalData";
import { useScreenWidthStore } from "../stores/screenWidth";
import { isClient } from "../utils/env";
const { page, frontmatter } = useGlobalData();
const screenWidthStore = useScreenWidthStore();
@@ -171,7 +172,7 @@ const resizeHandler = () => {
}
};
if (typeof window !== "undefined") {
if (isClient()) {
onMounted(() => {
screenWidthStore.init();

View File

@@ -1,4 +1,5 @@
import { ref, computed, onMounted } from "vue";
import { isClient } from "../utils/env";
let container: HTMLElement | Window | null = null;
let isInitialized = false;
@@ -47,7 +48,7 @@ function initGlobalScrollListener(initialThreshold: number = threshold, scrollCo
threshold = initialThreshold;
targetScrollable = scrollContainer;
if (typeof window !== "undefined") {
if (isClient()) {
const updateContainer = () => {
if (container) {
const target: any = container;

View File

@@ -1,5 +1,6 @@
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from "vue";
import { isClient } from "../utils/env";
const showImageViewer = ref(false);
const currentImageIndex = ref(0);
@@ -86,7 +87,7 @@ function olCountAttributes() {
});
}
if (typeof window !== "undefined") {
if (isClient()) {
onMounted(() => {
const anchors = document.querySelectorAll<HTMLAnchorElement>("a.title-anchor");
anchors.forEach((anchor) => {

View File

@@ -7,6 +7,7 @@ import { onMounted, nextTick, computed, ref, watch } from "vue";
import { useRoute } from "vitepress";
import { useGlobalData } from "../composables/useGlobalData";
import { usePostStore } from "../stores/posts";
import { isClient } from "../utils/env";
const { site, page, frontmatter, theme } = useGlobalData();
const route = useRoute();
@@ -40,7 +41,7 @@ function checkAndRedirect(path: string): boolean {
}
redirectTimer = setTimeout(() => {
if (typeof window !== "undefined") {
if (isClient()) {
window.location.replace(post.url);
}
}, 100);
@@ -133,7 +134,7 @@ function onBeforeLeave() {
isTransitioning.value = true;
}
if (typeof window !== "undefined") {
if (isClient()) {
onMounted(() => {
if (!route.path.startsWith("/p/")) {
updatePalette();

View File

@@ -1,5 +1,6 @@
import { defineStore } from "pinia";
import { ref, watch, onUnmounted } from "vue";
import { isClient } from "../utils/env";
/**
* 屏幕宽度响应式状态管理
@@ -14,13 +15,6 @@ export const useScreenWidthStore = defineStore("screenWidth", () => {
let resizeHandler: (() => void) | null = null;
let isInitialized = false;
/**
* 检查是否在客户端环境
*/
function isClient() {
return typeof window !== "undefined";
}
/**
* 更新屏幕宽度状态
*/

View File

@@ -0,0 +1,31 @@
/**
* 判断当前是否在客户端环境
* @returns 如果是客户端环境返回 true否则返回 false
*/
export function isClient(): boolean {
return typeof window !== "undefined";
}
/**
* 判断当前是否在服务器端环境
* @returns 如果是服务器端环境返回 true否则返回 false
*/
export function isServer(): boolean {
return !isClient();
}
/**
* 判断当前是否在开发环境
* @returns 如果是开发环境返回 true否则返回 false
*/
export function isDev(): boolean {
return process.env.NODE_ENV === "development";
}
/**
* 判断当前是否在生产环境
* @returns 如果是生产环境返回 true否则返回 false
*/
export function isProd(): boolean {
return process.env.NODE_ENV === "production";
}