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

Add support to sync node expand/collapse operations across editors #1440

Merged
merged 3 commits into from May 31, 2022

Conversation

himanshu520
Copy link
Contributor

  • Added onExpand callback option to be specified while creating JSONEditor, that is invoked when a node is expanded/collapsed.
  • Added JSONEditor.expand API that allows to expand/collapse JSON node by specifying its path.

The above two additions would allow library users to sync node expand/collapse operations when having multiple editors (#1217).

  * Added onExpand callback option to be passed while creating JSONEditor
  * The callback is invoked when the expand/collapse is done non-programatically
@josdejong
Copy link
Owner

Thanks @himanshu520 , your PR looks good at first sight. I'll review it soon.

Copy link
Owner

@josdejong josdejong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR looks good, exactly what we're looking for 👌. Thanks for the clear description in the docs.

I made one inline comment, and I have one more question: can you add a new, minimal example in the /examples section demonstrating two editors which keep their expanded state in sync? Is useful for users and also for you and me manually testing whether all works as expected.

docs/api.md Outdated
@@ -679,6 +693,24 @@ Destroy the editor. Clean up DOM, event listeners, and web workers.

Expand all fields. Only applicable for mode 'tree', 'view', and 'form'.

#### `JSONEditor.expand(path, isExpand, recursive)`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking aloud here: it may be nice to change the signature

JSONEditor.expand(path, isExpand, recursive) 

to

JSONEditor.expand({path, isExpand, recursive}) 

Then you can pass the options even more easily from one editor to an other:

const editor1 = new JSONEditor(container1, { onExpand: (props) => editor2.expand(props) })
const editor2 = new JSONEditor(container2, { onExpand: (props) => editor1.expand(props) })

What do you think?

  * Added JSONEditor.expand() method
  * Added examples/25_sync_node_expand.html
@himanshu520
Copy link
Contributor Author

Thanks for reviewing. I have updated the PR as per suggestions, please have a look again.

PS - I would like to add that though editor.expand() and options.onExpand() helps to sync node expanded/collapsed states across editors, my main motive was to use it to show JSON diff using two editors where common/modified nodes are always aligned (shown side-by-side, as most text-based-diff tools). However, I later realised that it is not possible to handle the scenario when there are additonal/missing nodes - then the alignment of common/modified nodes cannot be maintained. I guess, I would have to write something from scratch 😐️ but hope that the editor.expand() and options.onExpand() APIs would be useful for others 😀️

@josdejong josdejong merged commit d9c3220 into josdejong:develop May 31, 2022
@josdejong
Copy link
Owner

Thanks @himanshu520 , looks good 👌 .

You're right, expanding only works if a path exists. I'll add a remark on top in the example explaining this.

@josdejong
Copy link
Owner

Published now in v9.8.0

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

Successfully merging this pull request may close these issues.

None yet

2 participants