Created
February 4, 2022 07:04
-
-
Save AliMilani/e9e5e8a1205816350531b846c1546561 to your computer and use it in GitHub Desktop.
input-rtl-direction-fixer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.addEventListener( | |
"input", | |
function (e) { | |
e = e || window.event; | |
const target = e.target || e.srcElement; | |
const isFarsi = /^[\u0600-\u06FF\s]+$/.test(target.value.split(" ")[0]); | |
const hasDirection = target.style.cssText.includes("direction"); | |
if ( | |
!(target.style.direction == "rtl") && | |
isFarsi && | |
(!hasDirection || target.style.direction == "ltr") | |
) { | |
target.style.direction = "rtl"; | |
console.log("{ set to rtl}"); | |
} else if ( | |
!(target.style.direction == "ltr") && | |
!isFarsi && | |
(!hasDirection || target.style.direction == "rtl") | |
) { | |
console.log("{ set to ltr}"); | |
target.style.direction = "ltr"; | |
} | |
}, | |
false | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment