fix(editor): deletion undo and Back, Plus focus to the panel
This commit is contained in:
1 parent
270444e2ec
commit
085c56877c
14 files changed
+252
-18
No files matched your search
@@ -180,6 +180,26 @@ export function buildItems(doc: AnatomicalSheetDocument): FindingItem[] {
|
||||
})
|
||||
}
|
||||
|
||||
function itemsOfText(text: string): FindingItem[] {
|
||||
try {
|
||||
return buildItems(JSON.parse(text))
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The first element `after` holds that `before` lacked (document order, so the top of a
|
||||
* restored branch): what a step brought back. null when the step added none.
|
||||
*/
|
||||
export function appearedFinding(
|
||||
before: string,
|
||||
after: string,
|
||||
): ElementKey | null {
|
||||
const known = new Set(itemsOfText(before).map((item) => item.id))
|
||||
return itemsOfText(after).find((item) => !known.has(item.id))?.key ?? null
|
||||
}
|
||||
|
||||
/** The items whose ancestors are all expanded. */
|
||||
export function visibleItems(
|
||||
items: readonly FindingItem[],
|
||||
|
||||
@@ -14,6 +14,7 @@ export function initialData(): EditorData {
|
||||
preview: null,
|
||||
previewState: null,
|
||||
handleVersion: 0,
|
||||
revealed: null,
|
||||
documentText: '',
|
||||
savedText: '',
|
||||
savedRevision: 0,
|
||||
@@ -51,7 +52,9 @@ export function blockKind(state: EditorState): BlockKind | null {
|
||||
}
|
||||
|
||||
/** The handle of a state that accepts document changes, else the reason it does not. */
|
||||
export function editablePreview(state: EditorState): CataloguePreview | BlockKind {
|
||||
export function editablePreview(
|
||||
state: EditorState,
|
||||
): CataloguePreview | BlockKind {
|
||||
return blockKind(state) ?? state.preview ?? 'not_ready'
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import type {
|
||||
EditorState,
|
||||
StoreApi,
|
||||
} from './editorTypes'
|
||||
import { appearedFinding } from '../findings/tree'
|
||||
import { recordCommit, stepBack, stepForward, type History } from './undo'
|
||||
|
||||
function blocked(kind: BlockKind): CommitOutcome<never> {
|
||||
@@ -68,7 +69,10 @@ export function commit<R>(
|
||||
return commitBatch(api, [command], accept)
|
||||
}
|
||||
|
||||
export function stepHistory(api: StoreApi, direction: 'undo' | 'redo'): CommitOutcome {
|
||||
export function stepHistory(
|
||||
api: StoreApi,
|
||||
direction: 'undo' | 'redo',
|
||||
): CommitOutcome {
|
||||
const state = api.get()
|
||||
const preview = editablePreview(state)
|
||||
if (typeof preview === 'string') return blocked(preview)
|
||||
@@ -77,12 +81,17 @@ export function stepHistory(api: StoreApi, direction: 'undo' | 'redo'): CommitOu
|
||||
if (!step) return blocked('empty')
|
||||
const replaced = preview.replaceDocument(step.text)
|
||||
if (!replaced.ok) return replaced
|
||||
api.set(replacedPatch(state, step.text, replaced.state, step.history))
|
||||
const patch = replacedPatch(state, step.text, replaced.state, step.history)
|
||||
const key = appearedFinding(state.documentText, step.text)
|
||||
api.set(key ? { ...patch, revealed: { key } } : patch)
|
||||
return { ok: true }
|
||||
}
|
||||
|
||||
/** A drawing the core accepted becomes part of the handle (and of the copy's list). */
|
||||
export function addDrawing(api: StoreApi, drawing: AddedDrawing): CommitOutcome {
|
||||
export function addDrawing(
|
||||
api: StoreApi,
|
||||
drawing: AddedDrawing,
|
||||
): CommitOutcome {
|
||||
const state = api.get()
|
||||
const preview = editablePreview(state)
|
||||
if (typeof preview === 'string') return blocked(preview)
|
||||
@@ -92,7 +101,9 @@ export function addDrawing(api: StoreApi, drawing: AddedDrawing): CommitOutcome
|
||||
const entry = { asset_id: drawing.assetId, sha256: drawing.sha256 }
|
||||
const others = state.drawings.filter((d) => d.asset_id !== entry.asset_id)
|
||||
const failures = Object.fromEntries(
|
||||
Object.entries(state.drawingFailures).filter(([id]) => id !== entry.asset_id),
|
||||
Object.entries(state.drawingFailures).filter(
|
||||
([id]) => id !== entry.asset_id,
|
||||
),
|
||||
)
|
||||
api.set({
|
||||
drawings: [...others, entry],
|
||||
|
||||
@@ -26,9 +26,11 @@ export const useEditorStore = create<EditorState>()((set, get) => {
|
||||
close: () => close(api),
|
||||
setCanEdit: (canEdit) => setCanEdit(api, canEdit),
|
||||
commit: (command, options) => commit(api, command, options?.accept),
|
||||
commitBatch: (commands, options) => commitBatch(api, commands, options?.accept),
|
||||
commitBatch: (commands, options) =>
|
||||
commitBatch(api, commands, options?.accept),
|
||||
undoStep: () => stepHistory(api, 'undo'),
|
||||
redoStep: () => stepHistory(api, 'redo'),
|
||||
clearRevealed: () => set({ revealed: null }),
|
||||
addDrawing: (drawing) => addDrawing(api, drawing),
|
||||
discardChanges: (option) => discardChanges(api, option),
|
||||
reloadServerDraft: (option) => reloadServerDraft(api, option),
|
||||
@@ -39,7 +41,8 @@ export const useEditorStore = create<EditorState>()((set, get) => {
|
||||
dismissConflict: () => set({ conflict: null }),
|
||||
restoreCopy: () => restoreCopy(api),
|
||||
ignoreCopy: (option) => ignoreCopy(api, option),
|
||||
abandonOtherCopy: (versionId, option) => abandonOtherCopy(api, versionId, option),
|
||||
abandonOtherCopy: (versionId, option) =>
|
||||
abandonOtherCopy(api, versionId, option),
|
||||
retryCopyWrite: () => syncCopy(api),
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import type { CoreFailure } from '@faunosteo/core-wasm'
|
||||
import type { EditCommand, PreviewState } from '@faunosteo/core-wasm/types'
|
||||
import type {
|
||||
EditCommand,
|
||||
ElementKey,
|
||||
PreviewState,
|
||||
} from '@faunosteo/core-wasm/types'
|
||||
import type { SheetAdminPerson, SheetFailureKind } from '@faunosteo/supabase'
|
||||
import type { CataloguePreview, Acceptance } from '@/catalogue/preview'
|
||||
import type { CatalogueFailure } from '@/catalogue/failure'
|
||||
@@ -48,10 +52,20 @@ export interface LoadRefusal {
|
||||
failure: CatalogueFailure
|
||||
}
|
||||
|
||||
export type CommitOutcome<R = never> = { ok: true } | Blocked | CoreRefusal | Refused<R>
|
||||
export type CommitOutcome<R = never> =
|
||||
| { ok: true }
|
||||
| Blocked
|
||||
| CoreRefusal
|
||||
| Refused<R>
|
||||
export type ActionOutcome = { ok: true } | Blocked | CoreRefusal | LoadRefusal
|
||||
export type SaveOutcome = { ok: true; revision: number } | Blocked | ServerRefusal
|
||||
export type SubmitOutcome = { ok: true; reviewId: string } | Blocked | ServerRefusal
|
||||
export type SaveOutcome =
|
||||
| { ok: true; revision: number }
|
||||
| Blocked
|
||||
| ServerRefusal
|
||||
export type SubmitOutcome =
|
||||
| { ok: true; reviewId: string }
|
||||
| Blocked
|
||||
| ServerRefusal
|
||||
|
||||
/** A drawing the handle holds. */
|
||||
export interface DrawingEntry {
|
||||
@@ -110,6 +124,8 @@ export interface EditorData {
|
||||
previewState: PreviewState | null
|
||||
/** Bumped by every accepted replacement and added drawing (canvas rebuild signal). */
|
||||
handleVersion: number
|
||||
/** The finding a « Défaire » or « Refaire » brought back: Constats shows it, then clears. */
|
||||
revealed: { key: ElementKey } | null
|
||||
documentText: string
|
||||
savedText: string
|
||||
savedRevision: number
|
||||
@@ -144,7 +160,11 @@ export interface ConfirmOption {
|
||||
|
||||
export interface EditorActions {
|
||||
/** Starts a session and loads the version; returns its token at once. */
|
||||
open(versionId: string, userId: string, options?: { canEdit?: boolean }): number
|
||||
open(
|
||||
versionId: string,
|
||||
userId: string,
|
||||
options?: { canEdit?: boolean },
|
||||
): number
|
||||
close(): void
|
||||
setCanEdit(canEdit: boolean): void
|
||||
commit<R = never>(
|
||||
@@ -158,6 +178,8 @@ export interface EditorActions {
|
||||
): CommitOutcome<R>
|
||||
undoStep(): CommitOutcome
|
||||
redoStep(): CommitOutcome
|
||||
/** Constats has shown the finding the last step brought back. */
|
||||
clearRevealed(): void
|
||||
addDrawing(drawing: AddedDrawing): CommitOutcome
|
||||
discardChanges(option: ConfirmOption): Promise<ActionOutcome>
|
||||
reloadServerDraft(option: ConfirmOption): Promise<ActionOutcome>
|
||||
|
||||
Reference in new issue
Block a user