1
0
Fork 0
mirror of https://gitlab.com/bramw/baserow.git synced 2025-04-09 23:27:51 +00:00
bramw_baserow/web-frontend/utils/dom.js
2019-08-26 17:49:28 +00:00

35 lines
967 B
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 === target || element.contains(target)
}
/**
* This function will focus a contenteditable and place the cursor at the end.
*
* @param element
*/
export const focusEnd = element => {
element.focus()
if (
typeof window.getSelection !== 'undefined' &&
typeof document.createRange !== 'undefined'
) {
const range = document.createRange()
range.selectNodeContents(element)
range.collapse(false)
const sel = window.getSelection()
sel.removeAllRanges()
sel.addRange(range)
} else if (typeof document.body.createTextRange !== 'undefined') {
const textRange = document.body.createTextRange()
textRange.moveToElementText(element)
textRange.collapse(false)
textRange.select()
}
}