fix(editor): message line lifecycle, own row, sound no-shift check
Task 3 review, fix round 1. - The gesture message closes for good on any document or history change (store subscription): undo then redo no longer revives it. - Hover and focus hold only the message they were taken on: after « Défaire » the next message expires. - An accepted drop clears the strip's refusal hint again. - Controller ruling: the message has its own fixed row under the add buttons, column width; a refused gesture's full text also shows in the strip notice (red, aria-hidden copy of the row's alert). - expectNoShift: native requestAnimationFrame kept before page.clock (keepNativeFrames), every part box asserted non-null, message row measured; the e2e checks the message is not truncated. No assertion removed; the refused-drop unit test gains the strip copy.
This commit is contained in:
1 parent
1deb037fac
commit
7a45f1d885
9 files changed
+198
-49
No files matched your search
@@ -1,4 +1,10 @@
|
||||
import { useEffect, useState, type FocusEvent, type PointerEvent } from 'react'
|
||||
import {
|
||||
useCallback,
|
||||
useEffect,
|
||||
useState,
|
||||
type FocusEvent,
|
||||
type PointerEvent,
|
||||
} from 'react'
|
||||
import { useEditorStore } from '../state/editorStore'
|
||||
|
||||
/** A6/A14: how long a message stays (R-2) and how long a hint does (R-1). */
|
||||
@@ -26,34 +32,49 @@ function currentStamp(): Stamp {
|
||||
return { text: documentText, undo: undo.length, redo: redo.length }
|
||||
}
|
||||
|
||||
/** The shown message while the document and both history stacks are those it was shown on. */
|
||||
function useLive(message: GestureMessage | null): GestureMessage | null {
|
||||
const text = useEditorStore((s) => s.documentText)
|
||||
const undo = useEditorStore((s) => s.undo.length)
|
||||
const redo = useEditorStore((s) => s.redo.length)
|
||||
if (!message) return null
|
||||
const { stamp } = message
|
||||
const same = stamp.text === text && stamp.undo === undo && stamp.redo === redo
|
||||
return same ? message : null
|
||||
const sameStamp = (a: Stamp, b: Stamp) =>
|
||||
a.text === b.text && a.undo === b.undo && a.redo === b.redo
|
||||
|
||||
/**
|
||||
* Any change of the document or of either history stack closes the shown message for
|
||||
* good: an undo then a redo restores the same stamp, which must not bring it back (R-2).
|
||||
*/
|
||||
function useCloseOnChange(close: (now: Stamp) => void): void {
|
||||
useEffect(
|
||||
() =>
|
||||
useEditorStore.subscribe((state, before) => {
|
||||
const changed =
|
||||
state.documentText !== before.documentText ||
|
||||
state.undo.length !== before.undo.length ||
|
||||
state.redo.length !== before.redo.length
|
||||
if (changed) close(currentStamp())
|
||||
}),
|
||||
[close],
|
||||
)
|
||||
}
|
||||
|
||||
/** Hover with a mouse and focus inside hold the message; a touch never does (R-2). */
|
||||
function useHold() {
|
||||
const [hovered, setHovered] = useState(false)
|
||||
const [focused, setFocused] = useState(false)
|
||||
/**
|
||||
* Hover with a mouse and focus inside hold the message; a touch never does (R-2). The hold
|
||||
* belongs to the message it was taken on: a message that closes (« Défaire » pressed, the
|
||||
* pill unmounted without pointerleave or blur) leaves no hold on the next one.
|
||||
*/
|
||||
function useHold(message: GestureMessage | null) {
|
||||
const [hovered, setHovered] = useState<GestureMessage | null>(null)
|
||||
const [focused, setFocused] = useState<GestureMessage | null>(null)
|
||||
const handlers = {
|
||||
onPointerEnter: (event: PointerEvent) => {
|
||||
if (event.pointerType === 'mouse') setHovered(true)
|
||||
if (event.pointerType === 'mouse') setHovered(message)
|
||||
},
|
||||
onPointerLeave: () => setHovered(false),
|
||||
onFocus: () => setFocused(true),
|
||||
onPointerLeave: () => setHovered(null),
|
||||
onFocus: () => setFocused(message),
|
||||
onBlur: (event: FocusEvent<HTMLElement>) => {
|
||||
if (!event.currentTarget.contains(event.relatedTarget as Node | null)) {
|
||||
setFocused(false)
|
||||
setFocused(null)
|
||||
}
|
||||
},
|
||||
}
|
||||
return { held: hovered || focused, handlers }
|
||||
const held = message !== null && (hovered === message || focused === message)
|
||||
return { held, handlers }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,9 +83,17 @@ function useHold() {
|
||||
* mouse hovers it or the focus is inside; any commit, undo or redo closes it.
|
||||
*/
|
||||
export function useGestureMessage() {
|
||||
const [shown, setShown] = useState<GestureMessage | null>(null)
|
||||
const message = useLive(shown)
|
||||
const { held, handlers } = useHold()
|
||||
const [message, setShown] = useState<GestureMessage | null>(null)
|
||||
const { held, handlers } = useHold(message)
|
||||
// A message shown after its own commit carries that commit's stamp: it stays.
|
||||
const close = useCallback(
|
||||
(now: Stamp) =>
|
||||
setShown((shown) =>
|
||||
shown && sameStamp(shown.stamp, now) ? shown : null,
|
||||
),
|
||||
[],
|
||||
)
|
||||
useCloseOnChange(close)
|
||||
useEffect(() => {
|
||||
if (!message || held) return
|
||||
const timer = setTimeout(
|
||||
|
||||
@@ -95,12 +95,14 @@ function tracingActions(input: PlacementInput) {
|
||||
}
|
||||
|
||||
/** A drop commits one update; the line under the drawing says it, or the core's refusal. */
|
||||
function dropAction({ doc, context, message }: PlacementInput) {
|
||||
function dropAction({ doc, context, message, dispatch }: PlacementInput) {
|
||||
return (id: string, geometry: Geometry) => {
|
||||
const annotation = doc.annotations.find((a) => a.id === id)
|
||||
if (!annotation) return
|
||||
const result = updateGeometry(annotation, geometry, context)
|
||||
if (result.ok) {
|
||||
// A kept move ends any refusal the strip still shows.
|
||||
dispatch({ type: 'hint', hint: null })
|
||||
const text = context.t(
|
||||
`editor.annotations.message.moved.${annotation.kind}`,
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user