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: possible to use with codemirror, ace, etc? #306

Open
jsbain opened this issue Sep 11, 2014 · 11 comments
Open

Question: possible to use with codemirror, ace, etc? #306

jsbain opened this issue Sep 11, 2014 · 11 comments

Comments

@jsbain
Copy link

jsbain commented Sep 11, 2014

Is it possible to use this keyboard with editors like codemirror or ace?
I believe both use hidden textareas for input. I tried, without success... Just loading codemirror seems to kill the keyboard, even if loaded into another textarea...
With the ability to use custom keyboards, this would be the ideal coding environment for mobile!

@Mottie
Copy link
Owner

Mottie commented Sep 11, 2014

Hi @jsbain!

Intriguing question! As you stated the editors that I have looked at all use a unique method of cursor & caret placement.

Because this keyboard is tightly bound to caret positioning, it makes it difficult to integrate it with other software; this includes plugins that mask the input and other editors. Also, this keyboard does not currently support contenteditable elements.

I would really need to spend some time digging into the editor code and API to see if integration were possible, but I don't really have the time to do this kind of work.

@jsbain
Copy link
Author

jsbain commented Sep 13, 2014

Ok, turns out to be very straightforward in codemirror, as it exposes the hidden textarea used for input using getInputField. Set that as the keyboard target, and voila.

Backspace doesn't seem to work except for recently entered text, which seems to be a documented issue with code mirror on android for example.

@jsbain
Copy link
Author

jsbain commented Sep 13, 2014

working example:

@Mottie
Copy link
Owner

Mottie commented Sep 13, 2014

Oh wow that's awesome! Nice work!

@Mottie
Copy link
Owner

Mottie commented Sep 13, 2014

I figured out how to make the backspace work, but sadly it only works on the same line as you are inputting. If you move the cursor to a different line it stops working (demo):

$.keyboard.keyaction.delete = function(){
    CodeMirror.commands.delLineLeft(CodeMirror);
};

@Mottie
Copy link
Owner

Mottie commented Aug 22, 2015

I haven't forgotten about this request!

I got the caret/cursor keys to work in this demo: http://jsfiddle.net/Mottie/MK947/4021/

But there are still issues with the backspace. So, I'll see if I can get some help: http://discuss.codemirror.net/t/codemirror-virtual-keyboard/440

@knod
Copy link

knod commented Nov 21, 2015

I'm really interested in this as well and I found that it's not that hard with Ace editor. It might help that I skip the whole 'accept' process that just seems to be getting in the way of interacting at the moment. This is a simplification of what I did:

// Elsewhere
var editor = ace.edit("some-id");
...
// Inside the keyboard instantiation I found [on the setup page](https://github.com/Mottie/Keyboard/wiki/Setup#script)
change : function( evnt, keyboard, elem ) {

    // If they're trying to delete
    if ( keyboard.last.key === 'Bksp' ) {
        editor.remove( 'left' );

    // If they're actually trying to type stuff in
    } else {
        // var newInput = however you're getting your values;
        editor.insert( newInput );
    }

}, ...

I hide the keyboard input field with height: 0 in my css and ignore it completely (disabling the preview shows the textarea, which is hard to hide properly). Honestly I'm not sure what the ultimate consequences of my hacks will be, but this works for now. Also, it deletes new lines just fine and works even if the cursor has been moved.

I'm having problems getting the value I need, but that's for another issue (or maybe more research).

@Mottie
Copy link
Owner

Mottie commented Nov 21, 2015

Hi @knod!

Thanks for sharing! That's awesome that you got it to interact more smoothly. Would you be willing to update this demo to get it to work?

Also, instead of using css to hide the input, set the usePreview option to false.

@knod
Copy link

knod commented Nov 21, 2015

Hi @Mottie, I'll check out the demo and play around with it! (Edit: I'll probably make a new fiddle using Ace instead, since that's what I got working)

As for the usePrieview option, if I set it to false the textarea it's attached to reappears and I haven't yet figured out how to then hide it properly – there's always something left over. I should look at your code and see what you do to hide it. Right now, hiding the keyboard's preview field is dead simple and makes everything line up beautifully, but I'd be happy to listen to any input on that. Maybe that should be in its own issue though.

@knod
Copy link

knod commented Nov 21, 2015

@Mottie - here's the demo I've set up with the Ace editor: http://jsfiddle.net/MK947/4104/

Let me know if it's what you're looking for.

@Mottie
Copy link
Owner

Mottie commented Nov 21, 2015

Wow, that's awesome, thanks for sharing!

As for why the usePreview isn't working, I think it's because the plugin uses jQuery .show() so it can be used on hidden inputs (ref). Hmm, maybe I should make that optional...

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

No branches or pull requests

3 participants