Skip to content

feat: copy the path of the selected node (#165, #23)#219

Open
simon-vincent wants to merge 1 commit into
triggerdotdev:mainfrom
simon-vincent:features/copy-node-path
Open

feat: copy the path of the selected node (#165, #23)#219
simon-vincent wants to merge 1 commit into
triggerdotdev:mainfrom
simon-vincent:features/copy-node-path

Conversation

@simon-vincent

Copy link
Copy Markdown

What

Adds the ability to copy the path of the currently-selected node, in two formats:

Format Example
JSONPath $.data[0]["user-name"]
JavaScript accessor data[0]["user-name"]

Two ways to use it:

  • A clipboard button in the PathBar (next to the pencil-edit button) opens a small menu: Copy as JSONPath / Copy as JavaScript, with the existing "Copied!" feedback.
  • A Shift+P keyboard shortcut copies the JSONPath form of the selected node directly — mirroring the existing Shift+C ("copy selected node") shortcut.

Closes #165 and #23 (both request this; #165 was prototyped in a fork by a user).

How it works

Path formatting lives in a pure, dependency-light utility, app/utilities/pathFormatter.ts:

formatPath(path: string, format: "jsonpath" | "js"): string

It walks the JSONHeroPath components and:

  • uses bracket notation for array indices ([0]),
  • uses dot notation for keys that are safe JS identifiers,
  • falls back to quoted bracket notation for keys that aren't (e.g. user-name, keys with spaces/quotes), escaping embedded quotes/backslashes.

Root selection → $ (JSONPath) / "" (JS). No new dependencies — reuses the existing UI/Popover wrapper, react-hotkeys-hook, and @jsonhero/path.

Tests

  • tests/pathFormatter.test.ts — 12 unit tests covering nested keys, array indices, unsafe/hyphenated keys, quote escaping, leading-index, and the root case. Written test-first.
  • Full suite: 32/32 passing (npm test), npm run build ✅.
  • Manually verified in the running app: selecting data[0]["user-name"] and using both menu items and Shift+P produced the expected strings.

Notes

…otdev#23)

Adds a way to copy the path to the currently-selected JSON node, in two
formats:

- JSONPath: $.data[0]["user-name"]
- JavaScript accessor: data[0]["user-name"]

A clipboard button in the PathBar opens a small menu with both options,
and a Shift+P shortcut copies the JSONPath form directly (mirroring the
existing Shift+C 'copy selected node' shortcut).

Path formatting lives in a pure, unit-tested utility (formatPath) that
walks the JSONHeroPath components, using bracket notation for array
indices and for keys that are not safe JS identifiers.
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.

Copy path of a selected node

1 participant