feat(app): editable sheet preview, edit wrappers, cleaning preview and ids

This commit is contained in:
bchanot committed 2026-11-04 11:48:01 +01:00
1 parent 2f824a947c
commit 173be5949a
10 files changed
+663 -13

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
import { parseOutcome, type CoreOutcome } from '@faunosteo/core-wasm'
import type { SvgCleaning } from '@faunosteo/core-wasm/types'
import { CataloguePreview } from '@/catalogue/preview'
import { coreRuntime } from '@/core/runtime'
import { generateUuid } from '@/lib/id'
import { newElementId } from '../ids'
import { applyEdit } from './editOps'
/** The cleaning answer for a file picked for upload: clean, cleanable or rejected. */
export function cleanSvg(bytes: Uint8Array): CoreOutcome<SvgCleaning> {
return coreRuntime().invoke((glue) =>
parseOutcome<SvgCleaning>(glue.clean_svg(bytes)),
)
}
/** The cleaned drawing the core stores: its text and the sha256 the core computed for it. */
export interface StoredDrawing {
stored: string
sha256: string
}
export interface CleaningPreview {
preview: CataloguePreview
viewId: string
/** The id prefix the composed tree carries (`fv.<view>.`), unique in the page. */
scope: string
}
const NAME = { fr: 'Aperçu', en: 'Preview' }
/**
* A throwaway draft holding only the cleaned drawing, so the dialog renders the core's
* composed tree and never the original file. The caller frees `preview` on close.
*/
export function composeCleaningPreview(
drawing: StoredDrawing,
): CoreOutcome<CleaningPreview> {
const runtime = coreRuntime()
const empty = runtime.call<unknown>(
'sheet.empty',
JSON.stringify({ species_id: 'preview', name: NAME }),
)
if (!empty.ok) return empty
const assetId = generateUuid()
const viewId = newElementId('view')
const edited = applyEdit(JSON.stringify(empty.value), {
op: 'insert',
element: {
kind: 'view',
value: {
kind: 'detail',
id: viewId,
name: NAME,
svg_asset_id: assetId,
view_box: null,
parent_view_id: 'view-main',
},
},
})
if (!edited.ok) return edited
const preview = new CataloguePreview(
runtime,
{ status: 'draft', document: edited.value },
[],
)
const bytes = new TextEncoder().encode(drawing.stored)
const added = preview.addDrawing({ assetId, sha256: drawing.sha256, bytes })
if (!added.ok) {
preview.free()
return added
}
return { ok: true, value: { preview, viewId, scope: `fv.${viewId}.` } }
}
+36
View File
@@ -0,0 +1,36 @@
import type { CoreOutcome } from '@faunosteo/core-wasm'
import type {
Dependent,
EditCommand,
ElementKey,
SheetDependents,
SheetEditOutput,
} from '@faunosteo/core-wasm/types'
import { coreRuntime } from '@/core/runtime'
/**
* The editor's document rules, all the core's (BDR-043): the page holds the document as
* one JSON text and passes it through; the answer is serialised back to a text.
*/
/** One `sheet.edit` command on a document text; the refusal is the core's closed kind. */
export function applyEdit(
text: string,
command: EditCommand,
): CoreOutcome<string> {
const input = `{"document":${text},"command":${JSON.stringify(command)}}`
const outcome = coreRuntime().call<SheetEditOutput>('sheet.edit', input)
return outcome.ok
? { ok: true, value: JSON.stringify(outcome.value.document) }
: outcome
}
/** What refers to the element `key` (removal and rename warnings). */
export function dependentsOf(
text: string,
key: ElementKey,
): CoreOutcome<Dependent[]> {
const input = `{"document":${text},"key":${JSON.stringify(key)}}`
const outcome = coreRuntime().call<SheetDependents>('sheet.dependents', input)
return outcome.ok ? { ok: true, value: outcome.value.dependents } : outcome
}
+38
View File
@@ -0,0 +1,38 @@
function deepEqual(a: unknown, b: unknown): boolean {
if (a === b) return true
if (
typeof a !== 'object' ||
typeof b !== 'object' ||
a === null ||
b === null
)
return false
if (Array.isArray(a) || Array.isArray(b)) {
return (
Array.isArray(a) &&
Array.isArray(b) &&
a.length === b.length &&
a.every((item, index) => deepEqual(item, b[index]))
)
}
const left = a as Record<string, unknown>
const right = b as Record<string, unknown>
const keys = Object.keys(left)
return (
keys.length === Object.keys(right).length &&
keys.every((key) => key in right && deepEqual(left[key], right[key]))
)
}
/**
* Two document texts are the same document when their parsed JSON is deeply equal: key
* order is ignored (the server's jsonb order against the core's serde order), array order
* is not. A text that is not JSON equals only an identical string.
*/
export function sameDocument(a: string, b: string): boolean {
try {
return deepEqual(JSON.parse(a), JSON.parse(b))
} catch {
return a === b
}
}
+21
View File
@@ -0,0 +1,21 @@
const ALPHABET = 'abcdefghijklmnopqrstuvwxyz234567'
const SUFFIX_LENGTH = 10
export type IdPrefix =
| 'view'
| 'anno'
| 'cat'
| 'ref'
| 'zone'
| 'fcat'
| 'node'
/**
* A new element id, `<prefix>-<10 chars a-z2-7>`. 32 symbols: a byte masked to 5 bits
* is unbiased. No document walk; a `duplicate_key` answer is the caller's one retry.
*/
export function newElementId(prefix: IdPrefix): string {
const bytes = crypto.getRandomValues(new Uint8Array(SUFFIX_LENGTH))
const suffix = Array.from(bytes, (byte) => ALPHABET[byte & 31]).join('')
return `${prefix}-${suffix}`
}