2019-09-03 19:47:54 +02:00
|
|
|
/**
|
|
|
|
* Switch the CSS stylesheet used between 'rtl' and 'ltr'
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
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)
|
2022-01-07 16:57:33 +01:00
|
|
|
* @returns {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
|
2021-02-04 19:15:23 +01:00
|
|
|
direction = 'ltr';
|
2019-09-03 19:47:54 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
let updatedLink;
|
2019-09-03 19:47:54 +02:00
|
|
|
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) {
|
2021-02-04 19:15:23 +01:00
|
|
|
link.disabled = false;
|
|
|
|
updatedLink = link;
|
2019-09-03 19:47:54 +02:00
|
|
|
} else if (link.title !== direction && !link.disabled) {
|
2021-02-04 19:15:23 +01:00
|
|
|
link.disabled = true;
|
2019-09-03 19:47:54 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-09-03 19:47:54 +02:00
|
|
|
if (updatedLink) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
updatedLink.onload = () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
resolve();
|
|
|
|
};
|
2020-11-03 00:41:28 +01:00
|
|
|
updatedLink.onerror = () =>
|
2021-02-04 19:15:23 +01:00
|
|
|
reject(new Error(`Failed to load '${direction}' stylesheet`));
|
|
|
|
});
|
2019-09-03 19:47:54 +02:00
|
|
|
}
|
2020-08-12 21:06:57 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return undefined;
|
|
|
|
};
|
2019-09-03 19:47:54 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export default switchDirection;
|