fix(app): R1 Escape layers with the page focused, twin keyboard framing, « Plus » field guard
This commit is contained in:
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
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
isTextField,
|
||||
visibleElement,
|
||||
} from '@/editor/focusRequest'
|
||||
import { ReferencesOpenContext } from '@/editor/editingContext'
|
||||
import { CANVAS_BOX } from '@/editor/useEditorNavigation'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
@@ -54,7 +55,7 @@ export function useDeselect(id: string | null, clear: () => void) {
|
||||
}
|
||||
|
||||
/** Whether a key event happened in the editor workspace (or with nothing focused). */
|
||||
function inWorkspace(target: EventTarget | null): boolean {
|
||||
export function inWorkspace(target: EventTarget | null): boolean {
|
||||
if (target === document.body) return true
|
||||
return (
|
||||
target instanceof Element &&
|
||||
@@ -79,6 +80,9 @@ export function useEscapeDeselect(
|
||||
if (event.key !== 'Escape' || event.defaultPrevented) return
|
||||
if (event.isComposing || isTextField(event.target)) return
|
||||
if (!inWorkspace(event.target)) return
|
||||
// A keydown aimed at body never reaches the React root: the layers above (a modal,
|
||||
// the phone drawer) are checked here.
|
||||
if (document.querySelector('dialog[open]')) return
|
||||
if (handler.current(event)) event.preventDefault()
|
||||
}
|
||||
document.addEventListener('keydown', onKey)
|
||||
@@ -98,7 +102,8 @@ export function useSelectionEscape(
|
||||
) {
|
||||
const asideRef = useRef<HTMLElement>(null)
|
||||
const deselect = useDeselect(id, clear)
|
||||
useEscapeDeselect(active, (event) =>
|
||||
const referencesOpen = useContext(ReferencesOpenContext)
|
||||
useEscapeDeselect(active && !referencesOpen, (event) =>
|
||||
deselect.byKey(asideRef.current?.contains(event.target as Node) ?? false),
|
||||
)
|
||||
return { asideRef, byDrawing: deselect.byDrawing }
|
||||
|
||||
@@ -102,14 +102,27 @@ export function useRowFocus(focus: FocusRequest | undefined): void {
|
||||
)
|
||||
}
|
||||
|
||||
/** Input types that take no typed text: Escape there is not the field's. */
|
||||
const NON_TEXT_INPUTS = new Set([
|
||||
'checkbox',
|
||||
'radio',
|
||||
'range',
|
||||
'button',
|
||||
'submit',
|
||||
'reset',
|
||||
'color',
|
||||
'file',
|
||||
'image',
|
||||
])
|
||||
|
||||
/** A field that takes typed text (or a choice): Escape there belongs to the field. */
|
||||
export function isTextField(target: EventTarget | null): boolean {
|
||||
if (target instanceof HTMLInputElement) return !NON_TEXT_INPUTS.has(target.type)
|
||||
return (
|
||||
target instanceof HTMLInputElement ||
|
||||
target instanceof HTMLTextAreaElement ||
|
||||
target instanceof HTMLSelectElement ||
|
||||
(target instanceof HTMLElement &&
|
||||
target.closest('[contenteditable]') !== null)
|
||||
target.closest('[contenteditable="true"]') !== null)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user