From c57af6441fcb7bb84942ae00ec89ca21fddc575e Mon Sep 17 00:00:00 2001 From: bchanot Date: Fri, 6 Nov 2026 21:22:10 +0100 Subject: [PATCH] feat(editor): keep edit-only forms across a width change Edit-only parts (creation forms, submission note, crop group, annotation edit body) mount at the first editable render and stay mounted, hidden and detached from canvases, while read-only. Guards: the viewport hook commits the focused field before the tier crosses 1 024 px, the route releases focus left in a hidden part and treats a store out of step as read-only, delete and discard confirmations close when the editor turns read-only, a drag is dropped while the edit body is hidden. Read-only twins take an id prefix. R-24: no existing assertion rewritten; editor-no-loss.test.tsx and two editor-shell Chromium cases added. --- apps/app/e2e/shell/editor-shell.spec.ts | 48 +- .../routes/app/admin/AdminSheetDraftRoute.tsx | 28 +- .../src/components/editor/DeleteDialog.tsx | 8 +- apps/app/src/components/editor/EditOnly.tsx | 20 + .../src/components/editor/EditorWorkspace.tsx | 111 ++-- .../src/components/editor/HeaderActions.tsx | 10 +- .../editor/annotations/AnnotationFields.tsx | 24 +- .../annotations/EditableAnnotations.tsx | 5 +- .../editor/annotations/OverviewPanel.tsx | 15 +- .../editor/annotations/PropertiesPanel.tsx | 5 +- .../annotations/sections/RegionsSection.tsx | 7 +- .../annotations/sections/ViewsSection.tsx | 7 +- .../editor/general/CategoriesSection.tsx | 5 +- .../editor/references/ReferencesPanel.tsx | 13 +- .../components/editor/tabs/AnnotationsTab.tsx | 25 +- .../components/editor/tabs/FindingsTab.tsx | 17 +- .../editor/tabs/VerificationTab.tsx | 5 +- .../components/editor/views/CropEditor.tsx | 24 +- .../components/editor/views/DetailViewRow.tsx | 15 +- apps/app/src/editor/editingContext.ts | 66 +++ apps/app/src/editor/focusRequest.ts | 10 + apps/app/src/editor/useViewport.ts | 18 +- apps/app/tests/unit/editor-no-loss.test.tsx | 505 ++++++++++++++++++ 23 files changed, 883 insertions(+), 108 deletions(-) create mode 100644 apps/app/src/components/editor/EditOnly.tsx create mode 100644 apps/app/src/editor/editingContext.ts create mode 100644 apps/app/tests/unit/editor-no-loss.test.tsx diff --git a/apps/app/e2e/shell/editor-shell.spec.ts b/apps/app/e2e/shell/editor-shell.spec.ts index 207c773a..c1c734a0 100644 --- a/apps/app/e2e/shell/editor-shell.spec.ts +++ b/apps/app/e2e/shell/editor-shell.spec.ts @@ -4,6 +4,8 @@ import { expect, test } from '../support/browser' import { dropLateReplays, routeToSandbox } from '../support/sandbox' import { backToDrawing, + copyKeyOf, + storedCopy, closeReferences, editorActor, openEditor, @@ -34,7 +36,7 @@ async function open(page: Page) { } const seed = await seedDraft(actor, { shape: shapeWith(free) }) await openEditor(page, dict, actor, seed.sheetId) - return seed + return { ...seed, actor } } const active = (page: Page) => page.locator(':focus') @@ -141,3 +143,47 @@ test.describe('editor shell, kept places (B2a)', () => { await expect(active(page)).toHaveAttribute('id', 'editor-tab-annotations') }) }) + +// V1-2.6 U1 B2b: a 1 024 px crossing never loses typed input. +test.describe('editor shell, width crossing (B2b)', () => { + test.use({ locale: 'fr-FR', actionTimeout: 15_000 }) + test.beforeEach(async ({ context }, info) => { + test.skip(isPhone(info), 'editing needs 1 024 px') // floor-guard: allow C16 editing needs >= 1024 px; phone read-only covered in editor-access + await routeToSandbox(context) + }) + test.afterEach(async ({ context }) => { + await dropLateReplays(context) + }) + + test('a field typed without a blur is committed before the crossing and the copy is written', async ({ + page, + }) => { + const { actor, draftId } = await open(page) + await selectTab(page, dict, 'general') + const field = page.locator('#general-name-fr') + await field.click() + await field.fill('Nom saisi sans quitter le champ') + const key = copyKeyOf(actor.account.id, draftId) + expect(await storedCopy(page, key)).toBeNull() + await page.setViewportSize({ width: 800, height: 900 }) + await expect(page.locator('#general-name-fr')).toHaveText( + 'Nom saisi sans quitter le champ', + ) + await expect.poll(() => storedCopy(page, key)).not.toBeNull() + }) + + test('the category form text is hidden below 1 024 px and comes back on widening', async ({ + page, + }) => { + await open(page) + await openSection(page, dict, 'categories') + const field = page.locator('#category-new-fr') + await field.fill('Brouillon') + await page.setViewportSize({ width: 800, height: 900 }) + await expect(field).toBeHidden() + await expect(page.locator(':focus')).toHaveCount(0) + await page.setViewportSize({ width: 1280, height: 900 }) + await expect(field).toBeVisible() + await expect(field).toHaveValue('Brouillon') + }) +}) diff --git a/apps/app/src/app/routes/app/admin/AdminSheetDraftRoute.tsx b/apps/app/src/app/routes/app/admin/AdminSheetDraftRoute.tsx index d6f5aa6d..866b2645 100644 --- a/apps/app/src/app/routes/app/admin/AdminSheetDraftRoute.tsx +++ b/apps/app/src/app/routes/app/admin/AdminSheetDraftRoute.tsx @@ -1,4 +1,4 @@ -import { useEffect, useRef } from 'react' +import { useEffect, useLayoutEffect, useRef } from 'react' import { useParams } from 'react-router-dom' import { getOfficialSheetAdmin, @@ -18,10 +18,12 @@ import { isRightsRefusal, useAdminQuery } from '@/admin/useAdminQuery' import { useLeaveGuard } from '@/app/useLeaveGuard' import { editorMode, + type EditorMode, hasEditRight, modeAfterSave, storeCanEdit, } from '@/editor/mode' +import { releaseEditFocus } from '@/editor/focusRequest' import { leaveGuardArmed, useEditorStore } from '@/editor/state/editorStore' import { useUndoShortcuts } from '@/editor/useUndoShortcuts' import { useTabTier } from '@/editor/useViewport' @@ -37,6 +39,17 @@ interface DraftEditorProps { reloadSheet: () => void } +/** + * Not editable now: a mode that cannot save, a recovery offer not answered yet, or a + * store not yet in step with the mode. The last keeps every render before the recovery + * detection of `setCanEdit` read-only. + */ +function useReadOnly(mode: EditorMode, canEdit: boolean): boolean { + const recovery = useEditorStore((s) => s.recovery) + const storeEditable = useEditorStore((s) => s.canEdit) + return mode !== 'edit' || recovery !== 'none' || storeEditable !== canEdit +} + /** * The draft page once the sheet is read: opens the store session for this version, derives * the mode, and renders what the store holds. Components only render and call actions. @@ -54,13 +67,12 @@ function DraftEditor({ const status = useEditorStore((s) => s.status) const sessionVersion = useEditorStore((s) => s.versionId) const failure = useEditorStore((s) => s.failure) - const recovery = useEditorStore((s) => s.recovery) const mode = modeAfterSave( editorMode({ hasRight, sheet, wide: tier === 'wide' }), saveFailure, ) const canEdit = storeCanEdit(mode) - const readOnly = mode !== 'edit' || recovery !== 'none' + const readOnly = useReadOnly(mode, canEdit) const firstCanEdit = useRef(canEdit) useEffect(() => { @@ -69,7 +81,15 @@ function DraftEditor({ .open(versionId, userId, { canEdit: firstCanEdit.current }) return () => useEditorStore.getState().close() }, [versionId, userId]) - useEffect(() => useEditorStore.getState().setCanEdit(canEdit), [canEdit]) + // `sheet` too: after a submission whose refresh keeps the draft editable, the store + // (set to false by `submit()`) is brought back in step. + useEffect( + () => useEditorStore.getState().setCanEdit(canEdit), + [canEdit, sheet], + ) + useLayoutEffect(() => { + if (readOnly) releaseEditFocus() + }, [readOnly]) useLeaveGuard(useEditorStore(leaveGuardArmed)) useUndoShortcuts(!readOnly) diff --git a/apps/app/src/components/editor/DeleteDialog.tsx b/apps/app/src/components/editor/DeleteDialog.tsx index 12064d18..9fffd825 100644 --- a/apps/app/src/components/editor/DeleteDialog.tsx +++ b/apps/app/src/components/editor/DeleteDialog.tsx @@ -1,4 +1,4 @@ -import { useMemo, useState } from 'react' +import { useEffect, useMemo, useState } from 'react' import type { AnatomicalSheetDocument, Dependent, @@ -6,6 +6,7 @@ import type { } from '@faunosteo/core-wasm/types' import Button from '@/components/ui/Button' import Dialog from '@/components/ui/Dialog' +import { useEditorReadOnly } from '@/editor/editingContext' import { dependentsOf } from '@/editor/core/editOps' import { refusalMessage } from '@/editor/refusal' import { elementLabel, useSheetDocument } from '@/editor/sheetDocument' @@ -34,6 +35,11 @@ export interface DeleteDialogProps { export default function DeleteDialog({ target, onClose }: DeleteDialogProps) { const t = useT() const doc = useSheetDocument() + const readOnly = useEditorReadOnly() + // A dialog that confirms an edit never stays open once the editor is read-only. + useEffect(() => { + if (readOnly && target !== null) onClose() + }, [readOnly, target, onClose]) const heading = t('editor.delete.title').replace( '{name}', target?.label ?? '', diff --git a/apps/app/src/components/editor/EditOnly.tsx b/apps/app/src/components/editor/EditOnly.tsx new file mode 100644 index 00000000..b695b73c --- /dev/null +++ b/apps/app/src/components/editor/EditOnly.tsx @@ -0,0 +1,20 @@ +import type { ReactNode } from 'react' +import { useEverEditable } from '@/editor/editingContext' +import KeptPlace from './KeptPlace' + +/** + * An edit-only part (a creation form, the annotation edit body): mounted at the first + * editable render, then kept and hidden while the editor is read-only, so a width change + * never drops what was typed in it. Its canvases detach while it is hidden. + */ +export default function EditOnly({ + readOnly, + children, +}: { + readOnly: boolean + children: ReactNode +}) { + const mounted = useEverEditable(readOnly) + if (!mounted) return null + return {children} +} diff --git a/apps/app/src/components/editor/EditorWorkspace.tsx b/apps/app/src/components/editor/EditorWorkspace.tsx index 3e7d9a85..3bdc5404 100644 --- a/apps/app/src/components/editor/EditorWorkspace.tsx +++ b/apps/app/src/components/editor/EditorWorkspace.tsx @@ -1,5 +1,7 @@ +import type { ReactNode } from 'react' import type { OfficialSheetAdmin } from '@faunosteo/supabase' import Notice from '@/components/ui/Notice' +import { ReadOnlyContext, ReferencesOpenContext } from '@/editor/editingContext' import { AnnounceContext, FocusConsumedContext, @@ -47,6 +49,25 @@ function SaveFailureNotice() { ) } +/** What dialogs and edit-only parts below need of the editor's state. */ +function EditingState({ + readOnly, + referencesOpen, + children, +}: { + readOnly: boolean + referencesOpen: boolean + children: ReactNode +}) { + return ( + + + {children} + + + ) +} + /** * The draft page once the version is ready: header, notices, tab row, the references * panel and the shown place. Owns the navigation state, so the first tab is known at @@ -64,49 +85,51 @@ export default function EditorWorkspace(props: Props) { ? nav.closeReferences() : nav.openReferences(null, opener) return ( -
- - - - - - - - - {state.referencesMounted && ( - - - - )} - - - -

