svelte-jsoneditor icon indicating copy to clipboard operation
svelte-jsoneditor copied to clipboard

Flag for programmatic vs UI changes

Open wernerstucky opened this issue 2 years ago • 4 comments

Hi Jos I'm a big fan of your original library, we used it to develop a collaborative JSON editor.

The key functionality we relied on was that setting the content programmatically didn't emit a onChange. This prevented loops in the 2 editors updating each other. When we set the content a onChange didn't happen, when the human made a change we picked that up and transmitted to the other editor and did a programatic update.

Is there anyway to enhance the Svelte component to maybe pass a flag letting us know if it was a change from called the set() or update() functions or if the change was a result of a UI edit.

Regards

wernerstucky avatar Sep 17 '22 13:09 wernerstucky

I’d been struggling on this too while making json-editor-vue, I use two flags to solve this.

cloydlau avatar Sep 18 '22 03:09 cloydlau

Thanks @cloydlau I'll go have a look at how you solved it

wernerstucky avatar Sep 19 '22 13:09 wernerstucky

Thanks Werner, very good point.

For reference: the reason that I recently change the behavior from onChange events on user changes only to all changes was the discussion here #128.

I agree that the old behavior is also very handy to prevent infinite loops and needless callbacks. So both cases can be useful, and your idea to be able to distinguish programmatic vs user changes makes sense.

For the time being: you can indeed set some flag before doing a programmatic update, and reset it after the update, and in the onChange you can check for this flag. And alternatively, it may be possible to compare the updated content against the previous contents in onChange, and if there is no difference ignore the change.

josdejong avatar Sep 19 '22 18:09 josdejong

Thanks Jos, I'll do the flag thing for now.

wernerstucky avatar Sep 20 '22 08:09 wernerstucky

In v0.22.0 we've changed the behavior of onChange to not trigger anymore for programmatic changes. This resolves this issue indirectly, no flag needed anymore.

josdejong avatar Mar 01 '24 09:03 josdejong