uiwjs / react-codemirror

CodeMirror 6 component for React. @codemirror https://uiwjs.github.io/react-codemirror/
https://uiwjs.github.io/react-codemirror/
MIT License
1.69k stars 134 forks source link

Custom JS keyboard #633

Closed loc-trinh closed 9 months ago

loc-trinh commented 9 months ago

For coding on mobile app, I would like to use a custom keyboard instead of default iOS keyboard which is not good for coding.

For a textarea component, you can set inputType='none' to disable iOS keyboard from popping up. and with onHover or something I figure you probably can pull up a custom coding keyboard.

I was wondering if it's possible to do inputType='none' for a CodeMirror instance somehow?

jaywcjlove commented 9 months ago

@loc-trinh If you want to prevent the default keyboard from appearing on mobile devices for a contenteditable div, you can use a combination of CSS and JavaScript. For example:

<div id="myEditableDiv" contenteditable="true" style="touch-action: none;"></div>

In this example, the touch-action: none; CSS property is used to disable touch interactions, which can prevent the default keyboard from appearing on touch devices. However, keep in mind that this approach may have other implications for touch interactions, so you should thoroughly test it based on your specific use case.

image

You can try the method mentioned above by encapsulating an extension to add the touch-action: none; style to the div.cm-content.

Here's a simple extension example below, you can refer to and try it out.

https://github.com/uiwjs/react-codemirror/blob/923c16407843e10dd5221ce0fe224f5a4877975b/extensions/events/src/index.ts#L18-L48

loc-trinh commented 9 months ago

Thank you so much for the quick reply, I will give this a try.

Update: this doesn't work <div id="myEditableDiv" contenteditable="true" style="touch-action: none;"></div>

this works!

<div
    id='myEditableDiv'
    className='h-full w-full'
    contentEditable='true'
    inputmode='none'
>
</div>

Can you elaborate more on how to 'encapsulating an extension to add the touch-action: none; style to the div.cm-content'? I think I want to modify div.cm-content to add another attribute @jaywcjlove thanks

jaywcjlove commented 9 months ago

@loc-trinh, this is the most basic example extensions/events. This extension adds events to the div DOM node, allowing you to add properties to it.

jaywcjlove commented 9 months ago

@loc-trinh I extended the extensions-events plugin.

import { element } from '@uiw/codemirror-extensions-events';

const extension3 = element({
  type: 'content',
  props: {
    inputMode: 'none',
  },
});
loc-trinh commented 9 months ago

Wow you are fast, thank you!