All files / packages/tools/src/eventListeners/wheel normalizeWheel.ts

78.12% Statements 25/32
48.38% Branches 15/31
100% Functions 1/1
78.12% Lines 25/32

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  1x 1x 1x                   2x 2x 2x 2x     2x 2x   2x 2x   2x 2x   2x 2x     2x 2x   2x 2x   2x 2x     2x                         2x     2x       2x              
// Reasonable defaults
const PIXEL_STEP = 10;
const LINE_HEIGHT = 40;
const PAGE_HEIGHT = 800;
 
/**
 * Normalizes wheel events and provides properties that are more
 * consistent and helpful across different browsers
 *
 * @param event - the original mouse event
 * @returns a normalized eventDetail
 */
export default function normalizeWheel(event) {
  let spinX = 0,
    spinY = 0,
    pixelX = 0,
    pixelY = 0;
 
  // Legacy
  Eif ('detail' in event) {
    spinY = event.detail;
  }
  Eif ('wheelDelta' in event) {
    spinY = -event.wheelDelta / 120;
  }
  Eif ('wheelDeltaY' in event) {
    spinY = -event.wheelDeltaY / 120;
  }
  Eif ('wheelDeltaX' in event) {
    spinX = -event.wheelDeltaX / 120;
  }
 
  pixelX = spinX * PIXEL_STEP;
  pixelY = spinY * PIXEL_STEP;
 
  Eif ('deltaY' in event) {
    pixelY = event.deltaY;
  }
  Eif ('deltaX' in event) {
    pixelX = event.deltaX;
  }
 
  Iif ((pixelX || pixelY) && event.deltaMode) {
    if (event.deltaMode === 1) {
      // Delta in LINE units
      pixelX *= LINE_HEIGHT;
      pixelY *= LINE_HEIGHT;
    } else {
      // Delta in PAGE units
      pixelX *= PAGE_HEIGHT;
      pixelY *= PAGE_HEIGHT;
    }
  }
 
  // Fall-back if spin cannot be determined
  Iif (pixelX && !spinX) {
    spinX = pixelX < 1 ? -1 : 1;
  }
  Iif (pixelY && !spinY) {
    spinY = pixelY < 1 ? -1 : 1;
  }
 
  return {
    spinX,
    spinY,
    pixelX,
    pixelY,
  };
}