83b935c11b
title-breadcrumb 提供 resolveDocumentTitle('页面 - 数富AI')、updateDocumentTitle
(非浏览器静默)、crumbsForActiveRoute(分组->页面);AdminLayout 用 watch 同步
document.title 并在层级>1 时渲染 el-breadcrumb。
43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
import type { AdminMenuNode } from '../types/admin'
|
|
|
|
export const ADMIN_BRAND = '数富AI'
|
|
|
|
/** 文档标题规则:`页面标题 - 数富AI`;无页面标题时仅品牌。 */
|
|
export function resolveDocumentTitle(pageTitle: string, brand: string = ADMIN_BRAND): string {
|
|
const trimmed = (pageTitle || '').trim()
|
|
return trimmed ? `${trimmed} - ${brand}` : brand
|
|
}
|
|
|
|
/** 同步 HTML 文档标题;doc 为空(如非浏览器环境)时静默跳过。 */
|
|
export function updateDocumentTitle(title: string, doc: { title: string } | null | undefined): void {
|
|
if (doc) doc.title = title
|
|
}
|
|
|
|
export interface BreadcrumbItem {
|
|
key: string
|
|
name: string
|
|
route?: string
|
|
}
|
|
|
|
/**
|
|
* 面包屑规则:在菜单树中定位激活路由,返回“分组 -> 页面”层级(顶层叶子只返回自身)。
|
|
* 无命中返回空数组,不伪造路径。
|
|
*/
|
|
export function crumbsForActiveRoute(nodes: AdminMenuNode[] | null | undefined, activeRoute: string): BreadcrumbItem[] {
|
|
for (const node of nodes || []) {
|
|
if (node.route === activeRoute) return [{ key: node.key, name: node.name, route: node.route }]
|
|
const children = node.children || []
|
|
for (const child of children) {
|
|
if (child.route === activeRoute) {
|
|
return [
|
|
{ key: node.key, name: node.name },
|
|
{ key: child.key, name: child.name, route: child.route },
|
|
]
|
|
}
|
|
}
|
|
const nested = crumbsForActiveRoute(children, activeRoute)
|
|
if (nested.length) return [{ key: node.key, name: node.name }, ...nested]
|
|
}
|
|
return []
|
|
}
|