Header Annuler/Retablir on every tab (aria-disabled, commit on pointerdown, refused blur commit skips the undo). Save line: unsaved count selector (savedDistance), saveLineState forms, server save time via readSaveTime (lastSave in the store, never the browser clock). Discard relabelled and names the stored copy. Tool column pair removed, glyphs in EditorGlyph. Rewritten or removed assertions (R-24): - editor-route.test: Revision 3 / unsaved text / Annuler les modifications -> save-line testid, Abandonner label, copy sentence in the dialog - editor-route-conflict, editor-route-recovery: Revision n and copy_cap/ copy_quota text queries -> save-line testid - editor-no-loss: discard button label - editor-annotations-tab: tool column undo/redo test -> no pair left (behaviour moved to editor-history-buttons.test) - e2e journey/annotations/recovery/structures/support saveDraft/lan: undo/redo and revision/unsaved text -> historyButton, saveLine helpers; structures toHaveCount(0) on old text -> exact Revision n line - editor-shell clickability: header Annuler/Retablir added
234 lines
7.7 KiB
TypeScript
234 lines
7.7 KiB
TypeScript
import type { PreviewState } from '@faunosteo/core-wasm/types'
|
|
import type { FaunosteoClient } from '@faunosteo/supabase'
|
|
import {
|
|
readVersion,
|
|
type DrawingFetch,
|
|
type DraftAnswer,
|
|
} from '@/catalogue/catalogue'
|
|
import { coreFailure, type CatalogueFailure } from '@/catalogue/failure'
|
|
import { CataloguePreview } from '@/catalogue/preview'
|
|
import { coreRuntime } from '@/core/runtime'
|
|
import { getSupabaseClient } from '@/lib/supabase'
|
|
import { blockKind, initialData } from './editorBase'
|
|
import type { Attached } from './drawings'
|
|
import type {
|
|
ActionOutcome,
|
|
ConfirmOption,
|
|
EditorState,
|
|
StoreApi,
|
|
} from './editorTypes'
|
|
import { attachDrawings } from './drawings'
|
|
import { detectRecovery } from './recoveryFlow'
|
|
import { listOtherCopies, removeCopy } from './recovery'
|
|
import { emptyHistory } from './undo'
|
|
|
|
interface Loaded {
|
|
version: DraftAnswer
|
|
text: string
|
|
preview: CataloguePreview
|
|
state: PreviewState
|
|
attached: Attached
|
|
}
|
|
type LoadResult = { ok: true; loaded: Loaded } | { ok: false; failure: CatalogueFailure }
|
|
|
|
/**
|
|
* A draft handle is built with NO drawing (a draft builds with its drawings missing); each
|
|
* downloaded drawing is added after, so one bad drawing never blocks authoring.
|
|
*/
|
|
async function loadDraft(client: FaunosteoClient, versionId: string): Promise<LoadResult> {
|
|
const read = await readVersion(client, versionId, { draft: true })
|
|
if (!read.ok) return read
|
|
if (!(await coreRuntime().ready())) return { ok: false, failure: 'core_unavailable' }
|
|
const text = JSON.stringify(read.version.document)
|
|
const input = {
|
|
status: read.version.status,
|
|
document: text,
|
|
fingerprint: read.version.fingerprint ?? undefined,
|
|
}
|
|
const preview = new CataloguePreview(coreRuntime(), input, [])
|
|
const attached = attachDrawings(preview, read.drawings)
|
|
const state = preview.state()
|
|
if (!state.ok) {
|
|
preview.free()
|
|
return { ok: false, failure: coreFailure(state.kind) }
|
|
}
|
|
const loaded = {
|
|
version: read.version,
|
|
text: JSON.stringify(state.value.document),
|
|
preview,
|
|
state: state.value,
|
|
attached,
|
|
}
|
|
return { ok: true, loaded }
|
|
}
|
|
|
|
function readyPatch(state: EditorState, loaded: Loaded): Partial<EditorState> {
|
|
const { version, text } = loaded
|
|
const recovery = detectRecovery({
|
|
userId: state.userId ?? '',
|
|
versionId: state.versionId ?? '',
|
|
canEdit: state.canEdit,
|
|
savedText: text,
|
|
savedRevision: version.revision,
|
|
})
|
|
return {
|
|
status: 'ready',
|
|
failure: null,
|
|
sheetId: version.sheet_id,
|
|
preview: loaded.preview,
|
|
previewState: loaded.state,
|
|
handleVersion: 1,
|
|
documentText: text,
|
|
savedText: text,
|
|
savedRevision: version.revision,
|
|
drawings: loaded.attached.held,
|
|
drawingFailures: loaded.attached.failures,
|
|
otherCopies: listOtherCopies(state.userId ?? '', state.versionId ?? ''),
|
|
...recovery,
|
|
}
|
|
}
|
|
|
|
async function loadInto(api: StoreApi, session: number): Promise<void> {
|
|
const client = getSupabaseClient()
|
|
const versionId = api.get().versionId ?? ''
|
|
const result: LoadResult = client
|
|
? await loadDraft(client, versionId)
|
|
: { ok: false, failure: 'unavailable' }
|
|
if (api.get().session !== session) {
|
|
if (result.ok) result.loaded.preview.free()
|
|
return
|
|
}
|
|
api.set(
|
|
result.ok
|
|
? readyPatch(api.get(), result.loaded)
|
|
: { status: 'failed', failure: result.failure },
|
|
)
|
|
}
|
|
|
|
export function open(
|
|
api: StoreApi,
|
|
versionId: string,
|
|
userId: string,
|
|
options?: { canEdit?: boolean },
|
|
): number {
|
|
const previous = api.get()
|
|
previous.preview?.free()
|
|
const session = previous.session + 1
|
|
api.set({
|
|
...initialData(),
|
|
session,
|
|
status: 'loading',
|
|
userId,
|
|
versionId,
|
|
canEdit: options?.canEdit ?? true,
|
|
})
|
|
void loadInto(api, session)
|
|
return session
|
|
}
|
|
|
|
/** Invalidates the session (late answers are dropped) and frees its handle. */
|
|
export function close(api: StoreApi): void {
|
|
const state = api.get()
|
|
state.preview?.free()
|
|
api.set({ ...initialData(), session: state.session + 1 })
|
|
}
|
|
|
|
function reloadPatch(state: EditorState, text: string, next: PreviewState, revision: number) {
|
|
return {
|
|
documentText: text,
|
|
savedText: text,
|
|
savedRevision: revision,
|
|
staleBase: false,
|
|
unsaved: false,
|
|
previewState: next,
|
|
handleVersion: state.handleVersion + 1,
|
|
...emptyHistory(),
|
|
saveFailure: null,
|
|
lastSave: null,
|
|
conflict: null,
|
|
copyWritten: false,
|
|
copyIssue: null,
|
|
reloading: false,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* « Recharger » (confirmed): the server draft's text and revision from one
|
|
* `get_sheet_version`, the drawings the handle lacks downloaded and added, both stacks
|
|
* cleared, the copy removed. A refusal leaves everything, the copy included, as it was.
|
|
*/
|
|
export async function reloadServerDraft(
|
|
api: StoreApi,
|
|
{ confirmed }: ConfirmOption,
|
|
): Promise<ActionOutcome> {
|
|
const state = api.get()
|
|
const block = blockKind(state)
|
|
if (block || !state.preview) return { ok: false, reason: 'blocked', kind: block ?? 'not_ready' }
|
|
if (state.saving || state.submitting) return { ok: false, reason: 'blocked', kind: 'busy' }
|
|
if (!confirmed) return { ok: false, reason: 'blocked', kind: 'confirmation_required' }
|
|
const client = getSupabaseClient()
|
|
if (!client) return { ok: false, reason: 'load', failure: 'unavailable' }
|
|
api.set({ reloading: true })
|
|
const have = new Set(state.drawings.map((d) => d.asset_id))
|
|
const read = await readVersion(client, state.versionId ?? '', { draft: true, have })
|
|
if (api.get().session !== state.session) {
|
|
return { ok: false, reason: 'blocked', kind: 'session_changed' }
|
|
}
|
|
if (!read.ok) {
|
|
api.set({ reloading: false })
|
|
return { ok: false, reason: 'load', failure: read.failure }
|
|
}
|
|
return applyReload(api, state, state.preview, read)
|
|
}
|
|
|
|
function applyReload(
|
|
api: StoreApi,
|
|
state: EditorState,
|
|
preview: CataloguePreview,
|
|
read: { version: DraftAnswer; revision: number; drawings: DrawingFetch[] },
|
|
): ActionOutcome {
|
|
const text = JSON.stringify(read.version.document)
|
|
const replaced = preview.replaceDocument(text)
|
|
if (!replaced.ok) {
|
|
api.set({ reloading: false })
|
|
return replaced.reason === 'core'
|
|
? { ok: false, reason: 'core', failure: replaced.failure }
|
|
: { ok: false, reason: 'blocked', kind: 'not_ready' }
|
|
}
|
|
const attached = attachDrawings(preview, read.drawings)
|
|
const next = preview.state()
|
|
const filled = next.ok ? next.value : replaced.state
|
|
removeCopy(state.userId ?? '', state.versionId ?? '')
|
|
api.set({
|
|
...reloadPatch(state, JSON.stringify(filled.document), filled, read.revision),
|
|
drawings: [...state.drawings, ...attached.held],
|
|
drawingFailures: { ...state.drawingFailures, ...attached.failures },
|
|
})
|
|
return { ok: true }
|
|
}
|
|
|
|
/**
|
|
* « Annuler les modifications » (confirmed): back to the saved text; with `staleBase` the
|
|
* server draft is reloaded instead (the saved text is an older revision's).
|
|
*/
|
|
export async function discardChanges(
|
|
api: StoreApi,
|
|
option: ConfirmOption,
|
|
): Promise<ActionOutcome> {
|
|
const state = api.get()
|
|
const block = blockKind(state)
|
|
if (block || !state.preview) return { ok: false, reason: 'blocked', kind: block ?? 'not_ready' }
|
|
if (state.saving || state.submitting) return { ok: false, reason: 'blocked', kind: 'busy' }
|
|
if (!option.confirmed) return { ok: false, reason: 'blocked', kind: 'confirmation_required' }
|
|
if (state.staleBase) return reloadServerDraft(api, option)
|
|
const replaced = state.preview.replaceDocument(state.savedText)
|
|
if (!replaced.ok) {
|
|
return replaced.reason === 'core'
|
|
? { ok: false, reason: 'core', failure: replaced.failure }
|
|
: { ok: false, reason: 'blocked', kind: 'not_ready' }
|
|
}
|
|
removeCopy(state.userId ?? '', state.versionId ?? '')
|
|
api.set(reloadPatch(state, state.savedText, replaced.state, state.savedRevision))
|
|
return { ok: true }
|
|
}
|