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

Cursor jumps to beginning of line on mobile browsers when focus is lost #5500

Open
JannemanDev opened this issue Jul 12, 2018 · 5 comments
Open
Labels

Comments

@JannemanDev
Copy link

JannemanDev commented Jul 12, 2018

I have a button and a codemirror edit field where a user can type in a SQL query. When they press the button, they query gets send to the server to run and the recordset will be displayed. After clicking on the button, I refocus to the editorwindow so the user can change the SQL query immediately.
On non-mobile browsers this works perfectly, the cursor is put back on the last known position automatically with just this code:

$("#btnRunQuery").on("click", function( event ) {
[...]
editor.focus();
}

But on mobile browsers the cursor is always set to the beginning of the line of the last known position. I tested in latest FF, Chrome and Safari on iOS v11.4.1 with latest codemirror lib.

I tried to make a work around with this code, but same problem:

$("#btnRunQuery").on("click", function( event ) {
          //save last cursor pos
          var line=editor.getCursor().line;
          var ch=editor.getCursor().ch;
         [...]
          editor.focus();
          editor.setCursor({line: line, ch: ch});
}  

Also saving the cursor pos on the Blur event doesn't work.

@JannemanDev JannemanDev changed the title Cursor jumps to beginning of line on mobile browsers Cursor jumps to beginning of line on mobile browsers when focus is lost Jul 12, 2018
@JannemanDev
Copy link
Author

JannemanDev commented Jul 13, 2018

I found a workaround. If you change the inputStyle to "textarea" it works (even without the workaround code above)!
This bug is maybe related to #5415, #5405

By the way I could not find any information on inputStyle and the differences between textarea and contenteditable besides:

inputStyle: string
Selects the way CodeMirror handles input and focus. The core library defines the "textarea" and "contenteditable" input models. On mobile browsers, the default is "contenteditable". On desktop browsers, the default is "textarea". Support for IME and screen readers is better in the "contenteditable" model. The intention is to make it the default on modern desktop browsers in the future."

@marijnh marijnh added the mobile label Jul 18, 2018
@marijnh
Copy link
Member

marijnh commented Jul 18, 2018

If you change the inputStyle to "textarea" it works

That'll break touch selection, though

@martinschilliger
Copy link

Found the solution here:

[contenteditable="true"] {
    -webkit-user-select: text;
    user-select: text;
}

@dmortimer
Copy link

@martinschilliger what element are you applying that CSS to? Along with the cursor position jumping to the beginning, I am also having an issue where codemirror.hasFocus() returns false on IOS when it should return true.

@martinschilliger
Copy link

Just:

[contenteditable="true"] {
  user-select: text;
}

codemirror.hasFocus() is working that way also. 👍

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

No branches or pull requests

4 participants