jdorn / json-editor

JSON Schema Based Editor
MIT License
5.8k stars 1.07k forks source link

Extending string editor does not produce same results #310

Open brettz9 opened 9 years ago

brettz9 commented 9 years ago

Hi,

I was looking to build my own code editor type (where one could dump formatted text into an SCEditor instance, and then my code would split this up automatically into separate form fields and hide the field for dumping).

However, when I do a simple extend and successfully register my new type, SCEditor doesn't get applied, despite my new type not overriding a single method.

JSONEditor.defaults.editors.splitter = JSONEditor.defaults.editors.string.extend({
    // To keep things simple, we'll try with no overridden methods
});
JSONEditor.defaults.resolvers.unshift(function(schema) {
    if (schema.type === 'string' && schema.splitter) {
        return 'splitter';
    }
});

I would expect that an extension of the string type (or any other type) would behave in the same way as the base type unless one has overridden a method.

brettz9 commented 9 years ago

Looking more carefully, I see the source of the problem.

In defaults.js, there is this code:

for(var i in JSONEditor.defaults.editors) {
  if(!JSONEditor.defaults.editors.hasOwnProperty(i)) continue;
  JSONEditor.defaults.editors[i].options = JSONEditor.defaults.editors.options || {};
}

One problem is that it runs immediately so that it is not possible for anyone to add to JSONEditor.defaults.editors.options whether before or after the jsoneditor.js file loads.

And since this runs before the JSONEditor is accessible, one's own editor plugins must therefore remember to add the options object themselves.

I'd therefore suggest either a class method like say JSONEditor.setDefaultEditorOptions() (or a registration API for third-party editors) or jsoneditor.js checking for an already set global like JSONEditorDefaultEditorOptions or such.

brettz9 commented 9 years ago

And to be clear, I was trying to set this after my editor plugin code, but before instantiating my JSONEditor object:

JSONEditor.defaults.editors.splitter.options.wysiwyg = true;