MCPcopy
hub / github.com/dolphin-wood/smooth-scrollbar / selectHandler

Function selectHandler

src/events/select.ts:9–84  ·  view source on GitHub ↗
(scrollbar: I.Scrollbar)

Source from the content-addressed store, hash-verified

7} from '../utils/';
8
9export function selectHandler(scrollbar: I.Scrollbar) {
10 const addEvent = eventScope(scrollbar);
11 const { containerEl, contentEl } = scrollbar;
12
13 let isSelected = false;
14 let isContextMenuOpened = false; // flag to prevent selection when context menu is opened
15 let animationID: number;
16
17 function scroll({ x, y }) {
18 if (!x && !y) return;
19
20 const { offset, limit } = scrollbar;
21 // DISALLOW delta transformation
22 scrollbar.setMomentum(
23 clamp(offset.x + x, 0, limit.x) - offset.x,
24 clamp(offset.y + y, 0, limit.y) - offset.y,
25 );
26
27 animationID = requestAnimationFrame(() => {
28 scroll({ x, y });
29 });
30 }
31
32 addEvent(window, 'mousemove', (evt: MouseEvent) => {
33 if (!isSelected) return;
34
35 cancelAnimationFrame(animationID);
36
37 const dir = calcMomentum(scrollbar, evt);
38
39 scroll(dir);
40 });
41
42 // prevent scrolling when context menu is opened
43 // NOTE: `contextmenu` event may be fired
44 // 1. BEFORE `selectstart`: when user right-clicks on the text content -> prevent future scrolling,
45 // 2. AFTER `selectstart`: when user right-clicks on the blank area -> cancel current scrolling,
46 // so we need to both set the flag and cancel current scrolling
47 addEvent(contentEl, 'contextmenu', () => {
48 // set the flag to prevent future scrolling
49 isContextMenuOpened = true;
50
51 // stop current scrolling
52 cancelAnimationFrame(animationID);
53 isSelected = false;
54 });
55
56 // reset context menu flag on mouse down
57 // to ensure the scrolling is allowed in the next selection
58 addEvent(contentEl, 'mousedown', () => {
59 isContextMenuOpened = false;
60 });
61
62 addEvent(contentEl, 'selectstart', () => {
63 if (isContextMenuOpened) {
64 return;
65 }
66

Callers

nothing calls this directly

Calls 4

eventScopeFunction · 0.90
addEventFunction · 0.85
calcMomentumFunction · 0.70
scrollFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…