question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What about not moving the cursor if setValue does not change the content?

See original GitHub issue

Hello,

I’m trying to integrate CodeMirror into my Angular 2 based application. IIUIC, Angular encourages people to write forms in such a way that a controller listens to events fired from the form fields and updates the internal models of these fields. Under certain circumstances the controller fires events back to the form fields if the internal model changes. (This is useful e.g. if you need to initialize the field with a value.)

In such case in my application, every change in the CodeMirror editor results in an event that consequently calls the editor’s setValue with the same value which is already there. Since the setValue method apparently sets the cursor position to the beginning of the document, it results in resetting the cursor position after every change in the editor.

To resolve this, I have included a test if (value != editor.getDoc().getValue()) before the setValue call. There is a problem that if I hold a key down, it apparently updates the editor’s value before the previous event is handled which results in an endless loop. But I have to investigate this more deeply first. For now, I just wanted to suggest whether it would make sense to (optionally) not reset the cursor position if I (consequently) call something like doc.setValue(doc.getValue()).

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
marijnhcommented, Nov 5, 2018

Not really—this isn’t something that makes a lot of sense in the first place (if you set a different document there’s no obvious way the selection should be ‘preserved’) so I don’t think it belongs in the manual

2reactions
LeonStaufercommented, Nov 5, 2018

I do not believe this is an issue that “concerns precisely only [@radek-holy]”. I am working on a live editor and I believe this is an issue that would affect anyone replacing the content of the editor with other content (which I imagine is quite a number of people).

@marijnh explains why using setValue causes this behavior. However, maybe another function could be implemented that updates the content and maintains the cursor position.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Updating an input's value without losing cursor position
I want to mandate that the value of a text box is lowercase using JavaScript. I've tried the code below, but the cursor...
Read more >
CodeMirror resetting cursor in text insert/update - Joplin Forum
What about not moving the cursor if setValue does not change the content? · Issue #3734 · codemirror/CodeMirror · GitHub.
Read more >
QLineEdit Class | Qt Widgets 6.4.1
However, if the content is horizontally centered, the placeholder text is not displayed under the cursor when the line edit has focus.
Read more >
type - Cypress Documentation
Type into a DOM element. It is unsafe to chain further commands that rely on the subject after .type(). Syntax Usage Correct Usage...
Read more >
HTMLTextAreaElement - Web APIs | MDN
If this element is not contained in a form element, it can be the id ... contains the index of the character that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found