feat(web): draggable floating panels and a non-hanging tutorial demo open
- add a shared useDraggablePanel hook (pointer capture, clamping, offset persistence in localStorage, double-click / button / menu reset) and use it for the tutorial step panel, the quick toolbar, the adjust-last-operation panel and the robot tool drawer - quick toolbar drags out of the dock into a compact floating bar and can be folded; robot tool scrim is transparent and click-through so the 3D scene stays visible and usable while the tool is open - 窗口 > 恢复默认面板位置 clears every stored panel offset - fix a pre-existing hang: tutorial demo scene opens went through the "save current project?" prompt and waited forever; course demos now persist the current OPFS project like the dialog's 保存后继续 and continue - PBR tutorial spec: demos must not auto-download .blend (OPFS-only rule) Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import type { TutorialCourse } from "./course";
|
||||
import { sceneHierarchyTree } from "./scene-tree";
|
||||
import { TutorialTreeView } from "./TutorialTreeView";
|
||||
import { useTutorialProgress } from "./parts/usePartsTutorialProgress";
|
||||
import { useDraggablePanel } from "../app/use-draggable-panel";
|
||||
import type { StepCheck, TutorialAction, TutorialApi, TutorialContext, TutorialStep } from "./parts/types";
|
||||
|
||||
function CheckRow({ item }: { item: StepCheck }) {
|
||||
@@ -20,8 +21,6 @@ async function delay(ms: number): Promise<void> {
|
||||
/* A React remount must not re-open the last checkpoint over newer unsaved
|
||||
* edits in the same page session; a real reload resets this module state. */
|
||||
const preparedCourseIds = new Set<string>();
|
||||
/** Where the learner last dragged the panel, as a pixel offset from its CSS home. */
|
||||
const PANEL_OFFSET_KEY = "web-blender.tutorial-panel-offset.v1";
|
||||
|
||||
export function TutorialOverlay({ course, api, context }: { course: TutorialCourse; api: TutorialApi; context: TutorialContext }) {
|
||||
const { progress, hydrated, setCurrentStep, completeStep, recordProject, recordSavedProject, reset, completedRequired } = useTutorialProgress(course);
|
||||
@@ -35,18 +34,8 @@ export function TutorialOverlay({ course, api, context }: { course: TutorialCour
|
||||
const checkpointInFlightRef = useRef(false);
|
||||
/* The panel floats over the real viewport, so the learner must be able to move
|
||||
* it off whatever the current step is demonstrating. Position is page-level
|
||||
* (not per-course): it is a comfort setting, keyed by nothing but the app. */
|
||||
const [offset, setOffset] = useState<{ x: number; y: number }>(() => {
|
||||
try {
|
||||
const raw = JSON.parse(window.localStorage.getItem(PANEL_OFFSET_KEY) ?? "null") as { x?: unknown; y?: unknown } | null;
|
||||
return typeof raw?.x === "number" && typeof raw?.y === "number" && Number.isFinite(raw.x) && Number.isFinite(raw.y)
|
||||
? { x: raw.x, y: raw.y }
|
||||
: { x: 0, y: 0 };
|
||||
}
|
||||
catch { return { x: 0, y: 0 }; }
|
||||
});
|
||||
const dragRef = useRef<{ pointerId: number; startX: number; startY: number; originX: number; originY: number } | null>(null);
|
||||
const panelRef = useRef<HTMLElement | null>(null);
|
||||
* (not per-course) and shared with the other floating panels. */
|
||||
const { offset, dragging, panelRef, handle, reset: resetPosition } = useDraggablePanel("tutorial");
|
||||
/* Everything drawn in this page is clipped to the browser viewport, so a DOM
|
||||
* panel can never sit outside the window. A separate Document PiP window can:
|
||||
* it is a real OS window the learner can park on a second screen while the
|
||||
@@ -149,11 +138,6 @@ export function TutorialOverlay({ course, api, context }: { course: TutorialCour
|
||||
setDemoRunning(false);
|
||||
}
|
||||
};
|
||||
const commitOffset = (next: { x: number; y: number }) => {
|
||||
setOffset(next);
|
||||
try { window.localStorage.setItem(PANEL_OFFSET_KEY, JSON.stringify(next)); }
|
||||
catch { /* A read-only profile keeps the drag for this page only. */ }
|
||||
};
|
||||
/* Pop the panel into its own OS window so it can live outside the browser.
|
||||
* Falls back to a plain popup window on browsers without Document PiP. */
|
||||
const openFloatingWindow = async () => {
|
||||
@@ -190,40 +174,6 @@ export function TutorialOverlay({ course, api, context }: { course: TutorialCour
|
||||
setDemoError(`无法打开独立窗口:${error instanceof Error ? error.message : String(error)}`);
|
||||
}
|
||||
};
|
||||
const onHeaderPointerDown = (event: ReactPointerEvent<HTMLElement>) => {
|
||||
/* Never start a drag from the collapse button or a text selection inside the title. */
|
||||
if ((event.target as HTMLElement).closest("button")) return;
|
||||
if (event.button !== 0) return;
|
||||
dragRef.current = { pointerId: event.pointerId, startX: event.clientX, startY: event.clientY, originX: offset.x, originY: offset.y };
|
||||
event.currentTarget.setPointerCapture(event.pointerId);
|
||||
};
|
||||
const onHeaderPointerMove = (event: ReactPointerEvent<HTMLElement>) => {
|
||||
const drag = dragRef.current;
|
||||
if (!drag || drag.pointerId !== event.pointerId) return;
|
||||
const panel = event.currentTarget.closest(".parts-tutorial-overlay");
|
||||
if (!(panel instanceof HTMLElement)) return;
|
||||
/* In a floating window the panel fills its own document, so a page-space
|
||||
* offset means nothing; the OS moves the window instead. */
|
||||
if (panel.dataset.tutorialFloating === "true") return;
|
||||
const rect = panel.getBoundingClientRect();
|
||||
/* Free drag, but the header must stay grabbable: it is the only handle, so
|
||||
* every clamp below is expressed in terms of "how much header is on
|
||||
* screen". A DOM panel is clipped at the window edge anyway — to actually
|
||||
* sit outside the browser, use the floating-window button. */
|
||||
const wantedLeft = rect.left + (drag.originX + event.clientX - drag.startX - offset.x);
|
||||
const wantedTop = rect.top + (drag.originY + event.clientY - drag.startY - offset.y);
|
||||
/* Never lose more than half the title, and always keep 160px of header width. */
|
||||
const left = Math.min(Math.max(wantedLeft, 8 - rect.width * 0.5), window.innerWidth - 160);
|
||||
/* Header is ~60px tall: keep at least 12px of it below the top edge and it
|
||||
* fully visible above the bottom edge. */
|
||||
const top = Math.min(Math.max(wantedTop, -48), window.innerHeight - 60);
|
||||
commitOffset({ x: Math.round(offset.x + left - rect.left), y: Math.round(offset.y + top - rect.top) });
|
||||
};
|
||||
const onHeaderPointerUp = (event: ReactPointerEvent<HTMLElement>) => {
|
||||
if (dragRef.current?.pointerId !== event.pointerId) return;
|
||||
dragRef.current = null;
|
||||
if (event.currentTarget.hasPointerCapture(event.pointerId)) event.currentTarget.releasePointerCapture(event.pointerId);
|
||||
};
|
||||
/* Closing the floating window must bring the panel back into the page; the
|
||||
* `pagehide` listener covers most closes, the poll covers the rest. */
|
||||
useEffect(() => {
|
||||
@@ -237,16 +187,6 @@ export function TutorialOverlay({ course, api, context }: { course: TutorialCour
|
||||
pipWindow.close();
|
||||
setPipWindow(null);
|
||||
}, [collapsed, pipWindow]);
|
||||
/* The drag is deliberately unbounded, so a saved offset is honoured as-is.
|
||||
* Only the very first mount sanity-checks that the header is reachable, in
|
||||
* case the panel was parked outside a window that has since been resized. */
|
||||
useEffect(() => {
|
||||
const header = panelRef.current?.querySelector(".tutorial-head");
|
||||
if (!(header instanceof HTMLElement)) return;
|
||||
const rect = header.getBoundingClientRect();
|
||||
if (rect.bottom > 0 && rect.top < window.innerHeight && rect.right > 0 && rect.left < window.innerWidth) return;
|
||||
commitOffset({ x: 0, y: 0 });
|
||||
}, []);
|
||||
if (collapsed) {
|
||||
return <button type="button" className="tutorial-reopen" aria-label={course.reopenAria} onClick={() => setCollapsed(false)}>
|
||||
<span aria-hidden="true">?</span><span>教程</span>
|
||||
@@ -254,18 +194,14 @@ export function TutorialOverlay({ course, api, context }: { course: TutorialCour
|
||||
</button>;
|
||||
}
|
||||
const isSummary = step.id === course.summaryStepId;
|
||||
const panel = <aside ref={panelRef} className={demoRunning ? "parts-tutorial-overlay is-demoing" : "parts-tutorial-overlay"} aria-label={course.overlayTitle} data-course={course.id} data-tutorial-step={step.id} data-tutorial-ready={ready ? "true" : "false"} data-tutorial-project={projectState} data-tutorial-floating={pipWindow ? "true" : "false"} style={{ transform: `translate(${offset.x}px, ${offset.y}px)` }}>
|
||||
<header className="tutorial-head"
|
||||
onPointerDown={onHeaderPointerDown}
|
||||
onPointerMove={onHeaderPointerMove}
|
||||
onPointerUp={onHeaderPointerUp}
|
||||
onPointerCancel={onHeaderPointerUp}
|
||||
onDoubleClick={() => commitOffset({ x: 0, y: 0 })}
|
||||
title="拖动标题栏可移动面板,双击复位">
|
||||
const panel = <aside ref={panelRef} className={dragging ? "parts-tutorial-overlay is-dragging" : demoRunning ? "parts-tutorial-overlay is-demoing" : "parts-tutorial-overlay"} aria-label={course.overlayTitle} data-draggable-panel="tutorial" data-draggable-disabled={pipWindow ? "true" : "false"} data-course={course.id} data-tutorial-step={step.id} data-tutorial-ready={ready ? "true" : "false"} data-tutorial-project={projectState} data-tutorial-floating={pipWindow ? "true" : "false"} data-panel-offset={`${offset.x},${offset.y}`} style={{ transform: `translate(${offset.x}px, ${offset.y}px)` }}>
|
||||
<header className="tutorial-head" {...handle} title="拖动标题栏可移动面板,双击复位">
|
||||
<span className="tutorial-drag-grip" aria-hidden="true">⠿</span>
|
||||
<div>
|
||||
<p className="tutorial-kicker">{isSummary ? "课程完成" : step.chapter}</p>
|
||||
<h2>{step.title}</h2>
|
||||
</div>
|
||||
<button type="button" className="tutorial-icon-button" aria-label="复位教程面板位置" title="复位面板位置" onClick={resetPosition} disabled={offset.x === 0 && offset.y === 0}>⌖</button>
|
||||
<button type="button" className="tutorial-icon-button" aria-label={pipWindow ? "将教程面板收回本页" : "在独立窗口打开教程面板"} title={pipWindow ? "收回本页" : "在独立窗口打开(可拖到浏览器之外、置于另一块屏幕)"} onClick={() => void openFloatingWindow()} data-testid="tutorial-float">{pipWindow ? "⤡" : "⧉"}</button>
|
||||
<button type="button" className="tutorial-icon-button" aria-label="收起教程侧栏" title="收起教程" onClick={() => setCollapsed(true)}>×</button>
|
||||
</header>
|
||||
|
||||
@@ -115,6 +115,11 @@
|
||||
.tutorial-head { display: flex; align-items: start; justify-content: space-between; gap: 12px; padding: 16px 18px 10px; cursor: grab; touch-action: none; user-select: none; }
|
||||
.tutorial-head:active { cursor: grabbing; }
|
||||
.tutorial-head .tutorial-icon-button { cursor: pointer; }
|
||||
/* Visible affordance that the title bar is the drag handle. */
|
||||
.tutorial-drag-grip { flex: none; margin-top: 2px; color: #7c848b; font-size: 15px; line-height: 1; letter-spacing: -1px; }
|
||||
.parts-tutorial-overlay.is-dragging { box-shadow: 0 24px 60px rgba(0, 0, 0, .62); }
|
||||
.parts-tutorial-overlay.is-dragging .tutorial-drag-grip { color: #f28c28; }
|
||||
.tutorial-head .tutorial-icon-button[disabled] { opacity: .35; cursor: default; }
|
||||
/* Inside a floating PiP window the panel is the whole document: drop the
|
||||
* viewport anchoring, let it fill the window, and keep the drag handle. */
|
||||
.parts-tutorial-overlay[data-tutorial-floating="true"] {
|
||||
|
||||
Reference in New Issue
Block a user