2020-03-07 11:45:13 +00:00
|
|
|
import contains from '../vc-util/Dom/contains';
|
2019-01-12 03:33:27 +00:00
|
|
|
export function buffer(fn, ms) {
|
|
|
|
let timer;
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function clear() {
|
2018-09-05 13:28:54 +00:00
|
|
|
if (timer) {
|
2019-01-12 03:33:27 +00:00
|
|
|
clearTimeout(timer);
|
|
|
|
timer = null;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function bufferFn() {
|
|
|
|
clear();
|
|
|
|
timer = setTimeout(fn, ms);
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
bufferFn.clear = clear;
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
return bufferFn;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export function isSamePoint(prev, next) {
|
|
|
|
if (prev === next) return true;
|
|
|
|
if (!prev || !next) return false;
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
if ('pageX' in next && 'pageY' in next) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return prev.pageX === next.pageX && prev.pageY === next.pageY;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ('clientX' in next && 'clientY' in next) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return prev.clientX === next.clientX && prev.clientY === next.clientY;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
return false;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export function isWindow(obj) {
|
|
|
|
return obj && typeof obj === 'object' && obj.window === obj;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2019-09-03 14:59:41 +00:00
|
|
|
|
|
|
|
export function isSimilarValue(val1, val2) {
|
|
|
|
const int1 = Math.floor(val1);
|
|
|
|
const int2 = Math.floor(val2);
|
|
|
|
return Math.abs(int1 - int2) <= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function restoreFocus(activeElement, container) {
|
|
|
|
// Focus back if is in the container
|
2019-09-11 14:35:25 +00:00
|
|
|
if (activeElement !== document.activeElement && contains(container, activeElement)) {
|
2019-09-03 14:59:41 +00:00
|
|
|
activeElement.focus();
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 22:58:19 +00:00
|
|
|
export function monitorResize(element, callback) {
|
|
|
|
let prevWidth = null;
|
|
|
|
let prevHeight = null;
|
|
|
|
|
|
|
|
function onResize([{ target }]) {
|
|
|
|
if (!document.documentElement.contains(target)) return;
|
|
|
|
const { width, height } = target.getBoundingClientRect();
|
|
|
|
const fixedWidth = Math.floor(width);
|
|
|
|
const fixedHeight = Math.floor(height);
|
|
|
|
|
|
|
|
if (prevWidth !== fixedWidth || prevHeight !== fixedHeight) {
|
|
|
|
// https://webkit.org/blog/9997/resizeobserver-in-webkit/
|
|
|
|
Promise.resolve().then(() => {
|
|
|
|
callback({ width: fixedWidth, height: fixedHeight });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
prevWidth = fixedWidth;
|
|
|
|
prevHeight = fixedHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
const resizeObserver = new ResizeObserver(onResize);
|
|
|
|
if (element) {
|
|
|
|
resizeObserver.observe(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
resizeObserver.disconnect();
|
|
|
|
};
|
|
|
|
}
|