Files
FreeCADWasmDev/web/src/shell/command-registry.ts
2026-11-13 17:00:08 +08:00

75 lines
2.2 KiB
TypeScript

export type DesktopCommandMetadata = {
menuText: string;
toolTip: string;
icon: string;
accel: string;
};
export type DesktopMenuItem = {
type: "command" | "submenu" | "separator";
id?: string;
label?: string;
ref?: string;
items?: DesktopMenuItem[];
};
export type DesktopMenu = { label: string; items: DesktopMenuItem[] };
export type DesktopToolbar = { name: string; items: string[] };
export type DesktopWorkbench = {
id: string;
source: string;
menus: DesktopMenu[];
toolbars: DesktopToolbar[];
docks: string[];
};
export type DesktopManifest = {
schema: string;
generatedAt: string;
workbenchCount: number;
workbenches: DesktopWorkbench[];
commandMetadata: Record<string, DesktopCommandMetadata>;
commandCount: number;
menuCommandCount: number;
toolbarCommandCount: number;
};
let cache: DesktopManifest | null = null;
export async function loadDesktopManifest(url = "/api/desktop-gui-manifest.json"): Promise<DesktopManifest> {
if (cache) return cache;
const response = await fetch(url);
if (!response.ok) throw new Error(`desktop manifest HTTP ${response.status}`);
cache = (await response.json()) as DesktopManifest;
return cache;
}
export function workbenchById(manifest: DesktopManifest, id: string): DesktopWorkbench | undefined {
return manifest.workbenches.find(item => item.id === id);
}
const humanize = (command: string) => {
const stripped = command.replace(/^[A-Za-z0-9]+_/, "").replace(/_/g, " ");
const spaced = stripped.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/\s+/g, " ").trim();
return spaced || command;
};
export function commandLabel(manifest: DesktopManifest, command: string): string {
return manifest.commandMetadata[command]?.menuText?.trim() || humanize(command);
}
export function commandTooltip(manifest: DesktopManifest, command: string): string {
return manifest.commandMetadata[command]?.toolTip?.trim() || command;
}
export function menuCommandIds(menu: DesktopMenu): string[] {
const out: string[] = [];
const walk = (items: DesktopMenuItem[] = []) => {
for (const item of items) {
if (item.type === "command" && item.id) out.push(item.id);
if (item.items) walk(item.items);
}
};
walk(menu.items);
return out;
}