feat(editor): header history, save line and Abandonner les modifications
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
This commit is contained in:
1 parent
ab51ea764d
commit
99b669bf8f
29 files changed
+1397
-248
No files matched your search
@@ -0,0 +1,75 @@
|
||||
import { useMemo } from 'react'
|
||||
import { useEditorStore } from './state/editorStore'
|
||||
import type { CopyIssue, EditorData, LastSave } from './state/editorTypes'
|
||||
import type { HistoryEntry } from './state/undo'
|
||||
|
||||
type DistanceInput = Pick<
|
||||
EditorData,
|
||||
'staleBase' | 'documentText' | 'savedText' | 'undo' | 'redo'
|
||||
>
|
||||
|
||||
/** The 1-based depth, from the stack's top, of the nearest entry holding `text`. */
|
||||
function depthIn(stack: readonly HistoryEntry[], text: string): number | null {
|
||||
for (let at = stack.length - 1; at >= 0; at -= 1) {
|
||||
if (stack[at].text === text) return stack.length - at
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* How many history steps separate the document from the saved text: 0 when they are the
|
||||
* same text, the depth of the nearest undo entry (else redo entry) holding the saved text,
|
||||
* null when it is out of reach (older base, trimmed, or replaced by a later change).
|
||||
*/
|
||||
export function savedDistance(state: DistanceInput): number | null {
|
||||
if (state.staleBase) return null
|
||||
if (state.documentText === state.savedText) return 0
|
||||
return (
|
||||
depthIn(state.undo, state.savedText) ?? depthIn(state.redo, state.savedText)
|
||||
)
|
||||
}
|
||||
|
||||
/** `savedDistance`, recomputed only when one of its four inputs changes identity. */
|
||||
export function useSavedDistance(): number | null {
|
||||
const staleBase = useEditorStore((s) => s.staleBase)
|
||||
const documentText = useEditorStore((s) => s.documentText)
|
||||
const savedText = useEditorStore((s) => s.savedText)
|
||||
const undo = useEditorStore((s) => s.undo)
|
||||
const redo = useEditorStore((s) => s.redo)
|
||||
return useMemo(
|
||||
() => savedDistance({ staleBase, documentText, savedText, undo, redo }),
|
||||
[staleBase, documentText, savedText, undo, redo],
|
||||
)
|
||||
}
|
||||
|
||||
export interface SaveLineInput {
|
||||
copyIssue: CopyIssue | null
|
||||
saving: boolean
|
||||
unsaved: boolean
|
||||
distance: number | null
|
||||
lastSave: LastSave | null
|
||||
savedRevision: number
|
||||
}
|
||||
|
||||
export type SaveLineState =
|
||||
| { kind: 'copy_issue'; issue: CopyIssue }
|
||||
| { kind: 'saving' }
|
||||
| { kind: 'unsaved'; count: number | null }
|
||||
| { kind: 'saved'; revision: number; savedAt: string | null }
|
||||
| { kind: 'revision'; revision: number }
|
||||
|
||||
/** What the save line says, the first matching form wins. */
|
||||
export function saveLineState(input: SaveLineInput): SaveLineState {
|
||||
if (input.copyIssue !== null)
|
||||
return { kind: 'copy_issue', issue: input.copyIssue }
|
||||
if (input.saving) return { kind: 'saving' }
|
||||
if (input.unsaved) {
|
||||
const known = input.distance !== null && input.distance > 0
|
||||
return { kind: 'unsaved', count: known ? input.distance : null }
|
||||
}
|
||||
const { lastSave, savedRevision } = input
|
||||
if (lastSave && lastSave.revision === savedRevision) {
|
||||
return { kind: 'saved', revision: savedRevision, savedAt: lastSave.savedAt }
|
||||
}
|
||||
return { kind: 'revision', revision: savedRevision }
|
||||
}
|
||||
@@ -26,6 +26,7 @@ export function initialData(): EditorData {
|
||||
submitting: false,
|
||||
reloading: false,
|
||||
saveFailure: null,
|
||||
lastSave: null,
|
||||
conflict: null,
|
||||
recovery: 'none',
|
||||
offer: null,
|
||||
|
||||
@@ -144,6 +144,7 @@ function reloadPatch(state: EditorState, text: string, next: PreviewState, revis
|
||||
handleVersion: state.handleVersion + 1,
|
||||
...emptyHistory(),
|
||||
saveFailure: null,
|
||||
lastSave: null,
|
||||
conflict: null,
|
||||
copyWritten: false,
|
||||
copyIssue: null,
|
||||
|
||||
@@ -70,6 +70,37 @@ export async function readConflict(api: StoreApi, session: number): Promise<void
|
||||
}
|
||||
}
|
||||
|
||||
/** The server's time of save `revision` of this version in `admin`, else null. */
|
||||
function savedTimeOf(admin: OfficialSheetAdmin, state: EditorState, revision: number) {
|
||||
const last = admin.draft_last_save
|
||||
if (admin.draft_version?.version_id !== state.versionId) return null
|
||||
if (last?.revision !== revision || state.lastSave?.revision !== revision) return null
|
||||
return Number.isNaN(Date.parse(last.saved_at)) ? null : last.saved_at
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the server's time of save `revision` (`draft_last_save.saved_at`), never awaited by
|
||||
* the save. Called straight on the accessor with the store's client, as `readConflict` is,
|
||||
* so a refusal, a failure or an unreadable answer only leaves the time unknown. A late
|
||||
* answer is dropped unless the session and the `lastSave` it was asked for are unchanged.
|
||||
*/
|
||||
export async function readSaveTime(
|
||||
api: StoreApi,
|
||||
session: number,
|
||||
revision: number,
|
||||
): Promise<void> {
|
||||
const asked = api.get().lastSave
|
||||
try {
|
||||
const admin = await getOfficialSheetAdmin(requireClient(), api.get().sheetId ?? '')
|
||||
const state = api.get()
|
||||
if (state.session !== session || state.lastSave !== asked) return
|
||||
const savedAt = savedTimeOf(admin, state, revision)
|
||||
if (savedAt !== null) api.set({ lastSave: { revision, savedAt } })
|
||||
} catch {
|
||||
// Unreadable: the line stays « Enregistré · révision n ».
|
||||
}
|
||||
}
|
||||
|
||||
interface Attempt {
|
||||
session: number
|
||||
sentText: string
|
||||
@@ -89,6 +120,7 @@ function applySaved(api: StoreApi, attempt: Attempt, revision: number): SaveOutc
|
||||
unsaved,
|
||||
conflict: null,
|
||||
saveFailure: null,
|
||||
lastSave: { revision, savedAt: null },
|
||||
...(unsaved ? {} : { copyWritten: false, copyIssue: null }),
|
||||
})
|
||||
return { ok: true, revision }
|
||||
@@ -127,7 +159,9 @@ async function saveWith(api: StoreApi, expected: number): Promise<SaveOutcome> {
|
||||
document: JSON.parse(attempt.sentText),
|
||||
}
|
||||
const answer = parseSaveAnswer(await saveSheetDraft(requireClient(), args))
|
||||
return applySaved(api, attempt, answer.revision)
|
||||
const outcome = applySaved(api, attempt, answer.revision)
|
||||
if (outcome.ok) void readSaveTime(api, attempt.session, outcome.revision)
|
||||
return outcome
|
||||
} catch (error) {
|
||||
return failedSave(api, attempt, error)
|
||||
}
|
||||
|
||||
@@ -87,6 +87,15 @@ export type ConflictState =
|
||||
|
||||
export type CopyIssue = 'cap' | 'quota'
|
||||
|
||||
/**
|
||||
* The last save the server confirmed: its revision, and the server's time of that save once
|
||||
* read (null when unreadable, never the browser's clock).
|
||||
*/
|
||||
export interface LastSave {
|
||||
revision: number
|
||||
savedAt: string | null
|
||||
}
|
||||
|
||||
export interface EditorData {
|
||||
/** Session token: bumped by `open` and `close`; every async continuation checks it. */
|
||||
session: number
|
||||
@@ -115,6 +124,7 @@ export interface EditorData {
|
||||
submitting: boolean
|
||||
reloading: boolean
|
||||
saveFailure: SheetFailureKind | null
|
||||
lastSave: LastSave | null
|
||||
conflict: ConflictState | null
|
||||
recovery: RecoveryMode
|
||||
offer: RecoveryOffer | null
|
||||
|
||||
Reference in new issue
Block a user