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

Transform is occasionally applied without pressing button #401

Closed
Eldresh opened this issue Feb 5, 2024 · 6 comments
Closed

Transform is occasionally applied without pressing button #401

Eldresh opened this issue Feb 5, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@Eldresh
Copy link

Eldresh commented Feb 5, 2024

I cannot begin to tell you how much I wish I could consistently reproduce this error, but its reasonably consistent when you make an error typing in a function. A good example (in my experience) is typing .splice(2) and then deleting it, which will leave you with 2 things in your array or an empty array without ever once hitting 'transform' and without the ability to undo it, resulting in a loss of data.

I am using the website, https://jsoneditoronline.org/, and encounter this unfortunately quite often.

Browser: Chrome 120.0.6099.217 (Official Build) (64-bit)

@josdejong
Copy link
Owner

josdejong commented Feb 5, 2024

Thanks for reporting, I can consistently reproduce the issue the way you describe it, like:

  • Open the JSON Editor in tree mode
  • Open a JSON array with say 10 items
  • Open the Transform modal
  • Type something like the following as query:
     function query (data) {
       return data.splice(2)
     }
  • Close the transform modal without applying the query (press ESC, or click the X button top right)
  • Click "Collapse All", followed by "Expand All", to see that now only 2 items are left in the array, whereas the data should be unchanged

The reason is that the splice method mutates the original array. In the query the data variable points to the real data, and if you alter it, it is changed for real. The same happens for example if you do say array[0] = null inside the query, tha will clear the first item.

To solve this, we'll have to make sure you can't accidentally mutate the oringial data. Maybe we can use Mutative there somehow. Or maybe I can wrap the data in some readonly proxy, let me give that a try.

@josdejong
Copy link
Owner

This looks like a promising approach: https://github.com/kourge/readonly-proxy

@josdejong josdejong added the bug Something isn't working label Feb 5, 2024
@josdejong
Copy link
Owner

I tried out readonly-proxy but ran into some issues. Solved now via 337f812 (not yet published)

@josdejong
Copy link
Owner

Fixed now in svelte-jsoneditor@0.21.5 and JSON Editor Online v6.15.1. Can you give it a try @Eldresh?

@Eldresh
Copy link
Author

Eldresh commented Feb 5, 2024

Fixed now in svelte-jsoneditor@0.21.5 and JSON Editor Online v6.15.1. Can you give it a try @Eldresh?

Yup. Now it gives me TypeError: unable to delete property, which is much better than accidentally losing everything.

@josdejong
Copy link
Owner

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants