feat(editor): navigation model for the four-tab shell
This commit is contained in:
1 parent
9ec232ac22
commit
95b1b7e461
2 files changed
+348
No files matched your search
@@ -0,0 +1,156 @@
|
||||
import type { ElementKey } from '@faunosteo/core-wasm/types'
|
||||
import type { EditorTabId } from './tabs'
|
||||
|
||||
export const ANNOTATION_SECTIONS = [
|
||||
'drawing',
|
||||
'views',
|
||||
'categories',
|
||||
'regions',
|
||||
] as const
|
||||
export type AnnotationSection = (typeof ANNOTATION_SECTIONS)[number]
|
||||
|
||||
/** One encoding: a target on Annotations always names its section, null elsewhere. */
|
||||
export type EditorTarget =
|
||||
| {
|
||||
place: 'tab'
|
||||
tab: EditorTabId
|
||||
section: AnnotationSection | null
|
||||
key: ElementKey | null
|
||||
}
|
||||
| { place: 'references'; key: ElementKey | null; citedBy: ElementKey | null }
|
||||
|
||||
/** « Aller à … »: the place to show and a serial so each request is consumed once. */
|
||||
export interface FocusRequest {
|
||||
target: EditorTarget
|
||||
serial: number
|
||||
}
|
||||
|
||||
export interface NavigationState {
|
||||
tab: EditorTabId
|
||||
section: AnnotationSection
|
||||
references: { open: boolean; citedBy: ElementKey | null }
|
||||
/** Places already shown, so a body mounts on first visit and stays mounted. */
|
||||
visitedTabs: readonly EditorTabId[]
|
||||
visitedSections: readonly AnnotationSection[]
|
||||
referencesMounted: boolean
|
||||
focus: FocusRequest | undefined
|
||||
/** Last serial issued: it survives `focus_consumed`, so a serial is never reused. */
|
||||
serial: number
|
||||
}
|
||||
|
||||
export type NavigationAction =
|
||||
| { type: 'select_tab'; tab: EditorTabId }
|
||||
| { type: 'open_section'; section: AnnotationSection }
|
||||
| { type: 'back_to_drawing' }
|
||||
| { type: 'open_references'; citedBy: ElementKey | null }
|
||||
| { type: 'close_references' }
|
||||
| { type: 'go_to'; target: EditorTarget }
|
||||
| { type: 'focus_consumed'; serial: number }
|
||||
|
||||
const TAB_OF_KIND = {
|
||||
sheet: 'general',
|
||||
zone: 'findings',
|
||||
finding: 'findings',
|
||||
} as const
|
||||
|
||||
const SECTION_OF_KIND = {
|
||||
annotation: 'drawing',
|
||||
view: 'views',
|
||||
category: 'categories',
|
||||
region: 'regions',
|
||||
} as const
|
||||
|
||||
/** The only place an element key meets a place. */
|
||||
export function targetOfKey(key: ElementKey): EditorTarget {
|
||||
switch (key.kind) {
|
||||
case 'sheet':
|
||||
case 'zone':
|
||||
case 'finding':
|
||||
return { place: 'tab', tab: TAB_OF_KIND[key.kind], section: null, key }
|
||||
case 'reference':
|
||||
return { place: 'references', key, citedBy: null }
|
||||
default:
|
||||
return {
|
||||
place: 'tab',
|
||||
tab: 'annotations',
|
||||
section: SECTION_OF_KIND[key.kind],
|
||||
key,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function withItem<T>(list: readonly T[], item: T): readonly T[] {
|
||||
return list.includes(item) ? list : [...list, item]
|
||||
}
|
||||
|
||||
/** Records the tab, and the section when Annotations is the tab shown. */
|
||||
function shown(state: NavigationState): NavigationState {
|
||||
const visitedTabs = withItem(state.visitedTabs, state.tab)
|
||||
const visitedSections =
|
||||
state.tab === 'annotations'
|
||||
? withItem(state.visitedSections, state.section)
|
||||
: state.visitedSections
|
||||
return { ...state, visitedTabs, visitedSections }
|
||||
}
|
||||
|
||||
export function initialNavigation(tab: EditorTabId): NavigationState {
|
||||
return shown({
|
||||
tab,
|
||||
section: 'drawing',
|
||||
references: { open: false, citedBy: null },
|
||||
visitedTabs: [],
|
||||
visitedSections: [],
|
||||
referencesMounted: false,
|
||||
focus: undefined,
|
||||
serial: 0,
|
||||
})
|
||||
}
|
||||
|
||||
function openReferences(state: NavigationState, citedBy: ElementKey | null) {
|
||||
return {
|
||||
...state,
|
||||
references: { open: true, citedBy },
|
||||
referencesMounted: true,
|
||||
}
|
||||
}
|
||||
|
||||
function goTo(state: NavigationState, target: EditorTarget): NavigationState {
|
||||
const serial = state.serial + 1
|
||||
const focus = { target, serial }
|
||||
if (target.place === 'references') {
|
||||
return { ...openReferences(state, target.citedBy), focus, serial }
|
||||
}
|
||||
const moved = {
|
||||
...state,
|
||||
tab: target.tab,
|
||||
section: target.section ?? state.section,
|
||||
references: { open: false, citedBy: null },
|
||||
focus,
|
||||
serial,
|
||||
}
|
||||
return shown(moved)
|
||||
}
|
||||
|
||||
export function navigationReducer(
|
||||
state: NavigationState,
|
||||
action: NavigationAction,
|
||||
): NavigationState {
|
||||
switch (action.type) {
|
||||
case 'select_tab':
|
||||
return shown({ ...state, tab: action.tab })
|
||||
case 'open_section':
|
||||
return shown({ ...state, section: action.section })
|
||||
case 'back_to_drawing':
|
||||
return shown({ ...state, section: 'drawing' })
|
||||
case 'open_references':
|
||||
return openReferences(state, action.citedBy)
|
||||
case 'close_references':
|
||||
return { ...state, references: { open: false, citedBy: null } }
|
||||
case 'go_to':
|
||||
return goTo(state, action.target)
|
||||
case 'focus_consumed':
|
||||
return state.focus?.serial === action.serial
|
||||
? { ...state, focus: undefined }
|
||||
: state
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user