fix(app): closed hash and canonical-form drawing messages, split crop and drawing views

This commit is contained in:
bchanot committed 2026-11-04 13:34:02 +01:00
1 parent 7bf1404946
commit 1bbf97e65b
16 files changed
+487 -248

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
/** The reasons the reader gives for a refused file, each with a `editor.upload.reason.*`. */
const READER_REASONS: ReadonlySet<string> = new Set([
'malformed',
'doctype',
'not_svg',
'too_many_elements',
'too_deep',
'too_many_uses',
'too_complex',
'reference_cycle',
'dangling_reference',
'reserved_id',
'root_visibility',
'processing_instruction',
'removal',
])
/**
* A drawing the core refuses (`detail` of a `rejected` state, or a cleaning reason) as a
* closed key: `not_canonical` (valid but not in the stored form), a reader reason, `removal`
* (the file would lose elements) or `other`. The core's text itself is never shown.
*/
export function rejectionReason(detail: string | null): string {
if (detail === 'not_canonical') return detail
if (detail?.startsWith('removed')) return 'removal'
return detail && READER_REASONS.has(detail) ? detail : 'other'
}
+24 -7
View File
@@ -1,12 +1,14 @@
import type { PreviewState, SheetView } from '@faunosteo/core-wasm/types'
import type { CatalogueFailure } from '@/catalogue/failure'
import { rejectionReason } from './drawingReason'
import { loadFailureMessage } from './messages'
/** The state of the drawing a view names itself (not the one it inherits). */
export type DrawingStatus =
| { kind: 'none' }
| { kind: 'ok' }
| { kind: 'missing' | 'rejected'; detail: string | null }
| { kind: 'missing'; detail: null }
| { kind: 'rejected'; detail: string | null }
| { kind: 'failed'; failure: CatalogueFailure }
export function drawingStatusOf(
@@ -18,7 +20,9 @@ export function drawingStatusOf(
if (assetId === null) return { kind: 'none' }
const held = state.drawings.find((drawing) => drawing.asset_id === assetId)
if (held?.state === 'ok') return { kind: 'ok' }
if (held) return { kind: held.state, detail: held.detail }
if (held?.state === 'rejected')
return { kind: 'rejected', detail: held.detail }
// A drawing the handle refused at load is `missing` for the core: the failure says why.
const failure = failures[assetId]
return failure
? { kind: 'failed', failure }
@@ -28,9 +32,22 @@ export function drawingStatusOf(
type Translate = (key: string) => string
export function drawingStatusText(t: Translate, status: DrawingStatus): string {
if (status.kind === 'failed') return loadFailureMessage(t, status.failure)
const label = t(`editor.views.drawing.${status.kind}`)
if (status.kind === 'ok' || status.kind === 'none' || !status.detail)
return label
return `${label} ${t('editor.views.drawing.detail').replace('{detail}', status.detail)}`
if (status.kind === 'failed') return failedText(t, status.failure)
if (status.kind === 'rejected') return rejectedText(t, status.detail)
return t(`editor.views.drawing.${status.kind}`)
}
function failedText(t: Translate, failure: CatalogueFailure): string {
return failure === 'drawing_hash_mismatch'
? t('editor.views.drawing.hash_mismatch')
: loadFailureMessage(t, failure)
}
function rejectedText(t: Translate, detail: string | null): string {
const reason = rejectionReason(detail)
if (reason === 'not_canonical') return t('editor.views.drawing.not_canonical')
return t('editor.views.drawing.rejected_reason').replace(
'{reason}',
t(`editor.upload.reason.${reason}`),
)
}
+18 -4
View File
@@ -13,6 +13,7 @@ import { fetchDrawing } from '@/catalogue/catalogue'
import type { CatalogueFailure } from '@/catalogue/failure'
import { getSupabaseClient } from '@/lib/supabase'
import { cleanSvg } from './core/cleaning'
import { rejectionReason } from './drawingReason'
import type { GeometryRefusal } from './issues'
import { useEditorStore } from './state/editorStore'
import { updateView } from './viewUpdate'
@@ -22,7 +23,13 @@ export const MAX_DRAWING_BYTES = 2 * 1024 * 1024
/** Everything that can stop an upload, as closed codes (never text from a server). */
export type UploadError =
| { code: 'too_large' | 'svg_active_content' | 'object_conflict' }
| {
code:
| 'too_large'
| 'svg_active_content'
| 'object_conflict'
| 'svg_not_canonical'
}
| { code: 'caps' | 'core' | 'blocked' | 'stale' }
| { code: 'svg_rejected'; reason: string }
| { code: 'sheet'; kind: SheetFailureKind }
@@ -154,13 +161,20 @@ export async function downloadAndHold(
return fail({ code: held.failure.kind === 'caps' ? 'caps' : 'core' })
}
/** A drawing the core holds as refused is a closed upload code, not a frame message. */
function refusedAttach(refusal: GeometryRefusal): UploadError {
if (refusal.reason !== 'drawing_rejected') return { code: 'frame', refusal }
const reason = rejectionReason(refusal.drawingDetail ?? null)
return reason === 'not_canonical'
? { code: 'svg_not_canonical' }
: { code: 'svg_rejected', reason }
}
/** The last step: the view takes the drawing, under the geometry check of a frame change. */
export function attachToView(viewId: string, assetId: string): Step<null> {
const outcome = updateView(viewId, { svg_asset_id: assetId }, true)
if (outcome.ok) return done(null)
if (outcome.reason === 'refused') {
return fail({ code: 'frame', refusal: outcome.refusal })
}
if (outcome.reason === 'refused') return fail(refusedAttach(outcome.refusal))
return fail({ code: outcome.reason === 'blocked' ? 'blocked' : 'core' })
}
+3
View File
@@ -19,6 +19,9 @@ export function frameRefusalMessage(
ids
.map((id) => (doc ? elementLabel(doc, { kind, id }, locale) : id))
.join(', ')
if (refusal.reason === 'drawing_rejected') {
return t('editor.upload.err.svg_not_canonical')
}
if (refusal.reason === 'view_unmeasured') {
return t('editor.views.frame.view_unmeasured').replace(
'{names}',
+35 -4
View File
@@ -28,20 +28,28 @@ export function geometryErrorsById(
const index = GEOMETRY_PATH.exec(issue.path)?.[1]
const annotation = state.document.annotations[Number(index)]
if (index === undefined || !annotation) continue
const entry = { annotationId: annotation.id, code: issue.code, message: issue.message }
const entry = {
annotationId: annotation.id,
code: issue.code,
message: issue.message,
}
byId.set(annotation.id, [...(byId.get(annotation.id) ?? []), entry])
}
return byId
}
/** The asset a view draws: its own, else the first up its parent chain (cycle-safe). */
function drawingAssetOf(views: readonly SheetView[], viewId: string): string | null {
function drawingAssetOf(
views: readonly SheetView[],
viewId: string,
): string | null {
const seen = new Set<string>()
let view = views.find((candidate) => candidate.id === viewId)
while (view && !seen.has(view.id)) {
if (view.svg_asset_id) return view.svg_asset_id
seen.add(view.id)
const parent: string | null = view.kind === 'detail' ? view.parent_view_id : null
const parent: string | null =
view.kind === 'detail' ? view.parent_view_id : null
view = parent === null ? undefined : views.find((v) => v.id === parent)
}
return null
@@ -75,6 +83,7 @@ export type GeometryRefusalReason =
| 'changed_unmeasured'
| 'newly_invalid'
| 'view_unmeasured'
| 'drawing_rejected'
/** What `geometryAccept` answers on a refusal: the annotations, issues and views at fault. */
export interface GeometryRefusal {
@@ -82,6 +91,8 @@ export interface GeometryRefusal {
annotationIds: string[]
issues: GeometryIssue[]
viewIds: string[]
/** The core's detail for a `drawing_rejected` refusal (mapped, never shown). */
drawingDetail?: string
}
export interface GeometryAcceptInput {
@@ -129,7 +140,8 @@ function frameFault(
const newlyInvalid = [...geometryErrorsById(after)]
.filter(([id]) => !errorsBefore.has(id))
.flatMap(([, issues]) => issues)
if (newlyInvalid.length > 0) return refusal('newly_invalid', { issues: newlyInvalid })
if (newlyInvalid.length > 0)
return refusal('newly_invalid', { issues: newlyInvalid })
const unmeasuredBefore = unmeasuredViews(before)
const knownBefore = new Set(before.document.views.map((v) => v.id))
const lost = [...unmeasuredViews(after)].filter(
@@ -155,3 +167,22 @@ export function geometryAccept(
changedFault(after, input.changedIds) ??
frameFault(input.before, after) ?? { accepted: true }
}
/** A new drawing the core holds as `rejected` is refused before any geometry is judged. */
export function rejectedDrawingFault(
after: PreviewState,
assetId: string,
): Acceptance<GeometryRefusal> | null {
const held = after.drawings.find((drawing) => drawing.asset_id === assetId)
if (held?.state !== 'rejected') return null
return {
accepted: false,
refusal: {
reason: 'drawing_rejected',
annotationIds: [],
issues: [],
viewIds: [],
drawingDetail: held.detail ?? undefined,
},
}
}
+2 -16
View File
@@ -2,26 +2,12 @@ import type { AnatomicalSheetDocument } from '@faunosteo/core-wasm/types'
import { failureMessage } from '@/admin/labels'
import type { Locale } from '@/i18n/types'
import { frameRefusalMessage } from './frameRefusal'
import { rejectionReason } from './drawingReason'
import { loadFailureMessage } from './messages'
import type { UploadError } from './drawingUpload'
type Translate = (key: string) => string
/** The reasons the reader gives for a refused file, each with a `editor.upload.reason.*`. */
const READER_REASONS: ReadonlySet<string> = new Set([
'malformed',
'doctype',
'not_svg',
'too_many_elements',
'too_deep',
'too_many_uses',
'too_complex',
'reference_cycle',
'dangling_reference',
'reserved_id',
'root_visibility',
])
/** Server kinds with their own upload wording; the others go through `failureMessage`. */
const UPLOAD_KINDS: ReadonlySet<string> = new Set([
'size_mismatch',
@@ -39,7 +25,7 @@ export function uploadErrorMessage(
case 'stale':
return null
case 'svg_rejected': {
const reason = READER_REASONS.has(error.reason) ? error.reason : 'other'
const reason = rejectionReason(error.reason)
return t('editor.upload.err.svg_rejected').replace(
'{reason}',
t(`editor.upload.reason.${reason}`),
+15 -5
View File
@@ -1,5 +1,13 @@
import type { DetailView, SheetView } from '@faunosteo/core-wasm/types'
import { geometryAccept, type GeometryRefusal } from './issues'
import type {
DetailView,
PreviewState,
SheetView,
} from '@faunosteo/core-wasm/types'
import {
geometryAccept,
rejectedDrawingFault,
type GeometryRefusal,
} from './issues'
import { useEditorStore } from './state/editorStore'
import type { CommitOutcome } from './state/editorTypes'
@@ -25,14 +33,16 @@ export function updateView(
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) =>
(newAsset ? rejectedDrawingFault(after, newAsset) : null) ?? geometry(after)
return store.commit(
{
op: 'update',
key: { kind: 'view', id: viewId },
element: { kind: 'view', value },
},
guarded
? { accept: geometryAccept({ before, changedIds: [] }) }
: undefined,
guarded ? { accept } : undefined,
)
}