import type { CataloguePreview } from '@/catalogue/preview' import { emptyHistory } from './undo' import type { BlockKind, EditorData, EditorState } from './editorTypes' export function initialData(): EditorData { return { session: 0, status: 'idle', failure: null, userId: null, versionId: null, sheetId: null, canEdit: true, preview: null, previewState: null, handleVersion: 0, documentText: '', savedText: '', savedRevision: 0, staleBase: false, unsaved: false, drawings: [], drawingFailures: {}, ...emptyHistory(), saving: false, submitting: false, reloading: false, saveFailure: null, lastSave: null, conflict: null, recovery: 'none', offer: null, refusedKind: null, copyWritten: false, copyIssue: null, otherCopies: [], unusableCopy: false, } } /** * Why a document change cannot run now. A recovery offer, a refused or a restoring copy * block every change at the store level (keyboard shortcuts included). */ export function blockKind(state: EditorState): BlockKind | null { if (state.status !== 'ready' || state.preview === null) return 'not_ready' if (state.recovery !== 'none') return 'recovery.pending' if (!state.canEdit) return 'read_only' if (state.reloading) return 'busy' return null } /** The handle of a state that accepts document changes, else the reason it does not. */ export function editablePreview(state: EditorState): CataloguePreview | BlockKind { return blockKind(state) ?? state.preview ?? 'not_ready' } /** The leave guard is armed while unsaved or while a recovery offer is not answered. */ export function leaveGuardArmed(state: EditorData): boolean { return state.unsaved || state.recovery !== 'none' } export const canUndo = (state: EditorData): boolean => state.undo.length > 0 export const canRedo = (state: EditorData): boolean => state.redo.length > 0