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
@@ -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