优化 usePage

This commit is contained in:
icssoa
2025-08-11 19:14:03 +08:00
parent 1ddfec2bd3
commit f2621341a4
19 changed files with 266 additions and 162 deletions

View File

@@ -1,5 +1,4 @@
export * from "./refs";
export * from "./page";
export * from "./pager";
export * from "./long-press";
export * from "./cache";

View File

@@ -1,102 +0,0 @@
import { config } from "@/config";
import { router } from "../router";
import { getPx, isH5, isHarmony } from "../utils";
import { ctx } from "../ctx";
class Page {
scrolls: Map<string, ((top: number) => void)[]> = new Map();
path() {
return router.path();
}
/**
* 触发滚动事件
* @param top 滚动距离
*/
triggerScroll(top: number) {
const callbacks = this.scrolls.get(this.path()) ?? [];
callbacks.forEach((cb) => {
cb(top);
});
}
/**
* 注册滚动事件回调
* @param callback 回调函数
*/
onPageScroll(callback: (top: number) => void) {
const callbacks = this.scrolls.get(this.path()) ?? [];
callbacks.push(callback);
this.scrolls.set(this.path(), callbacks);
}
/**
* 是否需要计算 tabBar 高度
* @returns boolean
*/
hasCustomTabBar() {
if (router.isTabPage()) {
if (isHarmony()) {
return false;
}
return config.isCustomTabBar || isH5();
}
return false;
}
/**
* 是否存在自定义 topbar
* @returns boolean
*/
hasCustomTopbar() {
return router.route()?.isCustomNavbar ?? false;
}
/**
* 获取 tabBar 高度
* @returns tabBar 高度
*/
getTabBarHeight() {
let h = ctx.tabBar.height == null ? 50 : getPx(ctx.tabBar.height!);
if (this.hasCustomTabBar()) {
h += this.getSafeAreaHeight("bottom");
}
return h;
}
/**
* 获取安全区域高度
* @param type 类型
* @returns 安全区域高度
*/
getSafeAreaHeight(type: "top" | "bottom") {
const { safeAreaInsets } = uni.getWindowInfo();
let h: number;
if (type == "top") {
h = safeAreaInsets.top;
} else {
h = safeAreaInsets.bottom;
// #ifdef APP-ANDROID
if (h == 0) {
h = 16;
}
// #endif
}
return h;
}
}
export const page = new Page();
export function usePage(): Page {
return page;
}