Files
vibemake/js/walking.js
T
Jerry 1c837766cd feat: integrate robot assembly and simulation with resilient loading
Add backend-driven assembly and simulation experiences, MAKE branding, mobile rendering budgets and desktop-only CAD/PCB routes. Improve logo loading progress, document image recovery and URDF request handling; include dev packaging and regression coverage.
2026-10-30 18:38:39 +08:00

98 lines
4.6 KiB
JavaScript

import { icon } from './layout.js';
import { hasProjectWalking, loadProjectWalking } from './project-resources.js';
export const hasWalkingDemo = hasProjectWalking;
let activeDialog;
export function openWalkingDemo(project) {
if (!hasWalkingDemo(project) || activeDialog) return;
const previousFocus = document.activeElement;
const previousOverflow = document.body.style.overflow;
const dialog = document.createElement('dialog');
activeDialog = dialog;
dialog.className = 'assembly-dialog';
dialog.setAttribute('aria-label', `${project.name || '机器人'} 仿真`);
dialog.innerHTML = `
<button class="icon-btn preview-close" data-close aria-label="关闭仿真" autofocus>${icon('close', 20)}</button>
<iframe class="walking-frame" title="机器人仿真"></iframe>
<div class="walking-status" role="status"><span class="spinner"></span><span data-message>正在加载仿真…</span><button class="btn btn-sm" data-retry hidden>重新加载</button></div>`;
const frame = dialog.querySelector('iframe');
const status = dialog.querySelector('.walking-status');
const retry = dialog.querySelector('[data-retry]');
let timeout, controller, channel, packet;
let closed = false;
const showError = (message) => {
if (closed) return;
clearTimeout(timeout);
status.hidden = false;
status.querySelector('.spinner').hidden = true;
status.querySelector('[data-message]').textContent = message;
retry.hidden = false;
};
const start = async () => {
controller?.abort();
clearTimeout(timeout);
controller = new AbortController();
const signal = controller.signal;
frame.src = 'about:blank';
packet = null;
channel = Array.from(crypto.getRandomValues(new Uint32Array(4))).join('-');
retry.hidden = true;
status.hidden = false;
status.querySelector('.spinner').hidden = false;
status.querySelector('[data-message]').textContent = '正在加载仿真…';
timeout = setTimeout(() => showError('加载时间较长,请重试'), 90000);
try {
const pkg = await loadProjectWalking(project, signal);
if (closed || signal.aborted) return;
// 执行代码始终来自本站固定运行器;上传包只提供数据,不允许指定任意 iframe / JS。
packet = pkg;
const runtime = new URL(['legacy', 'microduck-package'].includes(pkg.format) ? '/play/microduck/index.html' : '/play/robots/index.html', location.origin);
if (pkg.format === 'legacy') runtime.searchParams.set('manifest', pkg.manifestUrl);
if (pkg.format === 'legacy' && pkg.resources) runtime.searchParams.set('bound', '1');
if (pkg.format === 'microduck-package') runtime.searchParams.set('bound', 'robot');
runtime.searchParams.set('channel', channel);
if (new URLSearchParams(location.search).get('profile') === '1') runtime.searchParams.set('profile', '1');
frame.title = `${project.name || '机器人'} 仿真`;
frame.src = runtime.href;
} catch (error) {
if (!signal.aborted) showError(error.message || '资源包加载失败,请检查地址和跨域配置');
}
};
const onMessage = (event) => {
if (event.origin !== location.origin || event.source !== frame.contentWindow || event.data?.channel !== channel) return;
if (event.data.type === 'vibemake:walk-request' && packet) {
frame.contentWindow.postMessage({ type: 'vibemake:walk-init', channel, packet }, location.origin);
}
if (event.data.type === 'vibemake:walk-ready') {
clearTimeout(timeout);
status.hidden = true;
frame.focus();
}
if (event.data.type === 'vibemake:walk-error') showError(String(event.data.message || '仿真加载失败'));
if (event.data.type === 'vibemake:walk-close') dialog.close();
};
window.addEventListener('message', onMessage);
dialog.addEventListener('close', () => {
closed = true;
controller?.abort();
clearTimeout(timeout);
window.removeEventListener('message', onMessage);
frame.src = 'about:blank';
dialog.remove();
document.body.style.overflow = previousOverflow;
activeDialog = null;
previousFocus?.focus({ preventScroll: true });
}, { once: true });
dialog.querySelector('[data-close]').addEventListener('click', () => dialog.close());
retry.addEventListener('click', start);
let outside = false;
dialog.addEventListener('pointerdown', (event) => { outside = event.target === dialog; });
dialog.addEventListener('click', (event) => { if (outside && event.target === dialog) dialog.close(); });
document.body.append(dialog);
dialog.showModal();
document.body.style.overflow = 'hidden';
start();
return dialog;
}