UNPKG

vitepress-plugin-announcement

Version:
47 lines (46 loc) 1.4 kB
import { onMounted, onUnmounted, ref } from 'vue'; const listDelimiterRE = /;(?![^(]*\))/g; const propertyDelimiterRE = /:([^]+)/; const styleCommentRE = /\/\*[^]*?\*\//g; export function parseStringStyle(cssText) { const ret = {}; cssText.replace(styleCommentRE, '').split(listDelimiterRE).forEach((item) => { if (item) { const tmp = item.split(propertyDelimiterRE); tmp.length > 1 && (ret[tmp[0].trim()] = tmp[1].trim()); } }); return ret; } export function useDebounceFn(fn, delay) { let timer; return (...args) => { clearTimeout(timer); timer = setTimeout(() => { fn(...args); }, delay); }; } export const inBrowser = typeof document !== 'undefined'; export function useWindowSize() { const width = ref(Number.POSITIVE_INFINITY); const height = ref(Number.POSITIVE_INFINITY); const updateSize = useDebounceFn(() => { if (inBrowser) { width.value = window.innerWidth; height.value = window.innerHeight; } }, 100); onMounted(() => { if (inBrowser) { window.addEventListener('resize', updateSize, { passive: true }); } }); onUnmounted(() => { if (inBrowser) { window.removeEventListener('resize', updateSize); } }); updateSize(); return { width, height }; }