jsoneditor icon indicating copy to clipboard operation
jsoneditor copied to clipboard

Reevaluate "onEditable"

Open neil-hardlight opened this issue 3 years ago • 4 comments

I want to use the "code" or "tree" modes for viewing JSON in readonly mode. However, I want to be able to switch the editor into read/write mode dynamically. At the moment "onEditable" is not reevaluated - can I force JSONEditor to reevaluate whether the JSON can be edited? The only method I've found is to switch mode and then back again - but this comes with its own problems (like cursor location changing, tree collapsing).

        const options = {
            mode: 'code',
            modes: ['code', 'form', 'text', 'tree', 'view', 'preview'],
            onEditable: function () {
                return self.editJSON; <-- this can change dynamically.
            }
        };
        this.jsoneditor = new JSONEditor(container, options);

Perhaps something like?:

this.jsoneditor.reevaluateOnEditable();

Thanks, -Neil

neil-hardlight avatar Nov 08 '21 10:11 neil-hardlight

Good point, thanks for bringing this up Neil.

Maybe we can extend the existing method this.jsoneditor.refresh() for that. This method is used for example to refresh class names in case you have a custom onClassName configured. (Or does it already work for onEditable too? I don't know by heart)

josdejong avatar Nov 08 '21 12:11 josdejong

I did try refresh but it didn't have the desired effect.

neil-hardlight avatar Nov 08 '21 13:11 neil-hardlight

Thanks for testing that. Let's see if we can extend refresh then to also trigger onEditable.

Anyone able to implement this? Help would be welcome.

josdejong avatar Nov 09 '21 18:11 josdejong

My work around is maintaining two instances of JSON editor, one editable and one read-only. Then write some code to decide which one to show based on my scenario. Not perfect but works. Hopefully we can get some update soon.

husbd avatar Nov 23 '21 01:11 husbd