If you want to imitate Google’s directional recognition algorithm, you will need to listen to the input change, find out if the inserted RTL or LTR character (or neutral) was changed and change the textbox dir="" attribute accordingly.
The Google algorithm, for the most part, seems to calculate most of the strong characters in a string and determine the direction from this. If you type RTL, it will switch the context to RTL, and if you then switch to LTR for the same paragraph, it can switch the context to LTR again if these characters exceed the number of RTL.
For comparison, Facebook also uses a directional algorithm, but it is slightly different - it seems that the first strong character determines the direction of the paragraph, and not the total number.
(For writing, Google also has several algorithms for this: Gmail behaves slightly different than Google Hangouts, which is different from how Google’s search input is aligned. These things basically don’t have “right” or “wrong” answers, but rather what suits your use)
Whichever method you choose, you first need to determine what the user enters. There are several ways to do this, but I would recommend the following:
You can create a JavaScript method that listens for user input, uses the regular expression above to determine which strong character is used (either by the first character, or by counting all of them, depending on what works best for your use and scale) - and change the textbox attribute dir="" accordingly.
Make sure you see the presented text later with the correct alignment later, so you may have to either use something to save your alignment or re-recognize it when you render it. In any case, do not forget that the same dir="" attribute is required for display.