What about not moving the cursor if setValue does not change the content?
See original GitHub issueHello,
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:
- Created 8 years ago
- Reactions:2
- Comments:9 (5 by maintainers)
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
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.