Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] Is it possible to type both kana and romaji in the same input without having the romaji immediately converted to kana? #100

Open
GoldenPunk opened this issue May 17, 2020 · 3 comments

Comments

@GoldenPunk
Copy link

GoldenPunk commented May 17, 2020

Question

I am pretty new to wanakana, github, and coding in general, so if I am posting in th wrong place, please let me know.

I am trying to create an input where the user can type things like:
その NASA は なん です か。

I gave the input a class that I toggle when pressing the AltGr key. Then, I check if the input has that class in order to bind, or unbind wanakana, thus making it possible to write romaji in that input. However, when I press the AltGr key again, to start writing kana again, all the romaji are converted to japanese characters.

This is my code:
https://codepen.io/goldenPunk/pen/PoPyxVN

I have tried having wanakana.bind(x, { passRomaji: true }); instead of just wanakana.bind(x); but I still get no results.

I have tested it on both Chrome and Firefox's most recent versions. None works as I would like it to.

Is it possible to have two different scripts on the same text input? Am I doing something wrong?

@DJTB
Copy link
Collaborator

DJTB commented May 18, 2020

Hey there!

Handling conversion gets a bit awkward due to a number of edge cases related to cursor positioning, whether the input is coming from an English input or from an IME that is set to Romaji input etc etc.

The main issue causing you trouble is that when a new romaji character is entered and the input cursor is not at the beginning, we check backwards for preceding romaji in order to handle situations where the user has moved the cursor (i.e. it's not at the final position, maybe they're trying to correct a typo because they missed a letter) or when the end result is somewhat ambiguous (i.e. when i is input, we might need to know if it is after sh so we check backwards).

So I believe that because you're rebinding and the cursor is not at the beginning, the result is not what you want:
https://github.com/WaniKani/WanaKana/blob/master/src/utils/dom.js#L101

One possible solution is for you to inform the users of your input to use capitalized characters when they want to ignore conversion.

You could make a PR for WaniKani adding a new option to bind along the lines of ignoreCapitalizedInput and use that flag here to include a regex exception of capital letter romaji
https://github.com/WaniKani/WanaKana/blob/master/src/utils/dom.js#L123

if (ignoreCapitalizedInput) {
    isJapanese(char, /[AZ]/)
} else {
    isJapanese(char)
}

I think that would probably do it.

FYI: passRomaji is only valid using kana conversion methods between Hiragana <-> Katakana, it didn't make any sense to add it when converting from romaji

@GoldenPunk
Copy link
Author

GoldenPunk commented May 18, 2020

Thank you very much for your help, @DJTB.

Isn't capitalization important, for typing both hiragana and katakana in the same input?

As for making a Pull Request, I am still getting my way around coding and GitHub. I will have to study it a bit more before doing something that I do not quite understand, yet.

In the meantime, would it be acceptable to post a feature request on the issues page, linking it to this post? I am not sure I can explain the possible solution you presented by myself.

Also, is there something I can do, locally, to fix the issue? Like a javascript function, in my page, that overrides the way wanakana usually works.

@DJTB
Copy link
Collaborator

DJTB commented May 25, 2020

As for capitalization, I was making an assumption based on this:

I am trying to create an input where the user can type things like:
その NASA は なん です か。

I'm not sure what you will be doing with the result from the input, so maybe that's not feasible.

In the meantime, would it be acceptable to post a feature request on the issues page, linking it to this post? I am not sure I can explain the possible solution you presented by myself.

You're more than welcome to. If capitalization is important to your input it probably makes more sense to add the methods .pause() and .resume() to the input listener, and on resume it will not try to convert anything that lies to the left of the text cursor. Adding some more context regarding what your input is for and how the result will be used would help in deriving a solution as well.

Also, is there something I can do, locally, to fix the issue? Like a javascript function, in my page, that overrides the way wanakana usually works.

Unfortunately not if you're using bind() since this handling occurs in the event listeners added to the input. The only alternative is to listen to the input yourself and convert the characters you want to convert using the other wanakana methods (e.g. toHiragana()). However you will lose all the logic in place that determines when and what to convert (some of which I mentioned in my first reply - e.g. handling cursor position changes, partial input, user is using an IME).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants