Skip to content

Switch "Copy Link to Highlight" to use custom highlight rendering + beginnings of annotations support#1548

Draft
cdrini wants to merge 15 commits into
masterfrom
fixes/copy-link-to-highlight
Draft

Switch "Copy Link to Highlight" to use custom highlight rendering + beginnings of annotations support#1548
cdrini wants to merge 15 commits into
masterfrom
fixes/copy-link-to-highlight

Conversation

@cdrini
Copy link
Copy Markdown
Contributor

@cdrini cdrini commented May 20, 2026

Various code review refactors / fixes to #1538 . Created a separate PR so that we can keep the original for any before/after testing we might want to do.

Known issues:

@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

❌ Patch coverage is 5.17241% with 330 lines in your changes missing coverage. Please review.
✅ Project coverage is 62.08%. Comparing base (452800b) to head (523ff97).

Files with missing lines Patch % Lines
src/util/TextSelectionManager.js 3.65% 308 Missing and 8 partials ⚠️
src/plugins/url/plugin.url.js 38.46% 8 Missing ⚠️
src/plugins/plugin.text_selection.js 0.00% 3 Missing ⚠️
src/BookReader.js 33.33% 2 Missing ⚠️
src/plugins/url/UrlPlugin.js 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1548      +/-   ##
==========================================
- Coverage   65.79%   62.08%   -3.72%     
==========================================
  Files          67       67              
  Lines        5853     6103     +250     
  Branches     1276     1330      +54     
==========================================
- Hits         3851     3789      -62     
- Misses       1968     2272     +304     
- Partials       34       42       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors “Copy Link to Highlight” away from native browser text fragments toward a BookReader-specific text fragment format and custom highlight rendering, and introduces early scaffolding for private (localStorage-backed) highlights/annotations.

Changes:

  • Introduces BookReaderTextFragment serialization/parsing plus DOM-based highlight rendering (renderHighlight) and localStorage-backed saved highlights.
  • Updates URL handling to parse ?text= into a BookReaderTextFragment and render the target highlight when the relevant page becomes visible.
  • Adds/adjusts experiments + selection UI and reorganizes/moves tests around the new utilities.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 14 comments.

Show a summary per file
File Description
src/util/TextSelectionManager.js Adds BookReaderTextFragment, highlight rendering, BRSelectMenu changes, and localStorage highlight support.
src/plugins/url/plugin.url.js Parses ?text= into targetTextFragment and renders target highlight on textLayerVisible.
src/plugins/url/UrlPlugin.js Simplifies getHash() now that text fragments are no longer appended into the hash.
src/BookReader.js Attempts to preserve text query param handling via BookReaderTextFragment during querystring rebuilds.
src/plugins/plugin.text_selection.js Adds an API to enable the highlight/annotation menu.
src/plugins/plugin.experiments.js Updates copy-link experiment metadata and adds an “annotateHighlight” experiment toggle.
src/css/_TextSelection.scss Adds BR highlight styling and prevents hiding text layers containing highlights.
src/css/_BRpages.scss Prevents hiding highlighted text layers during page flip animations.
tests/jest/util/TextSelectionManager.test.js Moves walkBetweenNodes tests here and attempts to adapt to new fragment logic (currently skips fragment tests).
tests/jest/plugins/plugin.text_selection.test.js Removes walkBetweenNodes tests now moved to util tests.
tests/jest/plugins/url/plugin.url.test.js Updates mock URL plugin surface (adds parseToText).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +61 to +63
const selectedElement = window.getSelection()?.anchorNode;
if (selectedElement.classList.contains('BRhighlight')) {
this.getHighlightedNodes(selectedElement);
}

if (selectEvent == 'cleared') {
console.log("detected cleared event");
Comment on lines 110 to +125
renderSelectionMenu() {
if (document.querySelector('.br-select-menu__option')) return;
document.body.append(this.selectMenu);
this.selectMenu.copyHighlightEnabled = true;
if (this.highlightAnnotationEnabled) {
this.selectMenu.requestUpdate();
} else {
document.body.append(this.selectMenu);
}
}

renderHighlightMenu() {
this.selectMenu.highlightAnnotationEnabled = true;
if (this.selectionMenuEnabled) {
this.selectMenu.requestUpdate();
} else {
document.body.append(this.selectMenu);
}
<ia-icon-share class="br-select-menu__icon" aria-hidden="true"></ia-icon-share>
<button @click=${this.handleCopyLinkToHighlight}
class="br-select-menu__option">
<ia-icon-share class="br-select-menu__icon aria-hidden="true"></ia-icon-share>
@@ -4,6 +4,8 @@ import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';
import '@internetarchive/icon-share';
Comment on lines +230 to +231
if (hasTargetText) {
const textLayer = pageContainerEl.querySelector('.BRtextLayer');
Comment thread src/util/TextSelectionManager.js
Comment thread src/util/TextSelectionManager.js
Comment thread src/util/TextSelectionManager.js
Comment thread tests/jest/util/TextSelectionManager.test.js
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.

3 participants