Skip to content

Commit 0b51785

Browse files
committed
perf: 优化部分 composable 代码细节
1 parent b5210eb commit 0b51785

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

src/composables/useTheme.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { getActiveThemeName, setActiveThemeName } from "@/utils/cache/local-stor
22
import { ref, watchEffect } from "vue"
33

44
const DEFAULT_THEME_NAME = "normal"
5+
56
type DefaultThemeName = typeof DEFAULT_THEME_NAME
67

78
/** 注册的主题名称, 其中 DefaultThemeName 是必填的 */

src/composables/useTitle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function setTitle(title?: string) {
1111
dynamicTitle.value = title ? `${VITE_APP_TITLE} | ${title}` : VITE_APP_TITLE
1212
}
1313

14-
/** 监听标题变化 */
14+
// 监听标题变化
1515
watch(dynamicTitle, (value, oldValue) => {
1616
if (document && value !== oldValue) {
1717
document.title = value

src/composables/useWatermark.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ interface Observer {
88
parentElResizeObserver?: ResizeObserver
99
}
1010

11-
type DefaultConfig = typeof defaultConfig
11+
type DefaultConfig = typeof DEFAULT_CONFIG
1212

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

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

231-
/** 在组件卸载前移除水印以及各种监听 */
232-
onBeforeUnmount(() => {
233-
clearWatermark()
234-
})
228+
// 在组件卸载前移除水印以及各种监听
229+
onBeforeUnmount(() => clearWatermark())
235230

236231
return { setWatermark, clearWatermark }
237232
}

0 commit comments

Comments
 (0)