Skip to content

Commit

Permalink
Fix check for allowMultipleSelections
Browse files Browse the repository at this point in the history
FIX: Make sure transactions cannot add multiple selections when
`allowMultipleSelections` is false.

Closes codemirror/dev#1288
  • Loading branch information
marijnh committed Nov 2, 2023
1 parent 10d5f42 commit a14b093
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ export class EditorState {
} else {
startValues = tr.startState.values.slice()
}
new EditorState(conf, tr.newDoc, tr.newSelection, startValues, (state, slot) => slot.update(state, tr), tr)
let selection = tr.startState.facet(allowMultipleSelections) ? tr.newSelection : tr.newSelection.asSingle()
new EditorState(conf, tr.newDoc, selection, startValues, (state, slot) => slot.update(state, tr), tr)
}

/// Create a [transaction spec](#state.TransactionSpec) that
Expand Down
10 changes: 10 additions & 0 deletions test/test-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ describe("EditorState", () => {
ist(state3.facet(facet).join(), "2,1")
})

it("blocks multiple selections when not allowed", () => {
let cursors = EditorSelection.create([EditorSelection.cursor(0), EditorSelection.cursor(1)])
let state = EditorState.create({
selection: cursors,
doc: "123"
})
ist(state.selection.ranges.length, 1)
ist(state.update({selection: cursors}).state.selection.ranges.length, 1)
})

describe("changeByRange", () => {
it("can make simple changes", () => {
let state = EditorState.create({doc: "hi"})
Expand Down

0 comments on commit a14b093

Please sign in to comment.