From 9a0fdb914d50c32f062de58704b40ea1ceb23203 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Fri, 4 Oct 2024 21:17:13 +0800 Subject: refactor: add GeoLinks * In current implementation, marks are generated in the first time link is hovered. This prevent complexity since rendering maps is async. --- src/utils.mjs | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'src/utils.mjs') diff --git a/src/utils.mjs b/src/utils.mjs index ffd8978..4eedf82 100644 --- a/src/utils.mjs +++ b/src/utils.mjs @@ -72,10 +72,10 @@ export const animateRectTransition = (element, rect, options = {}) => { * @param {Number} delay milliseconds * @returns {Any} return value of function call, or null if throttled */ -export function throttle (func, delay) { +export function throttle(func, delay) { let timerFlag = null - return function (...args) { + return function(...args) { const context = this if (timerFlag !== null) return null @@ -99,3 +99,37 @@ export const shiftByWindow = element => { const offsetY = window.innerHeight - rect.top - rect.height element.style.transform = `translate(${offsetX < 0 ? offsetX : 0}px, ${offsetY < 0 ? offsetY : 0}px)` } + +/** + * insideWindow. check DOMRect is inside window + * + * @param {HTMLElement} element + */ +export const insideWindow = element => { + const rect = element.getBoundingClientRect() + return ( + rect.left > 0 && + rect.right < window.innerWidth + rect.width && + rect.top > 0 && + rect.bottom < window.innerHeight + rect.height + ) +} + +/** + * insideParent. check children element is inside DOMRect of parent element + * + * @param {HTMLElement} childElement + * @param {HTMLElement} parentElement + */ +export const insideParent = (childElement, parentElement) => { + const childRect = childElement.getBoundingClientRect() + const parentRect = parentElement.getBoundingClientRect() + const offset = 20 + + return ( + childRect.left > parentRect.left + offset && + childRect.right < parentRect.right - offset && + childRect.top > parentRect.top + offset && + childRect.bottom < parentRect.bottom - offset + ) +} -- cgit v1.2.3-70-g09d2