- {nav.live} -

- -
+ +
+ + + + + + + + + {state.referencesMounted && ( + + + + )} + + + +

+ {nav.live} +

+ +
+
) } diff --git a/apps/app/src/components/editor/HeaderActions.tsx b/apps/app/src/components/editor/HeaderActions.tsx index c83b3104..9ba8b98a 100644 --- a/apps/app/src/components/editor/HeaderActions.tsx +++ b/apps/app/src/components/editor/HeaderActions.tsx @@ -1,6 +1,7 @@ -import { useState } from 'react' +import { useEffect, useState } from 'react' import Button from '@/components/ui/Button' import Icon from '@/components/ui/Icon' +import { useEditorReadOnly } from '@/editor/editingContext' import { loadFailureMessage } from '@/editor/messages' import { useEditorStore } from '@/editor/state/editorStore' import { REFERENCES_BUTTON_ID } from '@/editor/tabs' @@ -22,8 +23,11 @@ function DiscardButton({ onFailure }: Pick) { const t = useT() const unsaved = useEditorStore((s) => s.unsaved) const saving = useEditorStore((s) => s.saving) - const blocked = useEditorStore((s) => s.recovery !== 'none') + const readOnly = useEditorReadOnly() const [asking, setAsking] = useState(false) + useEffect(() => { + if (readOnly) setAsking(false) + }, [readOnly]) const discard = async () => { setAsking(false) const result = await useEditorStore @@ -40,7 +44,7 @@ function DiscardButton({ onFailure }: Pick) {