Files
FixIt/assets/js/utils/dom.ts
T
Cell 57f98b48c5 feat(assets): add isRTL to core module and fix tooltip placement for RTL
Add isRTL utility function in utils/dom.ts and expose it via CoreService
and window.fixit. Use isRTL to flip task list tooltip placement from
'right' to 'left' in RTL layouts.
2026-08-25 13:57:00 +08:00

60 lines
1.6 KiB
TypeScript

/**
* Check whether the document direction is right-to-left.
* @returns `true` if the document is RTL.
*/
export function isRTL(): boolean {
return document.documentElement.dir === 'rtl'
}
/**
* Get the current vertical scroll position.
* @returns The scroll offset in pixels.
*/
export function getScrollTop(): number {
return (document.documentElement ?? document.body).scrollTop
}
/**
* Scroll an element into view smoothly.
* @param selector - A CSS selector or `#id` string targeting the element.
*/
export function scrollIntoView(selector: string) {
const element = selector.startsWith('#')
? document.getElementById(selector.slice(1))
: document.querySelector(selector)
element?.scrollIntoView({
behavior: 'smooth',
})
}
/**
* Create a hidden staging element for temporary DOM operations.
* @returns A staging object with `stage`, `contentAsHtml`, `contentAsText`, `contentAsJson`, and `destroy` methods.
*/
export function getStagingDOM() {
const stagingElement = document.createElement('div')
stagingElement.style.display = 'none'
stagingElement.dataset.stagingId = Math.random().toString(36).slice(2)
document.body.appendChild(stagingElement)
return {
$el: stagingElement,
stage(dom: Node) {
stagingElement.innerHTML = ''
stagingElement.appendChild(dom)
},
contentAsHtml(): string {
return stagingElement.innerHTML
},
contentAsText(): string {
return stagingElement.textContent ?? ''
},
contentAsJson(): any {
return JSON.parse(stagingElement.innerHTML)
},
destroy() {
document.body.removeChild(stagingElement)
},
}
}