getMouseEventOptions.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getMouseEventOptions = getMouseEventOptions;
  6. function isMousePressEvent(event) {
  7. return event === 'mousedown' || event === 'mouseup' || event === 'click' || event === 'dblclick';
  8. } // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
  9. const BUTTONS_NAMES = {
  10. none: 0,
  11. primary: 1,
  12. secondary: 2,
  13. auxiliary: 4
  14. }; // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
  15. const BUTTON_NAMES = {
  16. primary: 0,
  17. auxiliary: 1,
  18. secondary: 2
  19. };
  20. function translateButtonNumber(value, from) {
  21. var _Object$entries$find;
  22. const [mapIn, mapOut] = from === 'button' ? [BUTTON_NAMES, BUTTONS_NAMES] : [BUTTONS_NAMES, BUTTON_NAMES];
  23. const name = (_Object$entries$find = Object.entries(mapIn).find(([, i]) => i === value)) == null ? void 0 : _Object$entries$find[0]; // istanbul ignore next
  24. return name && Object.prototype.hasOwnProperty.call(mapOut, name) ? mapOut[name] : 0;
  25. }
  26. function convertMouseButtons(event, init, property) {
  27. if (!isMousePressEvent(event)) {
  28. return 0;
  29. }
  30. if (typeof init[property] === 'number') {
  31. return init[property];
  32. } else if (property === 'button' && typeof init.buttons === 'number') {
  33. return translateButtonNumber(init.buttons, 'buttons');
  34. } else if (property === 'buttons' && typeof init.button === 'number') {
  35. return translateButtonNumber(init.button, 'button');
  36. }
  37. return property != 'button' && isMousePressEvent(event) ? 1 : 0;
  38. }
  39. function getMouseEventOptions(event, init, clickCount = 0) {
  40. var _init;
  41. init = (_init = init) != null ? _init : {};
  42. return { ...init,
  43. // https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail
  44. detail: event === 'mousedown' || event === 'mouseup' || event === 'click' ? 1 + clickCount : clickCount,
  45. buttons: convertMouseButtons(event, init, 'buttons'),
  46. button: convertMouseButtons(event, init, 'button')
  47. };
  48. }