Skip to content

Commit

Permalink
Merge pull request #137 from oleksandr-danylchenko/#136-fix-not-dismi…
Browse files Browse the repository at this point in the history
…sssed-annotation-on-native-range-click

#136 Fixed missing annotation dismissal clicking over native selection range
  • Loading branch information
rsimon authored Sep 2, 2024
2 parents dc4e1dc + 3444946 commit d88a1fa
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions packages/text-annotator/src/SelectionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const SelectionHandler = (
const hasChanged =
annotatableRanges.length !== currentTarget.selector.length ||
annotatableRanges.some((r, i) => r.toString() !== currentTarget.selector[i]?.quote);

if (!hasChanged) return;

currentTarget = {
Expand All @@ -98,7 +98,7 @@ export const SelectionHandler = (
} else {
// Proper lifecycle management: clear selection first...
selection.clear();

// ...then add annotation to store...
store.addAnnotation({
id: currentTarget.annotation,
Expand Down Expand Up @@ -149,13 +149,26 @@ export const SelectionHandler = (

const timeDifference = evt.timeStamp - lastPointerDown.timeStamp;

// Just a click, not a selection
if (document.getSelection().isCollapsed && timeDifference < 300) {
currentTarget = undefined;
clickSelect();
} else if (currentTarget) {
selection.userSelect(currentTarget.annotation, evt);
}
/**
* We must check the `isCollapsed` within the 0-timeout
* to handle the annotation dismissal after a click properly.
*
* Otherwise, the `isCollapsed` will return an obsolete `false` value,
* click won't be processed, and the annotation will get falsely re-selected.
*
* @see https://github.com/recogito/text-annotator-js/issues/136
*/
setTimeout(() => {
const sel = document.getSelection()

// Just a click, not a selection
if (sel?.isCollapsed && timeDifference < 300) {
currentTarget = undefined;
clickSelect();
} else if (currentTarget) {
selection.userSelect(currentTarget.annotation, evt);
}
});
}

container.addEventListener('pointerdown', onPointerDown);
Expand All @@ -172,6 +185,7 @@ export const SelectionHandler = (

container.removeEventListener('selectstart', onSelectStart);
document.removeEventListener('selectionchange', onSelectionChange);

}

return {
Expand Down

0 comments on commit d88a1fa

Please sign in to comment.