All files / core/blong-browser/src/feedback guard.ts

80.55% Statements 29/36
50% Branches 7/14
71.42% Functions 5/7
92.59% Lines 25/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78                                                    9x                 2x   2x 2x 2x     2x     2x 1x 1x     2x 2x 1x 1x 1x 1x     2x             2x 2x 2x 2x 2x   2x 6x 2x 2x      
/**
 * guard.ts — how the mode freezes the UI (`@@T-465`, `@@D-659`).
 *
 * One capture-phase listener set on the document, installed only while the mode
 * is active. It stops the events PrimeReact uses to close an overlay or run a menu
 * command from ever reaching it, and `preventDefault` keeps focus where it is so a
 * `Dropdown` does not blur either. Nothing in the component tree has to know the
 * mode exists.
 *
 * Events that start inside the mode's own chrome (`[data-feedback-ui]`) pass
 * through untouched, so typing in a callout and pressing its buttons work. Escape
 * is swallowed everywhere else — a menu closes on it, and a menu that closes is
 * exactly what the mode must prevent.
 *
 * A hover-only affordance (a tooltip) cannot survive the pointer leaving the
 * element, so it is not frozen; the element's `title` attribute is copied into the
 * item instead.
 */
 
import {closestFeedbackTarget, isFeedbackUi} from './ids.js';
 
export interface IFeedbackGuardOptions {
    /** Called when a click lands on an addressable element. */
    onTargetClick: (element: HTMLElement, event: MouseEvent) => void;
}
 
const POINTER_EVENTS = ['pointerdown', 'mousedown', 'touchstart'] as const;
 
/**
 * Install the guard; the returned function removes it.
 *
 * Must be called only while the mode is active — installing it permanently would
 * stop the whole application from responding to clicks.
 */
export function installFeedbackGuard({onTargetClick}: IFeedbackGuardOptions): () => void {
    Iif (typeof document === 'undefined') return () => undefined;
 
    const stop = (event: Event): void => {
        event.preventDefault();
        event.stopPropagation();
        // PrimeReact attaches some listeners to the same node we do; without this
        // a sibling capture listener still sees the event.
        Eif (typeof event.stopImmediatePropagation === 'function') event.stopImmediatePropagation();
    };
 
    const onPointer = (event: Event): void => {
        Iif (isFeedbackUi(event.target)) return;
        Eif (closestFeedbackTarget(event.target)) stop(event);
    };
 
    const onClick = (event: MouseEvent): void => {
        if (isFeedbackUi(event.target)) return;
        const element = closestFeedbackTarget(event.target);
        Iif (!element) return;
        stop(event);
        onTargetClick(element, event);
    };
 
    const onKeyDown = (event: KeyboardEvent): void => {
        if (isFeedbackUi(event.target)) return;
        // Every key is swallowed outside the chrome: a shortcut that opens a dialog,
        // a table that pages on an arrow, a menu that closes on Escape.
        stop(event);
    };
 
    document.addEventListener('pointerdown', onPointer, true);
    document.addEventListener('mousedown', onPointer, true);
    document.addEventListener('touchstart', onPointer, true);
    document.addEventListener('click', onClick, true);
    document.addEventListener('keydown', onKeyDown, true);
 
    return () => {
        for (const name of POINTER_EVENTS) document.removeEventListener(name, onPointer, true);
        document.removeEventListener('click', onClick, true);
        document.removeEventListener('keydown', onKeyDown, true);
    };
}