getMouseEventOptions.js
1.92 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getMouseEventOptions = getMouseEventOptions;
function isMousePressEvent(event) {
return event === 'mousedown' || event === 'mouseup' || event === 'click' || event === 'dblclick';
} // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
const BUTTONS_NAMES = {
none: 0,
primary: 1,
secondary: 2,
auxiliary: 4
}; // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
const BUTTON_NAMES = {
primary: 0,
auxiliary: 1,
secondary: 2
};
function translateButtonNumber(value, from) {
var _Object$entries$find;
const [mapIn, mapOut] = from === 'button' ? [BUTTON_NAMES, BUTTONS_NAMES] : [BUTTONS_NAMES, BUTTON_NAMES];
const name = (_Object$entries$find = Object.entries(mapIn).find(([, i]) => i === value)) == null ? void 0 : _Object$entries$find[0]; // istanbul ignore next
return name && Object.prototype.hasOwnProperty.call(mapOut, name) ? mapOut[name] : 0;
}
function convertMouseButtons(event, init, property) {
if (!isMousePressEvent(event)) {
return 0;
}
if (typeof init[property] === 'number') {
return init[property];
} else if (property === 'button' && typeof init.buttons === 'number') {
return translateButtonNumber(init.buttons, 'buttons');
} else if (property === 'buttons' && typeof init.button === 'number') {
return translateButtonNumber(init.button, 'button');
}
return property != 'button' && isMousePressEvent(event) ? 1 : 0;
}
function getMouseEventOptions(event, init, clickCount = 0) {
var _init;
init = (_init = init) != null ? _init : {};
return { ...init,
// https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail
detail: event === 'mousedown' || event === 'mouseup' || event === 'click' ? 1 + clickCount : clickCount,
buttons: convertMouseButtons(event, init, 'buttons'),
button: convertMouseButtons(event, init, 'button')
};
}