josdejong / jsoneditor

A web-based tool to view, edit, format, and validate JSON
http://jsoneditoronline.org
Apache License 2.0
11.44k stars 2.03k forks source link

Call functions "isValidColor"/"findUniqeName" #1604

Closed mbeekhuis closed 4 days ago

mbeekhuis commented 2 weeks ago

Hi Jos

I've discovered that I've defined a few functions in my code that are identical to some of the functions defined in jsoneditor. It would be very nice if I could strip these functions in my code and call the jsoneditor functions instead. Is it possible to call "isValidColor" or "findUniqueName" from a second js file?

Thank's a lot cheers Marc

josdejong commented 2 weeks ago

I'm ok with exposing these util files in a separate file 👍 . We could add them to the following unofficial exports and make the exports official:

https://github.com/josdejong/jsoneditor/blob/develop/src/js/JSONEditor.js#L480-L488

Or do you prefer exporting them in a separate file like import { isValidColor } from 'jsoneditor/utils.js'?

mbeekhuis commented 2 weeks ago

Hi Jos thank's a lot! Well, honestly I'm not really familiar with modules, so I can't tell you what makes more sense. At the end I'd like to be able to call the functions like

editor.isValidColor(color) editor.findUniqueName(field, existingFieldNames)

josdejong commented 5 days ago

The easiest will be:

JSONEditor.isValidColor(color)
JSONEditor.findUniqueName(field, existingFieldNames)

I think that will be just as easy from your point of view :)

mbeekhuis commented 4 days ago

Hi Jos I've written editor.isValidColor(color) cause of my implementation: const editor = new JSONEditor(jsoneditor, options), which is of course a bit confusing ;-)

Since these functions are quite small and easy to reproduce, it is not a problem if we close the issue if nobody wants to implement it.

mbeekhuis commented 4 days ago

I've just recognized that there's no need for an additonal "isValidColor" function in my implementation, since jsoneditor adds a class "jsoneditor-color-value" to the dom node if there's a valid color value. So I can simply check if the node has this class attached. Still need to define an additonal isUniqeName function but since my use case is probably unique as well, I'll close this issue. Sorry for taking up your time.

josdejong commented 4 days ago

Okido, sounds good!

The function findUniqueName is only a few lines of code so there is not that much duplication when copying it in your code base. Sounds good to leave it as it is right now.