josdejong / svelte-jsoneditor

A web-based tool to view, edit, format, repair, query, transform, and validate JSON
https://jsoneditoronline.org
Other
816 stars 108 forks source link

throw 'RangeError: Invalid position 1 in document of length 0' when calling `set` method with JSON value under text mode #381

Closed cloydlau closed 5 months ago

cloydlau commented 6 months ago

Hello jos, Here is the minimal reproduction: https://github.com/cloydlau/reproduction.git

thaneuk commented 5 months ago

A quick work around I'm using is to create it in tree mode; then immediately call jsonEditor.updateProps({ mode: Mode.text }) to convert it to text.

josdejong commented 5 months ago

Thanks for the reproduction Cloyd, that helps.

The steps to reproduce are:

The error only occurs when the new contents have a large length, i.e. to get the error again, you can call editor.set({ text: '12345' }). But the error does not occur when making the contents shorter, like editor.set({ text: '12' }).

I haven't figured out what is causing this error. When using editor.set, a new instance of the code editor (CodeMirror) is created and hence should not have any knowledge about the previous contents. Help debugging this further would be welcome.

josdejong commented 5 months ago

Fixed now in v0.21.3

thaneuk commented 5 months ago

I'm still seeing the issue with vanilla-jsoneditor.

"name": "vanilla-jsoneditor" "version": "0.21.3"

new JSONEditor({
  target: element,
  props: {
    readOnly: true,
    mainMenuBar: false,
    navigationBar: false,
    statusBar: false,
    mode: Mode.text
  }
});
RangeError: Invalid position 1 in document of length 0
    at TextLeaf.lineAt (index.js:10:19)
    at WrappedLineIndent.getVisibleLines (index.js:58:40)
    at WrappedLineIndent.addStyleToBuilder (index.js:40:35)
    at Object.read (index.js:31:30)
    at index.js:7249:34
    at Array.map (<anonymous>)
    at EditorView.measure (index.js:7247:42)
    at index.js:7382:79
josdejong commented 5 months ago

Thanks for testing with the latest version. I've tried to reproduce your issue but without success so far:

https://jsbin.com/pufufol/edit?html,output

Can you share more details?

thaneuk commented 5 months ago

I added an update to the jsbin code and got the error.

editor.update({
  json: {}
})
"RangeError: Invalid position 1 in document of length 0
    at t.value (https://cdn.jsdelivr.net/npm/vanilla-jsoneditor@0.21.3/standalone.min.js:25:416667)
    at e.value (https://cdn.jsdelivr.net/npm/vanilla-jsoneditor@0.21.3/standalone.min.js:25:879553)
    at e.value (https://cdn.jsdelivr.net/npm/vanilla-jsoneditor@0.21.3/standalone.min.js:25:879060)
    at Object.read (https://cdn.jsdelivr.net/npm/vanilla-jsoneditor@0.21.3/standalone.min.js:25:878894)
    at https://cdn.jsdelivr.net/npm/vanilla-jsoneditor@0.21.3/standalone.min.js:25:620350
    at Array.map (<anonymous>)
    at e.value (https://cdn.jsdelivr.net/npm/vanilla-jsoneditor@0.21.3/standalone.min.js:25:620320)
    at https://cdn.jsdelivr.net/npm/vanilla-jsoneditor@0.21.3/standalone.min.js:25:622924"
thaneuk commented 5 months ago

I also tested with an initial value, incase it was related to using update without an initial value to start but had no luck.

.update https://jsbin.com/carasariki/1/edit?html,console,output .set https://jsbin.com/yexapezasi/1/edit?html,console,output

josdejong commented 5 months ago

Thanks, I'll do further debugging.

josdejong commented 5 months ago

As a workaround you can, you can just not call .update right after the editor is created (the contents is passed to along the constructor anyway). If you need that right after construction, you can maybe add a setTimeout(() => { editor.update(...) }) for the time being.

thaneuk commented 5 months ago

This is the work around I'm using

https://jsbin.com/desowehibi/1/edit?html,console,output

I have an Angular component using the editor; so it's created in the component constructor and the data is set in an @Input set function. So the workaround I'm using flows best in this scenario.

josdejong commented 5 months ago

It looks like the issue originates from the codemirror-wrapped-line-indent extension that we use, I've reported the issue there: https://github.com/fauzi9331/codemirror-wrapped-line-indent/issues/1.

josdejong commented 5 months ago

This should be fixed now in v0.21.4, can you give it a try @thaneuk ?

thaneuk commented 5 months ago

Can confirm, not having the issue now with the latest update.

Many thanks for the attention to this.

josdejong commented 5 months ago

Thanks for your feedback, good to hear!