Files
faunosteo/apps/app/src/editor/viewUpdate.ts
T

54 lines
1.7 KiB
TypeScript

import type {
DetailView,
PreviewState,
SheetView,
} from '@faunosteo/core-wasm/types'
import {
geometryAccept,
rejectedDrawingFault,
type GeometryRefusal,
} from './issues'
import { markRepairable } from './repair'
import { useEditorStore } from './state/editorStore'
import type { CommitOutcome } from './state/editorTypes'
export type ViewPatch = Partial<
Pick<DetailView, 'name' | 'svg_asset_id' | 'view_box' | 'parent_view_id'>
>
/**
* One `update` of a view through the store. A frame change (drawing, crop, parent) is
* `guarded`: the core's geometry verdict decides through `geometryAccept`, so no annotation
* gets a geometry error it did not have and no measured view becomes unmeasured. A new
* drawing refused for annotations of a view that was unmeasured comes back repairable.
*/
export function updateView(
viewId: string,
patch: ViewPatch,
guarded: boolean,
): CommitOutcome<GeometryRefusal> {
const store = useEditorStore.getState()
const before = store.previewState
const view = before?.document.views.find(
(candidate) => candidate.id === viewId,
)
if (!before || !view)
return { ok: false, reason: 'blocked', kind: 'not_ready' }
const value = { ...view, ...patch } as SheetView
const geometry = geometryAccept({ before, changedIds: [] })
const newAsset = patch.svg_asset_id
const accept = (after: PreviewState) => {
const rejected = newAsset ? rejectedDrawingFault(after, newAsset) : null
const verdict = rejected ?? geometry(after)
return newAsset ? markRepairable(verdict, before, after) : verdict
}
return store.commit(
{
op: 'update',
key: { kind: 'view', id: viewId },
element: { kind: 'view', value },
},
guarded ? { accept } : undefined,
)
}