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
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,4 @@ export const TextAnnotatorPopup = (props: TextAnnotationPopupProps) => {
</FloatingPortal>
) : null;

}
}
3 changes: 2 additions & 1 deletion packages/text-annotator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"colord": "^2.9.3",
"dequal": "^2.0.3",
"hotkeys-js": "^3.13.7",
"poll": "^3.2.2",
"rbush": "^4.0.1",
"uuid": "^10.0.0"
}
}
}
57 changes: 38 additions & 19 deletions packages/text-annotator/src/SelectionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 {
Expand Down Expand Up @@ -188,7 +189,7 @@ export const SelectionHandler = (
}
}

const onPointerUp = (evt: PointerEvent) => {
const onPointerUp = async (evt: PointerEvent) => {
if (isContextMenuOpen) return;

const annotatable = !(evt.target as Node).parentElement?.closest(NOT_ANNOTATABLE_SELECTOR);
Expand All @@ -214,30 +215,48 @@ 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) {
selection.clear();
upsertCurrentTarget();
selection.userSelect(currentTarget.annotation, clonePointerEvent(evt));
return;
}
});
}

if (currentTarget && currentTarget.selector.length > 0) {
selection.clear();
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 = 2;
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) => {
Expand Down