fix(editor): repair dialog limits, pending drafts block apply, dialog wording
This commit is contained in:
1 parent
3776a33b79
commit
9c21fd3f86
8 files changed
+197
-52
No files matched your search
@@ -6,7 +6,7 @@ import type {
|
||||
PreviewState,
|
||||
SheetView,
|
||||
} from '@faunosteo/core-wasm/types'
|
||||
import type { Acceptance } from '@/catalogue/preview'
|
||||
import type { Acceptance, CataloguePreview } from '@/catalogue/preview'
|
||||
import { buildView } from '@/components/catalogue/CatalogueCanvas'
|
||||
import {
|
||||
describeOutcome,
|
||||
@@ -25,6 +25,7 @@ import {
|
||||
unmeasuredViews,
|
||||
type GeometryRefusal,
|
||||
} from './issues'
|
||||
import { elementLabel } from './sheetDocument'
|
||||
import { useEditorStore } from './state/editorStore'
|
||||
import type { CommitOutcome } from './state/editorTypes'
|
||||
|
||||
@@ -108,9 +109,11 @@ export function blockedIds(geometries: Record<string, Geometry>): string[] {
|
||||
.map(([id]) => id)
|
||||
}
|
||||
|
||||
/** The frame of `viewId` as the CANDIDATE document composes it (read inside `accept`). */
|
||||
function candidateFrame(viewId: string): Frame | null {
|
||||
const preview = useEditorStore.getState().preview
|
||||
/** The frame of `viewId` as the CANDIDATE document composes it: `preview` must hold it. */
|
||||
function candidateFrame(
|
||||
preview: CataloguePreview | null,
|
||||
viewId: string,
|
||||
): Frame | null {
|
||||
const built = preview ? buildView(preview, viewId) : null
|
||||
return built?.ok ? parseFrame(built.svg.getAttribute('viewBox')) : null
|
||||
}
|
||||
@@ -119,6 +122,7 @@ function framesOfListed(
|
||||
before: PreviewState,
|
||||
after: PreviewState,
|
||||
ids: readonly string[],
|
||||
preview: CataloguePreview | null,
|
||||
): Record<string, Frame> | null {
|
||||
const viewIds = after.document.annotations
|
||||
.filter((annotation) => ids.includes(annotation.id))
|
||||
@@ -126,7 +130,9 @@ function framesOfListed(
|
||||
const unmeasured = unmeasuredViews(before)
|
||||
const frames: Record<string, Frame> = {}
|
||||
for (const viewId of new Set(viewIds)) {
|
||||
const frame = unmeasured.has(viewId) ? candidateFrame(viewId) : null
|
||||
const frame = unmeasured.has(viewId)
|
||||
? candidateFrame(preview, viewId)
|
||||
: null
|
||||
if (!frame) return null
|
||||
frames[viewId] = frame
|
||||
}
|
||||
@@ -147,7 +153,9 @@ export function markRepairable(
|
||||
return verdict
|
||||
}
|
||||
const { refusal } = verdict
|
||||
const frames = framesOfListed(before, after, refusal.annotationIds)
|
||||
// Inside `accept` the store's preview handle holds the candidate document (not yet kept).
|
||||
const { preview } = useEditorStore.getState()
|
||||
const frames = framesOfListed(before, after, refusal.annotationIds, preview)
|
||||
return frames
|
||||
? { accepted: false, refusal: { ...refusal, repairFrames: frames } }
|
||||
: verdict
|
||||
@@ -176,9 +184,9 @@ function updateAnnotation(
|
||||
}
|
||||
|
||||
/**
|
||||
* The geometries first, the view last: the core then turns the redrawn view's annotations
|
||||
* `to_confirm` itself, after the page's copies of them were written. Only the end text is
|
||||
* judged, so the order of the intermediate texts is free.
|
||||
* The geometries first, the view last. The order is free: the core's `update_annotation`
|
||||
* keeps the current status and sets `to_confirm` on any change, so both orders end in the
|
||||
* same text, and only that end text is judged.
|
||||
*/
|
||||
export function repairCommands(
|
||||
view: SheetView,
|
||||
@@ -220,22 +228,45 @@ function blockedResult(context: CommitContext): RepairResult {
|
||||
return { ok: false, general, byAnnotation: {} }
|
||||
}
|
||||
|
||||
function refusedResult(
|
||||
const UNMEASURED: ReadonlySet<string> = new Set([
|
||||
'view_unmeasured',
|
||||
'changed_unmeasured',
|
||||
])
|
||||
|
||||
function namesOf(ids: readonly string[], { locale }: CommitContext): string {
|
||||
const doc = useEditorStore.getState().previewState?.document
|
||||
const label = (id: string) =>
|
||||
doc ? elementLabel(doc, { kind: 'annotation', id }, locale) : id
|
||||
return [...new Set(ids)].map(label).join(', ')
|
||||
}
|
||||
|
||||
/** Words that fit the dialog: none of them sends the user to a tab behind it. */
|
||||
function dialogMessage(
|
||||
outcome: Exclude<CommitOutcome<GeometryRefusal>, { ok: true }>,
|
||||
context: CommitContext,
|
||||
): string | null {
|
||||
const refusal = outcome.reason === 'refused' ? outcome.refusal : null
|
||||
if (refusal?.reason === 'newly_invalid') {
|
||||
return context
|
||||
.t('editor.views.repair.more_outside')
|
||||
.replace('{names}', namesOf(refusal.annotationIds, context))
|
||||
}
|
||||
if (refusal && UNMEASURED.has(refusal.reason)) {
|
||||
return context.t('editor.views.repair.not_measurable')
|
||||
}
|
||||
return describeOutcome(outcome, context).messages[0] ?? null
|
||||
}
|
||||
|
||||
export function refusedResult(
|
||||
outcome: Exclude<CommitOutcome<GeometryRefusal>, { ok: true }>,
|
||||
context: CommitContext,
|
||||
): RepairResult {
|
||||
if (
|
||||
outcome.reason === 'refused' &&
|
||||
outcome.refusal.reason === 'changed_invalid'
|
||||
) {
|
||||
return {
|
||||
ok: false,
|
||||
general: null,
|
||||
byAnnotation: issueMessages(outcome.refusal),
|
||||
}
|
||||
const refusal = outcome.reason === 'refused' ? outcome.refusal : null
|
||||
if (refusal?.reason === 'changed_invalid') {
|
||||
return { ok: false, general: null, byAnnotation: issueMessages(refusal) }
|
||||
}
|
||||
const [general] = describeOutcome(outcome, context).messages
|
||||
return { ok: false, general: general ?? null, byAnnotation: {} }
|
||||
const general = dialogMessage(outcome, context)
|
||||
return { ok: false, general, byAnnotation: {} }
|
||||
}
|
||||
|
||||
/** The end-state verdict: a refused drawing first, then the geometry of the repaired ids. */
|
||||
@@ -245,6 +276,26 @@ function repairAccept(before: PreviewState, assetId: string, ids: string[]) {
|
||||
rejectedDrawingFault(after, assetId) ?? geometry(after)
|
||||
}
|
||||
|
||||
/** The commands and the repaired ids, or null when the view is gone. */
|
||||
function repairBatch(
|
||||
offer: RepairOffer,
|
||||
before: PreviewState,
|
||||
geometries: Record<string, Geometry>,
|
||||
) {
|
||||
const view = before.document.views.find((v) => v.id === offer.viewId)
|
||||
if (!view) return null
|
||||
const repairs = listedAnnotations(offer, before.document).map(
|
||||
(annotation) => ({
|
||||
annotation,
|
||||
geometry: geometries[annotation.id] ?? (annotation.geometry as Geometry),
|
||||
}),
|
||||
)
|
||||
return {
|
||||
commands: repairCommands(view, offer.assetId, repairs),
|
||||
ids: repairs.map((r) => r.annotation.id),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ONE batch: the listed annotations take the typed geometries and the view takes the
|
||||
* drawing, judged by the core on the end state (`geometryAccept` on the repaired ids).
|
||||
@@ -257,19 +308,10 @@ export function applyRepair(
|
||||
): RepairResult {
|
||||
const store = useEditorStore.getState()
|
||||
const before = store.previewState
|
||||
const view = before?.document.views.find((v) => v.id === offer.viewId)
|
||||
if (!before || !view || store.session !== offer.session) {
|
||||
return blockedResult(context)
|
||||
}
|
||||
const repairs = listedAnnotations(offer, before.document).map(
|
||||
(annotation) => ({
|
||||
annotation,
|
||||
geometry: geometries[annotation.id] ?? (annotation.geometry as Geometry),
|
||||
}),
|
||||
)
|
||||
const ids = repairs.map((r) => r.annotation.id)
|
||||
const accept = repairAccept(before, offer.assetId, ids)
|
||||
const commands = repairCommands(view, offer.assetId, repairs)
|
||||
const outcome = store.commitBatch(commands, { accept })
|
||||
const live = before !== null && store.session === offer.session
|
||||
const batch = live ? repairBatch(offer, before, geometries) : null
|
||||
if (!before || !batch) return blockedResult(context)
|
||||
const accept = repairAccept(before, offer.assetId, batch.ids)
|
||||
const outcome = store.commitBatch(batch.commands, { accept })
|
||||
return outcome.ok ? { ok: true } : refusedResult(outcome, context)
|
||||
}
|
||||
Reference in new issue
Block a user