2020-03-07 11:45:13 +00:00
|
|
|
import contains from '../vc-util/Dom/contains';
|
2021-06-26 01:35:40 +00:00
|
|
|
import type { TargetPoint } from './interface';
|
2021-08-12 07:42:33 +00:00
|
|
|
import ResizeObserver from 'resize-observer-polyfill';
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
export function isSamePoint(prev: TargetPoint, next: TargetPoint) {
|
2019-01-12 03:33:27 +00:00
|
|
|
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-09-03 14:59:41 +00:00
|
|
|
export function restoreFocus(activeElement, container) {
|
|
|
|
// Focus back if is in the container
|
2021-06-07 09:35:03 +00:00
|
|
|
if (
|
|
|
|
activeElement !== document.activeElement &&
|
|
|
|
contains(container, activeElement) &&
|
|
|
|
typeof activeElement.focus === 'function'
|
|
|
|
) {
|
2019-09-03 14:59:41 +00:00
|
|
|
activeElement.focus();
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 22:58:19 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
export function monitorResize(element: HTMLElement, callback: Function) {
|
|
|
|
let prevWidth: number = null;
|
|
|
|
let prevHeight: number = null;
|
|
|
|
|
|
|
|
function onResize([{ target }]: ResizeObserverEntry[]) {
|
2020-10-09 22:58:19 +00:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
}
|