@@ -29,8 +29,8 @@ import { useEffect, useRef, useState } from 'react'
2929import type { MouseEvent , ReactNode } from 'react'
3030import type { BuildItResult , Evidence , Tour , WorldDoc } from '../../../../shared/world/types'
3131import { formatTokens } from '../../../../shared/world/tokens'
32- import { buildingCard , createWorldViewer , DEFAULT_LAYERS , edgeCard , hasWebGL , renderFallback , techCard , THEMES , usesTech } from '../../world/engine'
33- import type { LayerState , ThemeName , WorldDrawn , WorldViewer } from '../../world/engine'
32+ import { buildingCard , createWorldViewer , DEFAULT_LAYERS , edgeCard , hasWebGL , loadBookmarks , removeBookmark , renderFallback , saveBookmark , techCard , THEMES , usesTech } from '../../world/engine'
33+ import type { Bookmark , CameraState , LayerState , ThemeName , WorldDrawn , WorldViewer } from '../../world/engine'
3434import { exposesWorldProbe } from '../../lib/testAgents'
3535import { WorldToolbar } from './WorldToolbar'
3636import { WorldCard } from './WorldCard'
@@ -84,6 +84,7 @@ export interface WorldProbe {
8484 linePoints ( id : string ) : Array < { x : number ; y : number } >
8585 pickAt ( x : number , y : number ) : { kind : 'node' | 'edge' ; id : string } | null
8686 drawn ( ) : WorldDrawn
87+ cameraState ( ) : CameraState | null
8788}
8889
8990const messageOf = ( e : unknown ) : string => ( e instanceof Error ? e . message : String ( e ) )
@@ -93,11 +94,22 @@ const baseName = (p: string): string => p.replace(/[\\/]+$/, '').split(/[\\/]/).
9394const labelCostLine = ( tokens : number ) : string =>
9495 tokens > 0 ? `Describing parts: ${ formatTokens ( tokens ) } on the last update` : 'Describing parts: cached, no tokens'
9596
97+ /** True while the 2D map is chosen (kept between sessions). */
98+ const MAP2D_KEY = 'termpolis.world.map2d'
99+ const readMap2d = ( ) : boolean => {
100+ try { return localStorage . getItem ( MAP2D_KEY ) === '1' } catch { return false }
101+ }
102+ const hasPart = ( d : WorldDoc , id : string ) : boolean =>
103+ d . buildings . some ( ( b ) => b . id === id ) || d . externals . some ( ( x ) => x . id === id ) || d . edges . some ( ( e ) => e . id === id ) || d . districts . some ( ( x ) => x . id === id )
104+
96105const partCard = ( d : WorldDoc , id : string ) : string | null => buildingCard ( d , id ) || edgeCard ( d , id ) || null
97106
98107export function WorldView ( { root, onClose, askSlot, buildIt, width = DEFAULT_WIDTH } : WorldViewProps ) : JSX . Element {
99108 const { world, open, goHome, entries, refresh } = useOpenWorld ( root )
100109 const [ webgl ] = useState ( ( ) => hasWebGL ( ) )
110+ const [ map2d , setMap2d ] = useState ( readMap2d )
111+ /** The flat map shows: no WebGL, or the switch is on. */
112+ const flat = ! webgl || map2d
101113 const [ viewer , setViewer ] = useState < WorldViewer | null > ( null )
102114 const [ resolvedRoot , setResolvedRoot ] = useState < string | null > ( null )
103115 const [ attempt , setAttempt ] = useState ( 0 )
@@ -357,13 +369,13 @@ export function WorldView({ root, onClose, askSlot, buildIt, width = DEFAULT_WID
357369
358370 // Nothing to draw while the home or a loading screen covers the map: stop the render loop.
359371 const covered = world . kind === 'home' || ! doc
360- useEffect ( ( ) => { viewer ?. setPaused ( covered ) } , [ viewer , covered ] )
372+ useEffect ( ( ) => { viewer ?. setPaused ( covered || map2d ) } , [ viewer , covered , map2d ] )
361373
362374 // The e2e probe, while this viewer is up (and only under the test flag).
363375 useEffect ( ( ) => {
364376 if ( ! viewer || ! exposesWorldProbe ( ) ) return
365377 const w = window as unknown as { __termpolis_world ?: WorldProbe }
366- const probe : WorldProbe = { screenPoint : ( id ) => viewer . screenPoint ( id ) , linePoints : ( id ) => viewer . linePoints ( id ) , pickAt : ( x , y ) => viewer . pickAt ( x , y ) , drawn : ( ) => viewer . drawn ( ) }
378+ const probe : WorldProbe = { screenPoint : ( id ) => viewer . screenPoint ( id ) , linePoints : ( id ) => viewer . linePoints ( id ) , pickAt : ( x , y ) => viewer . pickAt ( x , y ) , drawn : ( ) => viewer . drawn ( ) , cameraState : ( ) => viewer . cameraState ( ) }
367379 w . __termpolis_world = probe
368380 return ( ) => { if ( w . __termpolis_world === probe ) delete w . __termpolis_world }
369381 } , [ viewer ] )
@@ -387,9 +399,41 @@ export function WorldView({ root, onClose, askSlot, buildIt, width = DEFAULT_WID
387399 // its part is gone).
388400 useEffect ( ( ) => {
389401 const id = fallbackIdRef . current
390- if ( viewer || ! doc || id === null ) return
402+ if ( ( viewer && ! map2d ) || ! doc || id === null ) return
391403 setCard ( ( c ) => ( c === null ? null : partCard ( doc , id ) ) )
392- } , [ viewer , doc ] )
404+ } , [ viewer , map2d , doc ] )
405+
406+ // Saved views of this world.
407+ const projectKey = doc ? doc . projectKey : null
408+ const [ bookmarks , setBookmarks ] = useState < Bookmark [ ] > ( [ ] )
409+ useEffect ( ( ) => { setBookmarks ( projectKey === null ? [ ] : loadBookmarks ( projectKey ) ) } , [ projectKey ] )
410+ const onSaveBookmark = ( name : string ) : void => {
411+ const v = viewer as WorldViewer // the save button is off without a viewer
412+ const cam = v . cameraState ( )
413+ if ( ! cam ) return
414+ setBookmarks ( saveBookmark ( ( docRef . current as WorldDoc ) . projectKey , { name, camera : cam , selection : v . getSelection ( ) } ) )
415+ }
416+ const onBookmark = ( b : Bookmark ) : void => {
417+ if ( ! viewer ) return
418+ const d = docRef . current as WorldDoc
419+ // A part the world no longer has is skipped.
420+ const ids = b . selection . filter ( ( id ) => hasPart ( d , id ) )
421+ armedRef . current = false
422+ // The card's own framing flight lands first, then the saved pose takes the camera from it.
423+ if ( ids . length > 0 ) void viewer . apply ( { type : 'openCard' , id : ids [ 0 ] } ) . then ( ( ) => viewer . setCamera ( b . camera , false ) )
424+ else {
425+ viewer . resetView ( )
426+ void viewer . setCamera ( b . camera , false )
427+ }
428+ }
429+ const onRemoveBookmark = ( name : string ) : void => setBookmarks ( removeBookmark ( ( docRef . current as WorldDoc ) . projectKey , name ) )
430+ const onMap2d = ( on : boolean ) : void => {
431+ try { localStorage . setItem ( MAP2D_KEY , on ? '1' : '0' ) } catch { /* not remembered */ }
432+ armedRef . current = false
433+ fallbackIdRef . current = null
434+ setCard ( null )
435+ setMap2d ( on )
436+ }
393437
394438 const onLayers = ( patch : Partial < LayerState > ) : void => {
395439 setLayersState ( ( l ) => ( { ...l , ...patch } ) )
@@ -401,7 +445,7 @@ export function WorldView({ root, onClose, askSlot, buildIt, width = DEFAULT_WID
401445 }
402446 const onTour = ( tour : Tour ) : void => {
403447 armedRef . current = false
404- if ( viewer ) void viewer . play ( [ { type : 'tour' , steps : tour . steps } ] )
448+ if ( viewer && ! map2d ) void viewer . play ( [ { type : 'tour' , steps : tour . steps } ] )
405449 else setCard ( null )
406450 }
407451 const resetView = ( ) : void => {
@@ -412,7 +456,7 @@ export function WorldView({ root, onClose, askSlot, buildIt, width = DEFAULT_WID
412456 /** Open a part's card: through the viewer (it flies there), or straight from the doc in 2D. */
413457 const openPart = ( id : string ) : void => {
414458 armedRef . current = false
415- if ( viewer ) { void viewer . apply ( { type : 'openCard' , id } ) ; return }
459+ if ( viewer && ! map2d ) { void viewer . apply ( { type : 'openCard' , id } ) ; return }
416460 const d = docRef . current as WorldDoc
417461 fallbackIdRef . current = id
418462 setCard ( partCard ( d , id ) )
@@ -421,7 +465,7 @@ export function WorldView({ root, onClose, askSlot, buildIt, width = DEFAULT_WID
421465 const openTech = ( techId : string ) : void => {
422466 armedRef . current = false
423467 const d = docRef . current as WorldDoc
424- if ( viewer ) {
468+ if ( viewer && ! map2d ) {
425469 void viewer . apply ( { type : 'openCard' , id : 'tech:' + techId } )
426470 void viewer . apply ( { type : 'highlight' , ids : usesTech ( d , techId ) } )
427471 return
@@ -547,6 +591,13 @@ export function WorldView({ root, onClose, askSlot, buildIt, width = DEFAULT_WID
547591 onClose = { onClose }
548592 switcher = { < WorldSwitcher entries = { entries } onOpen = { ( t ) => openWorld ( t ) } onHome = { goHome } /> }
549593 onBuildIt = { designKey !== null && buildIt && doc ? ( ) => setBuilding ( true ) : undefined }
594+ bookmarks = { bookmarks }
595+ onSaveBookmark = { onSaveBookmark }
596+ onBookmark = { onBookmark }
597+ onRemoveBookmark = { onRemoveBookmark }
598+ bookmarkSaveDisabled = { ! viewer || ! doc }
599+ map2d = { map2d }
600+ onMap2d = { onMap2d }
550601 labelCost = { labelling && doc && doc . labelTokens !== undefined ? labelCostLine ( doc . labelTokens ) : undefined }
551602 />
552603 < div ref = { stageRef } data-testid = "world-stage" style = { { position : 'relative' , flex : '1 1 auto' , minHeight : 0 , overflow : 'hidden' } } >
@@ -556,18 +607,18 @@ export function WorldView({ root, onClose, askSlot, buildIt, width = DEFAULT_WID
556607 data-testid = "world-canvas-host"
557608 // Its own stacking context beneath the panels: the label layer numbers each label's
558609 // z-index up from zero, which would otherwise draw names over the card and legend.
559- style = { { position : 'absolute' , inset : 0 , zIndex : 0 , isolation : 'isolate' , visibility : doc ? 'visible' : 'hidden' } }
610+ style = { { position : 'absolute' , inset : 0 , zIndex : 0 , isolation : 'isolate' , visibility : doc && ! map2d ? 'visible' : 'hidden' } }
560611 onPointerDown = { ( ) => { armedRef . current = false } }
561612 />
562613 ) }
563- { ! webgl && doc && (
614+ { flat && doc && (
564615 < div
565616 data-testid = "world-fallback"
566617 className = "overflow-auto p-6 text-sm [&_h2]:text-lg [&_h2]:font-semibold [&_h3]:mt-3 [&_h3]:font-semibold [&_li]:cursor-pointer [&_ul]:pl-4 [&_p]:text-[#9ca3af]"
567618 style = { { position : 'absolute' , inset : 0 } }
568619 onClick = { onFallbackClick }
569620 // renderFallback escapes every string from the doc.
570- dangerouslySetInnerHTML = { { __html : renderFallback ( doc ) } }
621+ dangerouslySetInnerHTML = { { __html : renderFallback ( doc , projectName ) } }
571622 />
572623 ) }
573624 { home && < WorldHome entries = { entries } projectCwd = { root } onOpen = { ( t ) => openWorld ( t ) } onChanged = { refresh } /> }
@@ -579,7 +630,7 @@ export function WorldView({ root, onClose, askSlot, buildIt, width = DEFAULT_WID
579630 onRetry = { ( ) => setAttempt ( ( n ) => n + 1 ) }
580631 />
581632 ) }
582- { webgl && doc && ! home && < WorldLegend doc = { doc } processes = { layers . processes } open = { legendOpen } onToggle = { ( ) => setLegendChoice ( ! legendOpen ) } /> }
633+ { webgl && ! map2d && doc && ! home && < WorldLegend doc = { doc } processes = { layers . processes } open = { legendOpen } onToggle = { ( ) => setLegendChoice ( ! legendOpen ) } /> }
583634 { confirmRelayout && (
584635 < div
585636 role = "alertdialog"
@@ -623,7 +674,7 @@ export function WorldView({ root, onClose, askSlot, buildIt, width = DEFAULT_WID
623674 ) }
624675 { askSlot && ! home && (
625676 < div ref = { askRef } style = { { position : 'absolute' , bottom : ASK_BOTTOM_PX , left : '50%' , transform : 'translateX(-50%)' , width : ASK_SLOT_WIDTH , zIndex : 3 } } data-testid = "world-ask-slot" >
626- { askSlot ( viewer , doc , worldKey , askLink ) }
677+ { askSlot ( map2d ? null : viewer , doc , worldKey , askLink ) }
627678 </ div >
628679 ) }
629680 </ div >
0 commit comments