@livelybone/mouse-mock
Version:
A lib for mock mouse events like click, move
307 lines (261 loc) • 8.95 kB
JavaScript
/**
* Bundle of @livelybone/mouse-mock
* Generated: 2021-07-01
* Version: 1.3.5
* License: MIT
* Author: 2631541504@qq.com
*/
import { getOwnerWindow } from '@livelybone/owner-window';
import { getScrollParent } from '@livelybone/scroll-get';
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
return target;
}
function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
function _iterableToArray(iter) {
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function getPoint(el) {
var win = 'nodeType' in el ? getOwnerWindow(el) : window;
var pagesRect = [{
x: 0,
y: 0,
width: window.innerWidth,
height: window.innerHeight
}];
var rect = 'style' in el ? el.getBoundingClientRect() : el;
var scrollParentsRect = function () {
if ('style' in el) {
var arr = [];
var $el = getScrollParent(el);
while ($el) {
arr.push($el.getBoundingClientRect());
$el = getScrollParent($el);
}
return arr.filter(function (it) {
return it.width && it.height;
});
}
return [];
}();
var minRect = function () {
var left = -Infinity;
var top = -Infinity;
var right = Infinity;
var bottom = Infinity;
[rect].concat(_toConsumableArray(scrollParentsRect), pagesRect).forEach(function (it) {
left = Math.max(left, it.x);
top = Math.max(top, it.y);
right = Math.min(right, it.x + it.width);
bottom = Math.min(bottom, it.y + it.height);
});
return {
x: left,
y: top,
width: right - left,
height: bottom - top
};
}();
if (minRect.width <= 0 || minRect.height <= 0) {
return {
clientX: -1,
clientY: -1,
screenY: -1,
screenX: -1
};
}
var clientPos = {
clientX: 0,
clientY: 0
};
var screenPos = {
screenX: 0,
screenY: 0
};
if (rect) {
var minRadius = Math.min(minRect.width / 2, minRect.height / 2);
var center = {
x: minRect.x + minRect.width / 2,
y: minRect.y + minRect.height / 2
};
if (minRadius >= 1) {
var isInCircle = function isInCircle() {
var a = clientPos.clientX - center.x;
var b = clientPos.clientY - center.y;
return a * a + b * b <= minRadius * minRadius;
};
do {
clientPos.clientX = Math.floor(center.x + minRadius * Math.random() * (Math.random() > 0.5 ? 1 : -1));
clientPos.clientY = Math.floor(center.y + minRadius * Math.random() * (Math.random() > 0.5 ? 1 : -1));
} while (!isInCircle());
} else {
clientPos.clientX = Math.floor(center.x);
clientPos.clientY = Math.floor(center.y);
}
}
var innerWidth = win.innerWidth,
innerHeight = win.innerHeight;
screenPos.screenX = clientPos.clientX - innerWidth;
screenPos.screenY = clientPos.clientY - innerHeight;
return _objectSpread2(_objectSpread2({}, clientPos), screenPos);
}
/* eslint-disable no-underscore-dangle,no-proto */
function eventDeal(ev) {
var _ev$__proto__, _ev$__proto__$constru;
if (ev.isTrusted) return ev;
var $ev = {};
var proto = ev;
while (proto.__proto__) {
Object.keys(proto.__proto__).forEach(function (k) {
Object.defineProperty($ev, k, {
get: function get() {
var val = ev[k];
if (typeof val === 'function') return val.bind(ev);
return val;
},
configurable: true
});
});
proto = proto.__proto__;
}
$ev.isTrusted = true;
$ev.__proto__ = ((_ev$__proto__ = ev.__proto__) === null || _ev$__proto__ === void 0 ? void 0 : (_ev$__proto__$constru = _ev$__proto__.constructor) === null || _ev$__proto__$constru === void 0 ? void 0 : _ev$__proto__$constru.prototype) || ev.__proto__;
return $ev;
}
var cache = new Map();
function rewriteListenerBinder() {
if (!window.$$RewriteListenerBinderSuccess$$) {
Element.prototype._addEventListener = Element.prototype.addEventListener;
Element.prototype._removeEventListener = Element.prototype.removeEventListener;
Element.prototype.addEventListener = function addEventListener(type, cb) {
var listener = cache.get(cb) || function (ev) {
return cb(eventDeal(ev));
};
this._addEventListener(type, listener);
};
Element.prototype.removeEventListener = function removeEventListener(type, cb) {
var listener = cache.get(cb);
if (listener) {
this._removeEventListener(type, listener);
cache.delete(cb);
}
};
Object.getOwnPropertyNames(HTMLElement.prototype).forEach(function (k) {
if (k.startsWith('on')) {
var descriptor = Object.getOwnPropertyDescriptor(HTMLElement.prototype, k);
Object.defineProperty(HTMLElement.prototype, "_".concat(k), descriptor);
Object.defineProperty(HTMLElement.prototype, k, _objectSpread2(_objectSpread2({}, descriptor), {}, {
set: function set(cb) {
this["_".concat(k)] = typeof cb === 'function' ? function _(ev) {
// @ts-ignore
return cb.call(this, eventDeal(ev));
} : cb;
}
}));
}
});
Object.defineProperty(window, '$$RewriteListenerBinderSuccess$$', {
value: true,
writable: false,
configurable: true
});
}
}
function eventTrigger(el, ev) {
if (el.dispatchEvent) {
el.dispatchEvent(ev);
} else {
var _fireEvent, _ref;
(_fireEvent = (_ref = el).fireEvent) === null || _fireEvent === void 0 ? void 0 : _fireEvent.call(_ref, ev);
}
}
function mockMouseEvent(eventType, el) {
if (el) {
var point = getPoint(el);
var $el = el && 'nodeName' in el ? el : document.elementFromPoint(point.clientX, point.clientY);
var ev = $el === null || $el === void 0 ? void 0 : $el.ownerDocument.createEvent('MouseEvent');
if (!$el) {
console.error("Cannot find element for ".concat(eventType, ", it seems that the element or the rect you provide is out of the viewport.\n"), {
el: el,
point: point
});
} else {
ev.initMouseEvent(eventType, true, true, getOwnerWindow($el), 0, point.screenX, point.screenY, point.clientX, point.clientY, false, false, false, false, 0, $el);
eventTrigger($el, ev);
}
return {
point: point
};
}
}
function mockClick(el) {
mockMouseEvent('mouseenter', el);
mockMouseEvent('mouseover', el);
mockMouseEvent('mousemove', el);
mockMouseEvent('mousedown', el);
mockMouseEvent('mouseup', el);
var res = mockMouseEvent('click', el);
mockMouseEvent('mousemove', el);
mockMouseEvent('mouseout', el);
mockMouseEvent('mouseleave', el);
return res;
}
export { mockClick, mockMouseEvent, rewriteListenerBinder };