fix(viewer): show Add-comment popover for selections released outside the article#577
Merged
Merged
Conversation
… the article Selecting a line's first word by dragging right-to-left releases the mouse to the left of the <article>, so the article-scoped `onmouseup` never fired and the "Add comment" popover never appeared. The same applied to any selection released past the article's right edge or below it. Move selection capture into `useSelectionPopover`, which already owns the document-level `selectionchange` dismiss listener. It now adds a sibling document-level `mouseup` listener (gated on `comments.enabled`, capture-only) that captures a non-collapsed selection whenever it lies inside the article, so a release anywhere is caught. The public `capture()` method is removed; the article handler keeps only the collapsed-click highlight toggle; and the Add-comment button preventDefaults its `mousedown` to keep the live selection intact on click. Edge cases hardened during review: a selection extended past the article boundary (which never collapses) now drops the popover instead of stranding it at the old anchor, and disabling comments clears any captured selection so it can't re-surface stale if comments are re-enabled mid-session. Tested with new hook unit tests (jsdom) and a real-mouse Playwright e2e that drags right-to-left releasing in the gutter and then clicks the popover. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Selecting a line's first word by dragging right-to-left releases the mouse to the left of
<article>, so the article-scopedonmouseupnever fired and the "Add comment" popover never appeared. The same happened for any selection released past the article's right edge or below it.The direction-dependence is the tell:
Range.getBoundingClientRect()is direction-independent, so a positioning bug would break both directions — only the mouse-release location differs, which is the event-delivery problem.Fix
Move selection capture into
useSelectionPopover, which already owns the document-levelselectionchangedismiss listener:mouseuplistener (gated oncomments.enabled, capture-only, cleaned up on teardown) that captures a non-collapsed selection whenever it lies inside the article — so a release anywhere is caught.capture()method (single owner now); the article handler keeps only the collapsed-click → highlight toggle.preventDefaults itsmousedownto keep the live selection/focus intact on click.Edge cases hardened in review
Testing
clear().selectTexthelper dispatches a mouseup on<article>and can't reproduce this.)svelte-checkand eslint clean; existing comment e2e unaffected.🤖 Generated with Claude Code