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
|
|
|
}
|
2022-09-26 19:09:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let updatedLink;
|
2022-09-26 19:09:38 +02:00
|
|
|
[...document.querySelectorAll('link[rel=stylesheet]')].forEach((link) => {
|
|
|
|
if (link.title === direction && link.disabled) {
|
|
|
|
link.disabled = false;
|
|
|
|
updatedLink = link;
|
|
|
|
} else if (link.title !== direction && !link.disabled) {
|
|
|
|
link.disabled = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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;
|