@@ -162,6 +162,20 @@ export const SelectionHandler = (
162162 isLeftClick = lastDownEvent . button === 0 ;
163163 } ;
164164
165+ // Helper
166+ const upsertCurrentTarget = ( ) => {
167+ const exists = store . getAnnotation ( currentTarget . annotation ) ;
168+ if ( exists ) {
169+ store . updateTarget ( currentTarget ) ;
170+ } else {
171+ store . addAnnotation ( {
172+ id : currentTarget . annotation ,
173+ bodies : [ ] ,
174+ target : currentTarget
175+ } ) ;
176+ }
177+ }
178+
165179 const onPointerUp = ( evt : PointerEvent ) => {
166180 const annotatable = ! ( evt . target as Node ) . parentElement ?. closest ( NOT_ANNOTATABLE_SELECTOR ) ;
167181 if ( ! annotatable || ! isLeftClick ) return ;
@@ -204,23 +218,8 @@ export const SelectionHandler = (
204218 currentTarget = undefined ;
205219 clickSelect ( ) ;
206220 } else if ( currentTarget ) {
207- // Proper lifecycle management: clear selection first...
208221 selection . clear ( ) ;
209-
210- const exists = store . getAnnotation ( currentTarget . annotation ) ;
211- if ( exists ) {
212- // ...then add annotation to store...
213- store . updateTarget ( currentTarget ) ;
214- } else {
215- // ...then add annotation to store...
216- store . addAnnotation ( {
217- id : currentTarget . annotation ,
218- bodies : [ ] ,
219- target : currentTarget
220- } ) ;
221- }
222-
223- // ...then make the new annotation the current selection
222+ upsertCurrentTarget ( ) ;
224223 selection . userSelect ( currentTarget . annotation , clonePointerEvent ( evt ) ) ;
225224 }
226225 } ) ;
@@ -231,20 +230,12 @@ export const SelectionHandler = (
231230
232231 if ( sel ?. isCollapsed ) return ;
233232
234- const exists = store . getAnnotation ( currentTarget . annotation ) ;
235- if ( exists ) {
236- // ...then add annotation to store...
237- store . updateTarget ( currentTarget ) ;
238- } else {
239- selection . clear ( ) ;
240-
241- // ...then add annotation to store...
242- store . addAnnotation ( {
243- id : currentTarget . annotation ,
244- bodies : [ ] ,
245- target : currentTarget
246- } ) ;
247- }
233+ // When selecting the initial word, Chrome Android fires `contextmenu`
234+ // before selectionChanged.
235+ if ( ! currentTarget || currentTarget . selector . length === 0 )
236+ onSelectionChange ( evt ) ;
237+
238+ upsertCurrentTarget ( ) ;
248239
249240 selection . userSelect ( currentTarget . annotation , clonePointerEvent ( evt ) ) ;
250241 }
@@ -254,23 +245,8 @@ export const SelectionHandler = (
254245 const sel = document . getSelection ( ) ;
255246
256247 if ( ! sel . isCollapsed ) {
257- // Proper lifecycle management: clear selection first...
258248 selection . clear ( ) ;
259-
260- const exists = store . getAnnotation ( currentTarget . annotation ) ;
261- if ( exists ) {
262- // ...then add annotation to store...
263- store . updateTarget ( currentTarget ) ;
264- } else {
265- // ...then add annotation to store...
266- store . addAnnotation ( {
267- id : currentTarget . annotation ,
268- bodies : [ ] ,
269- target : currentTarget
270- } ) ;
271- }
272-
273- // ...then make the new annotation the current selection
249+ upsertCurrentTarget ( ) ;
274250 selection . userSelect ( currentTarget . annotation , cloneKeyboardEvent ( evt ) ) ;
275251 }
276252 }
0 commit comments