feat(editor): keep edit-only forms across a width change
Edit-only parts (creation forms, submission note, crop group, annotation edit body) mount at the first editable render and stay mounted, hidden and detached from canvases, while read-only. Guards: the viewport hook commits the focused field before the tier crosses 1 024 px, the route releases focus left in a hidden part and treats a store out of step as read-only, delete and discard confirmations close when the editor turns read-only, a drag is dropped while the edit body is hidden. Read-only twins take an id prefix. R-24: no existing assertion rewritten; editor-no-loss.test.tsx and two editor-shell Chromium cases added.
This commit is contained in:
1 parent
9d89daed19
commit
c57af6441f
23 files changed
+883
-108
No files matched your search
@@ -0,0 +1,66 @@
|
||||
import { createContext, useContext, useState } from 'react'
|
||||
import { usePlaceVisible } from '@/lib/placeVisibility'
|
||||
|
||||
/** The editor cannot be edited now (route rule: mode, recovery, store not in step). */
|
||||
export const ReadOnlyContext = createContext(false)
|
||||
|
||||
/** The references panel is open: a place beside it is not where a modal may open. */
|
||||
export const ReferencesOpenContext = createContext(false)
|
||||
|
||||
export function useEditorReadOnly(): boolean {
|
||||
return useContext(ReadOnlyContext)
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a decision dialog of a kept part may open: only while editing, in a shown
|
||||
* place, with the references panel closed, so no native modal ever sits inside a hidden
|
||||
* subtree and focus is never lost to `body`.
|
||||
*/
|
||||
export function useDecisionsOpen(): boolean {
|
||||
const readOnly = useContext(ReadOnlyContext)
|
||||
const referencesOpen = useContext(ReferencesOpenContext)
|
||||
const visible = usePlaceVisible()
|
||||
return !readOnly && visible && !referencesOpen
|
||||
}
|
||||
|
||||
/**
|
||||
* True from the first render that is editable, and for good: an edit-only part mounts
|
||||
* then and is kept (hidden) afterwards; a page opened read-only never mounts it.
|
||||
*/
|
||||
export function useEverEditable(readOnly: boolean): boolean {
|
||||
const [ever, setEver] = useState(!readOnly)
|
||||
if (!readOnly && !ever) setEver(true)
|
||||
return ever || !readOnly
|
||||
}
|
||||
|
||||
/** What a decision dialog needs: the decision to show now, and whether one is held back. */
|
||||
export interface DecisionGate<T> {
|
||||
shown: T | null
|
||||
withheld: boolean
|
||||
resume: () => void
|
||||
}
|
||||
|
||||
/**
|
||||
* Gates a decision object (a cleaning candidate, a repair offer, a parent choice). A
|
||||
* decision that exists in any render where `open` is false is withheld for good: it never
|
||||
* reopens by itself, only through `resume` (the « Reprendre » button). A new decision
|
||||
* that arrives while `open` is true opens normally.
|
||||
*/
|
||||
export function useDecisionGate<T extends object>(
|
||||
decision: T | null,
|
||||
open: boolean,
|
||||
): DecisionGate<T> {
|
||||
const [held, setHeld] = useState<T | null>(null)
|
||||
const [resumed, setResumed] = useState<T | null>(null)
|
||||
if (decision !== null && !open) {
|
||||
if (held !== decision) setHeld(decision)
|
||||
if (resumed === decision) setResumed(null)
|
||||
}
|
||||
const withheld =
|
||||
decision !== null && held === decision && resumed !== decision
|
||||
return {
|
||||
shown: decision !== null && open && !withheld ? decision : null,
|
||||
withheld,
|
||||
resume: () => setResumed(decision),
|
||||
}
|
||||
}
|
||||
@@ -117,3 +117,13 @@ export function commitFocusedField(): void {
|
||||
active.blur()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The editor turned read-only: commits the text being typed, then releases any focus left
|
||||
* inside a hidden part, so no field is focused in a place that left the page.
|
||||
*/
|
||||
export function releaseEditFocus(): void {
|
||||
commitFocusedField()
|
||||
const active = document.activeElement
|
||||
if (active instanceof HTMLElement && active.closest('[hidden]')) active.blur()
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { commitFocusedField } from './focusRequest'
|
||||
import { MIN_EDIT_WIDTH } from './mode'
|
||||
|
||||
/** The tab lists of board 05: all, the four of 640 px and over, the two below. */
|
||||
@@ -11,11 +12,24 @@ function tierOf(width: number): TabTier {
|
||||
return width >= MEDIUM_WIDTH ? 'medium' : 'small'
|
||||
}
|
||||
|
||||
/** Whether the width moved across the 1 024 px edit threshold. */
|
||||
function crossesEditWidth(from: TabTier, to: TabTier): boolean {
|
||||
return (from === 'wide') !== (to === 'wide')
|
||||
}
|
||||
|
||||
/** The viewport's tier, kept in step with the window (resize and rotation). */
|
||||
export function useTabTier(): TabTier {
|
||||
const [tier, setTier] = useState<TabTier>(() => tierOf(window.innerWidth))
|
||||
const previous = useRef(tier)
|
||||
useEffect(() => {
|
||||
const update = () => setTier(tierOf(window.innerWidth))
|
||||
const update = () => {
|
||||
const next = tierOf(window.innerWidth)
|
||||
// The typed text is committed before the editor turns read-only (or back): a blur
|
||||
// after the hide would be too late, a hidden field keeps no focus event.
|
||||
if (crossesEditWidth(previous.current, next)) commitFocusedField()
|
||||
previous.current = next
|
||||
setTier(next)
|
||||
}
|
||||
update()
|
||||
window.addEventListener('resize', update)
|
||||
return () => window.removeEventListener('resize', update)
|
||||
|
||||
Reference in new issue
Block a user