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

9.37% Statements 3/32
0% Branches 0/31
0% Functions 0/1
9.37% Lines 3/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  428x 428x 428x                                                                                                                                    
// 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
  if ('detail' in event) {
    spinY = event.detail;
  }
  if ('wheelDelta' in event) {
    spinY = -event.wheelDelta / 120;
  }
  if ('wheelDeltaY' in event) {
    spinY = -event.wheelDeltaY / 120;
  }
  if ('wheelDeltaX' in event) {
    spinX = -event.wheelDeltaX / 120;
  }
 
  pixelX = spinX * PIXEL_STEP;
  pixelY = spinY * PIXEL_STEP;
 
  if ('deltaY' in event) {
    pixelY = event.deltaY;
  }
  if ('deltaX' in event) {
    pixelX = event.deltaX;
  }
 
  if ((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
  if (pixelX && !spinX) {
    spinX = pixelX < 1 ? -1 : 1;
  }
  if (pixelY && !spinY) {
    spinY = pixelY < 1 ? -1 : 1;
  }
 
  return {
    spinX,
    spinY,
    pixelX,
    pixelY,
  };
}