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

Editor loses focus if data is changed and displayed #15

Closed
MarcusRiemer opened this issue Jul 21, 2018 · 5 comments
Closed

Editor loses focus if data is changed and displayed #15

MarcusRiemer opened this issue Jul 21, 2018 · 5 comments

Comments

@MarcusRiemer
Copy link

MarcusRiemer commented Jul 21, 2018

I would like to reflect changes on the fly (e.g. as soon as the user changes something). But every time I change something in the code or the tree view, the jsoneditor loses focus. This issue can be replicated in the sample code by adding this.data = JSON.parse(this.editor.getText()) in the change-callback.

I suspect this happens because Angular re-renders the view and destroys the jsoneditor in that process. But would there be any way to work around this?

Edit (after further investigation): Its far more then the editing state that is lost on changes. The previously selected mode is lost as well and the current expansion state is lost as well. It seems that the editor is resetted entirely .

@mariohmol
Copy link
Owner

Hi!
Thanks for reporting this..

Can u please use this link and check if this behaviour happens?

https://stackblitz.com/edit/angular-json-editor?file=src%2Fapp%2Fapp.component.ts

@MarcusRiemer
Copy link
Author

Sure, I forked your example and added the assignment: https://stackblitz.com/edit/angular-json-editor-uodqc5

And just to make sure that we are talking about the same thing:
A quick screencast to show what happens

I am fully aware that it is impossible to automatically do much about the changing data itself: If the bound data changes there may be any number of conflicts that can't be resolved automatically. I do not expect any meaningful cursor position or expansion state if the bound data changes randomly! This is basically the same scenario a text editor is in when it detects that the edited file has changed on disk. And it resolves that by asking the user what to do: "Keep the state from the editor" or "Reload from disk"? Imho this kind of logic is far outside the scope of this neat little wrapper.

But maybe it would be possible to somehow preserve some state like the previous position of the cursor and the selected mode? At least for minor edits this should do the trick.

@mariohmol
Copy link
Owner

Gotcha!

My question is.. why you need to change the data? If you need to save this on database, just call the ajax, or if you need that in another component, you can call a event or service to pass it on

Because the problem here is to update the same data you used to build the jsoneditor

@MarcusRiemer
Copy link
Author

I use the editor as a (weak) excuse to postpone some "real" UI writing (screenshot 2 and 3). The edited data models are quite complex, so building their UI simply takes a while. Having some kind of two way databinding allows loads of experimentation: You can change the model from the (only partially finished) UI and from the editor.

As I said: I wouldn't expect the actually edited data to survive or to get merged. But I would love to see "trivial" parts the editor state preserved when changing the data. Currently even if I only "write back" the data selectively (shown in the first screenshot) things like the selected mode are lost.

Screenshot 1: Selective update, asking the user "Data has changed, which version do you prefer?"
screenshot_20180803_161609

Screenshot 2: Hardly any UI at all, only a JSON-editor
Hardly any UI at all

Screenshot 3: A little UI, but still a JSON-editor
A little UI

@mariohmol
Copy link
Owner

Thanks!!

I think i got a solution for you, check version 1.6.2.. if is not working as expected please reopen

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