josdejong / svelte-jsoneditor

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

Transform is occasionally applied without pressing button #401

Closed Eldresh closed 4 months ago

Eldresh commented 4 months ago

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 commented 4 months ago

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

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 commented 4 months ago

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

josdejong commented 4 months ago

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

josdejong commented 4 months ago

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

Eldresh commented 4 months ago

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 commented 4 months ago

👍