feat(app): editor store, undo, recovery copy and geometry accept rules
This commit is contained in:
1 parent
f7e7bf4ee1
commit
7f1d44065b
20 files changed
+2538
-5
No files matched your search
@@ -0,0 +1,67 @@
|
||||
import type { Annotation, EditCommand, NewElement } from '@faunosteo/core-wasm/types'
|
||||
import { emptySheetDocument } from '@/core/sheetDocumentOps'
|
||||
import { cleanSvg } from '@/editor/core/cleaning'
|
||||
import { applyEdit } from '@/editor/core/editOps'
|
||||
|
||||
export const ASSET = '11111111-1111-4111-8111-111111111111'
|
||||
const NAME = { fr: 'N', en: 'N' }
|
||||
|
||||
export function svgBytes(viewBox = '0 0 100 100'): {
|
||||
bytes: Uint8Array
|
||||
sha256: string
|
||||
} {
|
||||
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${viewBox}"><rect width="4" height="4"/></svg>`
|
||||
const cleaned = cleanSvg(new TextEncoder().encode(svg))
|
||||
if (!cleaned.ok || !cleaned.value.stored || !cleaned.value.sha256) {
|
||||
throw new Error('uncleanable drawing')
|
||||
}
|
||||
return { bytes: new TextEncoder().encode(cleaned.value.stored), sha256: cleaned.value.sha256 }
|
||||
}
|
||||
|
||||
export function edit(text: string, command: EditCommand): string {
|
||||
const result = applyEdit(text, command)
|
||||
if (!result.ok) throw new Error(`${result.kind} ${result.detail ?? ''}`)
|
||||
return result.value
|
||||
}
|
||||
|
||||
export function insert(text: string, element: NewElement): string {
|
||||
return edit(text, { op: 'insert', element })
|
||||
}
|
||||
|
||||
export function emptyText(name = 'Fiche'): string {
|
||||
const empty = emptySheetDocument({ speciesId: 'equine', name: { fr: name, en: name } })
|
||||
if (!empty.ok) throw new Error(empty.kind)
|
||||
return JSON.stringify(empty.value)
|
||||
}
|
||||
|
||||
export function point(id: string, cx: number, cy: number, r: number, viewId = 'view-main') {
|
||||
const value: Annotation = {
|
||||
id,
|
||||
view_id: viewId,
|
||||
category_id: 'cat-a',
|
||||
laterality: 'unpaired',
|
||||
geometry: { type: 'circle', cx, cy, r },
|
||||
review_status: 'to_confirm',
|
||||
color: null,
|
||||
name: NAME,
|
||||
finding_zone_id: null,
|
||||
vertebral_region_code: null,
|
||||
kind: 'point',
|
||||
}
|
||||
return { kind: 'annotation', value } satisfies NewElement
|
||||
}
|
||||
|
||||
/** An empty sheet whose main view draws ASSET and with one category `cat-a`. */
|
||||
export function drawnText(): string {
|
||||
const withCategory = insert(emptyText(), {
|
||||
kind: 'category',
|
||||
value: { id: 'cat-a', name: NAME, color: null },
|
||||
})
|
||||
const document = JSON.parse(withCategory)
|
||||
const main = document.views.find((v: { id: string }) => v.id === 'view-main')
|
||||
return edit(withCategory, {
|
||||
op: 'update',
|
||||
key: { kind: 'view', id: 'view-main' },
|
||||
element: { kind: 'view', value: { ...main, svg_asset_id: ASSET } },
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import { expect, vi } from 'vitest'
|
||||
import type { EditCommand } from '@faunosteo/core-wasm/types'
|
||||
import * as supabase from '@faunosteo/supabase'
|
||||
import { useEditorStore } from '@/editor/state/editorStore'
|
||||
import { ASSET, drawnText, insert, point, svgBytes } from './editorFixtures'
|
||||
|
||||
/**
|
||||
* The store harness. The calling test file declares the two module mocks (hoisted):
|
||||
* vi.mock('@/lib/supabase', () => ({ getSupabaseClient: () => ({}) }))
|
||||
* vi.mock('@faunosteo/supabase', async (orig) => ({ ...(await orig()), getSheetVersion: vi.fn(),
|
||||
* downloadSheetDrawing: vi.fn(), saveSheetDraft: vi.fn(), getOfficialSheetAdmin: vi.fn(),
|
||||
* submitSheetReview: vi.fn() }))
|
||||
*/
|
||||
export const USER = 'user-1'
|
||||
export const VERSION = 'ver-1'
|
||||
export const SHEET = 'sheet-1'
|
||||
|
||||
export const sb = vi.mocked(supabase)
|
||||
export const store = useEditorStore
|
||||
|
||||
export function baseText(): string {
|
||||
return insert(drawnText(), point('anno-a', 50, 50, 5))
|
||||
}
|
||||
|
||||
export function versionAnswer(text: string, revision: number, withDrawing = true) {
|
||||
const drawings = withDrawing ? [{ asset_id: ASSET, sha256: svgBytes().sha256 }] : []
|
||||
return {
|
||||
sheet_id: SHEET,
|
||||
version_id: VERSION,
|
||||
version_number: 1,
|
||||
status: 'draft',
|
||||
species_id: 'equine',
|
||||
document: JSON.parse(text),
|
||||
fingerprint: null,
|
||||
revision,
|
||||
drawings,
|
||||
}
|
||||
}
|
||||
|
||||
export function serverHas(text: string, revision: number, withDrawing = true): void {
|
||||
sb.getSheetVersion.mockResolvedValue(versionAnswer(text, revision, withDrawing) as never)
|
||||
sb.downloadSheetDrawing.mockResolvedValue(svgBytes().bytes)
|
||||
}
|
||||
|
||||
export async function openReady(
|
||||
options: { text?: string; revision?: number; canEdit?: boolean } = {},
|
||||
): Promise<number> {
|
||||
serverHas(options.text ?? baseText(), options.revision ?? 3)
|
||||
const token = store.getState().open(VERSION, USER, { canEdit: options.canEdit ?? true })
|
||||
await vi.waitFor(() => expect(store.getState().status).not.toBe('loading'))
|
||||
return token
|
||||
}
|
||||
|
||||
export function resetStore(): void {
|
||||
store.getState().close()
|
||||
sessionStorage.clear()
|
||||
vi.restoreAllMocks()
|
||||
vi.resetAllMocks()
|
||||
}
|
||||
|
||||
/** Moves anno-a to (x, 50): a valid, always different, document. */
|
||||
export function moveCommand(x: number): EditCommand {
|
||||
return {
|
||||
op: 'update',
|
||||
key: { kind: 'annotation', id: 'anno-a' },
|
||||
element: point('anno-a', x, 50, 5),
|
||||
}
|
||||
}
|
||||
|
||||
export function commitMove(x: number) {
|
||||
return store.getState().commit(moveCommand(x))
|
||||
}
|
||||
|
||||
export const copyKeyOf = (version = VERSION, user = USER) =>
|
||||
`osteo:sheet-draft:${user}:${version}`
|
||||
|
||||
export function storedCopy(version = VERSION, user = USER) {
|
||||
const raw = sessionStorage.getItem(copyKeyOf(version, user))
|
||||
return raw === null ? null : JSON.parse(raw)
|
||||
}
|
||||
Reference in new issue
Block a user