Skip to content

Commit 779ded7

Browse files
authored
Merge pull request #164 from oleksandr-danylchenko/fix-checking-null-elements-annotability
⚠️ Fixed checking `null` selection `anchorNode` for being `not-annotatable` ⚠️
2 parents d82e9f9 + 11dc436 commit 779ded7

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

packages/text-annotator/src/SelectionHandler.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ export const SelectionHandler = (
7575
const onSelectionChange = debounce((evt: Event) => {
7676
const sel = document.getSelection();
7777

78+
/**
79+
* In iOS when a user clicks on a button, the `selectionchange` event is fired.
80+
* However, the generated selection is empty and the `anchorNode` is `null`.
81+
* That doesn't give us information about whether the selection is in the annotatable area
82+
* or whether the previously selected text was dismissed.
83+
* Therefore - we should bail out from such a range processing.
84+
*
85+
* @see https://github.com/recogito/text-annotator-js/pull/164#issuecomment-2416961473
86+
*/
87+
if (!sel?.anchorNode) {
88+
return;
89+
}
90+
7891
/**
7992
* This is to handle cases where the selection is "hijacked"
8093
* by another element in a not-annotatable area.
@@ -189,7 +202,7 @@ export const SelectionHandler = (
189202
const currentIds = new Set(selected.map(s => s.id));
190203
const nextIds = Array.isArray(hovered) ? hovered.map(a => a.id) : [hovered.id];
191204

192-
const hasChanged =
205+
const hasChanged =
193206
currentIds.size !== nextIds.length ||
194207
!nextIds.every(id => currentIds.has(id));
195208

0 commit comments

Comments
 (0)