MCPcopy Create free account
hub / github.com/ronreiter/interactive-tutorials / onScrollWheel

Function onScrollWheel

static/js/codemirror/codemirror.js:1790–1852  ·  view source on GitHub ↗
(cm, e)

Source from the content-addressed store, hash-verified

1788 else if (safari) wheelPixelsPerUnit = -1/3;
1789
1790 function onScrollWheel(cm, e) {
1791 var dx = e.wheelDeltaX, dy = e.wheelDeltaY;
1792 if (dx == null && e.detail && e.axis == e.HORIZONTAL_AXIS) dx = e.detail;
1793 if (dy == null && e.detail && e.axis == e.VERTICAL_AXIS) dy = e.detail;
1794 else if (dy == null) dy = e.wheelDelta;
1795
1796 // Webkit browsers on OS X abort momentum scrolls when the target
1797 // of the scroll event is removed from the scrollable element.
1798 // This hack (see related code in patchDisplay) makes sure the
1799 // element is kept around.
1800 if (dy && mac && webkit) {
1801 for (var cur = e.target; cur != scroll; cur = cur.parentNode) {
1802 if (cur.lineObj) {
1803 cm.display.currentWheelTarget = cur;
1804 break;
1805 }
1806 }
1807 }
1808
1809 var display = cm.display, scroll = display.scroller;
1810 // On some browsers, horizontal scrolling will cause redraws to
1811 // happen before the gutter has been realigned, causing it to
1812 // wriggle around in a most unseemly way. When we have an
1813 // estimated pixels/delta value, we just handle horizontal
1814 // scrolling entirely here. It'll be slightly off from native, but
1815 // better than glitching out.
1816 if (dx && !gecko && !opera && wheelPixelsPerUnit != null) {
1817 if (dy)
1818 setScrollTop(cm, Math.max(0, Math.min(scroll.scrollTop + dy * wheelPixelsPerUnit, scroll.scrollHeight - scroll.clientHeight)));
1819 setScrollLeft(cm, Math.max(0, Math.min(scroll.scrollLeft + dx * wheelPixelsPerUnit, scroll.scrollWidth - scroll.clientWidth)));
1820 e_preventDefault(e);
1821 display.wheelStartX = null; // Abort measurement, if in progress
1822 return;
1823 }
1824
1825 if (dy && wheelPixelsPerUnit != null) {
1826 var pixels = dy * wheelPixelsPerUnit;
1827 var top = cm.doc.scrollTop, bot = top + display.wrapper.clientHeight;
1828 if (pixels < 0) top = Math.max(0, top + pixels - 50);
1829 else bot = Math.min(cm.doc.height, bot + pixels + 50);
1830 updateDisplay(cm, [], {top: top, bottom: bot});
1831 }
1832
1833 if (wheelSamples < 20) {
1834 if (display.wheelStartX == null) {
1835 display.wheelStartX = scroll.scrollLeft; display.wheelStartY = scroll.scrollTop;
1836 display.wheelDX = dx; display.wheelDY = dy;
1837 setTimeout(function() {
1838 if (display.wheelStartX == null) return;
1839 var movedX = scroll.scrollLeft - display.wheelStartX;
1840 var movedY = scroll.scrollTop - display.wheelStartY;
1841 var sample = (movedY && display.wheelDY && movedY / display.wheelDY) ||
1842 (movedX && display.wheelDX && movedX / display.wheelDX);
1843 display.wheelStartX = display.wheelStartY = null;
1844 if (!sample) return;
1845 wheelPixelsPerUnit = (wheelPixelsPerUnit * wheelSamples + sample) / (wheelSamples + 1);
1846 ++wheelSamples;
1847 }, 200);

Callers 1

registerEventHandlersFunction · 0.85

Calls 4

setScrollTopFunction · 0.85
setScrollLeftFunction · 0.85
e_preventDefaultFunction · 0.85
updateDisplayFunction · 0.85

Tested by

no test coverage detected