feat(editor): finding dialog and citations side sheet in the history
Constats opens each element in one modal dialog (view, edit with a local draft, creation) driven by the editor view layer of the browser history. The citations panel becomes a modal side sheet stacked over it; its geometry, exit machinery and page inertness are removed. Back and Forward restore the layers, a dirty form asks first.
This commit is contained in:
1 parent
8e4f02620a
commit
18085cecf8
50 files changed
+2642
-1504
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { createContext, useContext, useEffect, useRef } from 'react'
|
||||
import type { ElementKey } from '@faunosteo/core-wasm/types'
|
||||
import { afterModalsClosed } from '@/components/ui/afterModalsClosed'
|
||||
import type {
|
||||
AnnotationSection,
|
||||
EditorTarget,
|
||||
@@ -82,21 +83,34 @@ export function visibleElement(
|
||||
)
|
||||
}
|
||||
|
||||
/** The visible row tagged with `key`. */
|
||||
function rowOf(key: ElementKey): HTMLElement | null {
|
||||
const wanted = JSON.stringify(key)
|
||||
return visibleElement(
|
||||
'[data-focus-key]',
|
||||
(candidate) => candidate.dataset.focusKey === wanted,
|
||||
)
|
||||
}
|
||||
|
||||
/** Scrolls to and focuses the visible row tagged with `key`; false unless the row now holds the focus. */
|
||||
export function focusRow(key: ElementKey): boolean {
|
||||
const wanted = JSON.stringify(key)
|
||||
const row = [
|
||||
...document.querySelectorAll<HTMLElement>('[data-focus-key]'),
|
||||
].find(
|
||||
(candidate) =>
|
||||
candidate.dataset.focusKey === wanted && !candidate.closest('[hidden]'),
|
||||
)
|
||||
const row = rowOf(key)
|
||||
if (!row) return false
|
||||
row.scrollIntoView?.({ block: 'center' })
|
||||
row.focus()
|
||||
return document.activeElement === row
|
||||
}
|
||||
|
||||
/**
|
||||
* The row is there: its focus waits for the dialogs a move closed (the page behind them
|
||||
* is inert until then). False when no visible row carries `key`.
|
||||
*/
|
||||
export function focusRowLater(key: ElementKey): boolean {
|
||||
if (!rowOf(key)) return false
|
||||
afterModalsClosed(() => void focusRow(key))
|
||||
return true
|
||||
}
|
||||
|
||||
/** Row-based places (Général, Vues, Catégories, Régions): focus the named row. */
|
||||
export function useRowFocus(focus: FocusRequest | undefined): void {
|
||||
useFocusRequest(focus, (target) =>
|
||||
@@ -132,13 +146,17 @@ export function isTextField(target: EventTarget | null): boolean {
|
||||
/**
|
||||
* Commits the text being typed before a place is left: blurs the focused field, so its
|
||||
* `TextField` commits on blur. Safari and iPadOS do not move the focus to a tapped button,
|
||||
* so the blur is forced rather than assumed. Answers whether a field was blurred.
|
||||
* so the blur is forced rather than assumed. A field of a transactional form
|
||||
* (`data-draft-form`) is a local draft nothing commits: it keeps its focus. Answers
|
||||
* whether a field was blurred.
|
||||
*/
|
||||
export function commitFocusedField(): boolean {
|
||||
const active = document.activeElement
|
||||
const isField = isTextField(active)
|
||||
if (isField) (active as HTMLElement).blur()
|
||||
return isField
|
||||
if (!isTextField(active)) return false
|
||||
const field = active as HTMLElement
|
||||
if (field.closest('[data-draft-form]')) return false
|
||||
field.blur()
|
||||
return true
|
||||
}
|
||||
|
||||
/** The tab shown, or « Plus » while the shown tab sits in the closed list. */
|
||||
@@ -154,6 +172,15 @@ export function focusActiveTab(options?: FocusOptions): void {
|
||||
activeTabButton()?.focus(options)
|
||||
}
|
||||
|
||||
/**
|
||||
* The element sits in a transactional form of an open dialog: the form keeps its draft
|
||||
* and its focus when the editor turns read-only, nothing moves it. (The citations
|
||||
* sheet's tiles still edit inline until the reference dialog: they are not drafts.)
|
||||
*/
|
||||
function inOpenDialog(element: HTMLElement): boolean {
|
||||
return element.closest('dialog[open] [data-draft-form]') !== null
|
||||
}
|
||||
|
||||
/** The element holding the focus, when it is something other than `body`. */
|
||||
export function heldElement(): HTMLElement | null {
|
||||
const active = document.activeElement
|
||||
@@ -178,7 +205,7 @@ export function refocusIfHidden(
|
||||
held: HTMLElement | null,
|
||||
home: HTMLElement | null = null,
|
||||
): void {
|
||||
if (!held) return
|
||||
if (!held || inOpenDialog(held)) return
|
||||
const gone = !held.isConnected || held.closest('[hidden]')
|
||||
if (!gone && document.activeElement !== held) held.focus()
|
||||
if (!gone && document.activeElement === held) return
|
||||
@@ -193,6 +220,7 @@ export function refocusIfHidden(
|
||||
*/
|
||||
export function releaseEditFocus(): void {
|
||||
const held = heldElement()
|
||||
if (held && inOpenDialog(held)) return
|
||||
commitFocusedField()
|
||||
refocusIfHidden(held)
|
||||
}
|
||||
Reference in new issue
Block a user