teaching: run lessons in-app through an iframe-driven console
The teaching console now lives inside Web FreeCAD: web/teaching-console.html hosts the workbench in a same-origin iframe and drives its DOM directly, with no Node server and no spawned Chrome. The driver modules run in the framed page's own realm (bridge.ts publishes window.__teaching), so the proven driver/assembly code is reused unchanged and the CSP without 'unsafe-eval' is never crossed. - steps.ts globs both courses' ui-steps records (skipping their index.json manifests) so each lesson downloads only its own chunk - runner.ts checks prerequisites in dependency order, then runs part or assembly lessons; a pass stores the FCStd in OPFS freecad-documents under the course document name, which is what assembly lessons consume - assembly lessons insert, joint, solve and save through lib/assembly.mjs; insertLink falls back to UI-suffixed object names (Body1 for Body) - check-inapp-console.mjs (npm run check:console-inapp) is green: preflight, P01 volume within 6e-13, OPFS evidence, both viewports - run-inapp-lessons.mjs runs a whole course in one browser; reducer 23 parts + assembly and carrier 20 parts + assembly all pass in-browser Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
82
web/src/navigation.ts
Normal file
82
web/src/navigation.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
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<Location, "search"> = 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<HTMLAnchorElement>(".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) => `<a href="${navigationHref(route, context)}" data-route="${route.id}"${route.id === currentId ? " aria-current=page" : ""}>${route.label}</a>`).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);
|
||||
}
|
||||
Reference in New Issue
Block a user