diff --git a/app/javascript/components/bootcamp/SolveExercisePage/CodeMirror/extensions/read-only-ranges/readOnlyRanges.ts b/app/javascript/components/bootcamp/SolveExercisePage/CodeMirror/extensions/read-only-ranges/readOnlyRanges.ts index b3dd78045f..2728b83e4a 100644 --- a/app/javascript/components/bootcamp/SolveExercisePage/CodeMirror/extensions/read-only-ranges/readOnlyRanges.ts +++ b/app/javascript/components/bootcamp/SolveExercisePage/CodeMirror/extensions/read-only-ranges/readOnlyRanges.ts @@ -8,26 +8,30 @@ export const readOnlyRangesStateField = StateField.define({ return [] }, update(ranges, tr) { + // if we are adding lines if (tr.startState.doc.lines < tr.state.doc.lines) { const cursor = tr.state.selection.main.head const newLine = tr.state.doc.lineAt(cursor).number + const diff = tr.state.doc.lines - tr.startState.doc.lines return ranges.map((r) => { const rangeLine = r.from if (rangeLine >= newLine) { - return { from: r.from + 1, to: r.to + 1 } + return { from: r.from + diff, to: r.to + diff } } else return r }) } + // if we are deleting lines if (tr.startState.doc.lines > tr.state.doc.lines) { const cursor = tr.state.selection.main.head const lineAtCursor = tr.state.doc.lineAt(cursor) + const diff = tr.startState.doc.lines - tr.state.doc.lines + const lineDeletedAbove = lineAtCursor.number - 1 return ranges.map((r) => { if (r.from > lineDeletedAbove) { - return { from: r.from - 1, to: r.to - 1 } + return { from: r.from - diff, to: r.to - diff } } - return r }) }