josdejong / svelte-jsoneditor

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

inserting json code in the selected location #319

Closed tatarysh closed 6 months ago

tatarysh commented 8 months ago

I write a lot of JSON code in which I have to write the same structure, I would like to automate it and create a keyboard shortcut and insert ready-made json, is it possible and if so, how? I tried using document.execCommand and navigator.clipboard.writeText, unfortunately both solutions do not work.

josdejong commented 8 months ago

I'm, not sure what you mean. Can you maybe explain it with an example?

Do you mean you're inserting JSON from clipboard into the selected location? You can do that (by hand) by pasting with Ctrl+V. Or do you mean you would like a keyboard shortcut for the "Insert Structure" button in the context menu?

If you want to insert JSON programmatically, it may be easiest to just take your JSON data and the current selection and insert the JSON that you want directly into the JSON (not via the editor)?

tatarysh commented 8 months ago

I would like to add an option that will add json code where the cursor is, but the json code is generated and is always different (the structure is always the same)

my sample json:

{
   "name": "{name}",
   "data": { 
      "key": "{unique-string}"
   } 
}

maybe some event that will accept a function that will return what should be added to json?

josdejong commented 8 months ago

You can keep track on where the cursor is via onSelect. You can add a keydown event listener to the div where you have created the JSON editor, and listen for the quick key that you want to use for your insert action. When that triggers, you can insert the contents in one of the following ways: