valentin-morice / filament-json-column

A simple package to view and edit your JSON columns in Filament
MIT License
17 stars 4 forks source link

Update JSON editor on state update #5

Open Confectus opened 1 week ago

Confectus commented 1 week ago

I was trying to update the editor value by using the $set callable in Filament. By the way, only the JSON viewer is updated.

I think the project you based it on had the same problem. If you can think of any solution that could be implemented?

https://github.com/invaders-xx/filament-jsoneditor/issues/23

Confectus commented 1 week ago

The only solution I find viable is to listen to some event when instantiating the JSONEditor object.

For example, it could be something like this:

let json_editor = new JSONEditor($refs.editor, options); json_editor.set(JSON.parse(JSON.stringify(state)));

if(typeof json_editor !== 'undefined'){
    json_editor = new JSONEditor($refs.editor, options);
    json_editor.set(JSON.parse(JSON.stringify(state)));
} else {
    let json_editor = new JSONEditor($refs.editor, options);
    json_editor.set(JSON.parse(JSON.stringify(state)));

    window.addEventListener('json-state-updated', function(event) {
        json_editor.set(JSON.parse(JSON.stringify(event.detail.data)));
    });
}