josdejong / svelte-jsoneditor

A web-based tool to view, edit, format, repair, query, transform, and validate JSON
https://jsoneditoronline.org
Other
901 stars 109 forks source link

Allow copy node's path #13

Closed elvuel closed 2 years ago

elvuel commented 3 years ago

Could be copy on ContextMenu or emit on mousedown.

josdejong commented 3 years ago

Thanks, good suggestion. I think this should be an explicit button in the ContextMenu, and possibly also a quick key.

elvuel commented 3 years ago

@josdejong how about copy JSONPath instead of or over slice?

josdejong commented 3 years ago

Good point about what format to use: JSONPath, JSONPointer, JavaScript notation...

An other idea: I also want to implement a path (like "bread crumbs" on top, like in the old editor:

afbeelding

We could also make the copy button there.

josdejong commented 2 years ago

Another idea it would be nice if you could paste a path too, and then scroll towards this path in the document.

dranitski commented 2 years ago

vote up for copy node path button near the breadcrumbs. Very handy and easy to implement :+1:

feidhlimx commented 2 years ago

Hi, I had to recently work around this issue by getting the path from a selected node through onChange (patchResult.from) function then reverting the changes in the document - basically making the doc readOnly but keeping the editable state in order to get the currently selected path. This fix to the API would be really helpful, as the info is already on hand... The path should be available on the API onClick or something similar...

josdejong commented 2 years ago

I'm working on this feature right now.

I was wondering: what kind of path notation do you guys use? A JSON Path like $.data[2].nested.property, or a JSON Pointer like /data/2/nested/property?

dranitski commented 2 years ago

@josdejong thanks a lot! I guess any will be fine if we can easily parse, split, modify it as we want. Some kind of trigger (onNodePathCopy) will be cool.

personally I like the first one $.data[2].nested.property as default

josdejong commented 2 years ago

Yes I prefer a notation like $.data[2].nested.property better too. I'll start with that.

What would be the use case for a onNodePathCopy callback?

dranitski commented 2 years ago

@josdejong it would let anyone to parse and copy node path in the way one wants, not only as default $.data[2].nested.property format. Just an idea.

josdejong commented 2 years ago

Yes, that makes sense. I have a PR #164 ready. I have implemented a new option pathParser: { parse, stringify }, allowing you to customize how the path is parsed/stringified. By default it uses a JSON Path notation, but you can configure it to use JSON Pointer instead or anything you want.

josdejong commented 2 years ago

First version now published in v0.7.10.

Feedback is very welcome, I can imagine there are good ideas to refine this solution further.

feidhlimx commented 1 year ago

What would be the use case for a onNodePathCopy callback?

It would be nice to have the JSON path value through the API - on the current selected node, not just after a copy.

This would basically be the same info that is displayed in the navigation bar, but in the API.

How could I achieve this?

I imagine it would be part of the exposed object jsonEditor - which there is a ref to...

josdejong commented 1 year ago

@feidhlimx there are plans to implement an API to get and set the selection, see #163. I think that will address your suggestion.