Add helpful DOM commands

This commit is contained in:
Oliver Blanthorn 2020-12-08 19:50:24 +01:00
parent b49be04730
commit 238ba8013f
No known key found for this signature in database
GPG key ID: 2BB8C36BB504BFF3

View file

@ -689,7 +689,7 @@ export function simulateClick(target: HTMLElement) {
}
/** Recursively resolves an active shadow DOM element. */
export function deepestShadowRoot(sr: ShadowRoot|null): ShadowRoot|null {
export function deepestShadowRoot(sr: ShadowRoot | null): ShadowRoot | null {
if (sr === null) return sr
let shadowRoot = sr
while (shadowRoot.activeElement.shadowRoot != null) {
@ -698,3 +698,15 @@ export function deepestShadowRoot(sr: ShadowRoot|null): ShadowRoot|null {
return shadowRoot
}
export function getElementCentre(el) {
const pos = el.getBoundingClientRect()
return { x: 0.5 * (pos.left + pos.right), y: 0.5 * (pos.top + pos.bottom) }
}
export function getAbsolutePosition(el) {
const pos = getElementCentre(el)
return {
x: pos.x + (window as any).mozInnerScreenX,
y: pos.y + (window as any).mozInnerScreenY,
}
}