diff --git a/apps/app/e2e/shell/editor-journey.spec.ts b/apps/app/e2e/shell/editor-journey.spec.ts index a3876206..1cb15739 100644 --- a/apps/app/e2e/shell/editor-journey.spec.ts +++ b/apps/app/e2e/shell/editor-journey.spec.ts @@ -15,6 +15,7 @@ import { INKSCAPE_EXPORT, openDraft, openSection, + place, saveDraft, secondSession, selectTab, @@ -308,9 +309,10 @@ test.describe('editor journey (owner trial steps)', () => { // 4.5: a rename left unsaved survives a reload through the recovery copy. await selectTab(page, dict, 'general') - const name = page.getByLabel(dict['editor.general.name_fr'], { - exact: true, - }) + const name = place(page, 'general').getByLabel( + dict['editor.general.name_fr'], + { exact: true }, + ) await commitText(name, `Chien révisé ${tag}`) await reloadAndRestore(page, sheetId) await saveDraft(page, dict, 2) diff --git a/apps/app/e2e/shell/editor-shell.spec.ts b/apps/app/e2e/shell/editor-shell.spec.ts new file mode 100644 index 00000000..be9ff113 --- /dev/null +++ b/apps/app/e2e/shell/editor-shell.spec.ts @@ -0,0 +1,141 @@ +import { fr } from '@/i18n/fr' +import type { Page, TestInfo } from '@playwright/test' +import { expect, test } from '../support/browser' +import { dropLateReplays, routeToSandbox } from '../support/sandbox' +import { + backToDrawing, + closeReferences, + editorActor, + openEditor, + openReferences, + openSection, + place, + pointOf, + seedDraft, + selectTab, + shapeWith, + showDrawing, +} from '../support/editor' + +// V1-2.6 U1 B2a: tabs, Annotations sections and the references panel stay mounted. What +// was typed, the canvas zoom and the focus survive a change of place; focus lands on a +// named element, never on `body`. happy-dom does not refuse focus on hidden elements, so +// the focus rules are checked here with `document.activeElement`. + +const dict = fr + +const isPhone = (info: TestInfo) => info.project.name === 'chromium-375' + +async function open(page: Page) { + const actor = await editorActor('fr') + const free = { + ...pointOf('anno-free', 'main', { cx: 120, cy: 120, r: 6 }, 'Point libre'), + finding_zone_id: null, + } + const seed = await seedDraft(actor, { shape: shapeWith(free) }) + await openEditor(page, dict, actor, seed.sheetId) + return seed +} + +const active = (page: Page) => page.locator(':focus') +const layerTransform = (page: Page) => + page + .getByTestId('annotation-canvas') + .evaluate((box) => (box.firstElementChild as HTMLElement).style.transform) + +test.describe('editor shell, kept places (B2a)', () => { + 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('the category form text and the zoom survive tab and section changes', async ({ + page, + }) => { + await open(page) + await openSection(page, dict, 'categories') + await page.locator('#category-new-fr').fill('Brouillon') + await selectTab(page, dict, 'general') + await expect(place(page, 'annotations.categories')).toHaveCount(0) + await selectTab(page, dict, 'annotations') + await expect(page.locator('#category-new-fr')).toHaveValue('Brouillon') + + await backToDrawing(page, dict) + await page + .getByRole('button', { name: dict['catalogue.zoom.in'], exact: true }) + .click() + const zoomed = await layerTransform(page) + expect(zoomed).not.toContain('scale(1)') + await selectTab(page, dict, 'findings') + await showDrawing(page, dict) + expect(await layerTransform(page)).toBe(zoomed) + }) + + test('the references panel keeps its form text and no id repeats', async ({ + page, + }) => { + await open(page) + await openReferences(page, dict) + await page.locator('#reference-new-citation').fill('Smith 1999') + await closeReferences(page, dict) + await openReferences(page, dict) + await expect(page.locator('#reference-new-citation')).toHaveValue( + 'Smith 1999', + ) + for (const tab of ['general', 'findings', 'verification']) { + await selectTab(page, dict, tab) + } + for (const section of ['views', 'categories', 'regions'] as const) { + await openSection(page, dict, section) + } + const repeated = await page.evaluate(() => { + const ids = [...document.querySelectorAll('[id]')].map((e) => e.id) + return ids.filter((id, i) => ids.indexOf(id) !== i) + }) + expect(repeated).toEqual([]) + }) + + test('focus lands on a named element after a tab click, « Retour au dessin » and « Aller à »', async ({ + page, + }) => { + await open(page) + await selectTab(page, dict, 'findings') + await expect(active(page)).toHaveAttribute('id', 'editor-tab-findings') + + await openSection(page, dict, 'categories') + await backToDrawing(page, dict) + await expect(active(page)).toHaveAttribute( + 'data-section-entry', + 'categories', + ) + + await selectTab(page, dict, 'verification') + await page + .getByRole('button', { name: 'Aller à Point libre' }) + .first() + .click() + await expect(place(page, 'annotations.drawing')).toBeVisible() + await expect(active(page)).toHaveAttribute( + 'data-properties-heading', + 'true', + ) + }) + + test('arrowing back onto a tab keeps the focus on the tab', async ({ + page, + }) => { + await open(page) + await selectTab(page, dict, 'annotations') + await page + .getByRole('tab', { name: dict['editor.tab.annotations'] }) + .focus() + await page.keyboard.press('ArrowRight') + await expect(active(page)).toHaveAttribute('id', 'editor-tab-findings') + await page.keyboard.press('ArrowLeft') + await expect(active(page)).toHaveAttribute('id', 'editor-tab-annotations') + }) +}) diff --git a/apps/app/src/components/catalogue/CatalogueCanvas.tsx b/apps/app/src/components/catalogue/CatalogueCanvas.tsx index ac857440..f156d0b6 100644 --- a/apps/app/src/components/catalogue/CatalogueCanvas.tsx +++ b/apps/app/src/components/catalogue/CatalogueCanvas.tsx @@ -1,4 +1,5 @@ import { useCallback, useEffect, useRef, useState } from 'react' +import { usePlaceVisible } from '@/components/editor/KeptPlace' import Button from '@/components/ui/Button' import Notice from '@/components/ui/Notice' import { coreRuntime, useCoreStopped } from '@/core/runtime' @@ -60,6 +61,7 @@ export default function CatalogueCanvas({ const [attempt, setAttempt] = useState(0) const [failure, setFailure] = useState(null) const stopped = useCoreStopped() + const visible = usePlaceVisible() const { framing, zoomIn, zoomOut, reset, swallowClick, handlers } = useFraming( viewport, viewId, @@ -74,17 +76,19 @@ export default function CatalogueCanvas({ useEffect(() => { const element = host.current if (!element) return + // One composed copy of a view in the page (kept places): the ids repeat otherwise. + if (!visible) return element.replaceChildren() const build = buildView(preview, viewId) setFailure(build.ok ? null : build.failure) element.replaceChildren(...(build.ok ? [build.svg] : [])) - }, [preview, viewId, attempt]) + }, [preview, viewId, attempt, visible]) useEffect(() => { host.current?.querySelectorAll(ANNOTATED).forEach((el) => { if (el.getAttribute('data-anno-id') === selectedId) el.setAttribute('data-state', 'current') else el.removeAttribute('data-state') }) - }, [selectedId, preview, viewId, attempt]) + }, [selectedId, preview, viewId, attempt, visible]) useEffect(() => { const element = host.current diff --git a/apps/app/src/components/editor/EditorTabBody.tsx b/apps/app/src/components/editor/EditorTabBody.tsx index 008cdd8f..79a96706 100644 --- a/apps/app/src/components/editor/EditorTabBody.tsx +++ b/apps/app/src/components/editor/EditorTabBody.tsx @@ -1,4 +1,5 @@ import type { ComponentType } from 'react' +import { ownedBy } from '@/editor/focusRequest' import type { AnnotationSection } from '@/editor/navigation' import { panelDomId, @@ -6,6 +7,7 @@ import { type EditorTabId, } from '@/editor/tabs' import { useT } from '@/i18n/useT' +import KeptPlace from './KeptPlace' import AnnotationsTab from './tabs/AnnotationsTab' import FindingsTab from './tabs/FindingsTab' import GeneralTab from './tabs/GeneralTab' @@ -23,31 +25,55 @@ const BODIES: Record< /** What only the Annotations tab needs beyond the common place props. */ export interface AnnotationNav { section: AnnotationSection + /** The sections shown at least once: each stays mounted. */ + visited: readonly AnnotationSection[] onOpenSection: (section: AnnotationSection, opener: HTMLElement) => void onBack: () => void } interface Props extends EditorPlaceProps { tab: EditorTabId + /** The tabs shown at least once: each stays mounted, hidden while another is shown. */ + visited: readonly EditorTabId[] annotations: AnnotationNav } -/** The panel of the active tab; a place unmounts when left (kept alive in Lot B2). */ -export default function EditorTabBody({ tab, annotations, ...props }: Props) { +function TabPanel({ + id, + active, + annotations, + ...props +}: Omit & { id: EditorTabId; active: boolean }) { const t = useT() - const Body = tab === 'annotations' ? null : BODIES[tab] + const Body = id === 'annotations' ? null : BODIES[id] return ( -
{Body ? ( - + ) : ( )} -
+ + ) +} + +/** + * The panels of the tabs shown so far: a tab mounts at its first visit and stays mounted, + * hidden while another is shown, so nothing typed or selected is lost on a tab change. + * A focus request reaches only the place its target names. + */ +export default function EditorTabBody({ tab, visited, ...props }: Props) { + return ( + <> + {visited.map((id) => ( + + ))} + ) } diff --git a/apps/app/src/components/editor/EditorWorkspace.tsx b/apps/app/src/components/editor/EditorWorkspace.tsx index 9974be92..3e7d9a85 100644 --- a/apps/app/src/components/editor/EditorWorkspace.tsx +++ b/apps/app/src/components/editor/EditorWorkspace.tsx @@ -4,11 +4,9 @@ import { AnnounceContext, FocusConsumedContext, ownedBy, - type PlaceName, } from '@/editor/focusRequest' import { saveFailureMessage } from '@/editor/messages' import type { EditorMode } from '@/editor/mode' -import type { NavigationState } from '@/editor/navigation' import { useEditorStore } from '@/editor/state/editorStore' import { initialTab } from '@/editor/tabs' import { useEditorNavigation } from '@/editor/useEditorNavigation' @@ -19,6 +17,7 @@ import CopyIssueNotice from './CopyIssueNotice' import DraftHeader from './DraftHeader' import EditorTabBody from './EditorTabBody' import EditorTabs from './EditorTabs' +import KeptPlace from './KeptPlace' import ModeNotice from './ModeNotice' import RecoveryNotice from './RecoveryNotice' import ReferencesPanel from './references/ReferencesPanel' @@ -48,11 +47,6 @@ function SaveFailureNotice() { ) } -/** The `data-place` the shown tab and section name. */ -function shownPlace({ tab, section }: NavigationState): PlaceName { - return tab === 'annotations' ? `annotations.${section}` : tab -} - /** * 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 @@ -85,21 +79,24 @@ export default function EditorWorkspace(props: Props) { {state.referencesMounted && ( - + + + )} { + /** The `data-place` name; omitted for a wrapper that only nests places (tab panel). */ + name?: string + visible: boolean + children: ReactNode +} + +/** + * A place that stays mounted while hidden: `hidden` leaves the accessibility tree and keeps + * every component state (Tailwind's preflight makes `[hidden]` `display: none`). Nested + * places compose: a place is visible only when all the places around it are. + */ +export default function KeptPlace({ name, visible, children, ...rest }: Props) { + const parent = usePlaceVisible() + return ( + + + + ) +} diff --git a/apps/app/src/components/editor/annotations/sections/AnnotationSection.tsx b/apps/app/src/components/editor/annotations/sections/AnnotationSection.tsx index 6e304d20..4bf85d9a 100644 --- a/apps/app/src/components/editor/annotations/sections/AnnotationSection.tsx +++ b/apps/app/src/components/editor/annotations/sections/AnnotationSection.tsx @@ -30,7 +30,6 @@ export default function AnnotationSection({ const headingId = `annotation-section-${section}-title` return (
diff --git a/apps/app/src/components/editor/references/ReferencesPanel.tsx b/apps/app/src/components/editor/references/ReferencesPanel.tsx index 7fdbb56d..c912f012 100644 --- a/apps/app/src/components/editor/references/ReferencesPanel.tsx +++ b/apps/app/src/components/editor/references/ReferencesPanel.tsx @@ -11,19 +11,17 @@ import type { EditorPlaceProps } from '@/editor/tabs' import { useT } from '@/i18n/useT' interface Props extends Pick { - open: boolean onClose: () => void } /** * The references side panel, minimal until Lot D: the sheet's bibliography, cited by id * from the findings, opened by the header button. Not a dialog: it keeps the page usable. - * Mounted at the first opening and kept (hidden) afterwards. + * The workspace mounts it at the first opening and keeps it (hidden) afterwards. */ export default function ReferencesPanel({ readOnly, focus, - open, onClose, }: Props) { const t = useT() @@ -39,10 +37,8 @@ export default function ReferencesPanel({ return (