2019-09-03 19:47:54 +02:00
|
|
|
/**
|
|
|
|
* Switch the CSS stylesheet used between 'rtl' and 'ltr'
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {('ltr' | 'rtl')} direction - Text direction, either left-to-right (ltr) or right-to-left (rtl)
|
2019-09-03 19:47:54 +02:00
|
|
|
*/
|
|
|
|
const switchDirection = async (direction) => {
|
|
|
|
if (direction === 'auto') {
|
|
|
|
direction = 'ltr'
|
|
|
|
}
|
|
|
|
let updatedLink
|
|
|
|
Array.from(document.getElementsByTagName('link'))
|
2020-02-15 21:34:12 +01:00
|
|
|
.filter((link) => link.rel === 'stylesheet')
|
|
|
|
.forEach((link) => {
|
2019-09-03 19:47:54 +02:00
|
|
|
if (link.title === direction && link.disabled) {
|
|
|
|
link.disabled = false
|
|
|
|
updatedLink = link
|
|
|
|
} else if (link.title !== direction && !link.disabled) {
|
|
|
|
link.disabled = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (updatedLink) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
updatedLink.onload = () => {
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
updatedLink.onerror = () => reject(new Error(`Failed to load '${direction}' stylesheet`))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default switchDirection
|