1c837766cd
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.
225 lines
11 KiB
JavaScript
225 lines
11 KiB
JavaScript
import { icon } from './layout.js';
|
|
import { assemblySource, loadProjectAssembly } from './project-resources.js';
|
|
|
|
export const getAssemblyModel = (project) => assemblySource(project);
|
|
|
|
let activeDialog;
|
|
|
|
export function openAssemblyPreview(project, { initialStep = null, focusPartId = null } = {}) {
|
|
const model = getAssemblyModel(project);
|
|
if (!model || 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="关闭 AI 装配" autofocus>${icon('close', 20)}</button>
|
|
<div class="assembly-viewport" aria-label="机器人三维模型">
|
|
<div class="assembly-scene"></div>
|
|
<button class="assembly-motion-status" data-focus hidden aria-label="聚焦当前螺丝">
|
|
<span data-motion-label></span><span>查看特写 ↗</span>
|
|
</button>
|
|
<div class="assembly-loading" role="status" aria-live="polite">
|
|
<span class="spinner"></span><strong>正在加载装配模型</strong><span data-progress>准备模型与材质…</span>
|
|
<button class="btn btn-sm" data-retry hidden>重新加载</button>
|
|
</div>
|
|
<div class="assembly-step" hidden aria-live="polite">
|
|
<div class="assembly-step-row">
|
|
<button class="btn btn-sm" data-prev aria-label="上一步装配">←</button>
|
|
<span data-step-label></span>
|
|
<button class="btn btn-sm" data-next aria-label="下一步装配">→</button>
|
|
</div>
|
|
<input class="assembly-progress" data-seek type="range" min="0" step="1" value="0" aria-label="装配进度">
|
|
</div>
|
|
<label class="assembly-explosion" hidden>
|
|
<span>分散程度</span>
|
|
<input data-explosion type="range" min="0" max="100" step="1" value="65" aria-label="爆炸分散程度">
|
|
<output data-explosion-value>65%</output>
|
|
</label>
|
|
<div class="assembly-tools">
|
|
<button class="btn btn-sm" data-steps hidden>逐步装配</button>
|
|
<button class="btn btn-sm" data-explode hidden disabled aria-pressed="false">爆炸图</button>
|
|
<button class="btn btn-sm" data-play hidden aria-pressed="false">播放装配</button>
|
|
<button class="btn btn-sm" data-replay hidden>重播本步</button>
|
|
<button class="btn btn-sm" data-rotate aria-pressed="false" disabled>${icon('play', 13)}自动旋转</button>
|
|
<button class="btn btn-sm" data-reset disabled>${icon('history', 13)}重置视角</button>
|
|
</div>
|
|
</div>`;
|
|
document.body.append(dialog);
|
|
dialog.showModal();
|
|
document.body.style.overflow = 'hidden';
|
|
|
|
const find = (selector) => dialog.querySelector(selector);
|
|
const controller = new AbortController();
|
|
let preview;
|
|
let loading = false;
|
|
let config;
|
|
let stepIndex = null;
|
|
let playing = false;
|
|
let playTimer;
|
|
let activeScrew;
|
|
let exploded = false;
|
|
const updateExplosionUI = (enabled) => {
|
|
exploded = enabled;
|
|
find('.assembly-viewport').classList.toggle('is-exploded', enabled);
|
|
find('.assembly-explosion').hidden = !enabled;
|
|
find('[data-explode]').textContent = enabled ? '还原模型' : '爆炸图';
|
|
find('[data-explode]').setAttribute('aria-pressed', String(enabled));
|
|
};
|
|
const stopPlayback = () => {
|
|
playing = false;
|
|
clearTimeout(playTimer);
|
|
find('[data-play]').textContent = preview?.transitions?.length ? '继续装配' : '播放装配';
|
|
find('[data-play]').setAttribute('aria-pressed', 'false');
|
|
preview?.setAnimationPaused(true);
|
|
};
|
|
const showStep = (index, animate = true) => {
|
|
stepIndex = index;
|
|
updateExplosionUI(false);
|
|
preview.setStep(index, animate);
|
|
find('[data-play]').textContent = preview.transitions.length ? '暂停装配' : '播放装配';
|
|
find('[data-play]').setAttribute('aria-pressed', String(preview.transitions.length > 0));
|
|
find('.assembly-viewport').classList.toggle('is-stepping', index !== null);
|
|
find('.assembly-step').hidden = index === null;
|
|
find('[data-play]').hidden = index === null;
|
|
find('[data-replay]').hidden = index === null;
|
|
find('[data-rotate]').hidden = index !== null;
|
|
find('[data-explode]').hidden = index !== null || config.parts.length < 2;
|
|
find('[data-steps]').textContent = index === null ? '逐步装配' : '查看整体';
|
|
if (index !== null) {
|
|
preview.setAutoRotate(false);
|
|
find('[data-rotate]').setAttribute('aria-pressed', 'false');
|
|
}
|
|
if (index !== null) {
|
|
find('[data-step-label]').textContent = `${index + 1} / ${config.steps.length} · ${config.steps[index].label}`;
|
|
find('[data-step-label]').title = find('[data-step-label]').textContent;
|
|
find('[data-seek]').value = String(index);
|
|
find('[data-seek]').setAttribute('aria-valuetext', `第 ${index + 1} 步,共 ${config.steps.length} 步,${config.steps[index].label}`);
|
|
find('[data-prev]').disabled = index === 0;
|
|
find('[data-next]').disabled = index === config.steps.length - 1;
|
|
}
|
|
};
|
|
find('[data-explode]').addEventListener('click', () => {
|
|
stopPlayback();
|
|
updateExplosionUI(!exploded);
|
|
preview.setExploded(exploded, Number(find('[data-explosion]').value) / 100);
|
|
});
|
|
find('[data-explosion]').addEventListener('input', (event) => {
|
|
const amount = Number(event.target.value);
|
|
find('[data-explosion-value]').textContent = `${amount}%`;
|
|
preview.setExploded(true, amount / 100, false);
|
|
});
|
|
find('[data-steps]').addEventListener('click', () => { stopPlayback(); showStep(stepIndex === null ? 0 : null); });
|
|
find('[data-prev]').addEventListener('click', () => { stopPlayback(); showStep(Math.max(0, stepIndex - 1)); });
|
|
find('[data-next]').addEventListener('click', () => { stopPlayback(); showStep(Math.min(config.steps.length - 1, stepIndex + 1)); });
|
|
find('[data-seek]').addEventListener('input', (event) => { stopPlayback(); showStep(Number(event.target.value), false); });
|
|
find('[data-replay]').addEventListener('click', () => { stopPlayback(); showStep(stepIndex); });
|
|
find('[data-focus]').addEventListener('click', () => { if (activeScrew) preview.focusPart(activeScrew); });
|
|
find('[data-play]').addEventListener('click', () => {
|
|
if (playing || (preview.transitions.length && !preview.animationPaused)) { stopPlayback(); return; }
|
|
if (stepIndex === config.steps.length - 1 && !preview.transitions.length) showStep(0, false);
|
|
playing = true;
|
|
preview.setAnimationPaused(false);
|
|
find('[data-play]').textContent = '暂停装配';
|
|
find('[data-play]').setAttribute('aria-pressed', 'true');
|
|
const advance = () => {
|
|
if (!playing || controller.signal.aborted) return;
|
|
if (document.hidden) { stopPlayback(); return; }
|
|
// Wait for the visible transition, including on slower devices, before advancing.
|
|
if (!preview.transitions.length) {
|
|
if (stepIndex === config.steps.length - 1) { stopPlayback(); return; }
|
|
showStep(stepIndex + 1);
|
|
}
|
|
playTimer = setTimeout(advance, 700);
|
|
};
|
|
playTimer = setTimeout(advance, 700);
|
|
});
|
|
const visibility = () => { if (document.hidden) stopPlayback(); };
|
|
document.addEventListener('visibilitychange', visibility);
|
|
const setEnabled = (enabled) => {
|
|
dialog.querySelectorAll('[data-reset], [data-rotate], [data-explode]').forEach((b) => { b.disabled = !enabled; });
|
|
};
|
|
|
|
dialog.addEventListener('close', () => {
|
|
stopPlayback();
|
|
document.removeEventListener('visibilitychange', visibility);
|
|
controller.abort();
|
|
preview?.dispose();
|
|
document.body.style.overflow = previousOverflow;
|
|
dialog.remove();
|
|
activeDialog = null;
|
|
previousFocus?.focus({ preventScroll: true });
|
|
}, { once: true });
|
|
find('[data-close]').addEventListener('click', () => dialog.close());
|
|
// 只有整个手势都在弹窗外才关闭,避免旋转模型时拖出窗口误关。
|
|
let outside = false;
|
|
dialog.addEventListener('pointerdown', (e) => { outside = e.target === dialog; });
|
|
dialog.addEventListener('click', (e) => { if (outside && e.target === dialog) dialog.close(); });
|
|
|
|
find('[data-reset]').addEventListener('click', () => preview?.resetView());
|
|
find('[data-rotate]').addEventListener('click', (e) => {
|
|
const button = e.currentTarget;
|
|
const enabled = button.getAttribute('aria-pressed') !== 'true';
|
|
preview?.setAutoRotate(enabled);
|
|
button.setAttribute('aria-pressed', String(enabled));
|
|
});
|
|
|
|
async function load() {
|
|
if (loading || controller.signal.aborted) return;
|
|
loading = true;
|
|
const overlay = find('.assembly-loading');
|
|
overlay.hidden = false;
|
|
overlay.classList.remove('is-error');
|
|
find('[data-retry]').hidden = true;
|
|
find('[data-progress]').textContent = '准备模型与材质…';
|
|
overlay.querySelector('strong').textContent = '正在加载装配模型';
|
|
setEnabled(false);
|
|
try {
|
|
find('[data-progress]').textContent = '读取装配清单…';
|
|
const [{ AssemblyViewer }, pkg] = await Promise.all([import('./assembly-viewer.js'), loadProjectAssembly(project, controller.signal)]);
|
|
config = pkg;
|
|
if (controller.signal.aborted) return;
|
|
preview = new AssemblyViewer(find('.assembly-scene'));
|
|
preview.onMotionChange = (motion) => {
|
|
activeScrew = motion?.partId;
|
|
find('[data-focus]').hidden = !motion;
|
|
if (motion) {
|
|
const phase = { approach: '螺丝就位', engage: '插入扳手', tighten: '拧紧螺丝', retract: '退出扳手' }[motion.phase];
|
|
find('[data-motion-label]').textContent = `${phase} · ${motion.index} / ${motion.count}`;
|
|
}
|
|
};
|
|
await preview.load(config, controller.signal, (percent) => {
|
|
find('[data-progress]').textContent = percent < 100 ? `读取模型 ${percent}%` : '正在还原材质…';
|
|
});
|
|
if (controller.signal.aborted) return;
|
|
preview.onAnimationEnd = () => { if (!playing) stopPlayback(); };
|
|
overlay.hidden = true;
|
|
setEnabled(true);
|
|
find('[data-steps]').hidden = config.steps.length === 0;
|
|
find('[data-explode]').hidden = config.parts.length < 2;
|
|
find('[data-seek]').max = String(Math.max(0, config.steps.length - 1));
|
|
if (Number.isInteger(initialStep) && initialStep >= 0 && initialStep < config.steps.length) {
|
|
showStep(initialStep, false);
|
|
if (focusPartId) preview.focusPart(focusPartId);
|
|
showStep(initialStep);
|
|
}
|
|
} catch (err) {
|
|
preview?.dispose();
|
|
preview = null;
|
|
if (controller.signal.aborted) return;
|
|
console.error('装配模型加载失败', err);
|
|
overlay.classList.add('is-error');
|
|
overlay.querySelector('strong').textContent = '模型暂时无法加载';
|
|
find('[data-progress]').textContent = err.message || '请重试,或关闭后重新打开。';
|
|
find('[data-retry]').hidden = false;
|
|
} finally {
|
|
loading = false;
|
|
}
|
|
}
|
|
find('[data-retry]').addEventListener('click', load);
|
|
load();
|
|
return dialog;
|
|
}
|