jsoneditor icon indicating copy to clipboard operation
jsoneditor copied to clipboard

Event to get notified when a node gets expaneded

Open belaviyo opened this issue 4 years ago • 7 comments

Hello,

I was wondering if there is an event to get notified when a node gets expanded or collapsed. I need this to write a custom function to keep the list of expanded states. I couldn't find any function to restore expanded nodes after a page refresh

belaviyo avatar Jan 09 '21 06:01 belaviyo

Thanks for your suggestion. There is no such event right now. I think if we would add it, we will some other related methods as well to make this feature complete, such as editor.getExpandedNodes(), editor.setExpandedNodes().

Can you explain your use case? If we have a clear view on that, we can think though a good solution.

josdejong avatar Jan 10 '21 12:01 josdejong

the idea is to restore expanded states when browser restarts or when the page reloads

belaviyo avatar Jan 11 '21 09:01 belaviyo

That makes sense indeed. Then, maybe methods like getState() and setState() makes sense. It could contain the expanded nodes, but maybe also the scroll position and the selection.

josdejong avatar Jan 16 '21 09:01 josdejong

That would be perfect!

belaviyo avatar Jan 16 '21 09:01 belaviyo

Sounds good.

Anyone interested in implementing a getState and setState? I think it requires some thinking though of the details before actually implementing something. Help would be very welcome.

josdejong avatar Jan 16 '21 09:01 josdejong

editor.getState() and editor.setState() might be useful but they serve different use case compared to getting notified in case of a node expanded/collapsed event. Can we have something like onExpand(path, isExpanded) callback passed to the options while creating a JSONEditor, which allows us to execute whatever we want in case a node is expanded/collapsed ?

So, I'm trying to use two instances of JSONEditor to show JSON diff and I want to sync the node expand/collapse operations across the editors, but couldn't find a way as of now. Having onExpand callback and another editor.expand(path, isExpand) method would allow me to implement my use case.

Any plans of implementing this or comments on the use case (can this be achieved with the current APIs) ?

hi-rai avatar May 04 '22 14:05 hi-rai

Thanks for your input @himanshu520 . I like your proposal of an onExpand(path, isExpanded) and editor.expand(path, isExpand), this is much simpler to implement than a big, generic getState and setState.

Both these methods do not yet exist, the only thing that comes close is the editor.exapndAll() function 😄

I'm not planning on implementing this myself any time soon, help implementing this would be welcome.

josdejong avatar May 09 '22 08:05 josdejong