-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get the JSON value as string from content parameter of .onChange()? #173
Comments
The editor can indeed give either Here is a util function to convert function toTextContent(content: Content) : TextContent {
return isTextContent(content)
? content
: { text: JSON.stringify(content.json) }
} Good point to add this util function to the library, I'll do that 👍 The |
…ontent`, `toTextContent`, `toJSONContent` (#173) BREAKING CHANGE: Not exporting a set of undocumented utility functions anymore: `isValueSelection`, `isKeySelection`, `isInsideSelection`, `isAfterSelection`, `isMultiSelection`, `isEditingSelection`, `createValueSelection`, `createKeySelection`, `createInsideSelection`, `createAfterSelection`, `createMultiSelection`. And not exporting components `SortModal` and `TransformModal` anymore.
I've now published the following utility functions in I've also done some bookkeeping in |
@josdejong I got a similar issue, I wanna get one source of true of valid json / content when "onChange". |
The "one source of truth" that is always holding the editor contents is If you want a simpler model, you can always convert JSON to text, then the contents is just a string. It can be heavy on performance though to convert the JSON to text on every change. |
You're right, but I noticed there is a problems with the current way in my setup:
So I prefer to have maybe another valid value which will be used only for onChange and have one type only (object/json). |
You can't always represent the text as a valid JSON object, like this content where the user is halfway typing an array. An you will indeed loose indentation:
You can't (always) have a valid JSON object. You can always represent the contents as a string like I explain. You can also just accept the |
so I suggest this: |
If you need that, you can indeed create a |
I'm using this component inside my Vue project but I think it shouldn't matter.
I created a small reproduction example https://stackblitz.com/edit/vitejs-vite-tfqj8h?file=src/JsonEditor.vue
As you can see I have a small component wrapping the editor. This component takes in the raw JSON content as a string via props. Whenever the change event triggers I want to to send back the new string value to the parent via events. Based on
type Content = JSONContent | TextContent
I check if I can read the plain text fromTextContent
. If that's not the case I have to deal withcontent.json
. Unfortunately It's not clear to me how to deal with it. As you can see in my example I'm trying to read a raw string from it but it's alwaysundefined
.What needs to be done if the content is of type
JSONContent
to read the JSON string from it?Sidenotes
It would be helpful adding a type guard ( as you can see in the example I implemented my own one for now ... )
Second, if the solution should be straight forward, would you mind adding a function that returns the content as text from the JSON value? Maybe other people might want to read the value from it too.
The text was updated successfully, but these errors were encountered: