All files / tools/src/eventDispatchers/shared getTouchCallbackWithMouseFallback.ts

0% Statements 0/11
0% Branches 0/6
0% Functions 0/4
0% Lines 0/10

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                                                                                                 
import { MouseBindings } from '../../enums';
 
/**
 * Synthesizes the mouse-detail fields a mouse callback may read but which
 * the normalized touch event details lack (additive only - existing touch
 * fields are never overwritten).
 */
function augmentTouchDetailForMouseCallback(detail) {
  detail.mouseButton ??= MouseBindings.Primary;
  detail.startPoints ??= detail.currentPoints;
  detail.lastPoints ??= detail.currentPoints;
  detail.deltaPoints ??= {
    page: [0, 0],
    client: [0, 0],
    canvas: [0, 0],
    world: [0, 0, 0],
  };
}
 
/**
 * Resolves the callback a touch dispatcher should invoke on a tool:
 * - the explicit touch callback when the tool implements it (always wins,
 *   regardless of supportedInteractionTypes), otherwise
 * - the mouse counterpart, but only when the tool declares 'Touch' in its
 *   supportedInteractionTypes.
 *
 * Returns undefined when the tool has neither (or the mouse counterpart is
 * not enabled for touch), so callers can skip the tool.
 */
export default function getTouchCallbackWithMouseFallback(
  tool,
  touchCallbackName: string,
  mouseCallbackName: string
): ((evt) => unknown) | undefined {
  if (typeof tool?.[touchCallbackName] === 'function') {
    return (evt) => tool[touchCallbackName](evt);
  }
 
  if (
    tool?.supportedInteractionTypes?.includes('Touch') &&
    typeof tool[mouseCallbackName] === 'function'
  ) {
    return (evt) => {
      augmentTouchDetailForMouseCallback(evt.detail);
      return tool[mouseCallbackName](evt);
    };
  }
}