All files / utils gesture.ts

100% Statements 21/21
100% Branches 0/0
100% Functions 5/5
100% Lines 16/16

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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106        94x 94x       94x                                                                       94x 35x             94x 143x               94x 13x               94x 13x           94x 871x           94x                     94x               94x  
/**
 * @module Utils
 */
 
import * as MC from "@lisn/globals/minification-constants";
import * as MH from "@lisn/globals/minification-helpers";
 
import { Direction, GestureDevice, GestureIntent } from "@lisn/globals/types";
 
import { isValidStrList } from "@lisn/utils/validation";
 
/**
 * `deltaX` and `deltaY` together specify the precise direction in the XY plane
 * of the move if relevant (i.e. other than zoom intent). The direction
 * specifies the effective X ("left"/"right"), Y ("up"/"down") or Z ("in"/"out")
 * direction, or "none"/"ambiguous".
 *
 * `deltaZ` specifies relative zoom in or out for zoom intents.
 * For zoom in, deltaZ is always > 1, and for zoom out it is < 1.
 * For non-zoom events it is 1.
 *
 * For zoom intents, `direction` would be either in, out or none.
 * For other intents, it would be up, down, left, right, none or ambiguous.
 *
 * For important notes on the delta values see
 * - {@link Utils.getKeyGestureFragment | getKeyGestureFragment}
 * - {@link Utils.getTouchGestureFragment | getTouchGestureFragment}
 * - {@link Utils.getWheelGestureFragment | getWheelGestureFragment}
 *
 * @category Gestures
 */
export type GestureFragment = {
  device: GestureDevice;
  direction: Direction;
  intent: GestureIntent;
  deltaX: number;
  deltaY: number;
  deltaZ: number;
};
 
/**
 * Returns true if the given string is a valid gesture device.
 *
 * @category Validation
 */
export const isValidInputDevice = (device: string): device is GestureDevice =>
  MH.includes(DEVICES, device);
 
/**
 * Returns true if the given string is a valid gesture intent.
 *
 * @category Validation
 */
export const isValidIntent = (intent: string): intent is GestureIntent =>
  MH.includes(INTENTS, intent);
 
/**
 * Returns true if the given string or array is a list of valid gesture
 * devices.
 *
 * @category Validation
 */
export const isValidInputDeviceList = (devices: string | string[]) =>
  isValidStrList(devices, isValidInputDevice, false);
 
/**
 * Returns true if the given string or array is a list of valid gesture
 * intents.
 *
 * @category Validation
 */
export const isValidIntentList = (intents: string | string[]) =>
  isValidStrList(intents, isValidIntent, false);
 
/**
 * @ignore
 * @internal
 */
export const addDeltaZ = (current: number, increment: number) =>
  MH.max(MIN_DELTA_Z, current * increment);
 
/**
 * @ignore
 * @internal
 */
export const DEVICES: GestureDevice[] = [
  MC.S_KEY,
  MC.S_POINTER,
  MC.S_TOUCH,
  MC.S_WHEEL,
] as const;
 
/**
 * @ignore
 * @internal
 */
export const INTENTS: GestureIntent[] = [
  MC.S_SCROLL,
  MC.S_ZOOM,
  MC.S_DRAG,
  MC.S_UNKNOWN,
] as const;
 
// Do not allow zooming out more than this value
const MIN_DELTA_Z = 0.1;