diff --git a/packages/viewer/src/canvas/DiagramCanvas.tsx b/packages/viewer/src/canvas/DiagramCanvas.tsx index 9b913d4..6fa3c08 100644 --- a/packages/viewer/src/canvas/DiagramCanvas.tsx +++ b/packages/viewer/src/canvas/DiagramCanvas.tsx @@ -6,6 +6,7 @@ import { useFlow } from '../motion/useFlow'; import { hasFlow } from '../motion/flowOf'; import { useSceneReveal } from '../motion/useSceneReveal'; import { useExplore } from '../explore/ExploreContext'; +import { routeOf } from '../explore/state'; import { useInScope } from '../explore/scope'; import { routeBetween } from '../explore/graph'; import { inMenu } from '../chrome/Toolbar'; @@ -45,15 +46,16 @@ function useCameraEffects(scene: Scene, camera: Camera, restored: boolean) { else camera.fit(); }, [state.view, draft, rectOf, camera]); // A new route frames everything on it. - const shownRoute = useRef(restored ? state.route : null); + const route = routeOf(state.mode); + const shownRoute = useRef(restored ? route : null); useEffect(() => { - if (shownRoute.current === state.route) return; - shownRoute.current = state.route; - if (!state.route) return; - const on = routeBetween(graph, state.route.from, state.route.to)?.nodes ?? new Set([state.route.from, state.route.to]); + if (shownRoute.current === route) return; + shownRoute.current = route; + if (!route) return; + const on = routeBetween(graph, route.from, route.to)?.nodes ?? new Set([route.from, route.to]); const box = union([...on].flatMap((id) => rectOf.get(id) ?? [])); if (box) camera.fitRect(box); - }, [state.route, graph, rectOf, camera]); + }, [route, graph, rectOf, camera]); // The reveal a live reload restores from the hash is already on screen. const restoredReveal = useRef(restored ? state.reveal : -1); const latest = useRef({ state, draft }); diff --git a/packages/viewer/src/chrome/ExportMenu.tsx b/packages/viewer/src/chrome/ExportMenu.tsx index d1061ca..24e15c1 100644 --- a/packages/viewer/src/chrome/ExportMenu.tsx +++ b/packages/viewer/src/chrome/ExportMenu.tsx @@ -8,7 +8,9 @@ import { useExplore } from '../explore/ExploreContext'; import { useScene, useSceneContent } from '../canvas/ViewportContext'; import { standalonePage } from '../standalone'; +/** idle → busy → done | error; opening the menu again returns to idle (the button is disabled while busy). */ type Status = { kind: 'idle' } | { kind: 'busy'; text?: string } | { kind: 'done'; text: string } | { kind: 'error'; text: string }; +const IDLE: Status = { kind: 'idle' }; export function ExportMenu() { const { draft, state, graph, emphasis } = useExplore(); @@ -17,12 +19,18 @@ export function ExportMenu() { // What this browser can write, checked once. const [formats] = useState(() => ({ jpeg: canEncode('image/jpeg'), webp: canEncode('image/webp'), video: videoType() })); const [open, setOpen] = useState(false); - const [status, setStatus] = useState({ kind: 'idle' }); + const [status, setStatus] = useState(IDLE); const root = useRef(null); const button = useRef(null); const menu = useRef(null); // Opened from the keyboard (ArrowDown, or Enter/Space: a click with detail 0) the menu appears at once. const byKey = useRef(false); + const toggle = (keyboard: boolean) => { + byKey.current = keyboard; + // The last export's message makes way for the menu, which would otherwise open on top of an error. + if (!open) setStatus(IDLE); + setOpen(!open); + }; // The menu is right-aligned under the button: grow it from the button's centre. useLayoutEffect(() => { if (!open || !menu.current || byKey.current) return; @@ -140,15 +148,11 @@ export function ExportMenu() { aria-haspopup="menu" aria-expanded={open} disabled={status.kind === 'busy'} - onClick={(e) => { - byKey.current = e.detail === 0; - setOpen((v) => !v); - }} + onClick={(e) => toggle(e.detail === 0)} onKeyDown={(e) => { if (e.key === 'ArrowDown' && !open) { e.preventDefault(); - byKey.current = true; - setOpen(true); + toggle(true); } }} className="sm-press flex h-9 items-center gap-2 rounded-full bg-primary px-4 text-[14px] font-medium text-primary-fg disabled:opacity-60 @max-md:px-2.5" diff --git a/packages/viewer/src/chrome/Inspector.tsx b/packages/viewer/src/chrome/Inspector.tsx index 12edb50..ac77915 100644 --- a/packages/viewer/src/chrome/Inspector.tsx +++ b/packages/viewer/src/chrome/Inspector.tsx @@ -4,6 +4,7 @@ import { countByType, TYPE_LABELS, type DiagramDraft, type DiagramEdge, type Dia import { focusCard } from '../canvas/SceneLayers'; import { routeBetween } from '../explore/graph'; import { useExplore } from '../explore/ExploreContext'; +import { isRouting, routeOf } from '../explore/state'; import { useViewerRoot } from '../explore/scope'; import { useInFlight } from '../motion/inFlight'; import { swapIn } from '../motion/motion'; @@ -265,22 +266,23 @@ function RouteDetail({ toggle }: { toggle: ReactNode }) { ); - if (!state.route) { + const { mode } = state; + if (mode.kind !== 'route') { return ( <> {header}

- {state.routing?.next === 'to' ? `From ${title(state.routing.start)}: pick where the route ends.` : 'Pick the node the route starts from.'} + {mode.kind === 'pickEnd' ? `From ${title(mode.from)}: pick where the route ends.` : 'Pick the node the route starts from.'}

); } - const route = routeBetween(graph, state.route.from, state.route.to); + const route = routeBetween(graph, mode.from, mode.to); return ( <> {header} - {title(state.route.from)} → {title(state.route.to)} + {title(mode.from)} → {title(mode.to)} {!route ? (

No directed connections lead from one to the other.

@@ -390,7 +392,8 @@ export function Inspector({ // Collapsed, a selection or route would change nothing visible beyond the canvas (a phone starts collapsed): // the toggle names what it would show, so a tapped card visibly has details waiting. const name = (id: string) => draft.nodes.find((n) => n.id === id)?.card.title ?? id; - const showing = state.route ? `${name(state.route.from)} → ${name(state.route.to)}` : selected?.card.title; + const route = routeOf(state.mode); + const showing = route ? `${name(route.from)} → ${name(route.to)}` : selected?.card.title; return (