feat(observatory): persist scene profiles and stable device view bindings
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
import {type DockTree,dockIds} from '@nodedc/ui-core';
|
||||
import type {ObservationLayout} from './observationLayout';
|
||||
|
||||
/** Transfer chosen catalog slots once; never overwrite an existing device layout. */
|
||||
export function bindObservationLayout(layout:ObservationLayout,bindings:readonly {from:string;to:string}[]):ObservationLayout{
|
||||
const known=new Set([...layout.order,...layout.hidden,...Object.keys(layout.layers),...(layout.tree?dockIds(layout.tree):[])]);
|
||||
const map=new Map(bindings.filter(({from,to})=>(known.has(from)||layout.catalogVisible?.includes(from))&&!known.has(to)).map(({from,to})=>[from,to]));
|
||||
if(!map.size)return layout;
|
||||
const key=(id:string)=>map.get(id)??id;
|
||||
const tree=(value:DockTree):DockTree=>'id'in value?{id:key(value.id)}:{...value,first:tree(value.first),second:tree(value.second)};
|
||||
const splits=Object.fromEntries(Object.entries(layout.splits).map(([name,ratio])=>{
|
||||
try{const ids:unknown=JSON.parse(name);if(Array.isArray(ids)&&ids.every(id=>typeof id==='string'))return [JSON.stringify(ids.map(key)),ratio];}catch{/* Preserve unrelated legacy split keys. */}
|
||||
return [name,ratio];
|
||||
}));
|
||||
return {...layout,tree:layout.tree?tree(layout.tree):undefined,order:layout.order.map(key),hidden:layout.hidden.map(key),
|
||||
layers:Object.fromEntries(Object.entries(layout.layers).map(([id,value])=>[key(id),value])),splits,
|
||||
catalogVisible:layout.catalogVisible?.filter(id=>!map.has(id))};
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import {readSceneSettings,type SceneSettings} from '../../sceneSettings';
|
||||
import {dockIds, type DockTree} from '@nodedc/ui-core';
|
||||
import {decodeObservationLayout, type ObservationLayout} from './observationLayout';
|
||||
|
||||
@@ -6,7 +7,7 @@ export const observationArrangements = [
|
||||
{value:'rows',label:'Друг под другом'},
|
||||
] as const;
|
||||
export interface ObservationPreset {id:string;name:string;layout:ObservationLayout}
|
||||
export interface ObservationPresets {version:1;selected:string;layout:ObservationLayout;presets:ObservationPreset[]}
|
||||
export interface ObservationPresets {version:1;selected:string;layout:ObservationLayout;presets:ObservationPreset[];scenes?:Record<string,SceneSettings>}
|
||||
const clone = (layout:unknown):ObservationLayout => JSON.parse(JSON.stringify(decodeObservationLayout(layout)));
|
||||
export function decodeObservationPresets(value:unknown, legacy?:unknown):ObservationPresets {
|
||||
const source = value && typeof value === 'object' ? value as Partial<ObservationPresets> : {};
|
||||
@@ -19,7 +20,10 @@ export function decodeObservationPresets(value:unknown, legacy?:unknown):Observa
|
||||
}
|
||||
const layout=decodeObservationLayout(source.version===1 ? source.layout : legacy);
|
||||
const selected=source.selected;
|
||||
return {version:1,presets,layout,selected:typeof selected==='string' &&
|
||||
const scenes:Record<string,SceneSettings>={};
|
||||
if(source.version===1 && source.scenes && typeof source.scenes==='object')
|
||||
for(const [key,value] of Object.entries(source.scenes).slice(0,128)){const profile=readSceneSettings(value);if(key.length<=512&&profile)scenes[key]=profile;}
|
||||
return {version:1,presets,layout,...(Object.keys(scenes).length?{scenes}:{}),selected:typeof selected==='string' &&
|
||||
(presets.some(p=>p.id===selected) || observationArrangements.some(p=>p.value===selected))
|
||||
? selected : layout.arrangement};
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type {SceneSettings} from '../../sceneSettings';
|
||||
import {useEffect,useRef,useState,type SetStateAction} from 'react';
|
||||
import type {CameraPresentation} from '../../../../../packages/sensor-ui/src/observation';
|
||||
import type {ObservationLayout} from './observationLayout';
|
||||
@@ -132,6 +133,7 @@ export function useVehicleObservation(vehicleID:string) {
|
||||
try{localStorage.removeItem(draftKey+':recovery');}catch{}
|
||||
};
|
||||
return {...document.layouts,recovery:!!recovery,restore,ready,pending,cameras:document.cameras,setLayout,
|
||||
setScene:(id:string,value:SceneSettings)=>changeLayouts(current=>({...current,scenes:{...current.scenes,[id]:value}})),
|
||||
setCamera:(id:string,value:CameraPresentation)=>update(current=>({...current,cameras:{...current.cameras,[id]:value}})),
|
||||
select:(id:string)=>changeLayouts(current=>selectObservationPreset(current,id)),
|
||||
save:(name:string)=>changeLayouts(current=>saveObservationPreset(current,name,`saved:${crypto.randomUUID()}`)),
|
||||
|
||||
@@ -1,6 +1 @@
|
||||
/** View-only bounds. Each full-range endpoint disables its own clipping plane. */
|
||||
export function heightSlice([lower, upper]: readonly [number, number], [min, max]: readonly [number, number]) {
|
||||
const tolerance = Math.max(1, max - min) * 1e-9;
|
||||
return { floor: lower <= min + tolerance ? null : lower,
|
||||
ceiling: upper >= max - tolerance ? null : upper };
|
||||
}
|
||||
export {heightSlice} from "../../../../packages/spatial-ui/src/heightSlice";
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
/** Reparenting with append/remove destroys iframe browsing contexts. */
|
||||
export function observationPortal(portal: HTMLElement, inline: HTMLElement, overlay: HTMLElement) {
|
||||
inline.append(portal);
|
||||
return {
|
||||
setExpanded(expanded: boolean) {
|
||||
const target = (expanded ? overlay : inline) as HTMLElement & {
|
||||
moveBefore?: (node: Node, before: Node | null) => void;
|
||||
};
|
||||
if (portal.parentElement === target) return;
|
||||
// Atomic moves preserve the running renderer, its canvas and history.
|
||||
// Older hosts stay inline; the expanded CSS escapes its containing panel.
|
||||
if (typeof target.moveBefore === 'function') target.moveBefore(portal, null);
|
||||
},
|
||||
dispose() { portal.remove(); },
|
||||
};
|
||||
}
|
||||
@@ -35,7 +35,9 @@ export function decodeSessionDisplay(value: unknown, sessionId: string): SceneSe
|
||||
|| ['showPoints','showTrajectory','showGrid','showLabels','showCameraFrustums'].some(key => typeof s[key as keyof SceneSettings] !== 'boolean')) {
|
||||
throw new Error('Некорректные настройки отображения');
|
||||
}
|
||||
return {...s, accumulationMaxSeconds: Math.max(s.accumulationSeconds, s.accumulationMaxSeconds!)};
|
||||
// Older recording profiles do not own the optional live-view navigation
|
||||
// fields. Restore their neutral defaults when reading that wire format.
|
||||
return {...defaultSceneSettings, ...s, accumulationMaxSeconds: Math.max(s.accumulationSeconds, s.accumulationMaxSeconds!)};
|
||||
}
|
||||
|
||||
export async function loadSessionDisplay(sessionId: string, signal?: AbortSignal): Promise<SceneSettings> {
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import {useEffect,useState} from 'react';
|
||||
import {defaultSceneSettings,type SceneSettings} from '../../sceneSettings';
|
||||
import {fetchObservationWorkspaceLayoutProfile} from './workspaceLayout';
|
||||
/** Initial profile only; a board's persisted pane settings always take precedence. */
|
||||
export function useSpatialDefaults(){
|
||||
const [settings,setSettings]=useState<SceneSettings|null>(null);
|
||||
useEffect(()=>{let active=true;
|
||||
void fetchObservationWorkspaceLayoutProfile().then(profile=>{if(active)setSettings({...defaultSceneSettings,...profile?.sceneSettings});}).catch(()=>{if(active)setSettings({...defaultSceneSettings});});
|
||||
return()=>{active=false;};
|
||||
},[]);
|
||||
return settings;
|
||||
}
|
||||
Reference in New Issue
Block a user