来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
87 lines
2.7 KiB
TypeScript
87 lines
2.7 KiB
TypeScript
/** 侧栏菜单路径匹配:最长前缀 + 全局叶子消歧 */
|
|
|
|
export function normalizePathname(pathname: string): string {
|
|
if (pathname.length > 1 && pathname.endsWith('/')) {
|
|
return pathname.slice(0, -1);
|
|
}
|
|
return pathname;
|
|
}
|
|
|
|
export function collectMenuLeafPaths(items: Array<{ path: string; children?: Array<{ path: string }> }>): string[] {
|
|
const paths: string[] = [];
|
|
for (const item of items) {
|
|
if (item.children?.length) {
|
|
for (const child of item.children) {
|
|
paths.push(child.path);
|
|
}
|
|
} else {
|
|
paths.push(item.path);
|
|
}
|
|
}
|
|
return paths;
|
|
}
|
|
|
|
/**
|
|
* 判断 pathname 是否匹配某菜单路径。
|
|
* 短路径前缀匹配时,若全局存在更长的菜单路径也匹配当前页,则不算匹配
|
|
* (避免 /orders 误匹配 /orders/bids,尤其当二者分属不同菜单组时)。
|
|
*/
|
|
export function isMenuPathMatch(pathname: string, menuPath: string, allLeafPaths: string[]): boolean {
|
|
const normalized = normalizePathname(pathname);
|
|
if (normalized === menuPath) return true;
|
|
if (!normalized.startsWith(`${menuPath}/`)) return false;
|
|
return !allLeafPaths.some(
|
|
(p) =>
|
|
p !== menuPath &&
|
|
p.length > menuPath.length &&
|
|
(normalized === p || normalized.startsWith(`${p}/`))
|
|
);
|
|
}
|
|
|
|
/** 按最长路径前缀匹配当前子菜单(结合全局叶子路径消歧) */
|
|
export function findActiveMenuChild<T extends { path: string }>(
|
|
pathname: string,
|
|
children: T[],
|
|
allLeafPaths: string[]
|
|
): T | undefined {
|
|
const normalized = normalizePathname(pathname);
|
|
let best: T | undefined;
|
|
for (const child of children) {
|
|
if (!isMenuPathMatch(normalized, child.path, allLeafPaths)) continue;
|
|
if (!best || child.path.length > best.path.length) best = child;
|
|
}
|
|
return best;
|
|
}
|
|
|
|
export function isStandaloneMenuActive(pathname: string, itemPath: string): boolean {
|
|
const normalized = normalizePathname(pathname);
|
|
return normalized === itemPath || normalized.startsWith(`${itemPath}/`);
|
|
}
|
|
|
|
export function isActiveInMenuGroup<T extends { path: string }>(
|
|
pathname: string,
|
|
children: T[],
|
|
allLeafPaths: string[]
|
|
): boolean {
|
|
return Boolean(findActiveMenuChild(pathname, children, allLeafPaths));
|
|
}
|
|
|
|
export function isMenuChildActive<T extends { path: string }>(
|
|
pathname: string,
|
|
childPath: string,
|
|
siblings: T[],
|
|
allLeafPaths: string[]
|
|
): boolean {
|
|
return findActiveMenuChild(pathname, siblings, allLeafPaths)?.path === childPath;
|
|
}
|
|
|
|
/** 当前分组是否应完全展开(有激活子项且未手动折叠,或用户已点开) */
|
|
export function shouldExpandMenuGroup(input: {
|
|
hasActiveChild: boolean;
|
|
userCollapsed: boolean;
|
|
userExpanded: boolean;
|
|
}): boolean {
|
|
if (input.userCollapsed) return false;
|
|
return input.hasActiveChild || input.userExpanded;
|
|
}
|