fix(app): R1 Escape layers with the page focused, twin keyboard framing, « Plus » field guard

This commit is contained in:
bchanot committed 2026-11-12 13:42:00 +01:00
1 parent 90a5e7aa35
commit 132756eecd
7 files changed
+130 -8

No files matched your search

@@ -72,8 +72,11 @@ export default function CatalogueCanvas({
)
const tap = useEmptyTap()
const { takeClick } = tap
const onEmptyRef = useRef(onEmpty)
onEmptyRef.current = onEmpty
const emptyHandlers = onEmpty
? {
...handlers,
onPointerDown: (e: PointerEvent) => {
handlers.onPointerDown(e)
tap.onPointerDown(e)
@@ -125,16 +128,16 @@ export default function CatalogueCanvas({
if (!element) return
const onClick = (event: Event) => {
if (swallowClick()) return
const answer = onEmpty ? takeClick(event.target) : 'normal'
const answer = onEmptyRef.current ? takeClick(event.target) : 'normal'
if (answer === 'swallow') return
if (answer === 'deselect') return onEmpty?.()
if (answer === 'deselect') return onEmptyRef.current?.()
const target = event.target instanceof Element ? event.target.closest(ANNOTATED) : null
const id = target?.getAttribute('data-anno-id')
if (id) onAnnotation(id)
}
element.addEventListener('click', onClick)
return () => element.removeEventListener('click', onClick)
}, [onAnnotation, onEmpty, swallowClick, takeClick])
}, [onAnnotation, swallowClick, takeClick])
return (
<div className="space-y-2">
@@ -1,4 +1,6 @@
import { useEffect, useRef, useState, type KeyboardEvent } from 'react'
import { inWorkspace } from '@/editor/annotations/deselect'
import { isTextField } from '@/editor/focusRequest'
import { useEditorStore } from '@/editor/state/editorStore'
import { usePlural } from '@/editor/plural'
import {
@@ -103,6 +105,7 @@ function useMoreEscape(open: boolean, close: () => void) {
const onKey = (event: globalThis.KeyboardEvent) => {
if (event.key !== 'Escape' || event.isComposing) return
if (document.querySelector('dialog[open]')) return
if (isTextField(event.target) || !inWorkspace(event.target)) return
event.preventDefault()
closeRef.current()
document.getElementById(MORE_BUTTON_ID)?.focus()
@@ -1,4 +1,4 @@
import type { ReactNode } from 'react'
import { useEffect, useRef, type ReactNode } from 'react'
import type { OfficialSheetAdmin } from '@faunosteo/supabase'
import Notice from '@/components/ui/Notice'
import { ReadOnlyContext, ReferencesOpenContext } from '@/editor/editingContext'
@@ -130,6 +130,28 @@ function Places({ nav, readOnly, reloadSheet }: PlacesProps) {
)
}
/**
* A keydown aimed at `body` (nothing focused) never reaches the workspace's own handler:
* while the references panel is open, Escape there closes it all the same, unless a
* modal dialog is open or the key was already handled.
*/
function useBodyEscape(open: boolean, close: () => void) {
const closeRef = useRef(close)
closeRef.current = close
useEffect(() => {
if (!open) return
const onKey = (event: KeyboardEvent) => {
if (event.key !== 'Escape' || event.defaultPrevented) return
if (event.isComposing || event.target !== document.body) return
if (document.querySelector('dialog[open]')) return
event.preventDefault()
closeRef.current()
}
document.addEventListener('keydown', onKey)
return () => document.removeEventListener('keydown', onKey)
}, [open])
}
/**
* 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
@@ -146,6 +168,7 @@ export default function EditorWorkspace(props: Props) {
state.references.open
? nav.closeReferences()
: nav.openReferences(null, opener)
useBodyEscape(state.references.open, nav.closeReferences)
return (
<EditingState readOnly={readOnly} referencesOpen={state.references.open}>
<div