"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isMouseEvent = isMouseEvent; exports.useMouseMove = useMouseMove; var _react = require("react"); var _throttle = require("../../services/throttle"); function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ function isMouseEvent(event) { return _typeof(event) === 'object' && 'pageX' in event && 'pageY' in event; } function useMouseMove(handleChange) { var interactionConditional = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; (0, _react.useEffect)(function () { return unbindEventListeners; }, []); // eslint-disable-line react-hooks/exhaustive-deps var handleInteraction = function handleInteraction(e, isFirstInteraction) { if (e) { if (interactionConditional) { var x = isMouseEvent(e) ? e.pageX : e.touches[0].pageX; var y = isMouseEvent(e) ? e.pageY : e.touches[0].pageY; handleChange({ x: x, y: y }, isFirstInteraction); } } }; var handleMouseMove = (0, _throttle.throttle)(function (e) { handleChange({ x: e.pageX, y: e.pageY }, false); }); var handleMouseDown = function handleMouseDown(e) { handleInteraction(e, true); document.addEventListener('mousemove', handleMouseMove); document.addEventListener('mouseup', unbindEventListeners); }; var unbindEventListeners = function unbindEventListeners() { document.removeEventListener('mousemove', handleMouseMove); document.removeEventListener('mouseup', unbindEventListeners); }; return [handleMouseDown, handleInteraction]; }