Skip to content

fix: not displaying initially selected text#343

Merged
chmurson merged 1 commit intomainfrom
fix-not-displaying-initially-selected-text
Nov 18, 2025
Merged

fix: not displaying initially selected text#343
chmurson merged 1 commit intomainfrom
fix-not-displaying-initially-selected-text

Conversation

@chmurson
Copy link
Collaborator

@chmurson chmurson commented Nov 16, 2025

Summary by CodeRabbit

  • Refactor
    • Updated text layer rendering detection in the PDF viewer by restructuring internal logic for improved code organization.

@netlify
Copy link

netlify bot commented Nov 16, 2025

Deploy Preview for graypaper-reader ready!

Name Link
🔨 Latest commit cc00a54
🔍 Latest deploy log https://app.netlify.com/projects/graypaper-reader/deploys/691a46d816fe31000864f571
😎 Deploy Preview https://deploy-preview-343--graypaper-reader.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link

coderabbitai bot commented Nov 16, 2025

Walkthrough

A refactoring that extracts text layer rendering detection logic into a new custom hook useIsCurrentPageRendered. The hook manages state by checking if the current page's text layer is rendered through existing state or by subscribing to text layer render events via the event bus.

Changes

Cohort / File(s) Summary
SelectionRenderer Hook Refactoring
src/components/PdfViewer/SelectionRenderer/SelectionRenderer.tsx
Removed dependency on textLayerRenderedRef from PdfContext; replaced direct ref checks with a call to new useIsCurrentPageRendered hook; updated effect dependency array accordingly.
New Hook Implementation
src/components/PdfViewer/SelectionRenderer/useIsCurrentPageRendered.ts
Introduced new hook that reads pageNumber and textLayerRenderedRef from context; maintains isCurrentPageTextLayerRendered state; subscribes to textlayerrendered event bus events to update state when current page renders; guards against null values and unavailable event bus.

Sequence Diagram

sequenceDiagram
    participant SR as SelectionRenderer
    participant Hook as useIsCurrentPageRendered
    participant SC as SelectionContext
    participant PC as PdfContext
    participant EB as EventBus

    SR->>Hook: Calls useIsCurrentPageRendered()
    Hook->>SC: Reads pageNumber
    Hook->>PC: Reads textLayerRenderedRef, eventBus
    
    alt pageNumber exists
        Hook->>Hook: Check if pageNumber in textLayerRenderedRef.current
        activate Hook
            Hook->>Hook: Set isCurrentPageTextLayerRendered
        deactivate Hook
    else pageNumber is null
        Hook->>Hook: Reset isCurrentPageTextLayerRendered to false
    end

    opt eventBus available
        Hook->>EB: Subscribe to textlayerrendered event
        EB-->>Hook: Event fired (event.pageNumber matches)
        activate Hook
            Hook->>Hook: Set isCurrentPageTextLayerRendered = true
        deactivate Hook
    end

    Hook-->>SR: Return isCurrentPageTextLayerRendered
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Verify the hook correctly reads from both SelectionContext and PdfContext without missing edge cases
  • Confirm event listener subscription/cleanup logic is sound (unmount and dependency changes)
  • Ensure the refactoring maintains the same behavior as the previous direct ref checks

Possibly related PRs

Poem

A hook hops in where refs once lived, 🐰✨
Logic bundled, clean and swift,
Text layers dance to event bus tunes,
SelectionRenderer's burden floats away—
Simpler code, the rabbit's way! 🌿

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: not displaying initially selected text' directly describes the bug being fixed and aligns with the changes that introduce useIsCurrentPageRendered hook to ensure text layer rendering state is properly tracked.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-not-displaying-initially-selected-text

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@chmurson chmurson force-pushed the fix-not-displaying-initially-selected-text branch from df0297c to 1b6005e Compare November 16, 2025 21:48
@chmurson chmurson force-pushed the fix-not-displaying-initially-selected-text branch from 1b6005e to cc00a54 Compare November 16, 2025 21:49
@github-actions
Copy link
Contributor

github-actions bot commented Nov 16, 2025

Visual Regression Test Report ✅ Passed

Github run id: 19412563062

🔗 Artifacts: Download

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 895fc1f and cc00a54.

📒 Files selected for processing (2)
  • src/components/PdfViewer/SelectionRenderer/SelectionRenderer.tsx (3 hunks)
  • src/components/PdfViewer/SelectionRenderer/useIsCurrentPageRendered.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{js,jsx,ts,tsx}

⚙️ CodeRabbit configuration file

When reviewing Tailwind CSS classes, ensure they follow Tailwind 4.x conventions and suggest modern 4.x alternatives for deprecated patterns.

Files:

  • src/components/PdfViewer/SelectionRenderer/useIsCurrentPageRendered.ts
  • src/components/PdfViewer/SelectionRenderer/SelectionRenderer.tsx
