@@ -15,6 +15,7 @@ import {
1515 trimRangeToContainer ,
1616 isNotAnnotatable
1717} from './utils' ;
18+ import type { TextAnnotatorOptions } from './TextAnnotatorOptions' ;
1819
1920const CLICK_TIMEOUT = 300 ;
2021
@@ -30,12 +31,13 @@ const SELECTION_KEYS = [
3031export const SelectionHandler = (
3132 container : HTMLElement ,
3233 state : TextAnnotatorState < TextAnnotation , unknown > ,
33- annotatingEnabled : boolean ,
34- offsetReferenceSelector ?: string
34+ options : TextAnnotatorOptions < TextAnnotation , unknown >
3535) => {
3636
3737 let currentUser : User | undefined ;
3838
39+ const { annotatingEnabled, offsetReferenceSelector, selectionMode } = options ;
40+
3941 const setUser = ( user ?: User ) => currentUser = user ;
4042
4143 let currentFilter : Filter | undefined ;
@@ -179,14 +181,20 @@ export const SelectionHandler = (
179181 const hovered =
180182 evt . target instanceof Node &&
181183 container . contains ( evt . target ) &&
182- store . getAt ( evt . clientX - x , evt . clientY - y , currentFilter ) ;
184+ store . getAt ( evt . clientX - x , evt . clientY - y , selectionMode === 'all' , currentFilter ) ;
183185
184186 if ( hovered ) {
185187 const { selected } = selection ;
186188
187- if ( selected . length !== 1 || selected [ 0 ] . id !== hovered . id ) {
188- selection . userSelect ( hovered . id , evt ) ;
189- }
189+ const currentIds = new Set ( selected . map ( s => s . id ) ) ;
190+ const nextIds = Array . isArray ( hovered ) ? hovered . map ( a => a . id ) : [ hovered . id ] ;
191+
192+ const hasChanged =
193+ currentIds . size !== nextIds . length ||
194+ ! nextIds . every ( id => currentIds . has ( id ) ) ;
195+
196+ if ( hasChanged )
197+ selection . userSelect ( nextIds , evt ) ;
190198 } else {
191199 selection . clear ( ) ;
192200 }
0 commit comments