Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚠️ Fixed accessing an obsolete isCollapsed value on pointerup handling ⚠️ #163

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/text-annotator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"colord": "^2.9.3",
"dequal": "^2.0.3",
"hotkeys-js": "^3.13.9",
"poll": "^3.2.2",
"rbush": "^4.0.1",
"uuid": "^11.0.3"
}
}
}
61 changes: 41 additions & 20 deletions packages/text-annotator/src/SelectionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Origin } from '@annotorious/core';
import type { Filter, Selection, User } from '@annotorious/core';
import { v4 as uuidv4 } from 'uuid';
import hotkeys from 'hotkeys-js';
import { poll } from 'poll';

import type { TextAnnotatorState } from './state';
import type { TextAnnotation, TextAnnotationTarget } from './model';
import type { TextAnnotatorOptions } from './TextAnnotatorOptions';
Expand Down Expand Up @@ -184,7 +186,7 @@ export const SelectionHandler = (
isLeftClick = lastDownEvent.button === 0;
};

const onPointerUp = (evt: PointerEvent) => {
const onPointerUp = async (evt: PointerEvent) => {
if (isNotAnnotatable(evt.target as Node) || !isLeftClick) return;

// Logic for selecting an existing annotation
Expand Down Expand Up @@ -213,42 +215,61 @@ export const SelectionHandler = (
}
};


const timeDifference = evt.timeStamp - lastDownEvent.timeStamp;
if (timeDifference < CLICK_TIMEOUT) {
await pollSelectionCollapsed();

/**
* 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 < CLICK_TIMEOUT) {
if (sel?.isCollapsed) {
currentTarget = undefined;
clickSelect();
} else if (currentTarget && currentTarget.selector.length > 0) {
upsertCurrentTarget();
selection.userSelect(currentTarget.annotation, clonePointerEvent(evt));
return;
}
});
}

if (currentTarget && currentTarget.selector.length > 0) {

upsertCurrentTarget();
selection.userSelect(currentTarget.annotation, clonePointerEvent(evt));
}
}

/**
* We must check the `isCollapsed` after an unspecified 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#issue-2465915707
* @see https://github.com/recogito/text-annotator-js/issues/136#issuecomment-2413773804
*/
const pollSelectionCollapsed = async () => {
const sel = document.getSelection();

let stopPolling = false;
let isCollapsed = sel?.isCollapsed;
const shouldStopPolling = () => isCollapsed || stopPolling;

const pollingDelayMs = 1;
const stopPollingInMs = 50;
setTimeout(() => stopPolling = true, stopPollingInMs);

return poll(() => isCollapsed = sel?.isCollapsed, pollingDelayMs, shouldStopPolling);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first call is immediate, which won't hold off desktop users, for which the isCollapsed: false is available immediately.
And for the rest of the cases, the polling will stop as soon as the range finally gets recognized as collapsed. However, to prevent endless polling and not make the experience sluggish, I limited it with 50ms. cap

}

const onContextMenu = (evt: PointerEvent) => {
const sel = document.getSelection();

if (sel?.isCollapsed) return;

// When selecting the initial word, Chrome Android fires `contextmenu`
// When selecting the initial word, Chrome Android fires `contextmenu`
// before selectionChanged.
if (!currentTarget || currentTarget.selector.length === 0) {
onSelectionChange(evt);
}

upsertCurrentTarget();

selection.userSelect(currentTarget.annotation, clonePointerEvent(evt));
Expand Down