diff --git a/src/lib/components/modes/textmode/TextMode.svelte b/src/lib/components/modes/textmode/TextMode.svelte index 54b9ca9b..17b1efa6 100644 --- a/src/lib/components/modes/textmode/TextMode.svelte +++ b/src/lib/components/modes/textmode/TextMode.svelte @@ -555,7 +555,9 @@ const state = EditorState.create({ doc: initialText, - selection: toCodeMirrorSelection(externalSelection), + selection: isValidSelection(externalSelection, initialText) + ? toCodeMirrorSelection(externalSelection) + : undefined, extensions: [ keymap.of([indentWithTab, formatCompactKeyBinding]), linterCompartment.of(createLinter()), @@ -632,6 +634,14 @@ : false } + function isValidSelection(selection: JSONEditorSelection | null, text: string): boolean { + if (!isTextSelection(selection)) { + return false + } + + return selection.ranges.every((range) => range.anchor < text.length && range.head < text.length) + } + function toRichValidationError(validationError: ValidationError): RichValidationError { const { path, message, severity } = validationError const { line, column, from, to } = findTextLocation(normalization.escapeValue(text), path)