"use client"; import type { AnnotationItem, NormalisedRect } from "@/lib/reading-api"; import { useTranslation } from "react-i18next"; import Tooltip from "@/shared/ui/Tooltip"; /** Ink for each palette colour, tuned to stay readable over black-on-white text. */ const COLOR_INK: Record = { yellow: "250 220 90", green: "140 219 148", blue: "122 192 250", pink: "250 161 199", purple: "199 174 250", }; function ink(color: string): string { return COLOR_INK[color] ?? COLOR_INK.yellow; } export interface AnnotationLayerProps { annotations: AnnotationItem[]; highlightedAnnotationId?: string | null; flashRects?: NormalisedRect[]; onAnnotationClick?: (annotation: AnnotationItem) => void; } /** * Overlay that paints stored marks over one rendered unit. * * Positioned entirely in percentages from the annotation's normalised rects, so * it needs no measurement and stays correct through zoom and pane resizing. * * `multiply` blending is what makes a highlight look like a highlighter rather * than a coloured box on top of the words: the text below stays fully legible * instead of being tinted. * * The layer itself is click-through (`pointer-events-none`); only the marks take * clicks, so highlighting never blocks text selection on the layer beneath. */ export function AnnotationLayer({ annotations, highlightedAnnotationId, flashRects, onAnnotationClick, }: AnnotationLayerProps) { const { t } = useTranslation(); return (
{annotations.map((annotation) => annotation.rects.map((rect, index) => { const [x0, y0, x1, y1] = rect; const isFocused = annotation.annotation_id === highlightedAnnotationId; const isUnderline = annotation.kind === "underline"; const label = annotation.note || annotation.quote || t( annotation.kind === "citation" ? "Citation" : isUnderline ? "Underline" : "Highlight", ); return ( span]:h-full [&>span]:w-full [&>span>button]:h-full [&>span>button]:w-full", isUnderline ? "" : "mix-blend-multiply", isFocused ? "ring-2 ring-[var(--ring)] ring-offset-1" : "", ] .filter(Boolean) .join(" ")} style={{ left: `${x0 * 100}%`, top: `${y0 * 100}%`, width: `${Math.max(0, x1 - x0) * 100}%`, height: `${Math.max(0, y1 - y0) * 100}%`, background: isUnderline ? undefined : `rgb(${ink(annotation.color)} / ${isFocused ? 0.85 : 0.6})`, borderBottom: isUnderline ? `2px solid rgb(${ink(annotation.color)})` : undefined, borderRadius: isUnderline ? 0 : 2, }} > ); }), )} {(flashRects ?? []).map((rect, index) => { const [x0, y0, x1, y1] = rect; return ( ); })}
); }