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