CodingNic

Bookmarks, Highlights & Notes

Capture and Persist Highlights

Bookmarks, Highlights & Notes 22 min read

Capture and Persist Highlights

Capture and Persist Highlights

Task

Capture selected text from EPUB.js and persist it as a Highlight.

EPUB.js exposes selection information through rendition events. Register a selection handler:

ts
rendition.on("selected", (cfiRange, contents) => {
  const text = contents?.window?.getSelection()?.toString().trim();

  if (!text) return;

  // Open the highlight action UI with cfiRange and text.
});

After the user confirms the action, send the CFI range and selected text to the authenticated Highlight endpoint.

Do not trust a userId or bookId supplied by the client without verifying ownership on the server.

Test

Select text in a real EPUB.

Confirm:

  • selected text is shown in the prepared action UI
  • cancelling does not create a Highlight
  • confirming creates a database record
  • the Highlight is associated with the current Book and user

Checkpoint

Selected EPUB text can become a persistent Readly Highlight.