🧠 Learnings (4)
📓 Common learnings
Learnt from: chmurson
Repo: FluffyLabs/graypaper-reader PR: 333
File: src/components/PdfProvider/PdfProvider.tsx:34-37
Timestamp: 2025-10-28T20:41:06.887Z
Learning: In the graypaper-reader codebase, for text layer render tracking in PdfProvider, use RefObject<number[]> approach (textLayerRenderedRef) without adding reactive signals or version numbers. Consumers should check the ref imperatively when needed rather than reacting to changes.
📚 Learning: 2025-10-28T20:41:06.887Z
Learnt from: chmurson
Repo: FluffyLabs/graypaper-reader PR: 333
File: src/components/PdfProvider/PdfProvider.tsx:34-37
Timestamp: 2025-10-28T20:41:06.887Z
Learning: In the graypaper-reader codebase, for text layer render tracking in PdfProvider, use RefObject<number[]> approach (textLayerRenderedRef) without adding reactive signals or version numbers. Consumers should check the ref imperatively when needed rather than reacting to changes.

Applied to files:

  • src/components/PdfViewer/SelectionRenderer/useIsCurrentPageRendered.ts
  • src/components/PdfViewer/SelectionRenderer/SelectionRenderer.tsx
📚 Learning: 2025-10-22T20:36:10.440Z
Learnt from: chmurson
Repo: FluffyLabs/graypaper-reader PR: 330
File: src/components/Outline/OutlineLink.tsx:17-21
Timestamp: 2025-10-22T20:36:10.440Z
Learning: Project targets React 19+; prefer passing refs as a regular prop in function components and avoid React.forwardRef in new code. File: src/components/Outline/OutlineLink.tsx.

Applied to files:

  • src/components/PdfViewer/SelectionRenderer/SelectionRenderer.tsx
📚 Learning: 2025-10-22T20:36:10.440Z
Learnt from: chmurson
Repo: FluffyLabs/graypaper-reader PR: 330
File: src/components/Outline/OutlineLink.tsx:17-21
Timestamp: 2025-10-22T20:36:10.440Z
Learning: This repo targets React 19+; prefer passing refs as a normal prop to function components and avoid React.forwardRef in new code. File context: src/components/Outline/OutlineLink.tsx.

Applied to files:

  • src/components/PdfViewer/SelectionRenderer/SelectionRenderer.tsx
🧬 Code graph analysis (2)
src/components/PdfViewer/SelectionRenderer/useIsCurrentPageRendered.ts (2)
src/components/PdfProvider/PdfProvider.tsx (2)
  • PdfContext (13-13)
  • IPdfContext (27-37)
src/components/SelectionProvider/SelectionProvider.tsx (2)
  • SelectionContext (35-35)
  • ISelectionContext (21-29)
src/components/PdfViewer/SelectionRenderer/SelectionRenderer.tsx (4)
src/components/PdfViewer/Highlighter/Highlighter.tsx (1)
  • IHighlighterColor (12-16)
src/components/PdfProvider/PdfProvider.tsx (2)
  • PdfContext (13-13)
  • IPdfContext (27-37)
src/components/SelectionProvider/SelectionProvider.tsx (2)
  • SelectionContext (35-35)
  • ISelectionContext (21-29)
src/components/PdfViewer/SelectionRenderer/useIsCurrentPageRendered.ts (1)
  • useIsCurrentPageRendered (5-38)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: visual-tests
🔇 Additional comments (3)
src/components/PdfViewer/SelectionRenderer/SelectionRenderer.tsx (1)

7-7: LGTM! Clean refactoring that improves separation of concerns.

The extraction of text layer render detection into a dedicated hook makes the component cleaner and the logic more reusable. The hook properly encapsulates both imperative ref checking and event-driven updates.

Also applies to: 14-20, 64-66, 111-111

src/components/PdfViewer/SelectionRenderer/useIsCurrentPageRendered.ts (2)

19-33: LGTM! Event subscription correctly handles dynamic text layer rendering.

The event-driven approach ensures the state updates when new pages are rendered, properly handling the async nature of text layer rendering. Cleanup is correctly implemented.


5-38: Good approach: imperative ref checking + event subscription.

The hook correctly implements the pattern from learnings by checking the ref imperatively when needed (on pageNumber change) and using events for updates, rather than trying to make the ref itself reactive. This should fix the initially selected text display issue.

[Based on learnings]

@tomusdrw
Copy link
Member

When testing I feel that the previous selection is just left as-is, yet the note that we cannot selection for is correctly activated.

However, is it possible to reset the selection in case it cannot be found? Or maybe default to page/section first block or sth?

@chmurson chmurson merged commit ea52180 into main Nov 18, 2025
9 checks passed
@chmurson chmurson deleted the fix-not-displaying-initially-selected-text branch November 18, 2025 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants