Skip to content

Commit

Permalink
Allow pasting and deleting multiple lines when there are readonly ran…
Browse files Browse the repository at this point in the history
…ges (#7383)
  • Loading branch information
dem4ron authored Jan 24, 2025
1 parent 5abe4d6 commit 36bc075
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,30 @@ export const readOnlyRangesStateField = StateField.define<ReadOnlyRange[]>({
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
})
}
Expand Down

0 comments on commit 36bc075

Please sign in to comment.