You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+77
Original file line number
Diff line number
Diff line change
@@ -224,6 +224,8 @@ content: Content
224
224
225
225
Pass the JSON contents to be rendered in the JSONEditor. `Content` is an object containing a property `json` (a parsed JSON document) or `text` (a stringified JSON document). Only one of the two properties must be defined. You can pass both content types to the editor independent of in what mode it is. You can use two-way binding via `bind:content`.
226
226
227
+
> IMPORTANT: only make immutable changes to `content`. Mutable changes will mess up history and rendered contents. See section [Immutability](#immutability).
Update the loaded content, keeping the state of the editor (like expanded objects). You can also call `editor.updateProps({ content })`. See also method `set(content)`.
677
681
682
+
> IMPORTANT: only apply immutable changes to `content`. Mutable changes will mess up history and rendered contents. See section [Immutability](#immutability).
Apply a JSON patch document to update the contents of the JSON document. A JSON patch document is a list with JSON Patch operations.
685
691
692
+
> IMPORTANT: only apply immutable changes to the contents. Mutable changes will mess up history and rendered contents. See section [Immutability](#immutability).
693
+
686
694
#### updateProps
687
695
688
696
```ts
@@ -937,6 +945,75 @@ When updating CSS variables dynamically, it is necessary to refresh the via `edi
937
945
<JSONEditorbind:this="{editorRef}".../>
938
946
```
939
947
948
+
## Immutability
949
+
950
+
It is important that the `content` of the editor is only updated in an immutable way. Mutating the `content` will break the history (undo/redo), and will not always immediately update the user interface according to the changes.
951
+
952
+
The reasons for requiring immutable changes are:
953
+
954
+
1. It is necessary in order to support history (undo/redo).
955
+
2. It allows efficiently re-rendering only changed sections of the user interface.
956
+
957
+
Other advantages of an immutable way of working are that it makes the data that you work with much more predictive and less error-prone. You can learn more about immutability by searching for articles or videos about the subject, such as [this video](https://youtu.be/Wo0qiGPSV-s) or [this article](https://www.freecodecamp.org/news/immutability-in-javascript-with-examples/). Immutability is not _always_ the best choice, but in the case of this JSON Editor we're dealing with large and deeply nested data structures, in which we typically make only small changes like updating a single nested value. An immutable approach really shines here, enabling `svelte-jsoneditor` to smoothly render and edit JSON documents up to 512 MB.
958
+
959
+
Here is an example of a mutable change:
960
+
961
+
```js
962
+
// mutable change (NOT SUPPORTED!)
963
+
function updateDate() {
964
+
const lastEdited =newDate().toISOString()
965
+
const content =toJsonContent(myJsonEditor.get())
966
+
content.json.lastEdited=lastEdited// <- this is a mutable change
967
+
myJsonEditor.update(content)
968
+
// ERROR: The UI will not update immediately but only update after changing something
969
+
// inside the editor like the selection. And most importantly, history is broken now,
970
+
// because the original document is mutated. You cannot undo this action.
971
+
}
972
+
```
973
+
974
+
Instead, you can apply the same change in an immutable way. There are various options for that:
975
+
976
+
```js
977
+
// immutable change using a libary like "mutative" or "immer" (efficient and easy to work with)
## Differences between `josdejong/svelte-jsoneditor` and `josdejong/jsoneditor`
941
1018
942
1019
This library [`josdejong/svelte-jsoneditor`](https://github.com/josdejong/svelte-jsoneditor/) is the successor of [`josdejong/jsoneditor`](https://github.com/josdejong/jsoneditor). The main differences are:
0 commit comments