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

Custom JS keyboard #633

Closed
loc-trinh opened this issue Feb 21, 2024 · 5 comments
Closed

Custom JS keyboard #633

loc-trinh opened this issue Feb 21, 2024 · 5 comments

Comments

@loc-trinh
Copy link

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
Copy link
Member

@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.

function events<T extends keyof HTMLElementEventMap>(opts: Options<T>) {
const { type = 'scroll', events } = opts;
return ViewPlugin.fromClass(
class {
dom?: HTMLElement;
view: EditorView;
constructor(view: EditorView) {
this.view = view;
if (type === 'dom') {
this.dom = view.dom;
} else if (type === 'content') {
this.dom = view.contentDOM;
} else {
this.dom = view.scrollDOM;
}
(Object.keys(events || {}) as Array<keyof typeof events>).forEach((keyname) => {
if (events && events[keyname] && this.dom) {
this.dom.addEventListener(keyname, events[keyname]);
}
});
}
destroy() {
(Object.keys(events || {}) as Array<keyof typeof events>).forEach((keyname) => {
if (events && events[keyname] && this.dom) {
this.dom.removeEventListener(keyname, events[keyname]);
}
});
}
},
);
}

@loc-trinh
Copy link
Author

loc-trinh commented Feb 21, 2024

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
Copy link
Member

@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 added a commit that referenced this issue Feb 22, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@jaywcjlove
Copy link
Member

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

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

const extension3 = element({
  type: 'content',
  props: {
    inputMode: 'none',
  },
});

jaywcjlove added a commit that referenced this issue Feb 22, 2024
github-actions bot pushed a commit that referenced this issue Feb 22, 2024
@loc-trinh
Copy link
Author

Wow you are fast, thank you!

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