Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions app/d/[slug]/CommentsShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -697,9 +697,9 @@ function SelectionToolbar({
onComment: () => void;
onReact: (emoji: string) => void;
}) {
// The iframe spans the docwrap; the selection rect's viewport-top maps onto the
// docwrap (both share the top edge). Clamp into view.
const top = Math.max(8, viewTop);
// The iframe spans the docwrap; iframe viewport coordinates map onto this
// wrapper, then CSS clamps the toolbar inside the visible pane.
const top = `max(8px, min(${Math.max(8, Math.round(viewTop))}px, calc(100% - 84px)))`;
const [picking, setPicking] = useState(false);
return (
<div style={{ ...seltoolStyle, top }}>
Expand Down
40 changes: 36 additions & 4 deletions lib/docs/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,13 @@ export const OVERLAY_SCRIPT = String.raw`
var exact = (e > s) ? tm.full.slice(s, e) : sel.toString();
return { exact: exact, prefix: tm.full.slice(Math.max(0, s-32), s), suffix: tm.full.slice(e, e+32) };
}
document.addEventListener("mouseup", function(ev){
if (ev.target && ev.target.closest && (ev.target.closest("[data-jh-chip]") || ev.target.closest(".jh-pop"))) return;
setTimeout(function(){
var selectionTimer = null;
function fromOverlayChrome(ev){
var t = ev && ev.target;
return !!(t && t.closest && (t.closest("[data-jh-chip]") || t.closest(".jh-pop")));
}
function reportSelection(){
try {
var sel = window.getSelection();
if (!sel || !sel.rangeCount || sel.isCollapsed || !sel.toString().trim()){ send({type:"jh:selectionCleared"}); return; }
var anchor = anchorFromSelection(sel);
Expand All @@ -580,7 +584,35 @@ export const OVERLAY_SCRIPT = String.raw`
top: rect.top + window.scrollY, left: rect.left, right: rect.right, bottom: rect.bottom + window.scrollY,
viewTop: rect.top
}});
}, 10);
} catch(e) {
send({type:"jh:selectionCleared"});
}
}
function queueSelectionReport(delay){
if (selectionTimer) clearTimeout(selectionTimer);
selectionTimer = setTimeout(function(){
selectionTimer = null;
reportSelection();
}, delay);
}
document.addEventListener("mouseup", function(ev){
if (fromOverlayChrome(ev)) return;
queueSelectionReport(10);
});
document.addEventListener("keyup", function(ev){
if (fromOverlayChrome(ev)) return;
queueSelectionReport(10);
});
document.addEventListener("touchend", function(ev){
if (fromOverlayChrome(ev)) return;
queueSelectionReport(80);
}, {passive:true});
document.addEventListener("pointerup", function(ev){
if (fromOverlayChrome(ev)) return;
queueSelectionReport(30);
});
document.addEventListener("selectionchange", function(){
queueSelectionReport(120);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Shared timer drops intended delays

High Severity

The queueSelectionReport function's single shared timer means events with shorter delays can prematurely cancel and override pending selection reports from events with longer delays. This can cause selection to be read too early, particularly on touch devices where events are synthesized, leading to jh:selectionCleared or a stale selection rect before the selection fully settles.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit b511f84. Configure here.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Selectionchange ignores overlay chrome

Medium Severity

The new selectionchange listener always schedules reportSelection, but mouseup, touchend, and pointerup skip events from [data-jh-chip] and .jh-pop via fromOverlayChrome. Text selected inside a .jh-pop can still post jh:selection and show the parent comment/reaction toolbar for overlay UI, not document content.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit b511f84. Configure here.

});

window.addEventListener("message", function(ev){
Expand Down
Loading