2019-09-03 19:47:54 +02:00
|
|
|
/**
|
|
|
|
* Switch the CSS stylesheet used between 'rtl' and 'ltr'
|
2020-08-15 13:58:11 +02:00
|
|
|
* @param {('ltr' | 'rtl' | 'auto')} direction - Text direction, either left-to-right (ltr) or right-to-left (rtl)
|
2020-08-12 21:06:57 +02:00
|
|
|
* @return {Promise<void>}
|
2019-09-03 19:47:54 +02:00
|
|
|
*/
|
|
|
|
const switchDirection = async (direction) => {
|
|
|
|
if (direction === 'auto') {
|
2020-08-15 13:58:11 +02:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2019-09-03 19:47:54 +02:00
|
|
|
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()
|
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
updatedLink.onerror = () =>
|
|
|
|
reject(new Error(`Failed to load '${direction}' stylesheet`))
|
2019-09-03 19:47:54 +02:00
|
|
|
})
|
|
|
|
}
|
2020-08-12 21:06:57 +02:00
|
|
|
|
|
|
|
return undefined
|
2019-09-03 19:47:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default switchDirection
|