lisn.js
Version:
Simply handle user gestures and actions. Includes widgets.
1 lines • 8.82 kB
Source Map (JSON)
{"version":3,"file":"gesture-wheel.cjs","names":["MC","_interopRequireWildcard","require","MH","_directions","_math","_normalizeWheel","_gesture","e","t","WeakMap","r","n","__esModule","o","i","f","__proto__","default","has","get","set","hasOwnProperty","call","Object","defineProperty","getOwnPropertyDescriptor","getWheelGestureFragment","events","options","isIterableObject","direction","S_NONE","intent","deltaX","deltaY","deltaZ","event","isWheelEvent","type","S_WHEEL","data","normalizeWheel","thisIntent","S_SCROLL","thisDeltaX","pixelX","thisDeltaY","pixelY","thisDeltaZ","maxDelta","havingMaxAbs","ctrlKey","percentage","abs","S_ZOOM","shiftKey","addDeltaZ","S_UNKNOWN","S_AMBIGUOUS","S_IN","S_OUT","getVectorDirection","angleDiffThreshold","device","exports"],"sources":["../../../src/ts/utils/gesture-wheel.ts"],"sourcesContent":["/**\n * @module Utils\n */\n\nimport * as MC from \"@lisn/globals/minification-constants\";\nimport * as MH from \"@lisn/globals/minification-helpers\";\n\nimport { Direction, GestureIntent } from \"@lisn/globals/types\";\n\nimport { getVectorDirection } from \"@lisn/utils/directions\";\n\nimport { havingMaxAbs } from \"@lisn/utils/math\";\nimport { normalizeWheel } from \"@lisn/utils/normalize-wheel\";\n\nimport { GestureFragment, addDeltaZ } from \"@lisn/utils/gesture\";\n\n/**\n * Returns a {@link GestureFragment} for the given events. Only \"wheel\" events\n * will be considered.\n *\n * If there are no \"wheel\" events in the given list of events, returns `false`.\n *\n * The deltas of all events are summed together before determining final delta\n * and direction.\n *\n * If the events are of conflicting types, i.e. some scroll, some zoom, then\n * the intent will be \"unknown\" and the direction will be \"ambiguous\".\n *\n * If the deltas sum up to 0, the direction will be \"none\".\n *\n * **IMPORTANT NOTES ON THE DELTA VALUES**\n *\n * For wheel gestures the deltas are _highly_ unreliable, especially when\n * zooming (Control + wheel or pinching trackpad). You should not assume they\n * correspond to the would-be scroll or zoom amount that the browser would do.\n * But they can be used to determine relative amounts for animating, etc.\n *\n * If the browser reports the delta values of a WheelEvent to be in mode \"line\",\n * then a configurable fixed value is used\n * ({@link Settings.settings.deltaLineHeight | settings.deltaLineHeight}).\n *\n * If the browser reports the delta values of a WheelEvent to be in mode \"page\",\n * then a configurable fixed value is used\n * ({@link Settings.settings.deltaPageWidth | settings.deltaPageWidth} and\n * ({@link Settings.settings.deltaPageHeight | settings.deltaPageHeight}).\n *\n * For zoom intents `deltaZ` is based on what the browser reports as the\n * `deltaY`, which in most browsers roughly corresponds to a percentage zoom\n * factor.\n *\n * @param [options.angleDiffThreshold] See {@link getVectorDirection}.\n * Default is 5.\n *\n * @returns `false` if there are no \"wheel\" events in the list, otherwise a\n * {@link GestureFragment}.\n *\n * @category Gestures\n */\nexport const getWheelGestureFragment = (\n events: Event | readonly Event[],\n options?: {\n angleDiffThreshold?: number;\n },\n): GestureFragment | null | false => {\n if (!MH.isIterableObject(events)) {\n events = [events];\n }\n\n let direction: Direction = MC.S_NONE;\n let intent: GestureIntent | null = null;\n let deltaX = 0,\n deltaY = 0,\n deltaZ = 1;\n\n for (const event of events) {\n if (!MH.isWheelEvent(event) || event.type !== MC.S_WHEEL) {\n continue;\n }\n\n const data = normalizeWheel(event);\n let thisIntent: GestureIntent = MC.S_SCROLL;\n let thisDeltaX = data.pixelX;\n let thisDeltaY = data.pixelY;\n let thisDeltaZ = 1;\n const maxDelta = havingMaxAbs(thisDeltaX, thisDeltaY);\n\n if (event.ctrlKey && !thisDeltaX) {\n // Browsers report negative deltaY for zoom in, so swap sign\n let percentage = -maxDelta;\n // If it's more than 50, assume it's a mouse wheel => delta is roughly\n // multiple of 10%. Otherwise a trackpad => delta is roughly multiple of 1%\n if (MH.abs(percentage) >= 50) {\n percentage /= 10;\n }\n\n thisDeltaZ = 1 + percentage / 100;\n\n thisDeltaX = thisDeltaY = 0;\n thisIntent = MC.S_ZOOM;\n } else if (event.shiftKey && !thisDeltaX) {\n // Holding Shift while turning wheel or swiping trackpad in vertically\n // results in sideways scroll.\n thisDeltaX = thisDeltaY;\n thisDeltaY = 0;\n }\n\n deltaX += thisDeltaX;\n deltaY += thisDeltaY;\n deltaZ = addDeltaZ(deltaZ, thisDeltaZ);\n\n if (!thisIntent) {\n // not a relevant key\n } else if (!intent) {\n intent = thisIntent;\n } else if (intent !== thisIntent) {\n // mixture of zoom and scroll\n intent = MC.S_UNKNOWN;\n }\n }\n\n if (!intent) {\n return false; // no relevant events\n } else if (intent === MC.S_UNKNOWN) {\n direction = MC.S_AMBIGUOUS;\n } else if (intent === MC.S_ZOOM) {\n direction = deltaZ > 1 ? MC.S_IN : deltaZ < 1 ? MC.S_OUT : MC.S_NONE;\n } else {\n direction = getVectorDirection(\n [deltaX, deltaY],\n options?.angleDiffThreshold,\n );\n }\n\n return direction === MC.S_NONE\n ? false\n : {\n device: MC.S_WHEEL,\n direction,\n intent,\n deltaX,\n deltaY,\n deltaZ,\n };\n};\n"],"mappings":";;;;;;AAIA,IAAAA,EAAA,GAAAC,uBAAA,CAAAC,OAAA;AACA,IAAAC,EAAA,GAAAF,uBAAA,CAAAC,OAAA;AAIA,IAAAE,WAAA,GAAAF,OAAA;AAEA,IAAAG,KAAA,GAAAH,OAAA;AACA,IAAAI,eAAA,GAAAJ,OAAA;AAEA,IAAAK,QAAA,GAAAL,OAAA;AAAiE,SAAAD,wBAAAO,CAAA,EAAAC,CAAA,6BAAAC,OAAA,MAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAT,uBAAA,YAAAA,CAAAO,CAAA,EAAAC,CAAA,SAAAA,CAAA,IAAAD,CAAA,IAAAA,CAAA,CAAAK,UAAA,SAAAL,CAAA,MAAAM,CAAA,EAAAC,CAAA,EAAAC,CAAA,KAAAC,SAAA,QAAAC,OAAA,EAAAV,CAAA,iBAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA,SAAAQ,CAAA,MAAAF,CAAA,GAAAL,CAAA,GAAAG,CAAA,GAAAD,CAAA,QAAAG,CAA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