Skip to content

Commit

Permalink
perf: 优化部分 composable 代码细节
Browse files Browse the repository at this point in the history
  • Loading branch information
pany-ang committed Nov 25, 2024
1 parent b5210eb commit 0b51785
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/composables/useTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getActiveThemeName, setActiveThemeName } from "@/utils/cache/local-stor
import { ref, watchEffect } from "vue"

const DEFAULT_THEME_NAME = "normal"

type DefaultThemeName = typeof DEFAULT_THEME_NAME

/** 注册的主题名称, 其中 DefaultThemeName 是必填的 */
Expand Down
2 changes: 1 addition & 1 deletion src/composables/useTitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function setTitle(title?: string) {
dynamicTitle.value = title ? `${VITE_APP_TITLE} | ${title}` : VITE_APP_TITLE
}

/** 监听标题变化 */
// 监听标题变化
watch(dynamicTitle, (value, oldValue) => {
if (document && value !== oldValue) {
document.title = value
Expand Down
17 changes: 6 additions & 11 deletions src/composables/useWatermark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ interface Observer {
parentElResizeObserver?: ResizeObserver
}

type DefaultConfig = typeof defaultConfig
type DefaultConfig = typeof DEFAULT_CONFIG

/** 默认配置 */
const defaultConfig = {
const DEFAULT_CONFIG = {
/** 防御(默认开启,能防御水印被删除或隐藏,但可能会有性能损耗) */
defense: true,
/** 文本颜色 */
Expand Down Expand Up @@ -54,14 +54,11 @@ export function useWatermark(parentEl: Ref<HTMLElement | null> = bodyEl) {

/** 设置水印 */
const setWatermark = (text: string, config: Partial<DefaultConfig> = {}) => {
if (!parentEl.value) {
console.warn("请在 DOM 挂载完成后再调用 setWatermark 方法设置水印")
return
}
if (!parentEl.value) return console.warn("请在 DOM 挂载完成后再调用 setWatermark 方法设置水印")
// 备份文本
backupText = text
// 合并配置
mergeConfig = { ...defaultConfig, ...config }
mergeConfig = { ...DEFAULT_CONFIG, ...config }
// 创建或更新水印元素
watermarkEl ? updateWatermarkEl() : createWatermarkEl()
// 监听水印元素和容器元素的变化
Expand Down Expand Up @@ -228,10 +225,8 @@ export function useWatermark(parentEl: Ref<HTMLElement | null> = bodyEl) {
observer.parentElResizeObserver.observe(targetNode)
}

/** 在组件卸载前移除水印以及各种监听 */
onBeforeUnmount(() => {
clearWatermark()
})
// 在组件卸载前移除水印以及各种监听
onBeforeUnmount(() => clearWatermark())

return { setWatermark, clearWatermark }
}

0 comments on commit 0b51785

Please sign in to comment.