Skip to content

Cross-block annotation highlights not restored on shared URL or draft restore #216

@backnotprop

Description

@backnotprop

Problem

When an annotation spans multiple blocks (e.g., text selected across two paragraphs), the highlight mark is not visually restored — though the annotation itself appears in the sidebar panel. This affects both shared URL restore and draft restore.

Root Cause

findTextInDOM() in Viewer.tsx searches for the annotation's originalText as a literal string in the DOM. For cross-block selections, originalText contains \n\n characters representing block boundaries. But in the rendered DOM, those boundaries are structural (<p> elements), not text node newlines — so the search fails silently.

The web-highlighter library already captures startMeta/endMeta (DOM path metadata) when annotations are created, and provides highlighter.fromStore(startMeta, endMeta, text, id) for exact restoration. However:

  1. toShareable() strips startMeta/endMeta from the compact tuple format
  2. fromShareable() leaves them undefined
  3. applySharedAnnotations() falls back to text search, which fails for cross-block text

Affected Paths

  • Shared URL restore (useSharing.tsapplySharedAnnotations)
  • Draft restore (useAnnotationDraft.tsapplySharedAnnotations)

Possible Fixes

  1. Include startMeta/endMeta in shareable format — extend the tuple to carry DOM metadata, then use highlighter.fromStore() on restore
  2. Fix findTextInDOM() to handle cross-block text — normalize both search text and DOM text to handle structural block boundaries

Option 2 is more robust since it fixes all restore paths without changing the share payload format.

Key Files

  • packages/ui/components/Viewer.tsxfindTextInDOM(), applySharedAnnotations()
  • packages/ui/utils/sharing.tstoShareable(), fromShareable()
  • packages/ui/types.tsAnnotation.startMeta / endMeta

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions