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

Improve the enforce string option. Allow user to insert a number or a boolean value from the Insert context menu

Open bostondevin opened this issue 2 years ago • 6 comments

Currently user can't insert a number (only a string) in tree mode - maybe add an option for Number to the context menu? Also boolean too.

bostondevin avatar Jun 19 '22 17:06 bostondevin

The way to insert a text, number, or boolean is to click "Insert value" from the context menu. What you type as value determines what the type of the value will be, if you enter 123 it will be a number, if you enter true it will be a boolean, if you enter hello world it will be a string.

Is it that that icon of the "Insert value" button (a double quote) is misleading in this regard?

josdejong avatar Jun 20 '22 06:06 josdejong

Aha! Yes - that did make me assume it was inserting a string because of those quotes!

I was trying to somehow convert an existing string value to a number without going into the code view and deleting the quotes. The Convert To menu has Object and Array as options - should it have String, Number, Boolean as well excluding from the options whatever type it currently is?

bostondevin avatar Jun 20 '22 13:06 bostondevin

Ok I'll see if I can come up with a better icon.

Normally there is no need to specify the type explicitly, the editor detects the type itself and turns it into a number or boolean when needed. This conversion only works after you have edited the value though, it's not applied when loading a document. Does that working for you?

(On a side note: in rare cases, you want to enter a string containing a numeric value, you can enforce to keep the value as string via the dropdown of "Edit value", and then select "Enforce string").

josdejong avatar Jun 22 '22 13:06 josdejong

I discovered (by accident!) that clicking Edit Value->Enforce string on the pop-up menu when the current value is already a string representation of a number, will actually change the value a number (confirmed in the Code view).

So that menu option is actually a toggle. I'm not sure if that's by design or a bug though? If it's by design then we need the menu text to say "Enforce number" when the value is a string representation of a number. Note: it works with floats as well, so "Enforce number" rather than "Enforce integer" if you want to go that way.

brownieboy avatar Jun 29 '22 07:06 brownieboy

Thanks for your input @brownieboy. I'm indeed not totally happy with how it works now, the feature is quite deep hidden away and a bit vague to understand.

This menu item "Enforce string" is indeed a toggle, it has a toggle icon which is checked when the value is a string containing a numeric value, and unchecked when not. If you check it, the contents will be turned into a string. If you uncheck it (like you describe) it will be turned into a number if the contents are a numeric value.

Thinking about a solution: maybe I can add a new entry to the list "Convert to: Structure | Object | Array | value". It can become a list "Structure | Object | Array | string | auto", and it should highlight the type that the value currently has. When selecting "string" it will keep the value always a string also when it contains a numeric value, and when selecting "auto", it will convert the value into whatever contents the value has, that can be: null, boolean, number, string.

josdejong avatar Jun 29 '22 08:06 josdejong

This menu item "Enforce string" is indeed a toggle, it has a toggle icon which is checked when the value is a string containing a numeric value, and unchecked when not.

Yes, I'd totally missed the checkbox. So that makes sense now.

Thanks for your quick response, @josdejong .

brownieboy avatar Jun 29 '22 23:06 brownieboy