1
0
Fork 0
mirror of https://gitlab.com/bramw/baserow.git synced 2025-05-19 23:21:30 +00:00
bramw_baserow/web-frontend/modules/core/utils/dom.js
2021-07-11 18:02:37 +00:00

57 lines
1.3 KiB
JavaScript

/**
* Checks if the target is the same as the provided element of that the element
* contains the target. Returns true is this is the case.
*
* @returns boolean
*/
export const isElement = (element, target) => {
return element !== null && (element === target || element.contains(target))
}
/**
* Checks if the provided object is an html dom element.
*
* @returns boolean
*/
export const isDomElement = (obj) => {
try {
return obj instanceof HTMLElement
} catch (e) {
return (
typeof obj === 'object' &&
obj.nodeType === 1 &&
typeof obj.style === 'object' &&
typeof obj.ownerDocument === 'object'
)
}
}
/**
* This function will focus a contenteditable and place the cursor at the end.
*
* @param element
*/
export const focusEnd = (element) => {
const range = document.createRange()
const selection = window.getSelection()
range.selectNodeContents(element)
range.collapse(false)
selection.removeAllRanges()
selection.addRange(range)
element.focus()
}
/**
* Finds the closest scrollable parent element of the provided element.
*/
export const findScrollableParent = (element) => {
if (element == null) {
return null
}
if (element.scrollHeight > element.clientHeight) {
return element
} else {
return findScrollableParent(element.parentNode)
}
}