export interface NavigationRoute { id: string; label: string; href: string; scope: "core" | "workflow" | "runtime"; } export interface NavigationContext { documentId?: string; module?: string; command?: string; } import { WEB_MODULE_REGISTRY } from "./module-registry.generated.js"; export const WEB_NAVIGATION_ROUTES: readonly NavigationRoute[] = [ { id: "home", label: "3D 视图", href: "/index.html", scope: "core" }, { id: "document", label: "Document", href: "/document.html", scope: "core" }, { id: "main-chain", label: "主链", href: "/main-chain.html", scope: "workflow" }, { id: "project", label: "工程", href: "/project.html", scope: "workflow" }, { id: "full", label: "全线", href: "/full.html", scope: "workflow" }, { id: "workbench", label: "工作台", href: "/workbench.html", scope: "workflow" }, { id: "teaching-console", label: "教学", href: "/teaching-console.html", scope: "workflow" }, { id: "python", label: "Python", href: "/python.html", scope: "runtime" }, ...WEB_MODULE_REGISTRY.map((route) => ({ id: `module-${route.module}`, label: route.module, href: moduleHref(route.module), scope: "workflow" as const })), ]; const CONTEXT_KEYS: readonly [keyof NavigationContext, string][] = [ ["documentId", "document"], ["module", "module"], ["command", "command"], ]; function contextValue(value: string | null | undefined): string | undefined { if (!value) return undefined; const normalized = value.trim(); return normalized.length > 0 && normalized.length <= 160 ? normalized : undefined; } export function readNavigationContext(source: Pick = window.location): NavigationContext { const params = new URLSearchParams(source.search); return Object.fromEntries(CONTEXT_KEYS.flatMap(([key, queryKey]) => { const value = contextValue(params.get(queryKey)); return value ? [[key, value]] : []; })) as NavigationContext; } export function navigationHref(route: NavigationRoute, context: NavigationContext = readNavigationContext()): string { if (route.id.startsWith("module-")) context = { documentId: context.documentId, module: route.id.slice(7) }; const url = new URL(route.href, window.location.origin); for (const [key, queryKey] of CONTEXT_KEYS) { const value = contextValue(context[key]); if (value) url.searchParams.set(queryKey, value); else url.searchParams.delete(queryKey); } return `${url.pathname}${url.search}${url.hash}`; } export function updateNavigationContext(context: NavigationContext, mode: "push" | "replace" = "replace"): void { const url = new URL(window.location.href); for (const [key, queryKey] of CONTEXT_KEYS) { const value = contextValue(context[key]); if (value) url.searchParams.set(queryKey, value); else url.searchParams.delete(queryKey); } const next = `${url.pathname}${url.search}${url.hash}`; if (mode === "push") window.history.pushState({ navigationContext: context }, "", next); else window.history.replaceState({ navigationContext: context }, "", next); document.querySelectorAll(".global-nav a[data-route]").forEach(link => { const route = WEB_NAVIGATION_ROUTES.find(route => route.id === link.dataset.route); if (route) link.href = navigationHref(route, context); }); } function moduleHref(module: string): string { return `/workbench.html?module=${encodeURIComponent(module)}`; } export function installNavigation(currentId: string, context: NavigationContext = readNavigationContext()): void { if (document.querySelector(".global-nav")) return; const nav = document.createElement("nav"); nav.className = "global-nav"; nav.setAttribute("aria-label", "FreeCAD Web navigation"); nav.innerHTML = WEB_NAVIGATION_ROUTES.map((route) => `${route.label}`).join(""); if (context.documentId) nav.dataset.documentId = context.documentId; if (context.module) nav.dataset.module = context.module; if (context.command) nav.dataset.command = context.command; const style = document.createElement("style"); style.textContent = ".global-nav{align-items:center;background:#10161dcc;border-bottom:1px solid #2c3b48;box-sizing:border-box;contain:layout paint;display:flex;gap:4px;width:100%;min-width:0;max-width:100%;overflow-x:auto;padding:5px 12px;position:relative;z-index:10}.global-nav a{border:1px solid transparent;border-radius:4px;color:#9cb6c2;flex-shrink:0;font-size:12px;padding:5px 9px;text-decoration:none;white-space:nowrap}.global-nav a:hover,.global-nav a[aria-current=page]{background:#243b47;border-color:#435464;color:#e7edf3}"; document.head.append(style); document.body.prepend(nav); }